MDL-58386 core: Fixed CI issues
[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
e62cd85f
RW
29use core_calendar\local\event\exceptions\limit_invalid_parameter_exception;
30
392d6a49
RW
31/**
32 * Class containing the local calendar API.
33 *
34 * @package core_calendar
35 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class api {
258a5705
CB
39 /**
40 * Get all events restricted by various parameters, taking in to account user and group overrides.
41 *
42 * @param int|null $timestartfrom Events with timestart from this value (inclusive).
43 * @param int|null $timestartto Events with timestart until this value (inclusive).
44 * @param int|null $timesortfrom Events with timesort from this value (inclusive).
45 * @param int|null $timesortto Events with timesort until this value (inclusive).
46 * @param int|null $timestartaftereventid Restrict the events in the timestart range to ones
47 after this ID.
48 * @param int $timesortaftereventid Restrict the events in the timesort range to ones
49 after this ID.
50 * @param int $limitnum Return at most this number of events.
51 * @param int $type Return only events of this type.
52 * @param array|null $usersfilter Return only events for these users.
53 * @param array|null $groupsfilter Return only events for these groups.
54 * @param array|null $coursesfilter Return only events for these courses.
55 * @param bool $withduration If true return only events starting within specified
56 * timestart otherwise return in progress events as well.
57 * @param bool $ignorehidden If true don't return hidden events.
58 * @return \core_calendar\local\interfaces\event_interface[] Array of event_interfaces.
59 */
60 public static function get_events(
61 $timestartfrom = null,
62 $timestartto = null,
63 $timesortfrom = null,
64 $timesortto = null,
65 $timestartaftereventid = null,
66 $timesortaftereventid = null,
67 $limitnum = 20,
68 $type = null,
69 array $usersfilter = null,
70 array $groupsfilter = null,
71 array $coursesfilter = null,
72 $withduration = true,
73 $ignorehidden = true
74 ) {
75 global $USER;
76
77 $vault = \core_calendar\local\event\core_container::get_event_vault();
78
79 $timestartafterevent = null;
80 $timesortafterevent = null;
81
82 if ($timestartaftereventid && $event = $vault->get_event_by_id($timestartaftereventid)) {
83 $timestartafterevent = $event;
84 }
85
86 if ($timesortaftereventid && $event = $vault->get_event_by_id($timesortaftereventid)) {
87 $timesortafterevent = $event;
88 }
89
90 return $vault->get_events(
91 $timestartfrom,
92 $timestartto,
93 $timesortfrom,
94 $timesortto,
95 $timestartafterevent,
96 $timesortafterevent,
97 $limitnum,
98 $type,
99 $usersfilter,
100 $groupsfilter,
101 $coursesfilter,
102 $withduration,
103 $ignorehidden
104 );
105 }
392d6a49
RW
106
107 /**
108 * Get a list of action events for the logged in user by the given
109 * timesort values.
110 *
111 * @param int|null $timesortfrom The start timesort value (inclusive)
112 * @param int|null $timesortto The end timesort value (inclusive)
113 * @param int|null $aftereventid Only return events after this one
114 * @param int $limitnum Limit results to this amount (between 1 and 50)
115 * @return array A list of action_event_interface objects
116 */
117 public static function get_action_events_by_timesort(
118 $timesortfrom = null,
119 $timesortto = null,
120 $aftereventid = null,
121 $limitnum = 20
122 ) {
123 global $USER;
124
125 if (is_null($timesortfrom) && is_null($timesortto)) {
126 throw new \moodle_exception("Must provide a timesort to and/or from value");
127 }
128
129 if ($limitnum < 1 || $limitnum > 50) {
130 throw new \moodle_exception("Limit must be between 1 and 50 (inclusive)");
131 }
132
133 $vault = \core_calendar\local\event\core_container::get_event_vault();
134
135 $afterevent = null;
136 if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
137 $afterevent = $event;
138 }
139
140 return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum);
141 }
e62cd85f
RW
142
143 /**
144 * Get a list of action events for the logged in user by the given
145 * course and timesort values.
146 *
147 * @param \stdClass $course The course the events must belong to
148 * @param int|null $timesortfrom The start timesort value (inclusive)
149 * @param int|null $timesortto The end timesort value (inclusive)
150 * @param int|null $aftereventid Only return events after this one
151 * @param int $limitnum Limit results to this amount (between 1 and 50)
152 * @return array A list of action_event_interface objects
153 */
154 public static function get_action_events_by_course(
155 $course,
156 $timesortfrom = null,
157 $timesortto = null,
158 $aftereventid = null,
159 $limitnum = 20
160 ) {
161 global $USER;
162
163 if ($limitnum < 1 || $limitnum > 50) {
164 throw new limit_invalid_parameter_exception(
165 "Limit must be between 1 and 50 (inclusive)");
166 }
167
168 $vault = \core_calendar\local\event\core_container::get_event_vault();
169
170 $afterevent = null;
171 if ($aftereventid && $event = $vault->get_event_by_id($aftereventid)) {
172 $afterevent = $event;
173 }
174
175 return $vault->get_action_events_by_course(
176 $USER, $course, $timesortfrom, $timesortto, $afterevent, $limitnum);
177 }
8a082024
RW
178
179 /**
180 * Get a list of action events for the logged in user by the given
181 * courses and timesort values.
182 *
183 * The limit number applies per course, not for the result set as a whole.
184 * E.g. Requesting 3 courses with a limit of 10 will result in up to 30
185 * events being returned (up to 10 per course).
186 *
187 * @param array $courses The courses the events must belong to
188 * @param int|null $timesortfrom The start timesort value (inclusive)
189 * @param int|null $timesortto The end timesort value (inclusive)
190 * @param int $limitnum Limit results per course to this amount (between 1 and 50)
191 * @return array A list of action_event_interface objects indexed by course id
192 */
193 public static function get_action_events_by_courses(
194 $courses = [],
195 $timesortfrom = null,
196 $timesortto = null,
197 $limitnum = 20
198 ) {
199 $return = [];
200
201 foreach ($courses as $course) {
202 $return[$course->id] = self::get_action_events_by_course(
203 $course,
204 $timesortfrom,
205 $timesortto,
206 null,
207 $limitnum
208 );
209 }
210
211 return $return;
212 }
392d6a49 213}