MDL-57502 calendar: add local api class for internal use
[moodle.git] / calendar / classes / local / api.php
CommitLineData
392d6a49
RW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains class containing the internal calendar API.
19 *
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_calendar\local;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Class containing the local calendar API.
31 *
32 * @package core_calendar
33 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class api {
37
38 /**
39 * Get a list of action events for the logged in user by the given
40 * timesort values.
41 *
42 * @param int|null $timesortfrom The start timesort value (inclusive)
43 * @param int|null $timesortto The end timesort value (inclusive)
44 * @param int|null $aftereventid Only return events after this one
45 * @param int $limitnum Limit results to this amount (between 1 and 50)
46 * @return array A list of action_event_interface objects
47 */
48 public static function get_action_events_by_timesort(
49 $timesortfrom = null,
50 $timesortto = null,
51 $aftereventid = null,
52 $limitnum = 20
53 ) {
54 global $USER;
55
56 if (is_null($timesortfrom) && is_null($timesortto)) {
57 throw new \moodle_exception("Must provide a timesort to and/or from value");
58 }
59
60 if ($limitnum < 1 || $limitnum > 50) {
61 throw new \moodle_exception("Limit must be between 1 and 50 (inclusive)");
62 }
63
64 $vault = \core_calendar\local\event\core_container::get_event_vault();
65
66 $afterevent = null;
67 if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
68 $afterevent = $event;
69 }
70
71 return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum);
72 }
73}