Skip to content

Commit

Permalink
MDL-58424 core_calendar: moved api::get_events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdjnelson authored and danpoltawski committed Apr 4, 2017
1 parent f8443a2 commit 2229368
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 95 deletions.
89 changes: 0 additions & 89 deletions calendar/classes/api.php

This file was deleted.

50 changes: 50 additions & 0 deletions calendar/classes/local/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
/**
* Class containing the local calendar API.
*
* This should not be used outside of core_calendar.
*
* @package core_calendar
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down Expand Up @@ -102,6 +104,54 @@ public static function get_events(
);
}

/**
* 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.
Expand Down
4 changes: 2 additions & 2 deletions calendar/export_execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
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') {
Expand Down Expand Up @@ -180,7 +180,7 @@
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');
Expand Down
2 changes: 1 addition & 1 deletion calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static function get_calendar_events($events = array(), $options = array()
}

// 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.
Expand Down
4 changes: 2 additions & 2 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion calendar/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function show_month_detailed(calendar_information $calendar, moodle_url $
}

// 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) {
Expand Down
Loading

0 comments on commit 2229368

Please sign in to comment.