From e2b7dca00691f43cb70d54916d9ef482120bc6c8 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 16 Jul 2018 11:40:30 +0800 Subject: [PATCH] MDL-60826 groups: deprecate groups_get_all_groups_for_courses function --- lib/deprecatedlib.php | 146 ++++++++++++++++++++++++++++++++ lib/grouplib.php | 146 -------------------------------- lib/tests/grouplib_test.php | 164 ------------------------------------ 3 files changed, 146 insertions(+), 310 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index a2291280f5188..daf12c48e667e 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -6467,3 +6467,149 @@ function calendar_get_all_allowed_types() { return $types; } + +/** + * Gets array of all groups in a set of course. + * + * @category group + * @param array $courses Array of course objects or course ids. + * @return array Array of groups indexed by course id. + */ +function groups_get_all_groups_for_courses($courses) { + global $DB; + + if (empty($courses)) { + return []; + } + + $groups = []; + $courseids = []; + + foreach ($courses as $course) { + $courseid = is_object($course) ? $course->id : $course; + $groups[$courseid] = []; + $courseids[] = $courseid; + } + + $groupfields = [ + 'g.id as gid', + 'g.courseid', + 'g.idnumber', + 'g.name', + 'g.description', + 'g.descriptionformat', + 'g.enrolmentkey', + 'g.picture', + 'g.hidepicture', + 'g.timecreated', + 'g.timemodified' + ]; + + $groupsmembersfields = [ + 'gm.id as gmid', + 'gm.groupid', + 'gm.userid', + 'gm.timeadded', + 'gm.component', + 'gm.itemid' + ]; + + $concatidsql = $DB->sql_concat_join("'-'", ['g.id', 'COALESCE(gm.id, 0)']) . ' AS uniqid'; + list($courseidsql, $params) = $DB->get_in_or_equal($courseids); + $groupfieldssql = implode(',', $groupfields); + $groupmembersfieldssql = implode(',', $groupsmembersfields); + $sql = "SELECT {$concatidsql}, {$groupfieldssql}, {$groupmembersfieldssql} + FROM {groups} g + LEFT JOIN {groups_members} gm + ON gm.groupid = g.id + WHERE g.courseid {$courseidsql}"; + + $results = $DB->get_records_sql($sql, $params); + + // The results will come back as a flat dataset thanks to the left + // join so we will need to do some post processing to blow it out + // into a more usable data structure. + // + // This loop will extract the distinct groups from the result set + // and add it's list of members to the object as a property called + // 'members'. Then each group will be added to the result set indexed + // by it's course id. + // + // The resulting data structure for $groups should be: + // $groups = [ + // '1' = [ + // '1' => (object) [ + // 'id' => 1, + // + // 'members' => [ + // '1' => (object) [ + // + // ], + // '2' => (object) [ + // + // ] + // ] + // ], + // '2' => (object) [ + // 'id' => 2, + // + // 'members' => [ + // '1' => (object) [ + // + // ], + // '3' => (object) [ + // + // ] + // ] + // ] + // ] + // ] + // + foreach ($results as $key => $result) { + $groupid = $result->gid; + $courseid = $result->courseid; + $coursegroups = $groups[$courseid]; + $groupsmembersid = $result->gmid; + $reducefunc = function($carry, $field) use ($result) { + // Iterate over the groups properties and pull + // them out into a separate object. + list($prefix, $field) = explode('.', $field); + + if (property_exists($result, $field)) { + $carry[$field] = $result->{$field}; + } + + return $carry; + }; + + if (isset($coursegroups[$groupid])) { + $group = $coursegroups[$groupid]; + } else { + $initial = [ + 'id' => $groupid, + 'members' => [] + ]; + $group = (object) array_reduce( + $groupfields, + $reducefunc, + $initial + ); + } + + if (!empty($groupsmembersid)) { + $initial = ['id' => $groupsmembersid]; + $groupsmembers = (object) array_reduce( + $groupsmembersfields, + $reducefunc, + $initial + ); + + $group->members[$groupsmembers->userid] = $groupsmembers; + } + + $coursegroups[$groupid] = $group; + $groups[$courseid] = $coursegroups; + } + + return $groups; +} diff --git a/lib/grouplib.php b/lib/grouplib.php index 469a012869965..336e372dcf5fd 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -295,152 +295,6 @@ function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.* return $results; } -/** - * Gets array of all groups in a set of course. - * - * @category group - * @param array $courses Array of course objects or course ids. - * @return array Array of groups indexed by course id. - */ -function groups_get_all_groups_for_courses($courses) { - global $DB; - - if (empty($courses)) { - return []; - } - - $groups = []; - $courseids = []; - - foreach ($courses as $course) { - $courseid = is_object($course) ? $course->id : $course; - $groups[$courseid] = []; - $courseids[] = $courseid; - } - - $groupfields = [ - 'g.id as gid', - 'g.courseid', - 'g.idnumber', - 'g.name', - 'g.description', - 'g.descriptionformat', - 'g.enrolmentkey', - 'g.picture', - 'g.hidepicture', - 'g.timecreated', - 'g.timemodified' - ]; - - $groupsmembersfields = [ - 'gm.id as gmid', - 'gm.groupid', - 'gm.userid', - 'gm.timeadded', - 'gm.component', - 'gm.itemid' - ]; - - $concatidsql = $DB->sql_concat_join("'-'", ['g.id', 'COALESCE(gm.id, 0)']) . ' AS uniqid'; - list($courseidsql, $params) = $DB->get_in_or_equal($courseids); - $groupfieldssql = implode(',', $groupfields); - $groupmembersfieldssql = implode(',', $groupsmembersfields); - $sql = "SELECT {$concatidsql}, {$groupfieldssql}, {$groupmembersfieldssql} - FROM {groups} g - LEFT JOIN {groups_members} gm - ON gm.groupid = g.id - WHERE g.courseid {$courseidsql}"; - - $results = $DB->get_records_sql($sql, $params); - - // The results will come back as a flat dataset thanks to the left - // join so we will need to do some post processing to blow it out - // into a more usable data structure. - // - // This loop will extract the distinct groups from the result set - // and add it's list of members to the object as a property called - // 'members'. Then each group will be added to the result set indexed - // by it's course id. - // - // The resulting data structure for $groups should be: - // $groups = [ - // '1' = [ - // '1' => (object) [ - // 'id' => 1, - // - // 'members' => [ - // '1' => (object) [ - // - // ], - // '2' => (object) [ - // - // ] - // ] - // ], - // '2' => (object) [ - // 'id' => 2, - // - // 'members' => [ - // '1' => (object) [ - // - // ], - // '3' => (object) [ - // - // ] - // ] - // ] - // ] - // ] - // - foreach ($results as $key => $result) { - $groupid = $result->gid; - $courseid = $result->courseid; - $coursegroups = $groups[$courseid]; - $groupsmembersid = $result->gmid; - $reducefunc = function($carry, $field) use ($result) { - // Iterate over the groups properties and pull - // them out into a separate object. - list($prefix, $field) = explode('.', $field); - - if (property_exists($result, $field)) { - $carry[$field] = $result->{$field}; - } - - return $carry; - }; - - if (isset($coursegroups[$groupid])) { - $group = $coursegroups[$groupid]; - } else { - $initial = [ - 'id' => $groupid, - 'members' => [] - ]; - $group = (object) array_reduce( - $groupfields, - $reducefunc, - $initial - ); - } - - if (!empty($groupsmembersid)) { - $initial = ['id' => $groupsmembersid]; - $groupsmembers = (object) array_reduce( - $groupsmembersfields, - $reducefunc, - $initial - ); - - $group->members[$groupsmembers->userid] = $groupsmembers; - } - - $coursegroups[$groupid] = $group; - $groups[$courseid] = $coursegroups; - } - - return $groups; -} - /** * Gets array of all groups in current user. * diff --git a/lib/tests/grouplib_test.php b/lib/tests/grouplib_test.php index 36413270b9df3..f9bfbeeea888a 100644 --- a/lib/tests/grouplib_test.php +++ b/lib/tests/grouplib_test.php @@ -1547,168 +1547,4 @@ public function test_groups_get_activity_shared_group_members() { $this->assertCount(2, $members); // Now I see members of group 3. $this->assertEquals([$user1->id, $user3->id], array_keys($members), '', 0.0, 10, true); } - - /** - * Test groups_get_all_groups_for_courses() method. - */ - public function test_groups_get_all_groups_for_courses_no_courses() { - $this->resetAfterTest(true); - $generator = $this->getDataGenerator(); - - $this->assertEquals([], groups_get_all_groups_for_courses([])); - } - - /** - * Test groups_get_all_groups_for_courses() method. - */ - public function test_groups_get_all_groups_for_courses_with_courses() { - global $DB; - - $this->resetAfterTest(true); - $generator = $this->getDataGenerator(); - - // Create courses. - $course1 = $generator->create_course(); // no groups. - $course2 = $generator->create_course(); // one group, no members. - $course3 = $generator->create_course(); // one group, one member. - $course4 = $generator->create_course(); // one group, multiple members. - $course5 = $generator->create_course(); // two groups, no members. - $course6 = $generator->create_course(); // two groups, one member. - $course7 = $generator->create_course(); // two groups, multiple members. - - $courses = [$course1, $course2, $course3, $course4, $course5, $course6, $course7]; - // Create users. - $user1 = $generator->create_user(); - $user2 = $generator->create_user(); - $user3 = $generator->create_user(); - $user4 = $generator->create_user(); - - // Enrol users. - foreach ($courses as $course) { - $generator->enrol_user($user1->id, $course->id); - $generator->enrol_user($user2->id, $course->id); - $generator->enrol_user($user3->id, $course->id); - $generator->enrol_user($user4->id, $course->id); - } - - // Create groups. - $group1 = $generator->create_group(array('courseid' => $course2->id)); // no members. - $group2 = $generator->create_group(array('courseid' => $course3->id)); // one member. - $group3 = $generator->create_group(array('courseid' => $course4->id)); // multiple members. - $group4 = $generator->create_group(array('courseid' => $course5->id)); // no members. - $group5 = $generator->create_group(array('courseid' => $course5->id)); // no members. - $group6 = $generator->create_group(array('courseid' => $course6->id)); // one member. - $group7 = $generator->create_group(array('courseid' => $course6->id)); // one member. - $group8 = $generator->create_group(array('courseid' => $course7->id)); // multiple members. - $group9 = $generator->create_group(array('courseid' => $course7->id)); // multiple members. - - // Assign users to groups. - $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id)); - $generator->create_group_member(array('groupid' => $group6->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group7->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group8->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group8->id, 'userid' => $user2->id)); - $generator->create_group_member(array('groupid' => $group9->id, 'userid' => $user1->id)); - $generator->create_group_member(array('groupid' => $group9->id, 'userid' => $user2->id)); - - // The process of modifying group members changes the timemodified of the group. - // Refresh the group records. - $group1 = $DB->get_record('groups', ['id' => $group1->id]); - $group2 = $DB->get_record('groups', ['id' => $group2->id]); - $group3 = $DB->get_record('groups', ['id' => $group3->id]); - $group4 = $DB->get_record('groups', ['id' => $group4->id]); - $group5 = $DB->get_record('groups', ['id' => $group5->id]); - $group6 = $DB->get_record('groups', ['id' => $group6->id]); - $group7 = $DB->get_record('groups', ['id' => $group7->id]); - $group8 = $DB->get_record('groups', ['id' => $group8->id]); - $group9 = $DB->get_record('groups', ['id' => $group9->id]); - - $result = groups_get_all_groups_for_courses($courses); - $assertpropertiesmatch = function($expected, $actual) { - $props = get_object_vars($expected); - - foreach ($props as $name => $val) { - $got = $actual->{$name}; - $this->assertEquals( - $val, - $actual->{$name}, - "Failed asserting that {$got} equals {$val} for property {$name}" - ); - } - }; - - // Course 1 has no groups. - $this->assertEquals([], $result[$course1->id]); - - // Course 2 has one group with no members. - $coursegroups = $result[$course2->id]; - $coursegroup = $coursegroups[$group1->id]; - $this->assertCount(1, $coursegroups); - $this->assertEquals([], $coursegroup->members); - $assertpropertiesmatch($group1, $coursegroup); - - // Course 3 has one group with one member. - $coursegroups = $result[$course3->id]; - $coursegroup = $coursegroups[$group2->id]; - $groupmember1 = $coursegroup->members[$user1->id]; - $this->assertCount(1, $coursegroups); - $this->assertCount(1, $coursegroup->members); - $assertpropertiesmatch($group2, $coursegroup); - $this->assertEquals($user1->id, $groupmember1->userid); - - // Course 4 has one group with multiple members. - $coursegroups = $result[$course4->id]; - $coursegroup = $coursegroups[$group3->id]; - $groupmember1 = $coursegroup->members[$user1->id]; - $groupmember2 = $coursegroup->members[$user2->id]; - $this->assertCount(1, $coursegroups); - $this->assertCount(2, $coursegroup->members); - $assertpropertiesmatch($group3, $coursegroup); - $this->assertEquals($user1->id, $groupmember1->userid); - $this->assertEquals($user2->id, $groupmember2->userid); - - // Course 5 has multiple groups with no members. - $coursegroups = $result[$course5->id]; - $coursegroup1 = $coursegroups[$group4->id]; - $coursegroup2 = $coursegroups[$group5->id]; - $this->assertCount(2, $coursegroups); - $this->assertEquals([], $coursegroup1->members); - $this->assertEquals([], $coursegroup2->members); - $assertpropertiesmatch($group4, $coursegroup1); - $assertpropertiesmatch($group5, $coursegroup2); - - // Course 6 has multiple groups with one member. - $coursegroups = $result[$course6->id]; - $coursegroup1 = $coursegroups[$group6->id]; - $coursegroup2 = $coursegroups[$group7->id]; - $group1member1 = $coursegroup1->members[$user1->id]; - $group2member1 = $coursegroup2->members[$user1->id]; - $this->assertCount(2, $coursegroups); - $this->assertCount(1, $coursegroup1->members); - $this->assertCount(1, $coursegroup2->members); - $assertpropertiesmatch($group6, $coursegroup1); - $assertpropertiesmatch($group7, $coursegroup2); - $this->assertEquals($user1->id, $group1member1->userid); - $this->assertEquals($user1->id, $group2member1->userid); - - // Course 7 has multiple groups with multiple members. - $coursegroups = $result[$course7->id]; - $coursegroup1 = $coursegroups[$group8->id]; - $coursegroup2 = $coursegroups[$group9->id]; - $group1member1 = $coursegroup1->members[$user1->id]; - $group1member2 = $coursegroup1->members[$user2->id]; - $group2member1 = $coursegroup2->members[$user1->id]; - $group2member2 = $coursegroup2->members[$user2->id]; - $this->assertCount(2, $coursegroups); - $this->assertCount(2, $coursegroup1->members); - $this->assertCount(2, $coursegroup2->members); - $assertpropertiesmatch($group8, $coursegroup1); - $assertpropertiesmatch($group9, $coursegroup2); - $this->assertEquals($user1->id, $group1member1->userid); - $this->assertEquals($user2->id, $group1member2->userid); - $this->assertEquals($user1->id, $group2member1->userid); - $this->assertEquals($user2->id, $group2member2->userid); - } }