MDL-43252 Group menu in wrong order when groupings used
authorsam marshall <s.marshall@open.ac.uk>
Fri, 6 Dec 2013 18:15:09 +0000 (18:15 +0000)
committersam marshall <s.marshall@open.ac.uk>
Mon, 9 Dec 2013 13:02:12 +0000 (13:02 +0000)
lib/grouplib.php
lib/tests/grouplib_test.php

index 13e852d..c698bf0 100644 (file)
@@ -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();
     }
index 8f5fb54..ede1736 100644 (file)
@@ -735,4 +735,36 @@ class core_grouplib_testcase extends advanced_testcase {
         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);
+    }
 }