From a5327e449dee0da96348fb820959e214f85bddc9 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Tue, 24 Oct 2017 15:18:18 +0800 Subject: [PATCH] MDL-59973 calendar: Admin calendar course lists Consolidate the logic for the list of courses an admin will see in the calendar. They will see all courses if calendar_admincanseeall is enabled. They will see only courses they are enrolled in of calendar_admincanseeall is not enabled. They will also see the current course, if they are admin and it is not already in the list. This applies to the course selector and the create and update event forms. The calendar link in the navigation block has also been changed to link to the current course calendar instead of the global one. --- calendar/classes/local/event/forms/create.php | 5 ++- calendar/lib.php | 37 +++++++++++-------- calendar/renderer.php | 6 +-- lib/navigationlib.php | 10 ++++- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/calendar/classes/local/event/forms/create.php b/calendar/classes/local/event/forms/create.php index 0beb6e00bad..0707370b4f7 100644 --- a/calendar/classes/local/event/forms/create.php +++ b/calendar/classes/local/event/forms/create.php @@ -23,6 +23,8 @@ */ namespace core_calendar\local\event\forms; +use context_system; + defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/lib/formslib.php'); @@ -245,7 +247,8 @@ class create extends \moodleform { } if (isset($eventtypes['course'])) { - $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => true]); + $limit = !has_capability('moodle/calendar:manageentries', context_system::instance()); + $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => $limit]); $mform->hideIf('courseid', 'eventtype', 'noteq', 'course'); } diff --git a/calendar/lib.php b/calendar/lib.php index d858b9f28e9..69b7e6cbca6 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2137,31 +2137,29 @@ function calendar_delete_event_allowed($event) { * Returns the default courses to display on the calendar when there isn't a specific * course to display. * + * @param int $courseid (optional) If passed, an additional course can be returned for admins (the current course). * @return array $courses Array of courses to display */ -function calendar_get_default_courses() { +function calendar_get_default_courses($courseid = null) { global $CFG, $DB; if (!isloggedin()) { return array(); } - if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', \context_system::instance())) { - $select = ', ' . \context_helper::get_preload_record_columns_sql('ctx'); - $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; - $sql = "SELECT c.* $select - FROM {course} c - $join - WHERE EXISTS (SELECT 1 FROM {event} e WHERE e.courseid = c.id) - "; - $courses = $DB->get_records_sql($sql, array('contextlevel' => CONTEXT_COURSE), 0, 20); - foreach ($courses as $course) { - \context_helper::preload_from_record($course); - } - return $courses; + if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) { + $courses = get_courses('all', 'c.shortname', 'c.*'); + } else { + $courses = enrol_get_my_courses(); } - $courses = enrol_get_my_courses(); + if ($courseid && $courseid != SITEID) { + if (empty($courses[$courseid]) && has_capability('moodle/calendar:manageentries', context_system::instance())) { + // Allow a site admin to see calendars from courses he is not enrolled in. + // This will come from $COURSE. + $courses[$courseid] = get_course($courseid); + } + } return $courses; } @@ -2404,6 +2402,8 @@ function calendar_get_all_allowed_types() { $types = []; + $allowed = new stdClass(); + calendar_get_allowed_types($allowed); if ($allowed->user) { @@ -2421,7 +2421,12 @@ function calendar_get_all_allowed_types() { // This function warms the context cache for the course so the calls // to load the course context in calendar_get_allowed_types don't result // in additional DB queries. - $courses = enrol_get_users_courses($USER->id, true); + if (has_capability('moodle/calendar:manageentries', context_system::instance())) { + $courses = get_courses('all', 'c.shortname', 'c.*'); + } else { + $courses = calendar_get_default_courses(); + } + // We want to pre-fetch all of the groups for each course in a single // query to avoid calendar_get_allowed_types from hitting the DB for // each separate course. diff --git a/calendar/renderer.php b/calendar/renderer.php index 5688e2766c2..b5d36b7e5cc 100644 --- a/calendar/renderer.php +++ b/calendar/renderer.php @@ -247,11 +247,7 @@ class core_calendar_renderer extends plugin_renderer_base { return ''; } - if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) { - $courses = get_courses('all', 'c.shortname','c.id,c.shortname'); - } else { - $courses = enrol_get_my_courses(); - } + $courses = calendar_get_default_courses($courseid); unset($courses[SITEID]); diff --git a/lib/navigationlib.php b/lib/navigationlib.php index a239ebcd7cf..7c46fc357f5 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2741,7 +2741,7 @@ class global_navigation extends navigation_node { * @return bool True for successfull generation */ public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { - global $CFG, $USER; + global $CFG, $USER, $COURSE, $SITE; require_once($CFG->dirroot . '/course/lib.php'); if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { @@ -2793,8 +2793,14 @@ class global_navigation extends navigation_node { } if ($navoptions->calendar) { + $courseid = $COURSE->id; + $params = array('view' => 'month'); + if ($courseid != $SITE->id) { + $params['course'] = $courseid; + } + // Calendar - $calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month')); + $calendarurl = new moodle_url('/calendar/view.php', $params); $node = $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar'); $node->showinflatnavigation = true; }