MDL-60823 calendar: use first day of week on prepadding calc
[moodle.git] / calendar / classes / external / month_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 month 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
64ff737a
AN
29use core\external\exporter;
30use renderer_base;
31use moodle_url;
32
a4af4c96
SL
33/**
34 * Class for displaying the month view.
35 *
36 * @package core_calendar
37 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
64ff737a
AN
40class month_exporter extends exporter {
41
42 /**
43 * @var \calendar_information $calendar The calendar to be rendered.
44 */
45 protected $calendar;
46
47 /**
48 * @var int $firstdayofweek The first day of the week.
49 */
50 protected $firstdayofweek;
51
52 /**
53 * @var moodle_url $url The URL for the events page.
54 */
55 protected $url;
56
fee025ec 57 /**
6038d626 58 * @var bool $includenavigation Whether navigation should be included on the output.
fee025ec
AN
59 */
60 protected $includenavigation = true;
61
64ff737a
AN
62 /**
63 * Constructor for month_exporter.
64 *
65 * @param \calendar_information $calendar The calendar being represented
66 * @param \core_calendar\type_base $type The calendar type (e.g. Gregorian)
67 * @param array $related The related information
68 */
69 public function __construct(\calendar_information $calendar, \core_calendar\type_base $type, $related) {
70 $this->calendar = $calendar;
71 $this->firstdayofweek = $type->get_starting_weekday();
72
73 $this->url = new moodle_url('/calendar/view.php', [
74 'view' => 'month',
75 'time' => $calendar->time,
76 ]);
77
5ba6507e
AN
78 if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
79 $this->url->param('course', $this->calendar->course->id);
80 } else if ($this->calendar->categoryid) {
81 $this->url->param('category', $this->calendar->categoryid);
64ff737a
AN
82 }
83
84 $related['type'] = $type;
85
c8b6e9ab
AN
86 $data = [
87 'url' => $this->url->out(false),
88 ];
89
90 parent::__construct($data, $related);
91 }
92
93 protected static function define_properties() {
94 return [
95 'url' => [
96 'type' => PARAM_URL,
97 ],
98 ];
64ff737a
AN
99 }
100
a4af4c96
SL
101 /**
102 * Return the list of additional properties.
103 *
104 * @return array
105 */
64ff737a
AN
106 protected static function define_other_properties() {
107 return [
108 'courseid' => [
109 'type' => PARAM_INT,
110 ],
d0e56d84
AN
111 'categoryid' => [
112 'type' => PARAM_INT,
113 'optional' => true,
114 'default' => 0,
115 ],
64ff737a
AN
116 'filter_selector' => [
117 'type' => PARAM_RAW,
118 ],
64ff737a
AN
119 'weeks' => [
120 'type' => week_exporter::read_properties_definition(),
121 'multiple' => true,
122 ],
123 'daynames' => [
124 'type' => day_name_exporter::read_properties_definition(),
125 'multiple' => true,
126 ],
127 'view' => [
128 'type' => PARAM_ALPHA,
129 ],
f58424c7
AN
130 'date' => [
131 'type' => date_exporter::read_properties_definition(),
c8b6e9ab
AN
132 ],
133 'periodname' => [
134 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
135 // calendar format.
136 'type' => PARAM_RAW,
137 ],
fee025ec
AN
138 'includenavigation' => [
139 'type' => PARAM_BOOL,
140 'default' => true,
141 ],
64ff737a 142 'previousperiod' => [
f58424c7
AN
143 'type' => date_exporter::read_properties_definition(),
144 ],
145 'previousperiodlink' => [
146 'type' => PARAM_URL,
64ff737a 147 ],
c8b6e9ab
AN
148 'previousperiodname' => [
149 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
150 // calendar format.
151 'type' => PARAM_RAW,
152 ],
64ff737a 153 'nextperiod' => [
f58424c7 154 'type' => date_exporter::read_properties_definition(),
64ff737a 155 ],
c8b6e9ab
AN
156 'nextperiodname' => [
157 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
158 // calendar format.
159 'type' => PARAM_RAW,
160 ],
f58424c7
AN
161 'nextperiodlink' => [
162 'type' => PARAM_URL,
163 ],
c8b6e9ab
AN
164 'larrow' => [
165 // The left arrow defined by the theme.
166 'type' => PARAM_RAW,
167 ],
168 'rarrow' => [
169 // The right arrow defined by the theme.
170 'type' => PARAM_RAW,
171 ],
3fb45f57
AN
172 'defaulteventcontext' => [
173 'type' => PARAM_INT,
0d049757 174 'default' => 0,
3fb45f57 175 ],
64ff737a
AN
176 ];
177 }
178
a4af4c96
SL
179 /**
180 * Get the additional values to inject while exporting.
181 *
182 * @param renderer_base $output The renderer.
183 * @return array Keys are the property names, values are their values.
184 */
64ff737a 185 protected function get_other_values(renderer_base $output) {
f58424c7
AN
186 $previousperiod = $this->get_previous_month_data();
187 $nextperiod = $this->get_next_month_data();
188 $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
189
190 $nextperiodlink = new moodle_url($this->url);
191 $nextperiodlink->param('time', $nextperiod[0]);
192
193 $previousperiodlink = new moodle_url($this->url);
194 $previousperiodlink->param('time', $previousperiod[0]);
c8b6e9ab 195
3fb45f57 196 $return = [
64ff737a 197 'courseid' => $this->calendar->courseid,
64ff737a 198 'filter_selector' => $this->get_course_filter_selector($output),
64ff737a
AN
199 'weeks' => $this->get_weeks($output),
200 'daynames' => $this->get_day_names($output),
c8b6e9ab 201 'view' => 'month',
f58424c7 202 'date' => (new date_exporter($date))->export($output),
c8b6e9ab 203 'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
f58424c7
AN
204 'previousperiod' => (new date_exporter($previousperiod))->export($output),
205 'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
206 'previousperiodlink' => $previousperiodlink->out(false),
207 'nextperiod' => (new date_exporter($nextperiod))->export($output),
208 'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
209 'nextperiodlink' => $nextperiodlink->out(false),
c8b6e9ab
AN
210 'larrow' => $output->larrow(),
211 'rarrow' => $output->rarrow(),
fee025ec 212 'includenavigation' => $this->includenavigation,
64ff737a 213 ];
3fb45f57
AN
214
215 if ($context = $this->get_default_add_context()) {
216 $return['defaulteventcontext'] = $context->id;
217 }
218
d0e56d84
AN
219 if ($this->calendar->categoryid) {
220 $return['categoryid'] = $this->calendar->categoryid;
221 }
222
3fb45f57 223 return $return;
64ff737a
AN
224 }
225
226 /**
227 * Get the course filter selector.
64ff737a 228 *
a4af4c96
SL
229 * @param renderer_base $output
230 * @return string The html code for the course filter selector.
64ff737a
AN
231 */
232 protected function get_course_filter_selector(renderer_base $output) {
233 $content = '';
72c4cacf
MN
234 $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'),
235 $this->calendar->course->id);
64ff737a
AN
236
237 return $content;
238 }
239
64ff737a
AN
240 /**
241 * Get the list of day names for display, re-ordered from the first day
242 * of the week.
243 *
244 * @param renderer_base $output
245 * @return day_name_exporter[]
246 */
247 protected function get_day_names(renderer_base $output) {
248 $weekdays = $this->related['type']->get_weekdays();
249 $daysinweek = count($weekdays);
250
251 $daynames = [];
252 for ($i = 0; $i < $daysinweek; $i++) {
253 // Bump the currentdayno and ensure it loops.
254 $dayno = ($i + $this->firstdayofweek + $daysinweek) % $daysinweek;
255 $dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
256 $daynames[] = $dayname->export($output);
257 }
258
259 return $daynames;
260 }
261
262 /**
263 * Get the list of week days, ordered into weeks and padded according
264 * to the value of the first day of the week.
265 *
a4af4c96
SL
266 * @param renderer_base $output
267 * @return array The list of weeks.
64ff737a
AN
268 */
269 protected function get_weeks(renderer_base $output) {
270 $weeks = [];
271 $alldays = $this->get_days();
272
273 $daysinweek = count($this->related['type']->get_weekdays());
274
275 // Calculate which day number is the first, and last day of the week.
276 $firstdayofweek = $this->firstdayofweek;
64ff737a
AN
277
278 // The first week is special as it may have padding at the beginning.
279 $day = reset($alldays);
280 $firstdayno = $day['wday'];
281
1a765e32 282 $prepadding = ($firstdayno + $daysinweek - $firstdayofweek) % $daysinweek;
64ff737a
AN
283 $daysinfirstweek = $daysinweek - $prepadding;
284 $days = array_slice($alldays, 0, $daysinfirstweek);
39e40fda 285 $week = new week_exporter($this->calendar, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
64ff737a
AN
286 $weeks[] = $week->export($output);
287
288 // Now chunk up the remaining day. and turn them into weeks.
289 $daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
290 foreach ($daychunks as $days) {
39e40fda 291 $week = new week_exporter($this->calendar, $days, 0, ($daysinweek - count($days)), $this->related);
64ff737a
AN
292 $weeks[] = $week->export($output);
293 }
294
295 return $weeks;
296 }
297
298 /**
299 * Get the list of days with the matching date array.
300 *
a4af4c96 301 * @return array
64ff737a
AN
302 */
303 protected function get_days() {
304 $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
305 $monthdays = $this->related['type']->get_num_days_in_month($date['year'], $date['mon']);
306
307 $days = [];
308 for ($dayno = 1; $dayno <= $monthdays; $dayno++) {
309 // Get the gregorian representation of the day.
310 $timestamp = $this->related['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
311
312 $days[] = $this->related['type']->timestamp_to_date_array($timestamp);
313 }
314
315 return $days;
316 }
317
a4af4c96
SL
318 /**
319 * Returns a list of objects that are related.
320 *
321 * @return array
322 */
64ff737a
AN
323 protected static function define_related() {
324 return [
325 'events' => '\core_calendar\local\event\entities\event_interface[]',
326 'cache' => '\core_calendar\external\events_related_objects_cache',
327 'type' => '\core_calendar\type_base',
328 ];
329 }
330
f58424c7
AN
331 /**
332 * Get the current month timestamp.
333 *
334 * @return int The month timestamp.
335 */
336 protected function get_month_data() {
337 $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
338 $monthtime = $this->related['type']->convert_to_gregorian($date['year'], $date['month'], 1);
339
340 return make_timestamp($monthtime['year'], $monthtime['month']);
341 }
342
a4af4c96
SL
343 /**
344 * Get the previous month timestamp.
345 *
346 * @return int The previous month timestamp.
347 */
f58424c7
AN
348 protected function get_previous_month_data() {
349 $type = $this->related['type'];
350 $date = $type->timestamp_to_date_array($this->calendar->time);
351 list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
352 $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
a4af4c96 353
f58424c7 354 return $type->timestamp_to_date_array($time);
64ff737a
AN
355 }
356
a4af4c96
SL
357 /**
358 * Get the next month timestamp.
359 *
360 * @return int The next month timestamp.
361 */
f58424c7
AN
362 protected function get_next_month_data() {
363 $type = $this->related['type'];
364 $date = $type->timestamp_to_date_array($this->calendar->time);
365 list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
366 $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
a4af4c96 367
f58424c7 368 return $type->timestamp_to_date_array($time);
64ff737a 369 }
fee025ec 370
6038d626
AN
371 /**
372 * Set whether the navigation should be shown.
373 *
374 * @param bool $include
375 * @return $this
376 */
fee025ec
AN
377 public function set_includenavigation($include) {
378 $this->includenavigation = $include;
379
380 return $this;
381 }
3fb45f57
AN
382
383 /**
384 * Get the default context for use when adding a new event.
385 *
386 * @return null|\context
387 */
388 protected function get_default_add_context() {
389 if (calendar_user_can_add_event($this->calendar->course)) {
390 return \context_course::instance($this->calendar->course->id);
391 }
392
393 return null;
394 }
64ff737a 395}