MDL-59382 calendar: add modal to create and update events

This commit is contained in:
Ryan Wyllie 2017-07-24 08:01:14 +00:00
parent 6103fd2efe
commit aa0912258d
23 changed files with 2392 additions and 157 deletions

View file

@ -294,6 +294,150 @@ 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 useable 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.