From d9df0e34fc1ab8835fa8a3d489ff48dd9f5b594b Mon Sep 17 00:00:00 2001 From: Henning Bostelmann Date: Sat, 22 Oct 2011 23:43:11 +0100 Subject: [PATCH] MDL-28557 Calendar filters: show all group events in course to teacher --- calendar/lib.php | 51 ++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/calendar/lib.php b/calendar/lib.php index 6712ff1d5b2..40efee72931 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -1237,6 +1237,10 @@ function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) { $user = false; $group = false; + // capabilities that allow seeing group events from all groups + // TODO: rewrite so that moodle/calendar:manageentries is not necessary here + $allgroupscaps = array('moodle/site:accessallgroups', 'moodle/calendar:manageentries'); + $isloggedin = isloggedin(); if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE)) { @@ -1262,26 +1266,35 @@ function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) { if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP) || $ignorefilters)) { - if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', get_system_context())) { - $group = true; - } else if ($isloggedin) { - $groupids = array(); - - // We already have the courses to examine in $courses - // For each course... - foreach ($courseeventsfrom as $courseid => $course) { - // If the user is an editing teacher in there, - if (!empty($USER->groupmember[$course->id])) { - // We've already cached the users groups for this course so we can just use that - $groupids = array_merge($groupids, $USER->groupmember[$course->id]); - } else if (($course->groupmode != NOGROUPS || !$course->groupmodeforce) && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $course->id))) { - // If this course has groups, show events from all of them - $coursegroups = groups_get_user_groups($course->id, $USER->id); - $groupids = array_merge($groupids, $coursegroups['0']); - } + if (count($courseeventsfrom)==1) { + $course = reset($courseeventsfrom); + if (has_any_capability($allgroupscaps, get_context_instance(CONTEXT_COURSE, $course->id))) { + $coursegroups = groups_get_all_groups($course->id, 0, 0, 'g.id'); + $group = array_keys($coursegroups); } - if (!empty($groupids)) { - $group = $groupids; + } + if ($group === false) { + if (!empty($CFG->calendar_adminseesall) && has_any_capability($allgroupscaps, get_system_context())) { + $group = true; + } else if ($isloggedin) { + $groupids = array(); + + // We already have the courses to examine in $courses + // For each course... + foreach ($courseeventsfrom as $courseid => $course) { + // If the user is an editing teacher in there, + if (!empty($USER->groupmember[$course->id])) { + // We've already cached the users groups for this course so we can just use that + $groupids = array_merge($groupids, $USER->groupmember[$course->id]); + } else if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) { + // If this course has groups, show events from all of those related to the current user + $coursegroups = groups_get_user_groups($course->id, $USER->id); + $groupids = array_merge($groupids, $coursegroups['0']); + } + } + if (!empty($groupids)) { + $group = $groupids; + } } } }