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