MDL-58768 Calendar: Added userid param to calendar_get_default_courses

This commit is contained in:
Shamim Rezaie 2018-03-06 15:22:11 +11:00
parent 6e8235c7d3
commit bd8705732d
3 changed files with 37 additions and 9 deletions

View file

@ -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 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 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 * 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). * 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 * @return array $courses Array of courses to display
*/ */
function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage=false) { function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage = false, int $userid = null) {
global $CFG, $DB; global $CFG, $USER;
if (!$userid) {
if (!isloggedin()) { if (!isloggedin()) {
return array(); return array();
} }
$userid = $USER->id;
}
if (has_capability('moodle/calendar:manageentries', context_system::instance()) && if ((!empty($CFG->calendar_adminseesall) || $canmanage) &&
(!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. // Add a c. prefix to every field as expected by get_courses function.
$fieldlist = explode(',', $fields); $fieldlist = explode(',', $fields);
@ -2340,11 +2345,11 @@ function calendar_get_default_courses($courseid = null, $fields = '*', $canmanag
}, $fieldlist); }, $fieldlist);
$courses = get_courses('all', 'c.shortname', implode(',', $prefixedfields)); $courses = get_courses('all', 'c.shortname', implode(',', $prefixedfields));
} else { } else {
$courses = enrol_get_my_courses($fields); $courses = enrol_get_users_courses($userid, true, $fields);
} }
if ($courseid && $courseid != SITEID) { 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. // Allow a site admin to see calendars from courses he is not enrolled in.
// This will come from $COURSE. // This will come from $COURSE.
$courses[$courseid] = get_course($courseid); $courses[$courseid] = get_course($courseid);

View file

@ -472,6 +472,26 @@ class core_calendar_lib_testcase extends advanced_testcase {
// Enrolled course only (ignore current). // Enrolled course only (ignore current).
$this->assertCount(1, $courses); $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);
} }
/** /**

View file

@ -1,6 +1,9 @@
This files describes API changes in /calendar/* , This files describes API changes in /calendar/* ,
information provided here is intended especially for developers. information provided here is intended especially for developers.
=== 3.6 ===
* calendar_get_default_courses() function now has optional $userid parameter.
=== 3.5 === === 3.5 ===
* core_calendar_external::get_calendar_events now returns the categoryid for category events. * core_calendar_external::get_calendar_events now returns the categoryid for category events.