MDL-65318 core_calendar: Additional fixes
[moodle.git] / calendar / classes / external / calendar_event_exporter.php
CommitLineData
64ff737a
AN
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 event class for displaying a calendar event.
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\external;
26
27defined('MOODLE_INTERNAL') || die();
28
c56dd950 29use \core_calendar\local\event\container;
64ff737a
AN
30use \core_course\external\course_summary_exporter;
31use \renderer_base;
63b13467 32require_once($CFG->dirroot . '/course/lib.php');
64ff737a
AN
33/**
34 * Class for displaying a calendar event.
35 *
36 * @package core_calendar
37 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class calendar_event_exporter extends event_exporter_base {
41
42 /**
43 * Return the list of additional properties.
44 *
45 * @return array
46 */
47 protected static function define_other_properties() {
1652934f
RW
48
49 $values = parent::define_other_properties();
50 $values['url'] = ['type' => PARAM_URL];
c8b6e9ab
AN
51 $values['islastday'] = [
52 'type' => PARAM_BOOL,
53 'default' => false,
54 ];
c8b6e9ab
AN
55 $values['popupname'] = [
56 'type' => PARAM_RAW,
57 ];
c56dd950
RW
58 $values['mindaytimestamp'] = [
59 'type' => PARAM_INT,
60 'optional' => true
61 ];
62 $values['mindayerror'] = [
63 'type' => PARAM_TEXT,
64 'optional' => true
65 ];
66 $values['maxdaytimestamp'] = [
67 'type' => PARAM_INT,
68 'optional' => true
69 ];
70 $values['maxdayerror'] = [
71 'type' => PARAM_TEXT,
72 'optional' => true
73 ];
6688ae2b
RW
74 $values['draggable'] = [
75 'type' => PARAM_BOOL,
76 'default' => false
77 ];
1652934f
RW
78
79 return $values;
64ff737a
AN
80 }
81
82 /**
83 * Get the additional values to inject while exporting.
84 *
85 * @param renderer_base $output The renderer.
86 * @return array Keys are the property names, values are their values.
87 */
88 protected function get_other_values(renderer_base $output) {
c8b6e9ab
AN
89 global $CFG;
90
64ff737a 91 $values = parent::get_other_values($output);
63b13467 92 $event = $this->event;
bb4691ec
RW
93 $course = $this->related['course'];
94 $hascourse = !empty($course);
64ff737a 95
6688ae2b
RW
96 // By default all events that can be edited are
97 // draggable.
98 $values['draggable'] = $values['canedit'];
99
63b13467
SL
100 if ($moduleproxy = $event->get_course_module()) {
101 $modulename = $moduleproxy->get('modname');
102 $moduleid = $moduleproxy->get('id');
103 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
64ff737a 104
63b13467
SL
105 // Build edit event url for action events.
106 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
107 $editurl = new \moodle_url('/course/mod.php', $params);
108 $values['editurl'] = $editurl->out(false);
d0e56d84
AN
109 } else if ($event->get_type() == 'category') {
110 $url = $event->get_category()->get_proxied_instance()->get_view_link();
63b13467
SL
111 } else {
112 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
bb4691ec 113 $url = course_get_url($hascourse ? $course : SITEID);
63b13467 114 }
c56dd950 115
63b13467 116 $values['url'] = $url->out(false);
c8b6e9ab
AN
117 $values['islastday'] = false;
118 $today = $this->related['type']->timestamp_to_date_array($this->related['today']);
119
120 $values['popupname'] = $this->event->get_name();
121
122 $times = $this->event->get_times();
123 if ($duration = $times->get_duration()) {
124 $enddate = $this->related['type']->timestamp_to_date_array($times->get_end_time()->getTimestamp());
125 $values['islastday'] = true;
126 $values['islastday'] = $values['islastday'] && $enddate['year'] == $today['year'];
127 $values['islastday'] = $values['islastday'] && $enddate['mon'] == $today['mon'];
128 $values['islastday'] = $values['islastday'] && $enddate['mday'] == $today['mday'];
129 }
130
131 $subscription = $this->event->get_subscription();
132 if ($subscription && !empty($subscription->get('id')) && $CFG->calendar_showicalsource) {
133 $a = (object) [
134 'name' => $values['popupname'],
135 'source' => $subscription->get('name'),
136 ];
137 $values['popupname'] = get_string('namewithsource', 'calendar', $a);
138 } else {
139 if ($values['islastday']) {
140 $startdate = $this->related['type']->timestamp_to_date_array($times->get_start_time()->getTimestamp());
141 $samedate = true;
142 $samedate = $samedate && $startdate['mon'] == $enddate['mon'];
143 $samedate = $samedate && $startdate['year'] == $enddate['year'];
144 $samedate = $samedate && $startdate['mday'] == $enddate['mday'];
145
146 if (!$samedate) {
147 $values['popupname'] = get_string('eventendtimewrapped', 'calendar', $values['popupname']);
148 }
149 }
150 }
151
5ba6507e
AN
152 // Include category name into the event name, if applicable.
153 $proxy = $this->event->get_category();
154 if ($proxy && $proxy->get('id')) {
155 $category = $proxy->get_proxied_instance();
156 $eventnameparams = (object) [
157 'name' => $values['popupname'],
158 'category' => $category->get_formatted_name(),
159 ];
160 $values['popupname'] = get_string('eventnameandcategory', 'calendar', $eventnameparams);
161 }
162
c8b6e9ab 163 // Include course's shortname into the event name, if applicable.
bb4691ec 164 if ($hascourse && $course->id !== SITEID) {
c8b6e9ab
AN
165 $eventnameparams = (object) [
166 'name' => $values['popupname'],
6b024885 167 'course' => $values['course']->shortname,
c8b6e9ab
AN
168 ];
169 $values['popupname'] = get_string('eventnameandcourse', 'calendar', $eventnameparams);
170 }
171
c56dd950
RW
172 if ($event->get_course_module()) {
173 $values = array_merge($values, $this->get_module_timestamp_limits($event));
b0e5f229
SR
174 } else if ($hascourse && $course->id != SITEID && empty($event->get_group())) {
175 // This is a course event.
176 $values = array_merge($values, $this->get_course_timestamp_limits($event));
c56dd950
RW
177 }
178
64ff737a
AN
179 return $values;
180 }
181
182 /**
183 * Returns a list of objects that are related.
184 *
185 * @return array
186 */
187 protected static function define_related() {
188 $related = parent::define_related();
189 $related['daylink'] = \moodle_url::class;
c8b6e9ab
AN
190 $related['type'] = '\core_calendar\type_base';
191 $related['today'] = 'int';
3152de6a 192 $related['moduleinstance'] = 'stdClass?';
64ff737a
AN
193
194 return $related;
195 }
c8b6e9ab
AN
196
197 /**
198 * Return the normalised event type.
199 * Activity events are normalised to be course events.
200 *
201 * @return string
202 */
203 public function get_calendar_event_type() {
70569dad
AN
204 if ($this->event->get_course_module()) {
205 return 'course';
c8b6e9ab
AN
206 }
207
70569dad 208 return $this->event->get_type();
c8b6e9ab 209 }
c56dd950 210
b0e5f229
SR
211 /**
212 * Return the set of minimum and maximum date timestamp values
213 * for the given event.
214 *
215 * @param event_interface $event
216 * @return array
217 */
218 protected function get_course_timestamp_limits($event) {
219 $values = [];
220 $mapper = container::get_event_mapper();
221 $starttime = $event->get_times()->get_start_time();
222
223 list($min, $max) = component_callback(
224 'core_course',
225 'core_calendar_get_valid_event_timestart_range',
226 [$mapper->from_event_to_legacy_event($event), $event->get_course()->get_proxied_instance()],
227 [false, false]
228 );
229
230 // The callback will return false for either of the
231 // min or max cutoffs to indicate that there are no
232 // valid timestart values. In which case the event is
233 // not draggable.
234 if ($min === false || $max === false) {
235 return ['draggable' => false];
236 }
237
238 if ($min) {
239 $values = array_merge($values, $this->get_timestamp_min_limit($starttime, $min));
240 }
241
242 if ($max) {
243 $values = array_merge($values, $this->get_timestamp_max_limit($starttime, $max));
244 }
245
246 return $values;
247 }
248
c56dd950
RW
249 /**
250 * Return the set of minimum and maximum date timestamp values
251 * for the given event.
252 *
253 * @param event_interface $event
254 * @return array
255 */
256 protected function get_module_timestamp_limits($event) {
257 $values = [];
258 $mapper = container::get_event_mapper();
259 $starttime = $event->get_times()->get_start_time();
478b1d19 260 $modname = $event->get_course_module()->get('modname');
3152de6a 261 $moduleinstance = $this->related['moduleinstance'];
c56dd950
RW
262
263 list($min, $max) = component_callback(
478b1d19 264 'mod_' . $modname,
c56dd950 265 'core_calendar_get_valid_event_timestart_range',
478b1d19 266 [$mapper->from_event_to_legacy_event($event), $moduleinstance],
6688ae2b 267 [false, false]
c56dd950
RW
268 );
269
6688ae2b
RW
270 // The callback will return false for either of the
271 // min or max cutoffs to indicate that there are no
272 // valid timestart values. In which case the event is
273 // not draggable.
274 if ($min === false || $max === false) {
275 return ['draggable' => false];
276 }
277
c56dd950 278 if ($min) {
084d5ee4 279 $values = array_merge($values, $this->get_timestamp_min_limit($starttime, $min));
c56dd950
RW
280 }
281
282 if ($max) {
084d5ee4 283 $values = array_merge($values, $this->get_timestamp_max_limit($starttime, $max));
c56dd950
RW
284 }
285
286 return $values;
287 }
288
289 /**
290 * Get the correct minimum midnight day limit based on the event start time
084d5ee4 291 * and the minimum timestamp limit of what the event belongs to.
c56dd950
RW
292 *
293 * @param DateTimeInterface $starttime The event start time
294 * @param array $min The module's minimum limit for the event
084d5ee4 295 * @return array Returns an array with mindaytimestamp and mindayerror keys.
c56dd950 296 */
084d5ee4 297 protected function get_timestamp_min_limit(\DateTimeInterface $starttime, $min) {
c56dd950
RW
298 // We need to check that the minimum valid time is earlier in the
299 // day than the current event time so that if the user drags and drops
300 // the event to this day (which changes the date but not the time) it
301 // will result in a valid time start for the event.
302 //
303 // For example:
304 // An event that starts on 2017-01-10 08:00 with a minimum cutoff
305 // of 2017-01-05 09:00 means that 2017-01-05 is not a valid start day
306 // for the drag and drop because it would result in the event start time
307 // being set to 2017-01-05 08:00, which is invalid. Instead the minimum
308 // valid start day would be 2017-01-06.
309 $values = [];
310 $timestamp = $min[0];
311 $errorstring = $min[1];
312 $mindate = (new \DateTimeImmutable())->setTimestamp($timestamp);
313 $minstart = $mindate->setTime(
314 $starttime->format('H'),
315 $starttime->format('i'),
316 $starttime->format('s')
317 );
318 $midnight = usergetmidnight($timestamp);
319
320 if ($mindate <= $minstart) {
321 $values['mindaytimestamp'] = $midnight;
322 } else {
323 $tomorrow = (new \DateTime())->setTimestamp($midnight)->modify('+1 day');
324 $values['mindaytimestamp'] = $tomorrow->getTimestamp();
325 }
326
327 // Get the human readable error message to display if the min day
328 // timestamp is violated.
329 $values['mindayerror'] = $errorstring;
330 return $values;
331 }
332
333 /**
334 * Get the correct maximum midnight day limit based on the event start time
084d5ee4 335 * and the maximum timestamp limit of what the event belongs to.
c56dd950
RW
336 *
337 * @param DateTimeInterface $starttime The event start time
338 * @param array $max The module's maximum limit for the event
084d5ee4 339 * @return array Returns an array with maxdaytimestamp and maxdayerror keys.
c56dd950 340 */
084d5ee4 341 protected function get_timestamp_max_limit(\DateTimeInterface $starttime, $max) {
c56dd950
RW
342 // We're doing a similar calculation here as we are for the minimum
343 // day timestamp. See the explanation above.
084d5ee4 344 $values = [];
c56dd950
RW
345 $timestamp = $max[0];
346 $errorstring = $max[1];
347 $maxdate = (new \DateTimeImmutable())->setTimestamp($timestamp);
348 $maxstart = $maxdate->setTime(
349 $starttime->format('H'),
350 $starttime->format('i'),
351 $starttime->format('s')
352 );
353 $midnight = usergetmidnight($timestamp);
354
355 if ($maxdate >= $maxstart) {
356 $values['maxdaytimestamp'] = $midnight;
357 } else {
358 $yesterday = (new \DateTime())->setTimestamp($midnight)->modify('-1 day');
359 $values['maxdaytimestamp'] = $yesterday->getTimestamp();
360 }
361
362 // Get the human readable error message to display if the max day
363 // timestamp is violated.
364 $values['maxdayerror'] = $errorstring;
365 return $values;
366 }
084d5ee4
SR
367
368 /**
369 * Get the correct minimum midnight day limit based on the event start time
370 * and the module's minimum timestamp limit.
371 *
372 * @deprecated since Moodle 3.6. Please use get_timestamp_min_limit().
373 * @todo final deprecation. To be removed in Moodle 4.0
374 * @param DateTimeInterface $starttime The event start time
375 * @param array $min The module's minimum limit for the event
376 * @return array Returns an array with mindaytimestamp and mindayerror keys.
377 */
378 protected function get_module_timestamp_min_limit(\DateTimeInterface $starttime, $min) {
379 debugging('get_module_timestamp_min_limit() has been deprecated. Please call get_timestamp_min_limit() instead.',
380 DEBUG_DEVELOPER);
381 return $this->get_timestamp_min_limit($starttime, $min);
382 }
383
384 /**
385 * Get the correct maximum midnight day limit based on the event start time
386 * and the module's maximum timestamp limit.
387 *
388 * @deprecated since Moodle 3.6. Please use get_timestamp_max_limit().
389 * @todo final deprecation. To be removed in Moodle 4.0
390 * @param DateTimeInterface $starttime The event start time
391 * @param array $max The module's maximum limit for the event
392 * @return array Returns an array with maxdaytimestamp and maxdayerror keys.
393 */
394 protected function get_module_timestamp_max_limit(\DateTimeInterface $starttime, $max) {
395 debugging('get_module_timestamp_max_limit() has been deprecated. Please call get_timestamp_max_limit() instead.',
396 DEBUG_DEVELOPER);
397 return $this->get_timestamp_max_limit($starttime, $max);
398 }
64ff737a 399}