Merge branch 'MDL-37810-master' of https://github.com/snake/moodle
[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->courseid) {
79             $this->url->param('course', $this->calendar->courseid);
80         }
82         $related['type'] = $type;
84         $data = [
85             'url' => $this->url->out(false),
86         ];
88         parent::__construct($data, $related);
89     }
91     protected static function define_properties() {
92         return [
93             'url' => [
94                 'type' => PARAM_URL,
95             ],
96         ];
97     }
99     /**
100      * Return the list of additional properties.
101      *
102      * @return array
103      */
104     protected static function define_other_properties() {
105         return [
106             'courseid' => [
107                 'type' => PARAM_INT,
108             ],
109             'filter_selector' => [
110                 'type' => PARAM_RAW,
111             ],
112             'weeks' => [
113                 'type' => week_exporter::read_properties_definition(),
114                 'multiple' => true,
115             ],
116             'daynames' => [
117                 'type' => day_name_exporter::read_properties_definition(),
118                 'multiple' => true,
119             ],
120             'view' => [
121                 'type' => PARAM_ALPHA,
122             ],
123             'date' => [
124                 'type' => date_exporter::read_properties_definition(),
125             ],
126             'periodname' => [
127                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
128                 // calendar format.
129                 'type' => PARAM_RAW,
130             ],
131             'includenavigation' => [
132                 'type' => PARAM_BOOL,
133                 'default' => true,
134             ],
135             'previousperiod' => [
136                 'type' => date_exporter::read_properties_definition(),
137             ],
138             'previousperiodlink' => [
139                 'type' => PARAM_URL,
140             ],
141             'previousperiodname' => [
142                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
143                 // calendar format.
144                 'type' => PARAM_RAW,
145             ],
146             'nextperiod' => [
147                 'type' => date_exporter::read_properties_definition(),
148             ],
149             'nextperiodname' => [
150                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
151                 // calendar format.
152                 'type' => PARAM_RAW,
153             ],
154             'nextperiodlink' => [
155                 'type' => PARAM_URL,
156             ],
157             'larrow' => [
158                 // The left arrow defined by the theme.
159                 'type' => PARAM_RAW,
160             ],
161             'rarrow' => [
162                 // The right arrow defined by the theme.
163                 'type' => PARAM_RAW,
164             ],
165         ];
166     }
168     /**
169      * Get the additional values to inject while exporting.
170      *
171      * @param renderer_base $output The renderer.
172      * @return array Keys are the property names, values are their values.
173      */
174     protected function get_other_values(renderer_base $output) {
175         $previousperiod = $this->get_previous_month_data();
176         $nextperiod = $this->get_next_month_data();
177         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
179         $nextperiodlink = new moodle_url($this->url);
180         $nextperiodlink->param('time', $nextperiod[0]);
182         $previousperiodlink = new moodle_url($this->url);
183         $previousperiodlink->param('time', $previousperiod[0]);
185         return [
186             'courseid' => $this->calendar->courseid,
187             'filter_selector' => $this->get_course_filter_selector($output),
188             'weeks' => $this->get_weeks($output),
189             'daynames' => $this->get_day_names($output),
190             'view' => 'month',
191             'date' => (new date_exporter($date))->export($output),
192             'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
193             'previousperiod' => (new date_exporter($previousperiod))->export($output),
194             'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
195             'previousperiodlink' => $previousperiodlink->out(false),
196             'nextperiod' => (new date_exporter($nextperiod))->export($output),
197             'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
198             'nextperiodlink' => $nextperiodlink->out(false),
199             'larrow' => $output->larrow(),
200             'rarrow' => $output->rarrow(),
201             'includenavigation' => $this->includenavigation,
202         ];
203     }
205     /**
206      * Get the course filter selector.
207      *
208      * @param renderer_base $output
209      * @return string The html code for the course filter selector.
210      */
211     protected function get_course_filter_selector(renderer_base $output) {
212         $content = '';
213         $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'));
214         if (calendar_user_can_add_event($this->calendar->course)) {
215             $content .= $output->add_event_button($this->calendar->courseid, 0, 0, 0, $this->calendar->time);
216         }
218         return $content;
219     }
221     /**
222      * Get the list of day names for display, re-ordered from the first day
223      * of the week.
224      *
225      * @param   renderer_base $output
226      * @return  day_name_exporter[]
227      */
228     protected function get_day_names(renderer_base $output) {
229         $weekdays = $this->related['type']->get_weekdays();
230         $daysinweek = count($weekdays);
232         $daynames = [];
233         for ($i = 0; $i < $daysinweek; $i++) {
234             // Bump the currentdayno and ensure it loops.
235             $dayno = ($i + $this->firstdayofweek + $daysinweek) % $daysinweek;
236             $dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
237             $daynames[] = $dayname->export($output);
238         }
240         return $daynames;
241     }
243     /**
244      * Get the list of week days, ordered into weeks and padded according
245      * to the value of the first day of the week.
246      *
247      * @param renderer_base $output
248      * @return array The list of weeks.
249      */
250     protected function get_weeks(renderer_base $output) {
251         $weeks = [];
252         $alldays = $this->get_days();
254         $daysinweek = count($this->related['type']->get_weekdays());
256         // Calculate which day number is the first, and last day of the week.
257         $firstdayofweek = $this->firstdayofweek;
258         $lastdayofweek = ($firstdayofweek + $daysinweek - 1) % $daysinweek;
260         // The first week is special as it may have padding at the beginning.
261         $day = reset($alldays);
262         $firstdayno = $day['wday'];
264         $prepadding = ($firstdayno + $daysinweek - 1) % $daysinweek;
265         $daysinfirstweek = $daysinweek - $prepadding;
266         $days = array_slice($alldays, 0, $daysinfirstweek);
267         $week = new week_exporter($this->calendar, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
268         $weeks[] = $week->export($output);
270         // Now chunk up the remaining day. and turn them into weeks.
271         $daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
272         foreach ($daychunks as $days) {
273             $week = new week_exporter($this->calendar, $days, 0, ($daysinweek - count($days)), $this->related);
274             $weeks[] = $week->export($output);
275         }
277         return $weeks;
278     }
280     /**
281      * Get the list of days with the matching date array.
282      *
283      * @return array
284      */
285     protected function get_days() {
286         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
287         $monthdays = $this->related['type']->get_num_days_in_month($date['year'], $date['mon']);
289         $days = [];
290         for ($dayno = 1; $dayno <= $monthdays; $dayno++) {
291             // Get the gregorian representation of the day.
292             $timestamp = $this->related['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
294             $days[] = $this->related['type']->timestamp_to_date_array($timestamp);
295         }
297         return $days;
298     }
300     /**
301      * Returns a list of objects that are related.
302      *
303      * @return array
304      */
305     protected static function define_related() {
306         return [
307             'events' => '\core_calendar\local\event\entities\event_interface[]',
308             'cache' => '\core_calendar\external\events_related_objects_cache',
309             'type' => '\core_calendar\type_base',
310         ];
311     }
313     /**
314      * Get the current month timestamp.
315      *
316      * @return int The month timestamp.
317      */
318     protected function get_month_data() {
319         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
320         $monthtime = $this->related['type']->convert_to_gregorian($date['year'], $date['month'], 1);
322         return make_timestamp($monthtime['year'], $monthtime['month']);
323     }
325     /**
326      * Get the previous month timestamp.
327      *
328      * @return int The previous month timestamp.
329      */
330     protected function get_previous_month_data() {
331         $type = $this->related['type'];
332         $date = $type->timestamp_to_date_array($this->calendar->time);
333         list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
334         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
336         return $type->timestamp_to_date_array($time);
337     }
339     /**
340      * Get the next month timestamp.
341      *
342      * @return int The next month timestamp.
343      */
344     protected function get_next_month_data() {
345         $type = $this->related['type'];
346         $date = $type->timestamp_to_date_array($this->calendar->time);
347         list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
348         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
350         return $type->timestamp_to_date_array($time);
351     }
353     /**
354      * Set whether the navigation should be shown.
355      *
356      * @param   bool    $include
357      * @return  $this
358      */
359     public function set_includenavigation($include) {
360         $this->includenavigation = $include;
362         return $this;
363     }