Skip to content

Commit

Permalink
MDL-60826 groups: deprecate groups_get_all_groups_for_courses function
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Jul 23, 2018
1 parent 0f1e7ec commit e2b7dca
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 310 deletions.
146 changes: 146 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// <rest of group properties>
// 'members' => [
// '1' => (object) [
// <group member properties>
// ],
// '2' => (object) [
// <group member properties>
// ]
// ]
// ],
// '2' => (object) [
// 'id' => 2,
// <rest of group properties>
// 'members' => [
// '1' => (object) [
// <group member properties>
// ],
// '3' => (object) [
// <group member properties>
// ]
// ]
// ]
// ]
// ]
//
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;
}
146 changes: 0 additions & 146 deletions lib/grouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// <rest of group properties>
// 'members' => [
// '1' => (object) [
// <group member properties>
// ],
// '2' => (object) [
// <group member properties>
// ]
// ]
// ],
// '2' => (object) [
// 'id' => 2,
// <rest of group properties>
// 'members' => [
// '1' => (object) [
// <group member properties>
// ],
// '3' => (object) [
// <group member properties>
// ]
// ]
// ]
// ]
// ]
//
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.
*
Expand Down
Loading

0 comments on commit e2b7dca

Please sign in to comment.