diff --git a/lib/grouplib.php b/lib/grouplib.php index 826d0c481db..13e852df308 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -203,12 +203,18 @@ function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.* // aliased. If its something else we need to avoid the cache and run the query as who knows whats going on. $knownfields = true; if ($fields !== 'g.*') { - $fieldbits = explode(',', $fields); - foreach ($fieldbits as $bit) { - $bit = trim($bit); - if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) { - $knownfields = false; - break; + // Quickly check if the first field is no longer g.id as using the + // cache will return an array indexed differently than when expect + if (strpos($fields, 'g.*') !== 0 && strpos($fields, 'g.id') !== 0) { + $knownfields = false; + } else { + $fieldbits = explode(',', $fields); + foreach ($fieldbits as $bit) { + $bit = trim($bit); + if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) { + $knownfields = false; + break; + } } } } diff --git a/lib/tests/grouplib_test.php b/lib/tests/grouplib_test.php index ada661631d2..8f5fb5468a4 100644 --- a/lib/tests/grouplib_test.php +++ b/lib/tests/grouplib_test.php @@ -26,6 +26,10 @@ defined('MOODLE_INTERNAL') || die(); +/** + * Unit tests for lib/grouplib.php + * @group core_group + */ class core_grouplib_testcase extends advanced_testcase { public function test_groups_get_group_by_idnumber() { @@ -404,6 +408,14 @@ class core_grouplib_testcase extends advanced_testcase { $this->assertNotContains($group2->id, $groupkeys); $this->assertContains($group3->id, $groupkeys); $this->assertContains($group4->id, $groupkeys); + + // Test this function using an alternate column for the result index + $groups = groups_get_all_groups($course->id, null, $grouping2->id, 'g.name, g.id'); + $groupkeys = array_keys($groups); + $this->assertCount(2, $groups); + $this->assertNotContains($group3->id, $groupkeys); + $this->assertContains($group3->name, $groupkeys); + $this->assertEquals($group3->id, $groups[$group3->name]->id); } /**