mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 12:40:01 +01:00
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.
This commit is contained in:
parent
d12e409784
commit
a5327e449d
@ -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');
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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]);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user