MDL-60564 calendar: removed unnecessary get_new_event_button()
[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
146d3713
SL
49 /**
50 * @var moodle_url
51 */
52 protected $url;
39e40fda
AN
53 /**
54 * Constructor.
55 *
56 * @param \calendar_information $calendar The calendar information for the period being displayed
57 * @param mixed $data Either an stdClass or an array of values.
58 * @param array $related Related objects.
59 */
60 public function __construct(\calendar_information $calendar, $data, $related) {
61 $this->calendar = $calendar;
5ba6507e
AN
62
63 $url = new moodle_url('/calendar/view.php', [
64 'view' => 'day',
65 'time' => $calendar->time,
66 ]);
67
68 if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
69 $url->param('course', $this->calendar->course->id);
70 } else if ($this->calendar->categoryid) {
71 $url->param('category', $this->calendar->categoryid);
72 }
73
74 $this->url = $url;
75
39e40fda
AN
76 parent::__construct($data, $related);
77 }
78
a4af4c96
SL
79 /**
80 * Return the list of properties.
81 *
82 * @return array
83 */
64ff737a
AN
84 protected static function define_properties() {
85 // These are the default properties as returned by getuserdate()
86 // but without the formatted month and week names.
87 return [
88 'seconds' => [
89 'type' => PARAM_INT,
90 ],
91 'minutes' => [
92 'type' => PARAM_INT,
93 ],
94 'hours' => [
95 'type' => PARAM_INT,
96 ],
97 'mday' => [
98 'type' => PARAM_INT,
99 ],
100 'wday' => [
101 'type' => PARAM_INT,
102 ],
103 'year' => [
104 'type' => PARAM_INT,
105 ],
106 'yday' => [
107 'type' => PARAM_INT,
108 ],
109 ];
110 }
111
a4af4c96
SL
112 /**
113 * Return the list of additional properties.
114 *
115 * @return array
116 */
64ff737a
AN
117 protected static function define_other_properties() {
118 return [
119 'timestamp' => [
120 'type' => PARAM_INT,
121 ],
f6e8cc83
RW
122 'neweventtimestamp' => [
123 'type' => PARAM_INT,
124 ],
64ff737a
AN
125 'viewdaylink' => [
126 'type' => PARAM_URL,
127 'optional' => true,
128 ],
129 'events' => [
836aa3f6 130 'type' => calendar_event_exporter::read_properties_definition(),
64ff737a 131 'multiple' => true,
c8b6e9ab 132 ],
a4f94944
RW
133 'hasevents' => [
134 'type' => PARAM_BOOL,
135 'default' => false,
136 ],
c8b6e9ab
AN
137 'calendareventtypes' => [
138 'type' => PARAM_RAW,
139 'multiple' => true,
140 ],
146d3713
SL
141 'previousperiod' => [
142 'type' => PARAM_INT,
143 ],
144 'nextperiod' => [
145 'type' => PARAM_INT,
146 ],
147 'navigation' => [
148 'type' => PARAM_RAW,
149 ],
c8b6e9ab
AN
150 'popovertitle' => [
151 'type' => PARAM_RAW,
152 'default' => '',
153 ],
154 'haslastdayofevent' => [
155 'type' => PARAM_BOOL,
156 'default' => false,
157 ],
64ff737a
AN
158 ];
159 }
160
a4af4c96
SL
161 /**
162 * Get the additional values to inject while exporting.
163 *
164 * @param renderer_base $output The renderer.
165 * @return array Keys are the property names, values are their values.
166 */
64ff737a 167 protected function get_other_values(renderer_base $output) {
146d3713 168 $daytimestamp = $this->calendar->time;
f6e8cc83
RW
169 $timestamp = $this->data[0];
170 // Need to account for user's timezone.
171 $usernow = usergetdate(time());
172 $today = new \DateTimeImmutable();
173 // The start time should use the day's date but the current
174 // time of the day (adjusted for user's timezone).
175 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
176 $usernow['hours'],
177 $usernow['minutes'],
178 $usernow['seconds']
179 );
180
64ff737a 181 $return = [
f6e8cc83 182 'timestamp' => $timestamp,
146d3713
SL
183 'neweventtimestamp' => $neweventstarttime->getTimestamp(),
184 'previousperiod' => $this->get_previous_day_timestamp($daytimestamp),
185 'nextperiod' => $this->get_next_day_timestamp($daytimestamp),
186 'navigation' => $this->get_navigation(),
5ba6507e 187 'viewdaylink' => $this->url->out(false),
64ff737a
AN
188 ];
189
64ff737a 190
64ff737a 191 $cache = $this->related['cache'];
146d3713 192 $eventexporters = array_map(function($event) use ($cache, $output) {
64ff737a
AN
193 $context = $cache->get_context($event);
194 $course = $cache->get_course($event);
195 $exporter = new calendar_event_exporter($event, [
196 'context' => $context,
197 'course' => $course,
146d3713 198 'daylink' => $this->url,
c8b6e9ab
AN
199 'type' => $this->related['type'],
200 'today' => $this->data[0],
64ff737a
AN
201 ]);
202
c8b6e9ab 203 return $exporter;
64ff737a
AN
204 }, $this->related['events']);
205
c8b6e9ab
AN
206 $return['events'] = array_map(function($exporter) use ($output) {
207 return $exporter->export($output);
208 }, $eventexporters);
209
a4f94944
RW
210 $return['hasevents'] = !empty($return['events']);
211
c8b6e9ab
AN
212 $return['calendareventtypes'] = array_map(function($exporter) {
213 return $exporter->get_calendar_event_type();
214 }, $eventexporters);
215 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
216
217 $return['haslastdayofevent'] = false;
218 foreach ($return['events'] as $event) {
219 if ($event->islastday) {
220 $return['haslastdayofevent'] = true;
221 break;
222 }
223 }
224
64ff737a
AN
225 return $return;
226 }
227
a4af4c96
SL
228 /**
229 * Returns a list of objects that are related.
230 *
231 * @return array
232 */
64ff737a
AN
233 protected static function define_related() {
234 return [
235 'events' => '\core_calendar\local\event\entities\event_interface[]',
236 'cache' => '\core_calendar\external\events_related_objects_cache',
237 'type' => '\core_calendar\type_base',
238 ];
239 }
c8b6e9ab
AN
240
241 /**
146d3713 242 * Get the previous day timestamp.
c8b6e9ab 243 *
146d3713
SL
244 * @param int $daytimestamp The current day timestamp.
245 * @return int The previous day timestamp.
c8b6e9ab 246 */
146d3713
SL
247 protected function get_previous_day_timestamp($daytimestamp) {
248 return $this->related['type']->get_prev_day($daytimestamp);
249 }
250
251 /**
252 * Get the next day timestamp.
253 *
254 * @param int $daytimestamp The current day timestamp.
255 * @return int The next day timestamp.
256 */
257 protected function get_next_day_timestamp($daytimestamp) {
258 return $this->related['type']->get_next_day($daytimestamp);
259 }
260
261 /**
262 * Get the calendar navigation controls.
263 *
264 * @return string The html code to the calendar top navigation.
265 */
266 protected function get_navigation() {
267 return calendar_top_controls('day', [
268 'id' => $this->calendar->courseid,
269 'time' => $this->calendar->time,
270 ]);
271 }
64ff737a 272}