Commit a5327e44 authored by Damyon Wiese's avatar Damyon Wiese
Browse files

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.
parent d12e4097
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
*/ */
namespace core_calendar\local\event\forms; namespace core_calendar\local\event\forms;
use context_system;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php'); require_once($CFG->dirroot.'/lib/formslib.php');
...@@ -245,7 +247,8 @@ class create extends \moodleform { ...@@ -245,7 +247,8 @@ class create extends \moodleform {
} }
if (isset($eventtypes['course'])) { 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'); $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
} }
......
...@@ -2137,31 +2137,29 @@ function calendar_delete_event_allowed($event) { ...@@ -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 * Returns the default courses to display on the calendar when there isn't a specific
* course to display. * 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 * @return array $courses Array of courses to display
*/ */
function calendar_get_default_courses() { function calendar_get_default_courses($courseid = null) {
global $CFG, $DB; global $CFG, $DB;
if (!isloggedin()) { if (!isloggedin()) {
return array(); return array();
} }
if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', \context_system::instance())) { if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
$select = ', ' . \context_helper::get_preload_record_columns_sql('ctx'); $courses = get_courses('all', 'c.shortname', 'c.*');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; } else {
$sql = "SELECT c.* $select $courses = enrol_get_my_courses();
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;
} }
$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; return $courses;
} }
...@@ -2404,6 +2402,8 @@ function calendar_get_all_allowed_types() { ...@@ -2404,6 +2402,8 @@ function calendar_get_all_allowed_types() {
$types = []; $types = [];
$allowed = new stdClass();
calendar_get_allowed_types($allowed); calendar_get_allowed_types($allowed);
if ($allowed->user) { if ($allowed->user) {
...@@ -2421,7 +2421,12 @@ function calendar_get_all_allowed_types() { ...@@ -2421,7 +2421,12 @@ function calendar_get_all_allowed_types() {
// This function warms the context cache for the course so the calls // 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 // to load the course context in calendar_get_allowed_types don't result
// in additional DB queries. // 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 // 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 // query to avoid calendar_get_allowed_types from hitting the DB for
// each separate course. // each separate course.
......
...@@ -247,11 +247,7 @@ class core_calendar_renderer extends plugin_renderer_base { ...@@ -247,11 +247,7 @@ class core_calendar_renderer extends plugin_renderer_base {
return ''; return '';
} }
if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) { $courses = calendar_get_default_courses($courseid);
$courses = get_courses('all', 'c.shortname','c.id,c.shortname');
} else {
$courses = enrol_get_my_courses();
}
unset($courses[SITEID]); unset($courses[SITEID]);
......
...@@ -2741,7 +2741,7 @@ class global_navigation extends navigation_node { ...@@ -2741,7 +2741,7 @@ class global_navigation extends navigation_node {
* @return bool True for successfull generation * @return bool True for successfull generation
*/ */
public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { 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'); require_once($CFG->dirroot . '/course/lib.php');
if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) {
...@@ -2793,8 +2793,14 @@ class global_navigation extends navigation_node { ...@@ -2793,8 +2793,14 @@ class global_navigation extends navigation_node {
} }
if ($navoptions->calendar) { if ($navoptions->calendar) {
$courseid = $COURSE->id;
$params = array('view' => 'month');
if ($courseid != $SITE->id) {
$params['course'] = $courseid;
}
// Calendar // 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 = $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar');
$node->showinflatnavigation = true; $node->showinflatnavigation = true;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment