MDL-58768 Calendar: Added userid param to calendar_get_default_courses

This commit is contained in:
Shamim Rezaie
2018-07-31 15:31:58 +10:00
parent e8ebf0f088
commit c71fe8690c
3 changed files with 37 additions and 9 deletions
+14 -9
View File
@@ -2303,20 +2303,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);
@@ -2326,11 +2331,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);
+20
View File
@@ -727,6 +727,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);
}
public function test_calendar_get_allowed_event_types_no_types() {
+3
View File
@@ -1,6 +1,9 @@
This files describes API changes in /calendar/* ,
information provided here is intended especially for developers.
=== 3.4.5 ===
* calendar_get_default_courses() function now has optional $userid parameter.
=== 3.4 ===
* calendar_get_mini, and calendar_get_upcoming have been deprecated. Please update to use the new exporters and renderers.
* added core_calendar_get_valid_event_timestart_range and core_calendar_event_timestart_updated callbacks for module events