mirror of
https://github.com/moodle/moodle.git
synced 2025-04-19 07:25:30 +02:00
MDL-58768 Calendar: Added user param to calendar_set_filters
Decoupled calendar_set_filters from $USER. Also removed the backward compatibility bit from the function because it was a left over.
This commit is contained in:
parent
bd8705732d
commit
d8c6c21c95
@ -2039,34 +2039,29 @@ function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$duratio
|
||||
*
|
||||
* @param array $courseeventsfrom An array of courses to load calendar events for
|
||||
* @param bool $ignorefilters specify the use of filters, false is set as default
|
||||
* @param stdClass $user The user object. This defaults to the global $USER object.
|
||||
* @return array An array of courses, groups, and user to load calendar events for based upon filters
|
||||
*/
|
||||
function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) {
|
||||
global $USER, $CFG;
|
||||
function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false, stdClass $user = null) {
|
||||
global $CFG, $USER;
|
||||
|
||||
// For backwards compatability we have to check whether the courses array contains
|
||||
// just id's in which case we need to load course objects.
|
||||
$coursestoload = array();
|
||||
foreach ($courseeventsfrom as $id => $something) {
|
||||
if (!is_object($something)) {
|
||||
$coursestoload[] = $id;
|
||||
unset($courseeventsfrom[$id]);
|
||||
}
|
||||
if (is_null($user)) {
|
||||
$user = $USER;
|
||||
}
|
||||
|
||||
$courses = array();
|
||||
$user = false;
|
||||
$userid = false;
|
||||
$group = false;
|
||||
|
||||
// Get the capabilities that allow seeing group events from all groups.
|
||||
$allgroupscaps = array('moodle/site:accessallgroups', 'moodle/calendar:manageentries');
|
||||
|
||||
$isloggedin = isloggedin();
|
||||
$isvaliduser = !empty($user->id);
|
||||
|
||||
if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
|
||||
if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE, $user)) {
|
||||
$courses = array_keys($courseeventsfrom);
|
||||
}
|
||||
if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
|
||||
if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_GLOBAL, $user)) {
|
||||
$courses[] = SITEID;
|
||||
}
|
||||
$courses = array_unique($courses);
|
||||
@ -2080,11 +2075,11 @@ function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) {
|
||||
$courses[] = SITEID;
|
||||
}
|
||||
|
||||
if ($ignorefilters || ($isloggedin && calendar_show_event_type(CALENDAR_EVENT_USER))) {
|
||||
$user = $USER->id;
|
||||
if ($ignorefilters || ($isvaliduser && calendar_show_event_type(CALENDAR_EVENT_USER, $user))) {
|
||||
$userid = $user->id;
|
||||
}
|
||||
|
||||
if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP) || $ignorefilters)) {
|
||||
if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP, $user) || $ignorefilters)) {
|
||||
|
||||
if (count($courseeventsfrom) == 1) {
|
||||
$course = reset($courseeventsfrom);
|
||||
@ -2096,16 +2091,16 @@ function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) {
|
||||
if ($group === false) {
|
||||
if (!empty($CFG->calendar_adminseesall) && has_any_capability($allgroupscaps, \context_system::instance())) {
|
||||
$group = true;
|
||||
} else if ($isloggedin) {
|
||||
} else if ($isvaliduser) {
|
||||
$groupids = array();
|
||||
foreach ($courseeventsfrom as $courseid => $course) {
|
||||
// If the user is an editing teacher in there.
|
||||
if (!empty($USER->groupmember[$course->id])) {
|
||||
if (!empty($user->groupmember[$course->id])) {
|
||||
// We've already cached the users groups for this course so we can just use that.
|
||||
$groupids = array_merge($groupids, $USER->groupmember[$course->id]);
|
||||
$groupids = array_merge($groupids, $user->groupmember[$course->id]);
|
||||
} else if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
|
||||
// If this course has groups, show events from all of those related to the current user.
|
||||
$coursegroups = groups_get_user_groups($course->id, $USER->id);
|
||||
$coursegroups = groups_get_user_groups($course->id, $user->id);
|
||||
$groupids = array_merge($groupids, $coursegroups['0']);
|
||||
}
|
||||
}
|
||||
@ -2119,7 +2114,7 @@ function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) {
|
||||
$courses = false;
|
||||
}
|
||||
|
||||
return array($courses, $group, $user);
|
||||
return array($courses, $group, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -647,4 +647,129 @@ class core_calendar_lib_testcase extends advanced_testcase {
|
||||
$types = calendar_get_allowed_event_types($course->id);
|
||||
$this->assertTrue($types['group']);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a setup helper function that create some users, courses, groups and group memberships.
|
||||
* This is useful to prepare the environment for testing the calendar_set_filters function.
|
||||
*
|
||||
* @return array An array of ($users, $courses, $coursegroups)
|
||||
*/
|
||||
protected function setup_test_calendar_set_filters() {
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Create some users.
|
||||
$users = [];
|
||||
$users[] = $generator->create_user();
|
||||
$users[] = $generator->create_user();
|
||||
$users[] = $generator->create_user();
|
||||
|
||||
// Create some courses.
|
||||
$courses = [];
|
||||
$courses[] = $generator->create_course();
|
||||
$courses[] = $generator->create_course();
|
||||
$courses[] = $generator->create_course();
|
||||
$courses[] = $generator->create_course();
|
||||
|
||||
// Create some groups.
|
||||
$coursegroups = [];
|
||||
$coursegroups[$courses[0]->id] = [];
|
||||
$coursegroups[$courses[0]->id][] = $generator->create_group(['courseid' => $courses[0]->id]);
|
||||
$coursegroups[$courses[0]->id][] = $generator->create_group(['courseid' => $courses[0]->id]);
|
||||
$coursegroups[$courses[2]->id] = [];
|
||||
$coursegroups[$courses[2]->id][] = $generator->create_group(['courseid' => $courses[2]->id]);
|
||||
$coursegroups[$courses[2]->id][] = $generator->create_group(['courseid' => $courses[2]->id]);
|
||||
$coursegroups[$courses[3]->id] = [];
|
||||
$coursegroups[$courses[3]->id][] = $generator->create_group(['courseid' => $courses[3]->id]);
|
||||
$coursegroups[$courses[3]->id][] = $generator->create_group(['courseid' => $courses[3]->id]);
|
||||
|
||||
// Create some enrolments and group memberships.
|
||||
$generator->enrol_user($users[0]->id, $courses[0]->id, 'student');
|
||||
$generator->create_group_member(['groupid' => $coursegroups[$courses[0]->id][0]->id, 'userid' => $users[0]->id]);
|
||||
$generator->enrol_user($users[1]->id, $courses[0]->id, 'student');
|
||||
$generator->create_group_member(['groupid' => $coursegroups[$courses[0]->id][1]->id, 'userid' => $users[1]->id]);
|
||||
$generator->enrol_user($users[0]->id, $courses[1]->id, 'student');
|
||||
$generator->enrol_user($users[0]->id, $courses[2]->id, 'student');
|
||||
|
||||
return array($users, $courses, $coursegroups);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tests calendar_set_filters for the case when user is not logged in.
|
||||
*/
|
||||
public function test_calendar_set_filters_not_logged_in() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
|
||||
|
||||
$defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
|
||||
list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses);
|
||||
|
||||
$this->assertEquals(
|
||||
[$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID],
|
||||
array_values($courseids),
|
||||
'', 0.0, 10, true);
|
||||
$this->assertFalse($groupids);
|
||||
$this->assertFalse($userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tests calendar_set_filters for the case when no one is logged in, but a user id is provided.
|
||||
*/
|
||||
public function test_calendar_set_filters_not_logged_in_with_user() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
|
||||
|
||||
$defaultcourses = calendar_get_default_courses(null, '*', false, $users[1]->id);
|
||||
list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[1]);
|
||||
|
||||
$this->assertEquals(array($courses[0]->id, SITEID), array_values($courseids));
|
||||
$this->assertEquals(array($coursegroups[$courses[0]->id][1]->id), $groupids);
|
||||
$this->assertEquals($users[1]->id, $userid);
|
||||
|
||||
$defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
|
||||
list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[0]);
|
||||
|
||||
$this->assertEquals(
|
||||
[$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID],
|
||||
array_values($courseids),
|
||||
'', 0.0, 10, true);
|
||||
$this->assertEquals(array($coursegroups[$courses[0]->id][0]->id), $groupids);
|
||||
$this->assertEquals($users[0]->id, $userid);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tests calendar_set_filters for the case when user is logged in, but no user id is provided.
|
||||
*/
|
||||
public function test_calendar_set_filters_logged_in_no_user() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
|
||||
|
||||
$this->setUser($users[0]);
|
||||
$defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
|
||||
list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false);
|
||||
$this->assertEquals([$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID], array_values($courseids), '', 0.0, 10,
|
||||
true);
|
||||
$this->assertEquals(array($coursegroups[$courses[0]->id][0]->id), $groupids);
|
||||
$this->assertEquals($users[0]->id, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tests calendar_set_filters for the case when a user is logged in, but another user id is provided.
|
||||
*/
|
||||
public function test_calendar_set_filters_logged_in_another_user() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
|
||||
|
||||
$this->setUser($users[0]);
|
||||
$defaultcourses = calendar_get_default_courses(null, '*', false, $users[1]->id);
|
||||
list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[1]);
|
||||
|
||||
$this->assertEquals(array($courses[0]->id, SITEID), array_values($courseids));
|
||||
$this->assertEquals(array($coursegroups[$courses[0]->id][1]->id), $groupids);
|
||||
$this->assertEquals($users[1]->id, $userid);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ information provided here is intended especially for developers.
|
||||
|
||||
=== 3.6 ===
|
||||
* calendar_get_default_courses() function now has optional $userid parameter.
|
||||
* calendar_set_filters() function now has optional $user parameter.
|
||||
|
||||
=== 3.5 ===
|
||||
* core_calendar_external::get_calendar_events now returns the categoryid for category events.
|
||||
|
Loading…
x
Reference in New Issue
Block a user