MDL-58566 calendar: Move \core_calendar\local\api::get_legacy_events()
authorJun Pataleta <jun@moodle.com>
Wed, 12 Apr 2017 06:01:10 +0000 (14:01 +0800)
committerJun Pataleta <jun@moodle.com>
Tue, 18 Apr 2017 02:32:09 +0000 (10:32 +0800)
* Move and rename \core_calendar\local\api::get_legacy_events() to
  calendar_get_legacy_events() in calendar/lib.php.
* Fix the parameter-normalising callback function in
  calendar_get_legacy_events() so that it correctly handles false values
  by returning an empty array instead of it returning a non-empty array
  that contains an empty element.

calendar/classes/local/api.php
calendar/export_execute.php
calendar/externallib.php
calendar/lib.php
calendar/renderer.php
calendar/tests/lib_test.php
calendar/tests/local_api_test.php

index 18682ff..4eb25fd 100644 (file)
@@ -104,62 +104,6 @@ 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\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.
index 0d0d75a..41e611d 100644 (file)
@@ -180,7 +180,7 @@ if(!empty($what) && !empty($time)) {
         die();
     }
 }
-$events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
+$events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
 
 $ical = new iCalendar;
 $ical->add_property('method', 'PUBLISH');
index 418731e..074f0e0 100644 (file)
@@ -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 = \core_calendar\local\api::get_legacy_events($params['options']['timestart'], $params['options']['timeend'],
+        $eventlist = calendar_get_legacy_events($params['options']['timestart'], $params['options']['timeend'],
             $funcparam['users'], $funcparam['groups'], $funcparam['courses'], true, $params['options']['ignorehidden']);
 
         // WS expects arrays.
index 01abe8d..94233f1 100644 (file)
@@ -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 = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $users, $groups, $courses);
+    $events = calendar_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 = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $users, $groups, $courses);
+    $events = calendar_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
@@ -3374,3 +3374,62 @@ function core_calendar_user_preferences() {
         'choices' => array(0, 1));
     return $preferences;
 }
+
+/**
+ * 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)
+ */
+function calendar_get_legacy_events($tstart, $tend, $users, $groups, $courses, $withduration = true, $ignorehidden = true) {
+    // Normalise the users, groups and courses parameters so that they are compliant with \core_calendar\local\api::get_events().
+    // Existing functions that were using the old calendar_get_events() were passing a mixture of array, int, boolean for these
+    // parameters, but with the new API method, only null and arrays are accepted.
+    list($userparam, $groupparam, $courseparam) = array_map(function($param) {
+        // If parameter is true, return null.
+        if ($param === true) {
+            return null;
+        }
+
+        // If parameter is false, return an empty array.
+        if ($param === false) {
+            return [];
+        }
+
+        // If the parameter is a scalar value, enclose it in an array.
+        if (!is_array($param)) {
+            return [$param];
+        }
+
+        // No normalisation required.
+        return $param;
+    }, [$users, $groups, $courses]);
+
+    $mapper = \core_calendar\local\event\container::get_event_mapper();
+    $events = \core_calendar\local\api::get_events(
+        $tstart,
+        $tend,
+        null,
+        null,
+        null,
+        null,
+        40,
+        null,
+        $userparam,
+        $groupparam,
+        $courseparam,
+        $withduration,
+        $ignorehidden
+    );
+
+    return array_reduce($events, function($carry, $event) use ($mapper) {
+        return $carry + [$event->get_id() => $mapper->from_event_to_stdclass($event)];
+    }, []);
+}
index 014262c..9a8de95 100644 (file)
@@ -371,7 +371,7 @@ class core_calendar_renderer extends plugin_renderer_base {
         }
 
         // Get events from database
-        $events = \core_calendar\local\api::get_legacy_events($display->tstart, $display->tend, $calendar->users, $calendar->groups,
+        $events = calendar_get_legacy_events($display->tstart, $display->tend, $calendar->users, $calendar->groups,
             $calendar->courses);
         if (!empty($events)) {
             foreach($events as $eventid => $event) {
index 797dc89..750bb90 100644 (file)
@@ -392,4 +392,187 @@ class core_calendar_lib_testcase extends advanced_testcase {
         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
         $this->assertEquals($count, 1);
     }
+
+    /**
+     * Test for calendar_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 = calendar_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 = calendar_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 = calendar_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 = calendar_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 = calendar_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 = calendar_get_legacy_events($timestart, $timeend, true, true, true);
+        $this->assertCount(3, $events);
+    }
 }
\ No newline at end of file
index 599a605..b206454 100644 (file)
@@ -661,7 +661,7 @@ class core_calendar_local_api_testcase extends advanced_testcase {
         $timeend = time() + 60;
 
         // Get all events.
-        $events = \core_calendar\local\api::get_legacy_events($timestart, $timeend, true, 0, true);
+        $events = calendar_get_legacy_events($timestart, $timeend, true, 0, true);
         $this->assertCount(2, $events);
 
         // Disable the lesson module.
@@ -670,192 +670,9 @@ class core_calendar_local_api_testcase extends advanced_testcase {
         $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);
+        $events = calendar_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);
-    }
 }