diff --git a/calendar/lib.php b/calendar/lib.php index 15dee2ae977..b9aa08c89e1 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2317,20 +2317,25 @@ function calendar_delete_event_allowed($event) { * * @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 + * @param bool $canmanage If true, this will return the list of courses the 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). + * @param int $userid (optional) The user which this function returns the default courses for. + * By default the current user. * @return array $courses Array of courses to display */ -function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage=false) { - global $CFG, $DB; +function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage = false, int $userid = null) { + global $CFG, $USER; - if (!isloggedin()) { - return array(); + if (!$userid) { + if (!isloggedin()) { + return array(); + } + $userid = $USER->id; } - if (has_capability('moodle/calendar:manageentries', context_system::instance()) && - (!empty($CFG->calendar_adminseesall) || $canmanage)) { + if ((!empty($CFG->calendar_adminseesall) || $canmanage) && + has_capability('moodle/calendar:manageentries', context_system::instance(), $userid)) { // Add a c. prefix to every field as expected by get_courses function. $fieldlist = explode(',', $fields); @@ -2340,11 +2345,11 @@ function calendar_get_default_courses($courseid = null, $fields = '*', $canmanag }, $fieldlist); $courses = get_courses('all', 'c.shortname', implode(',', $prefixedfields)); } else { - $courses = enrol_get_my_courses($fields); + $courses = enrol_get_users_courses($userid, true, $fields); } if ($courseid && $courseid != SITEID) { - if (empty($courses[$courseid]) && has_capability('moodle/calendar:manageentries', context_system::instance())) { + if (empty($courses[$courseid]) && has_capability('moodle/calendar:manageentries', context_system::instance(), $userid)) { // Allow a site admin to see calendars from courses he is not enrolled in. // This will come from $COURSE. $courses[$courseid] = get_course($courseid); diff --git a/calendar/tests/lib_test.php b/calendar/tests/lib_test.php index e50e58c2a5e..0e3b4c4441e 100644 --- a/calendar/tests/lib_test.php +++ b/calendar/tests/lib_test.php @@ -472,6 +472,26 @@ class core_calendar_lib_testcase extends advanced_testcase { // Enrolled course only (ignore current). $this->assertCount(1, $courses); + // Now, log out and test again. + $this->setUser(); + + $CFG->calendar_adminseesall = false; + + $courses = calendar_get_default_courses(null, '*', false, $teacher->id); + // Only enrolled in one course. + $this->assertCount(1, $courses); + $courses = calendar_get_default_courses($course2->id, '*', false, $teacher->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(null, '*', false, $teacher->id); + // Only enrolled in one course. + $this->assertCount(1, $courses); + $courses = calendar_get_default_courses($course2->id, '*', false, $teacher->id); + // Enrolled course only (ignore current). + $this->assertCount(1, $courses); + } /** diff --git a/calendar/upgrade.txt b/calendar/upgrade.txt index 359ee3e796b..733ddaee18a 100644 --- a/calendar/upgrade.txt +++ b/calendar/upgrade.txt @@ -1,6 +1,9 @@ This files describes API changes in /calendar/* , information provided here is intended especially for developers. +=== 3.6 === +* calendar_get_default_courses() function now has optional $userid parameter. + === 3.5 === * core_calendar_external::get_calendar_events now returns the categoryid for category events.