MDL-60823 calendar: use first day of week on prepadding calc
[moodle.git] / calendar / classes / external / month_exporter.php
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/>.
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  */
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.
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  */
40 class month_exporter extends exporter {
42     /**
43      * @var \calendar_information $calendar The calendar to be rendered.
44      */
45     protected $calendar;
47     /**
48      * @var int $firstdayofweek The first day of the week.
49      */
50     protected $firstdayofweek;
52     /**
53      * @var moodle_url $url The URL for the events page.
54      */
55     protected $url;
57     /**
58      * @var bool $includenavigation Whether navigation should be included on the output.
59      */
60     protected $includenavigation = true;
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();
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);
82         }
84         $related['type'] = $type;
86         $data = [
87             'url' => $this->url->out(false),
88         ];
90         parent::__construct($data, $related);
91     }
93     protected static function define_properties() {
94         return [
95             'url' => [
96                 'type' => PARAM_URL,
97             ],
98         ];
99     }
101     /**
102      * Return the list of additional properties.
103      *
104      * @return array
105      */
106     protected static function define_other_properties() {
107         return [
108             'courseid' => [
109                 'type' => PARAM_INT,
110             ],
111             'categoryid' => [
112                 'type' => PARAM_INT,
113                 'optional' => true,
114                 'default' => 0,
115             ],
116             'filter_selector' => [
117                 'type' => PARAM_RAW,
118             ],
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             ],
130             'date' => [
131                 'type' => date_exporter::read_properties_definition(),
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             ],
138             'includenavigation' => [
139                 'type' => PARAM_BOOL,
140                 'default' => true,
141             ],
142             'previousperiod' => [
143                 'type' => date_exporter::read_properties_definition(),
144             ],
145             'previousperiodlink' => [
146                 'type' => PARAM_URL,
147             ],
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             ],
153             'nextperiod' => [
154                 'type' => date_exporter::read_properties_definition(),
155             ],
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             ],
161             'nextperiodlink' => [
162                 'type' => PARAM_URL,
163             ],
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             ],
172             'defaulteventcontext' => [
173                 'type' => PARAM_INT,
174                 'default' => 0,
175             ],
176         ];
177     }
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      */
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,
213         ];
215         if ($context = $this->get_default_add_context()) {
216             $return['defaulteventcontext'] = $context->id;
217         }
219         if ($this->calendar->categoryid) {
220             $return['categoryid'] = $this->calendar->categoryid;
221         }
223         return $return;
224     }
226     /**
227      * Get the course filter selector.
228      *
229      * @param renderer_base $output
230      * @return string The html code for the course filter selector.
231      */
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;
238     }
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);
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         }
259         return $daynames;
260     }
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      *
266      * @param renderer_base $output
267      * @return array The list of weeks.
268      */
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);
293         }
295         return $weeks;
296     }
298     /**
299      * Get the list of days with the matching date array.
300      *
301      * @return array
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']);
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);
313         }
315         return $days;
316     }
318     /**
319      * Returns a list of objects that are related.
320      *
321      * @return array
322      */
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     }
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);
340         return make_timestamp($monthtime['year'], $monthtime['month']);
341     }
343     /**
344      * Get the previous month timestamp.
345      *
346      * @return int The previous month timestamp.
347      */
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);
355     }
357     /**
358      * Get the next month timestamp.
359      *
360      * @return int The next month timestamp.
361      */
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);
369     }
371     /**
372      * Set whether the navigation should be shown.
373      *
374      * @param   bool    $include
375      * @return  $this
376      */
377     public function set_includenavigation($include) {
378         $this->includenavigation = $include;
380         return $this;
381     }
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         }
393         return null;
394     }