From 2229368a3ca032ce6cb5d0f33399a018d31c43ba Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 31 Mar 2017 11:05:05 +0800 Subject: [PATCH] MDL-58424 core_calendar: moved api::get_events This function was moved to local_api::get_legacy_events. Also removed the calendar/classes/api.php file since it no longer contained any functionality and added unit tests for local_api::get_legacy_events (a copy of the unit tests for calendar_get_events). Part of MDL-55611 epic. --- calendar/classes/api.php | 89 ----------- calendar/classes/local/api.php | 50 ++++++ calendar/export_execute.php | 4 +- calendar/externallib.php | 2 +- calendar/lib.php | 4 +- calendar/renderer.php | 2 +- calendar/tests/local_api_test.php | 252 ++++++++++++++++++++++++++++++ 7 files changed, 308 insertions(+), 95 deletions(-) delete mode 100644 calendar/classes/api.php diff --git a/calendar/classes/api.php b/calendar/classes/api.php deleted file mode 100644 index 4d55577590a..00000000000 --- a/calendar/classes/api.php +++ /dev/null @@ -1,89 +0,0 @@ -. - -/** - * Contains class containing the calendar API. - * - * @package core_calendar - * @copyright 2017 Mark Nelson - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace core_calendar; - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->dirroot . '/calendar/lib.php'); - -use core_calendar\local\api as local_api; - -/** - * Class containing the calendar API. - * - * @package core_calendar - * @copyright 2017 Mark Nelson - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class api { - - /** - * Get calendar events - * - * @param int $tstart Start time of time range for events - * @param int $tend End time of time range for events - * @param array|int|boolean $users array of users, user id or boolean for all/no user events - * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events - * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events - * @param boolean $withduration whether only events starting within time range selected - * or events in progress/already started selected as well - * @param boolean $ignorehidden whether to select only visible events or all events - * @return array $events of selected events or an empty array if there aren't any (or there was an error) - */ - public static function get_events($tstart, $tend, $users, $groups, $courses, $withduration = true, $ignorehidden = true) { - $fixedparams = array_map(function($param) { - if ($param === true) { - return null; - } - - if (!is_array($param)) { - return [$param]; - } - - return $param; - }, [$users, $groups, $courses]); - - $mapper = \core_calendar\local\event\core_container::get_event_mapper(); - $events = local_api::get_events( - $tstart, - $tend, - null, - null, - null, - null, - 40, - null, - $fixedparams[0], - $fixedparams[1], - $fixedparams[2], - $withduration, - $ignorehidden - ); - - return array_reduce($events, function($carry, $event) use ($mapper) { - return $carry + [$event->get_id() => $mapper->from_event_to_stdclass($event)]; - }, []); - } -} diff --git a/calendar/classes/local/api.php b/calendar/classes/local/api.php index e9c13266737..4aaefdb59a6 100644 --- a/calendar/classes/local/api.php +++ b/calendar/classes/local/api.php @@ -31,6 +31,8 @@ use core_calendar\local\event\exceptions\limit_invalid_parameter_exception; /** * Class containing the local calendar API. * + * This should not be used outside of core_calendar. + * * @package core_calendar * @copyright 2017 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -102,6 +104,54 @@ class api { ); } + /** + * Get legacy calendar events + * + * @param int $tstart Start time of time range for events + * @param int $tend End time of time range for events + * @param array|int|boolean $users array of users, user id or boolean for all/no user events + * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events + * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events + * @param boolean $withduration whether only events starting within time range selected + * or events in progress/already started selected as well + * @param boolean $ignorehidden whether to select only visible events or all events + * @return array $events of selected events or an empty array if there aren't any (or there was an error) + */ + public static function get_legacy_events($tstart, $tend, $users, $groups, $courses, $withduration = true, $ignorehidden = true) { + $fixedparams = array_map(function($param) { + if ($param === true) { + return null; + } + + if (!is_array($param)) { + return [$param]; + } + + return $param; + }, [$users, $groups, $courses]); + + $mapper = \core_calendar\local\event\core_container::get_event_mapper(); + $events = self::get_events( + $tstart, + $tend, + null, + null, + null, + null, + 40, + null, + $fixedparams[0], + $fixedparams[1], + $fixedparams[2], + $withduration, + $ignorehidden + ); + + return array_reduce($events, function($carry, $event) use ($mapper) { + return $carry + [$event->get_id() => $mapper->from_event_to_stdclass($event)]; + }, []); + } + /** * Get a list of action events for the logged in user by the given * timesort values. diff --git a/calendar/export_execute.php b/calendar/export_execute.php index e3abec8ab83..0d0d75a28e6 100644 --- a/calendar/export_execute.php +++ b/calendar/export_execute.php @@ -60,7 +60,7 @@ if (!empty($generateurl)) { if(!empty($what) && !empty($time)) { if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) { $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname'); - // Array of courses that we will pass to calendar_get_events() which + // Array of courses that we will pass to \core_calendar\local\api::get_legacy_events() which // is initially set to the list of the user's courses. $paramcourses = $courses; if ($what == 'all' || $what == 'groups') { @@ -180,7 +180,7 @@ if(!empty($what) && !empty($time)) { die(); } } -$events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false); +$events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false); $ical = new iCalendar; $ical->add_property('method', 'PUBLISH'); diff --git a/calendar/externallib.php b/calendar/externallib.php index c6208f4c3de..418731e6f57 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -233,7 +233,7 @@ class core_calendar_external extends external_api { } // Event list does not check visibility and permissions, we'll check that later. - $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], + $eventlist = \core_calendar\local\api::get_legacy_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'], $funcparam['courses'], true, $params['options']['ignorehidden']); // WS expects arrays. diff --git a/calendar/lib.php b/calendar/lib.php index 8a7b200e605..01abe8d38b7 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -1398,7 +1398,7 @@ function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyea } // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ. - $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); + $events = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $users, $groups, $courses); // Set event course class for course events. if (!empty($events)) { @@ -1715,7 +1715,7 @@ function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxeve $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1; // Get the events matching our criteria. - $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); + $events = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $users, $groups, $courses); // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month diff --git a/calendar/renderer.php b/calendar/renderer.php index ec209c34880..014262c15a5 100644 --- a/calendar/renderer.php +++ b/calendar/renderer.php @@ -371,7 +371,7 @@ class core_calendar_renderer extends plugin_renderer_base { } // Get events from database - $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, + $events = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses); if (!empty($events)) { foreach($events as $eventid => $event) { diff --git a/calendar/tests/local_api_test.php b/calendar/tests/local_api_test.php index 1bf3cac8ccd..599a6052048 100644 --- a/calendar/tests/local_api_test.php +++ b/calendar/tests/local_api_test.php @@ -606,4 +606,256 @@ class core_calendar_local_api_testcase extends advanced_testcase { $this->assertCount(1, $result[$course3->id]); $this->assertEquals('Event 6', $result[$course3->id][0]->get_name()); } + + /** + * Test that the get_legacy_events() function only returns activity events that are enabled. + */ + public function test_get_legacy_events_with_disabled_module() { + global $DB; + + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + + $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); + $assigninstance = $assigngenerator->create_instance(['course' => $course->id]); + + $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + $lessoninstance = $lessongenerator->create_instance(['course' => $course->id]); + $student = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student'); + $this->setUser($student); + $events = [ + [ + 'name' => 'Start of assignment', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => 0, + 'userid' => 2, + 'modulename' => 'assign', + 'instance' => $assigninstance->id, + 'eventtype' => 'due', + 'timestart' => time(), + 'timeduration' => 86400, + 'visible' => 1 + ], [ + 'name' => 'Start of lesson', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => 0, + 'userid' => 2, + 'modulename' => 'lesson', + 'instance' => $lessoninstance->id, + 'eventtype' => 'end', + 'timestart' => time(), + 'timeduration' => 86400, + 'visible' => 1 + ] + ]; + foreach ($events as $event) { + calendar_event::create($event, false); + } + $timestart = time() - 60; + $timeend = time() + 60; + + // Get all events. + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, true, 0, true); + $this->assertCount(2, $events); + + // Disable the lesson module. + $modulerecord = $DB->get_record('modules', ['name' => 'lesson']); + $modulerecord->visible = 0; + $DB->update_record('modules', $modulerecord); + + // Check that we only return the assign event. + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, true, 0, true); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('assign', $event->modulename); + } + + /** + * Test for \core_calendar\local\api::get_legacy_events() when there are user and group overrides. + */ + public function test_get_legacy_events_with_overrides() { + $generator = $this->getDataGenerator(); + + $course = $generator->create_course(); + + $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); + if (!isset($params['course'])) { + $params['course'] = $course->id; + } + + $instance = $plugingenerator->create_instance($params); + + // Create users. + $useroverridestudent = $generator->create_user(); + $group1student = $generator->create_user(); + $group2student = $generator->create_user(); + $group12student = $generator->create_user(); + $nogroupstudent = $generator->create_user(); + + // Enrol users. + $generator->enrol_user($useroverridestudent->id, $course->id, 'student'); + $generator->enrol_user($group1student->id, $course->id, 'student'); + $generator->enrol_user($group2student->id, $course->id, 'student'); + $generator->enrol_user($group12student->id, $course->id, 'student'); + $generator->enrol_user($nogroupstudent->id, $course->id, 'student'); + + // Create groups. + $group1 = $generator->create_group(['courseid' => $course->id]); + $group2 = $generator->create_group(['courseid' => $course->id]); + + // Add members to groups. + $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]); + $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]); + $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]); + $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]); + $now = time(); + + // Events with the same module name, instance and event type. + $events = [ + [ + 'name' => 'Assignment 1 due date', + 'description' => '', + 'format' => 0, + 'courseid' => $course->id, + 'groupid' => 0, + 'userid' => 2, + 'modulename' => 'assign', + 'instance' => $instance->id, + 'eventtype' => 'due', + 'timestart' => $now, + 'timeduration' => 0, + 'visible' => 1 + ], [ + 'name' => 'Assignment 1 due date - User override', + 'description' => '', + 'format' => 1, + 'courseid' => 0, + 'groupid' => 0, + 'userid' => $useroverridestudent->id, + 'modulename' => 'assign', + 'instance' => $instance->id, + 'eventtype' => 'due', + 'timestart' => $now + 86400, + 'timeduration' => 0, + 'visible' => 1, + 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY + ], [ + 'name' => 'Assignment 1 due date - Group A override', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => $group1->id, + 'userid' => 2, + 'modulename' => 'assign', + 'instance' => $instance->id, + 'eventtype' => 'due', + 'timestart' => $now + (2 * 86400), + 'timeduration' => 0, + 'visible' => 1, + 'priority' => 1, + ], [ + 'name' => 'Assignment 1 due date - Group B override', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => $group2->id, + 'userid' => 2, + 'modulename' => 'assign', + 'instance' => $instance->id, + 'eventtype' => 'due', + 'timestart' => $now + (3 * 86400), + 'timeduration' => 0, + 'visible' => 1, + 'priority' => 2, + ], + ]; + + foreach ($events as $event) { + calendar_event::create($event, false); + } + + $timestart = $now - 100; + $timeend = $now + (3 * 86400); + $groups = [$group1->id, $group2->id]; + + // Get user override events. + $this->setUser($useroverridestudent); + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('Assignment 1 due date - User override', $event->name); + + // Get event for user with override but with the timestart and timeend parameters only covering the original event. + $events = \core_calendar\local\api::get_legacy_events($timestart, $now, $useroverridestudent->id, $groups, $course->id); + $this->assertCount(0, $events); + + // Get events for user that does not belong to any group and has no user override events. + $this->setUser($nogroupstudent); + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('Assignment 1 due date', $event->name); + + // Get events for user that belongs to groups A and B and has no user override events. + $this->setUser($group12student); + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $group12student->id, $groups, $course->id); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('Assignment 1 due date - Group B override', $event->name); + + // Get events for user that belongs to group A and has no user override events. + $this->setUser($group1student); + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $group1student->id, $groups, $course->id); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('Assignment 1 due date - Group A override', $event->name); + + // Add repeating events. + $repeatingevents = [ + [ + 'name' => 'Repeating site event', + 'description' => '', + 'format' => 1, + 'courseid' => SITEID, + 'groupid' => 0, + 'userid' => 2, + 'repeatid' => $event->id, + 'modulename' => '0', + 'instance' => 0, + 'eventtype' => 'site', + 'timestart' => $now + 86400, + 'timeduration' => 0, + 'visible' => 1, + ], + [ + 'name' => 'Repeating site event', + 'description' => '', + 'format' => 1, + 'courseid' => SITEID, + 'groupid' => 0, + 'userid' => 2, + 'repeatid' => $event->id, + 'modulename' => '0', + 'instance' => 0, + 'eventtype' => 'site', + 'timestart' => $now + (2 * 86400), + 'timeduration' => 0, + 'visible' => 1, + ], + ]; + + foreach ($repeatingevents as $event) { + calendar_event::create($event, false); + } + + // Make sure repeating events are not filtered out. + $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, true, true, true); + $this->assertCount(3, $events); + } }