MDL-58386 core: Fixed CI issues
[moodle.git] / calendar / classes / local / event / data_access / event_vault.php
CommitLineData
84d865d6
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 * Event vault class
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\event\data_access;
26
27use core_calendar\local\event\exceptions\limit_invalid_parameter_exception;
28use core_calendar\local\event\exceptions\timesort_invalid_parameter_exception;
29use core_calendar\local\interfaces\action_event_interface;
84d865d6 30use core_calendar\local\interfaces\event_factory_interface;
258a5705 31use core_calendar\local\interfaces\event_interface;
84d865d6 32use core_calendar\local\interfaces\event_vault_interface;
258a5705 33use core_calendar\local\interfaces\raw_event_retrieval_strategy_interface;
84d865d6
RW
34
35/**
36 * This class will handle interacting with the database layer to retrieve
37 * the records. This is required to house the complex logic required for
38 * pagination because it's not a one-to-one mapping between database records
39 * and users.
40 *
41 * This is a repository. It's called a vault to reduce confusion because
42 * Moodle has already taken the name repository. Vault is cooler anyway.
43 *
44 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47class event_vault implements event_vault_interface {
48
5a0974c7
RW
49 /**
50 * @var event_factory_interface $factory Factory for creating events.
51 */
258a5705
CB
52 protected $factory;
53
54 /**
55 * @var raw_event_retrieval_strategy_interface $retrievalstrategy Strategy for getting events from the DB.
56 */
57 protected $retrievalstrategy;
84d865d6
RW
58
59 /**
60 * Create an event vault.
61 *
62 * @param event_factory_interface $factory An event factory
63 */
258a5705
CB
64 public function __construct(
65 event_factory_interface $factory,
66 raw_event_retrieval_strategy_interface $retrievalstrategy
67 ) {
84d865d6 68 $this->factory = $factory;
258a5705 69 $this->retrievalstrategy = $retrievalstrategy;
84d865d6
RW
70 }
71
5e6e3bab 72 public function get_event_by_id($id) {
84d865d6
RW
73 global $DB;
74
75 if ($record = $DB->get_record('event', ['id' => $id])) {
76 return $this->transform_from_database_record($record);
77 } else {
78 return false;
79 }
80 }
81
258a5705
CB
82 public function get_events(
83 $timestartfrom = null,
84 $timestartto = null,
5e6e3bab
MN
85 $timesortfrom = null,
86 $timesortto = null,
258a5705
CB
87 event_interface $timestartafterevent = null,
88 event_interface $timesortafterevent = null,
89 $limitnum = 20,
90 $type = null,
91 array $usersfilter = null,
92 array $groupsfilter = null,
93 array $coursesfilter = null,
94 $withduration = true,
95 $ignorehidden = true,
96 callable $filter = null
84d865d6
RW
97 ) {
98 global $DB;
99
84d865d6
RW
100 if ($limitnum < 1 || $limitnum > 50) {
101 throw new limit_invalid_parameter_exception("Limit must be between 1 and 50 (inclusive)");
102 }
103
258a5705
CB
104 $fromquery = function($field, $timefrom, $lastseenmethod, $afterevent) {
105 if (!$timefrom) {
106 return false;
107 }
84d865d6 108
258a5705
CB
109 return $this->timefield_pagination_from(
110 $field,
111 $timefrom,
112 $afterevent ? $afterevent->get_times()->{$lastseenmethod}()->getTimestamp() : null,
113 $afterevent ? $afterevent->get_id() : null
114 );
115 };
84d865d6 116
258a5705
CB
117 $toquery = function($field, $timeto, $lastseenmethod, $afterevent) {
118 if (!$timeto) {
119 return false;
5a0974c7 120 }
84d865d6 121
258a5705
CB
122 return $this->timefield_pagination_to(
123 $field,
124 $timeto,
125 $afterevent ? $afterevent->get_times()->{$lastseenmethod}()->getTimestamp() : null,
126 $afterevent ? $afterevent->get_id() : null
127 );
128 };
129
130 $timesortfromquery = $fromquery('timesort', $timesortfrom, 'get_sort_time', $timesortafterevent);
131 $timesorttoquery = $toquery('timesort', $timesortto, 'get_sort_time', $timesortafterevent);
132 $timestartfromquery = $fromquery('timestart', $timestartfrom, 'get_start_time', $timestartafterevent);
133 $timestarttoquery = $toquery('timestart', $timestartto, 'get_start_time', $timestartafterevent);
134
135 if (($timesortto && !$timesorttoquery) || ($timestartto && !$timestarttoquery)) {
136 return [];
84d865d6
RW
137 }
138
258a5705
CB
139 $params = array_merge(
140 $type ? ['type' => $type] : [],
141 $timesortfromquery ? $timesortfromquery['params'] : [],
142 $timesorttoquery ? $timesorttoquery['params'] : [],
143 $timestartfromquery ? $timestartfromquery['params'] : [],
144 $timestarttoquery ? $timestarttoquery['params'] : []
145 );
146
147 $where = array_merge(
148 $type ? ['type = :type'] : [],
149 $timesortfromquery ? $timesortfromquery['where'] : [],
150 $timesorttoquery ? $timesorttoquery['where'] : [],
151 $timestartfromquery ? $timestartfromquery['where'] : [],
152 $timestarttoquery ? $timestarttoquery['where'] : []
153 );
84d865d6
RW
154
155 $offset = 0;
156 $events = [];
258a5705
CB
157
158 while ($records = array_values($this->retrievalstrategy->get_raw_events(
159 $usersfilter,
160 $groupsfilter,
161 $coursesfilter,
162 $where,
163 $params,
164 "timesort ASC, id ASC",
165 $offset,
166 $limitnum,
167 $withduration,
168 $ignorehidden
169 ))) {
e62cd85f
RW
170 foreach ($records as $record) {
171 if ($event = $this->transform_from_database_record($record)) {
258a5705
CB
172 $filtertest = $filter ? $filter($event) : true;
173
174 if ($event && $filtertest) {
e62cd85f
RW
175 $events[] = $event;
176 }
177
178 if (count($events) == $limitnum) {
179 // We've got all of the events so break both loops.
180 break 2;
181 }
182 }
183 }
184
185 $offset += $limitnum;
186 }
187
188 return $events;
189 }
190
258a5705
CB
191 public function get_action_events_by_timesort(
192 \stdClass $user,
193 $timesortfrom = null,
194 $timesortto = null,
195 event_interface $afterevent = null,
196 $limitnum = 20
197 ) {
198 return $this->get_events(
199 null,
200 null,
201 $timesortfrom,
202 $timesortto,
203 null,
204 $afterevent,
205 $limitnum,
206 CALENDAR_EVENT_TYPE_ACTION,
207 [$user->id],
208 null,
209 null,
8d4e0f29
CB
210 true,
211 true,
258a5705
CB
212 function ($event) {
213 return $event instanceof action_event_interface;
214 }
215 );
216 }
217
e62cd85f
RW
218 public function get_action_events_by_course(
219 \stdClass $user,
220 \stdClass $course,
8aa761b0
MN
221 $timesortfrom = null,
222 $timesortto = null,
e62cd85f 223 event_interface $afterevent = null,
8aa761b0 224 $limitnum = 20
e62cd85f 225 ) {
258a5705
CB
226 return array_values(
227 $this->get_events(
228 null,
229 null,
230 $timesortfrom,
231 $timesortto,
232 null,
233 $afterevent,
234 $limitnum,
235 CALENDAR_EVENT_TYPE_ACTION,
236 [$user->id],
237 null,
238 [$course->id],
8d4e0f29
CB
239 true,
240 true,
258a5705
CB
241 function ($event) use ($course) {
242 return $event instanceof action_event_interface && $event->get_course()->get_id() == $course->id;
243 }
244 )
245 );
246 }
e62cd85f 247
258a5705
CB
248 protected function timefield_pagination_from(
249 $field,
250 $timefrom,
251 $lastseentime = null,
252 $lastseenid = null,
253 $withduration = true
254 ) {
255 $where = '';
256 $params = [];
e62cd85f 257
258a5705
CB
258 if ($lastseentime && $lastseentime >= $timefrom) {
259 $where = '((timesort = :timefrom1 AND e.id > :timefromid) OR timesort > :timefrom2)';
260 if ($field === 'timestart') {
261 $where = '((timestart = :timefrom1 AND e.id > :timefromid) OR timestart > :timefrom2' .
262 ($withduration ? ' OR timestart + timeduration > :timefrom3' : '') . ')';
5a0974c7 263 }
258a5705
CB
264 $params['timefromid'] = $lastseenid;
265 $params['timefrom1'] = $lastseentime;
266 $params['timefrom2'] = $lastseentime;
267 $params['timefrom3'] = $lastseentime;
268 } else {
269 $where = 'timesort >= :timefrom';
270 if ($field === 'timestart') {
271 $where = '(timestart >= :timefrom' .
272 ($withduration ? ' OR timestart + timeduration > :timefrom2' : '') . ')';
5a0974c7 273 }
258a5705
CB
274
275 $params['timefrom'] = $timefrom;
276 $params['timefrom2'] = $timefrom;
e62cd85f
RW
277 }
278
258a5705
CB
279 return ['where' => [$where], 'params' => $params];
280 }
84d865d6 281
258a5705
CB
282 protected function timefield_pagination_to(
283 $field,
284 $timeto,
285 $lastseentime = null,
286 $lastseenid = null
287 ) {
288 $where = [];
289 $params = [];
84d865d6 290
258a5705
CB
291 if ($lastseentime && $lastseentime > $timeto) {
292 // The last seen event from this set is after the time sort range which
293 // means all events in this range have been seen, so we can just return
294 // early here.
295 return false;
296 } else if ($lastseentime && $lastseentime == $timeto) {
297 $where[] = '((timesort = :timeto1 AND e.id > :timetoid) OR timesort < :timeto2)';
298 if ($field === 'timestart') {
299 $where[] = '((timestart = :timeto1 AND e.id > :timetoid) OR timestart < :timeto2)';
300 }
301 $params['timetoid'] = $lastseenid;
302 $params['timeto1'] = $timeto;
303 $params['timeto2'] = $timeto;
304 } else {
305 $where[] = ($field === 'timestart' ? 'timestart' : 'timesort') . ' <= :timeto';
306 $params['timeto'] = $timeto;
84d865d6
RW
307 }
308
258a5705 309 return ['where' => $where, 'params' => $params];
84d865d6
RW
310 }
311
312 /**
313 * Create an event from a database record.
314 *
315 * @param \stdClass $record The database record
316 * @return event_interface|false
317 */
258a5705
CB
318 protected function transform_from_database_record(\stdClass $record) {
319 if ($record->courseid == 0 && $record->instance && $record->modulename) {
320 list($course, $cm) = get_course_and_cm_from_instance($record->instance, $record->modulename);
321 $record->courseid = $course->id;
322 }
323
84d865d6
RW
324 return $this->factory->create_instance($record);
325 }
258a5705
CB
326
327 protected function get_from_db(
328 $userid,
329 $whereconditions,
330 $whereparams,
331 $ordersql,
332 $offset,
333 $limitnum
334 ) {
335 return array_values(
336 $this->retrievalstrategy->get_raw_events(
337 [$userid],
338 null,
339 null,
340 $whereconditions,
341 $whereparams,
342 $ordersql,
343 $offset,
344 $limitnum
345 )
346 );
347 }
84d865d6 348}