Permalink
Browse files

Merge branch 'MDL-59973-master' of git://github.com/damyon/moodle

  • Loading branch information...
dmonllao committed Oct 26, 2017
2 parents 341505e + ed6d81c commit fd39db29e247ddff8379753331f33c03d0d3b99a
@@ -23,6 +23,8 @@
*/
namespace core_calendar\local\event\forms;
use context_system;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php');
@@ -246,7 +248,8 @@ protected function add_event_type_elements($mform, $eventtypes) {
}
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,41 @@ 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).
* @param string $fields Comma separated list of course fields to return.
* @param bool $canmanage If true, this will return the list of courses the current user can create events in, rather
* than the list of courses they see events from (an admin can always add events in a course
* calendar, even if they are not enrolled in the course).
* @return array $courses Array of courses to display
*/
function calendar_get_default_courses() {
function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage=false) {
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) || $canmanage)) {
// Add a c. prefix to every field as expected by get_courses function.
$fieldlist = explode(',', $fields);
$prefixedfields = array_map(function($value) {
return 'c.' . trim($value);
}, $fieldlist);
$courses = get_courses('all', 'c.shortname', implode(',', $prefixedfields));
} else {
$courses = enrol_get_my_courses($fields);
}
$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 +2414,8 @@ function calendar_get_all_allowed_types() {
$types = [];
$allowed = new stdClass();
calendar_get_allowed_types($allowed);
if ($allowed->user) {
@@ -2421,7 +2433,8 @@ 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);
$courses = calendar_get_default_courses(null, '*', true);
// 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 @@ public function course_filter_selector(moodle_url $returnurl, $label = null, $co
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, 'id, shortname');
unset($courses[SITEID]);
@@ -672,4 +672,60 @@ public function test_calendar_get_all_allowed_types_group_no_access_all_groups()
$this->assertEquals($group1->id, $typegroups[0]->id);
$this->assertEquals($group2->id, $typegroups[1]->id);
}
public function test_calendar_get_default_courses() {
global $USER, $CFG;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course1 = $generator->create_course();
$course2 = $generator->create_course();
$course3 = $generator->create_course();
$context = context_course::instance($course1->id);
$this->setAdminUser();
$admin = clone $USER;
$teacher = $generator->create_user();
$generator->enrol_user($teacher->id, $course1->id, 'teacher');
$generator->enrol_user($admin->id, $course1->id, 'teacher');
$CFG->calendar_adminseesall = false;
$courses = calendar_get_default_courses();
// Only enrolled in one course.
$this->assertCount(1, $courses);
$courses = calendar_get_default_courses($course2->id);
// Enrolled course + current course.
$this->assertCount(2, $courses);
$CFG->calendar_adminseesall = true;
$courses = calendar_get_default_courses();
// All courses + SITE.
$this->assertCount(4, $courses);
$courses = calendar_get_default_courses($course2->id);
// All courses + SITE.
$this->assertCount(4, $courses);
$this->setUser($teacher);
$CFG->calendar_adminseesall = false;
$courses = calendar_get_default_courses();
// Only enrolled in one course.
$this->assertCount(1, $courses);
$courses = calendar_get_default_courses($course2->id);
// Enrolled course only (ignore current).
$this->assertCount(1, $courses);
// This setting should not affect teachers.
$CFG->calendar_adminseesall = true;
$courses = calendar_get_default_courses();
// Only enrolled in one course.
$this->assertCount(1, $courses);
$courses = calendar_get_default_courses($course2->id);
// Enrolled course only (ignore current).
$this->assertCount(1, $courses);
}
}
@@ -581,7 +581,7 @@
$string['gradeexport'] = 'Primary grade export methods';
$string['guestroleid'] = 'Role for guest';
$string['guestroleid_help'] = 'This role is automatically assigned to the guest user. It is also temporarily assigned to not enrolled users that enter the course via guest enrolment plugin.';
$string['helpadminseesall'] = 'Do admins see all calendar events or just those that apply to themselves?';
$string['helpadminseesall'] = 'In the site calendar, do admins see and filter events from all course calendars or just those from courses they are enrolled in? Regardless of the chosen option, admins will always be able to manage events for each course calendar by navigating to the course first, and then accessing the course calendar directly.';
$string['helpcalendarcustomexport'] = 'Enable custom date range export option in calendar exports. Calendar exports must be enabled before this is effective.';
$string['helpexportlookahead'] = 'How many days in the future does the calendar look for events during export for the custom export option?';
$string['helpexportlookback'] = 'How many days in the past does the calendar look for events during export for the custom export option?';
@@ -2741,7 +2741,7 @@ public function add_course_essentials($coursenode, stdClass $course) {
* @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 @@ public function add_front_page_course_essentials(navigation_node $coursenode, st
}
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;
}

0 comments on commit fd39db2

Please sign in to comment.