MDL-59750 core_calendar: better handling of event subscription
[moodle.git] / calendar / classes / external / day_exporter.php
CommitLineData
64ff737a 1<?php
a4af4c96
SL
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 the day view.
19 *
20 * @package core_calendar
21 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
64ff737a
AN
24
25namespace core_calendar\external;
26
a4af4c96
SL
27defined('MOODLE_INTERNAL') || die();
28
8afe9f8a
SL
29require_once($CFG->dirroot . '/calendar/lib.php');
30
64ff737a
AN
31use core\external\exporter;
32use renderer_base;
33use moodle_url;
34
a4af4c96
SL
35/**
36 * Class for displaying the day view.
37 *
38 * @package core_calendar
39 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
64ff737a
AN
42class day_exporter extends exporter {
43
39e40fda
AN
44 /**
45 * @var \calendar_information $calendar The calendar being displayed.
46 */
47 protected $calendar;
48
49 /**
50 * Constructor.
51 *
52 * @param \calendar_information $calendar The calendar information for the period being displayed
53 * @param mixed $data Either an stdClass or an array of values.
54 * @param array $related Related objects.
55 */
56 public function __construct(\calendar_information $calendar, $data, $related) {
57 $this->calendar = $calendar;
58
59 parent::__construct($data, $related);
60 }
61
a4af4c96
SL
62 /**
63 * Return the list of properties.
64 *
65 * @return array
66 */
64ff737a
AN
67 protected static function define_properties() {
68 // These are the default properties as returned by getuserdate()
69 // but without the formatted month and week names.
70 return [
71 'seconds' => [
72 'type' => PARAM_INT,
73 ],
74 'minutes' => [
75 'type' => PARAM_INT,
76 ],
77 'hours' => [
78 'type' => PARAM_INT,
79 ],
80 'mday' => [
81 'type' => PARAM_INT,
82 ],
83 'wday' => [
84 'type' => PARAM_INT,
85 ],
86 'year' => [
87 'type' => PARAM_INT,
88 ],
89 'yday' => [
90 'type' => PARAM_INT,
91 ],
c8b6e9ab
AN
92 // These are additional params.
93 'istoday' => [
94 'type' => PARAM_BOOL,
95 'default' => false,
96 ],
97 'isweekend' => [
98 'type' => PARAM_BOOL,
99 'default' => false,
100 ],
64ff737a
AN
101 ];
102 }
103
a4af4c96
SL
104 /**
105 * Return the list of additional properties.
106 *
107 * @return array
108 */
64ff737a
AN
109 protected static function define_other_properties() {
110 return [
111 'timestamp' => [
112 'type' => PARAM_INT,
113 ],
f6e8cc83
RW
114 'neweventtimestamp' => [
115 'type' => PARAM_INT,
116 ],
64ff737a
AN
117 'viewdaylink' => [
118 'type' => PARAM_URL,
119 'optional' => true,
120 ],
121 'events' => [
836aa3f6 122 'type' => calendar_event_exporter::read_properties_definition(),
64ff737a 123 'multiple' => true,
c8b6e9ab
AN
124 ],
125 'calendareventtypes' => [
126 'type' => PARAM_RAW,
127 'multiple' => true,
128 ],
129 'popovertitle' => [
130 'type' => PARAM_RAW,
131 'default' => '',
132 ],
133 'haslastdayofevent' => [
134 'type' => PARAM_BOOL,
135 'default' => false,
136 ],
64ff737a
AN
137 ];
138 }
139
a4af4c96
SL
140 /**
141 * Get the additional values to inject while exporting.
142 *
143 * @param renderer_base $output The renderer.
144 * @return array Keys are the property names, values are their values.
145 */
64ff737a 146 protected function get_other_values(renderer_base $output) {
f6e8cc83
RW
147 $timestamp = $this->data[0];
148 // Need to account for user's timezone.
149 $usernow = usergetdate(time());
150 $today = new \DateTimeImmutable();
151 // The start time should use the day's date but the current
152 // time of the day (adjusted for user's timezone).
153 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
154 $usernow['hours'],
155 $usernow['minutes'],
156 $usernow['seconds']
157 );
158
64ff737a 159 $return = [
f6e8cc83
RW
160 'timestamp' => $timestamp,
161 'neweventtimestamp' => $neweventstarttime->getTimestamp()
64ff737a
AN
162 ];
163
164 $url = new moodle_url('/calendar/view.php', [
165 'view' => 'day',
f6e8cc83 166 'time' => $timestamp,
c8b6e9ab 167 'course' => $this->calendar->course->id,
64ff737a
AN
168 ]);
169 $return['viewdaylink'] = $url->out(false);
170
64ff737a 171 $cache = $this->related['cache'];
c8b6e9ab 172 $eventexporters = array_map(function($event) use ($cache, $output, $url) {
64ff737a
AN
173 $context = $cache->get_context($event);
174 $course = $cache->get_course($event);
175 $exporter = new calendar_event_exporter($event, [
176 'context' => $context,
177 'course' => $course,
178 'daylink' => $url,
c8b6e9ab
AN
179 'type' => $this->related['type'],
180 'today' => $this->data[0],
64ff737a
AN
181 ]);
182
c8b6e9ab 183 return $exporter;
64ff737a
AN
184 }, $this->related['events']);
185
c8b6e9ab
AN
186 $return['events'] = array_map(function($exporter) use ($output) {
187 return $exporter->export($output);
188 }, $eventexporters);
189
190 if ($popovertitle = $this->get_popover_title()) {
191 $return['popovertitle'] = $popovertitle;
192 }
193
194 $return['calendareventtypes'] = array_map(function($exporter) {
195 return $exporter->get_calendar_event_type();
196 }, $eventexporters);
197 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
198
199 $return['haslastdayofevent'] = false;
200 foreach ($return['events'] as $event) {
201 if ($event->islastday) {
202 $return['haslastdayofevent'] = true;
203 break;
204 }
205 }
206
64ff737a
AN
207 return $return;
208 }
209
a4af4c96
SL
210 /**
211 * Returns a list of objects that are related.
212 *
213 * @return array
214 */
64ff737a
AN
215 protected static function define_related() {
216 return [
217 'events' => '\core_calendar\local\event\entities\event_interface[]',
218 'cache' => '\core_calendar\external\events_related_objects_cache',
219 'type' => '\core_calendar\type_base',
220 ];
221 }
c8b6e9ab
AN
222
223 /**
224 * Get the title for this popover.
225 *
226 * @return string
227 */
228 protected function get_popover_title() {
229 $title = null;
230
231 $userdate = userdate($this->data[0], get_string('strftimedayshort'));
232 if (count($this->related['events'])) {
233 $title = get_string('eventsfor', 'calendar', $userdate);
234 } else if ($this->data['istoday']) {
235 $title = $userdate;
236 }
237
238 if ($this->data['istoday']) {
239 $title = get_string('todayplustitle', 'calendar', $userdate);
240 }
241
242 return $title;
243 }
64ff737a 244}