Permalink
Browse files

MDL-43252 Group menu in wrong order when groupings used

  • Loading branch information...
1 parent 3e6e38b commit b1b09903b0e705392c5781a5f5d03d1e4a03dd7c @sammarshallou sammarshallou committed Dec 6, 2013
Showing with 39 additions and 1 deletion.
  1. +7 −1 lib/grouplib.php
  2. +32 −0 lib/tests/grouplib_test.php
View
8 lib/grouplib.php
@@ -993,7 +993,13 @@ function groups_cache_groupdata($courseid, cache $cache = null) {
if (!empty($groupings)) {
// Finally get the mappings between the two.
- $mappings = $DB->get_records_list('groupings_groups', 'groupingid', array_keys($groupings), '', 'id,groupingid,groupid');
+ list($insql, $params) = $DB->get_in_or_equal(array_keys($groupings));
+ $mappings = $DB->get_records_sql("
+ SELECT gg.id, gg.groupingid, gg.groupid
+ FROM {groupings_groups} gg
+ JOIN {groups} g ON g.id = gg.groupid
+ WHERE gg.groupingid $insql
+ ORDER BY g.name ASC", $params);
} else {
$mappings = array();
}
View
32 lib/tests/grouplib_test.php
@@ -735,4 +735,36 @@ public function test_groups_allgroups_course_menu() {
groups_allgroups_course_menu($course, 'someurl.php', true, 256);
$this->assertEquals($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
}
+
+ /**
+ * This unit test checks that groups_get_all_groups returns groups in
+ * alphabetical order even if they are in a grouping.
+ */
+ public function test_groups_ordering() {
+ $generator = $this->getDataGenerator();
+ $this->resetAfterTest();
+
+ // Create a course category and course.
+ $cat = $generator->create_category(array('parent' => 0));
+ $course = $generator->create_course(array('category' => $cat->id));
+ $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping'));
+
+ // Create groups in reverse order.
+ $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
+ $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
+
+ // Assign the groups to the grouping in reverse order.
+ $this->assertTrue(groups_assign_grouping($grouping->id, $group2->id));
+ $this->assertTrue(groups_assign_grouping($grouping->id, $group1->id));
+
+ // Get all groups and check they are alphabetical.
+ $groups = array_values(groups_get_all_groups($course->id, 0));
+ $this->assertEquals('Group 1', $groups[0]->name);
+ $this->assertEquals('Group 2', $groups[1]->name);
+
+ // Now check the same is true when accessed by grouping.
+ $groups = array_values(groups_get_all_groups($course->id, 0, $grouping->id));
+ $this->assertEquals('Group 1', $groups[0]->name);
+ $this->assertEquals('Group 2', $groups[1]->name);
+ }
}

0 comments on commit b1b0990

Please sign in to comment.