MDL-60823 calendar: use first day of week on prepadding calc
[moodle.git] / calendar / classes / external / month_exporter.php
blobb735d36d12ec7f6a6151af3ea4456bc6e9c2b60c
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.
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/>.
17 /**
18 * Contains event class for displaying the month view.
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
25 namespace core_calendar\external;
27 defined('MOODLE_INTERNAL') || die();
29 use core\external\exporter;
30 use renderer_base;
31 use moodle_url;
33 /**
34 * Class for displaying the month view.
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
40 class month_exporter extends exporter {
42 /**
43 * @var \calendar_information $calendar The calendar to be rendered.
45 protected $calendar;
47 /**
48 * @var int $firstdayofweek The first day of the week.
50 protected $firstdayofweek;
52 /**
53 * @var moodle_url $url The URL for the events page.
55 protected $url;
57 /**
58 * @var bool $includenavigation Whether navigation should be included on the output.
60 protected $includenavigation = true;
62 /**
63 * Constructor for month_exporter.
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
69 public function __construct(\calendar_information $calendar, \core_calendar\type_base $type, $related) {
70 $this->calendar = $calendar;
71 $this->firstdayofweek = $type->get_starting_weekday();
73 $this->url = new moodle_url('/calendar/view.php', [
74 'view' => 'month',
75 'time' => $calendar->time,
76 ]);
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);
84 $related['type'] = $type;
86 $data = [
87 'url' => $this->url->out(false),
90 parent::__construct($data, $related);
93 protected static function define_properties() {
94 return [
95 'url' => [
96 'type' => PARAM_URL,
102 * Return the list of additional properties.
104 * @return array
106 protected static function define_other_properties() {
107 return [
108 'courseid' => [
109 'type' => PARAM_INT,
111 'categoryid' => [
112 'type' => PARAM_INT,
113 'optional' => true,
114 'default' => 0,
116 'filter_selector' => [
117 'type' => PARAM_RAW,
119 'weeks' => [
120 'type' => week_exporter::read_properties_definition(),
121 'multiple' => true,
123 'daynames' => [
124 'type' => day_name_exporter::read_properties_definition(),
125 'multiple' => true,
127 'view' => [
128 'type' => PARAM_ALPHA,
130 'date' => [
131 'type' => date_exporter::read_properties_definition(),
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,
138 'includenavigation' => [
139 'type' => PARAM_BOOL,
140 'default' => true,
142 'previousperiod' => [
143 'type' => date_exporter::read_properties_definition(),
145 'previousperiodlink' => [
146 'type' => PARAM_URL,
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,
153 'nextperiod' => [
154 'type' => date_exporter::read_properties_definition(),
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,
161 'nextperiodlink' => [
162 'type' => PARAM_URL,
164 'larrow' => [
165 // The left arrow defined by the theme.
166 'type' => PARAM_RAW,
168 'rarrow' => [
169 // The right arrow defined by the theme.
170 'type' => PARAM_RAW,
172 'defaulteventcontext' => [
173 'type' => PARAM_INT,
174 'default' => 0,
180 * Get the additional values to inject while exporting.
182 * @param renderer_base $output The renderer.
183 * @return array Keys are the property names, values are their values.
185 protected function get_other_values(renderer_base $output) {
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);
190 $nextperiodlink = new moodle_url($this->url);
191 $nextperiodlink->param('time', $nextperiod[0]);
193 $previousperiodlink = new moodle_url($this->url);
194 $previousperiodlink->param('time', $previousperiod[0]);
196 $return = [
197 'courseid' => $this->calendar->courseid,
198 'filter_selector' => $this->get_course_filter_selector($output),
199 'weeks' => $this->get_weeks($output),
200 'daynames' => $this->get_day_names($output),
201 'view' => 'month',
202 'date' => (new date_exporter($date))->export($output),
203 'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
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),
210 'larrow' => $output->larrow(),
211 'rarrow' => $output->rarrow(),
212 'includenavigation' => $this->includenavigation,
215 if ($context = $this->get_default_add_context()) {
216 $return['defaulteventcontext'] = $context->id;
219 if ($this->calendar->categoryid) {
220 $return['categoryid'] = $this->calendar->categoryid;
223 return $return;
227 * Get the course filter selector.
229 * @param renderer_base $output
230 * @return string The html code for the course filter selector.
232 protected function get_course_filter_selector(renderer_base $output) {
233 $content = '';
234 $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'),
235 $this->calendar->course->id);
237 return $content;
241 * Get the list of day names for display, re-ordered from the first day
242 * of the week.
244 * @param renderer_base $output
245 * @return day_name_exporter[]
247 protected function get_day_names(renderer_base $output) {
248 $weekdays = $this->related['type']->get_weekdays();
249 $daysinweek = count($weekdays);
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);
259 return $daynames;
263 * Get the list of week days, ordered into weeks and padded according
264 * to the value of the first day of the week.
266 * @param renderer_base $output
267 * @return array The list of weeks.
269 protected function get_weeks(renderer_base $output) {
270 $weeks = [];
271 $alldays = $this->get_days();
273 $daysinweek = count($this->related['type']->get_weekdays());
275 // Calculate which day number is the first, and last day of the week.
276 $firstdayofweek = $this->firstdayofweek;
278 // The first week is special as it may have padding at the beginning.
279 $day = reset($alldays);
280 $firstdayno = $day['wday'];
282 $prepadding = ($firstdayno + $daysinweek - $firstdayofweek) % $daysinweek;
283 $daysinfirstweek = $daysinweek - $prepadding;
284 $days = array_slice($alldays, 0, $daysinfirstweek);
285 $week = new week_exporter($this->calendar, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
286 $weeks[] = $week->export($output);
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) {
291 $week = new week_exporter($this->calendar, $days, 0, ($daysinweek - count($days)), $this->related);
292 $weeks[] = $week->export($output);
295 return $weeks;
299 * Get the list of days with the matching date array.
301 * @return array
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']);
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);
312 $days[] = $this->related['type']->timestamp_to_date_array($timestamp);
315 return $days;
319 * Returns a list of objects that are related.
321 * @return array
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',
332 * Get the current month timestamp.
334 * @return int The month timestamp.
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);
340 return make_timestamp($monthtime['year'], $monthtime['month']);
344 * Get the previous month timestamp.
346 * @return int The previous month timestamp.
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);
354 return $type->timestamp_to_date_array($time);
358 * Get the next month timestamp.
360 * @return int The next month timestamp.
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);
368 return $type->timestamp_to_date_array($time);
372 * Set whether the navigation should be shown.
374 * @param bool $include
375 * @return $this
377 public function set_includenavigation($include) {
378 $this->includenavigation = $include;
380 return $this;
384 * Get the default context for use when adding a new event.
386 * @return null|\context
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);
393 return null;