MDL-59890 calendar: Exporters support for categories
[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             'filter_selector' => [
112                 'type' => PARAM_RAW,
113             ],
114             'weeks' => [
115                 'type' => week_exporter::read_properties_definition(),
116                 'multiple' => true,
117             ],
118             'daynames' => [
119                 'type' => day_name_exporter::read_properties_definition(),
120                 'multiple' => true,
121             ],
122             'view' => [
123                 'type' => PARAM_ALPHA,
124             ],
125             'date' => [
126                 'type' => date_exporter::read_properties_definition(),
127             ],
128             'periodname' => [
129                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
130                 // calendar format.
131                 'type' => PARAM_RAW,
132             ],
133             'includenavigation' => [
134                 'type' => PARAM_BOOL,
135                 'default' => true,
136             ],
137             'previousperiod' => [
138                 'type' => date_exporter::read_properties_definition(),
139             ],
140             'previousperiodlink' => [
141                 'type' => PARAM_URL,
142             ],
143             'previousperiodname' => [
144                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
145                 // calendar format.
146                 'type' => PARAM_RAW,
147             ],
148             'nextperiod' => [
149                 'type' => date_exporter::read_properties_definition(),
150             ],
151             'nextperiodname' => [
152                 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
153                 // calendar format.
154                 'type' => PARAM_RAW,
155             ],
156             'nextperiodlink' => [
157                 'type' => PARAM_URL,
158             ],
159             'larrow' => [
160                 // The left arrow defined by the theme.
161                 'type' => PARAM_RAW,
162             ],
163             'rarrow' => [
164                 // The right arrow defined by the theme.
165                 'type' => PARAM_RAW,
166             ],
167             'defaulteventcontext' => [
168                 'type' => PARAM_INT,
169                 'default' => null,
170             ],
171         ];
172     }
174     /**
175      * Get the additional values to inject while exporting.
176      *
177      * @param renderer_base $output The renderer.
178      * @return array Keys are the property names, values are their values.
179      */
180     protected function get_other_values(renderer_base $output) {
181         $previousperiod = $this->get_previous_month_data();
182         $nextperiod = $this->get_next_month_data();
183         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
185         $nextperiodlink = new moodle_url($this->url);
186         $nextperiodlink->param('time', $nextperiod[0]);
188         $previousperiodlink = new moodle_url($this->url);
189         $previousperiodlink->param('time', $previousperiod[0]);
191         $return = [
192             'courseid' => $this->calendar->courseid,
193             'filter_selector' => $this->get_course_filter_selector($output),
194             'weeks' => $this->get_weeks($output),
195             'daynames' => $this->get_day_names($output),
196             'view' => 'month',
197             'date' => (new date_exporter($date))->export($output),
198             'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
199             'previousperiod' => (new date_exporter($previousperiod))->export($output),
200             'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
201             'previousperiodlink' => $previousperiodlink->out(false),
202             'nextperiod' => (new date_exporter($nextperiod))->export($output),
203             'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
204             'nextperiodlink' => $nextperiodlink->out(false),
205             'larrow' => $output->larrow(),
206             'rarrow' => $output->rarrow(),
207             'includenavigation' => $this->includenavigation,
208         ];
210         if ($context = $this->get_default_add_context()) {
211             $return['defaulteventcontext'] = $context->id;
212         }
214         return $return;
215     }
217     /**
218      * Get the course filter selector.
219      *
220      * @param renderer_base $output
221      * @return string The html code for the course filter selector.
222      */
223     protected function get_course_filter_selector(renderer_base $output) {
224         $content = '';
225         $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'));
227         return $content;
228     }
230     /**
231      * Get the list of day names for display, re-ordered from the first day
232      * of the week.
233      *
234      * @param   renderer_base $output
235      * @return  day_name_exporter[]
236      */
237     protected function get_day_names(renderer_base $output) {
238         $weekdays = $this->related['type']->get_weekdays();
239         $daysinweek = count($weekdays);
241         $daynames = [];
242         for ($i = 0; $i < $daysinweek; $i++) {
243             // Bump the currentdayno and ensure it loops.
244             $dayno = ($i + $this->firstdayofweek + $daysinweek) % $daysinweek;
245             $dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
246             $daynames[] = $dayname->export($output);
247         }
249         return $daynames;
250     }
252     /**
253      * Get the list of week days, ordered into weeks and padded according
254      * to the value of the first day of the week.
255      *
256      * @param renderer_base $output
257      * @return array The list of weeks.
258      */
259     protected function get_weeks(renderer_base $output) {
260         $weeks = [];
261         $alldays = $this->get_days();
263         $daysinweek = count($this->related['type']->get_weekdays());
265         // Calculate which day number is the first, and last day of the week.
266         $firstdayofweek = $this->firstdayofweek;
267         $lastdayofweek = ($firstdayofweek + $daysinweek - 1) % $daysinweek;
269         // The first week is special as it may have padding at the beginning.
270         $day = reset($alldays);
271         $firstdayno = $day['wday'];
273         $prepadding = ($firstdayno + $daysinweek - 1) % $daysinweek;
274         $daysinfirstweek = $daysinweek - $prepadding;
275         $days = array_slice($alldays, 0, $daysinfirstweek);
276         $week = new week_exporter($this->calendar, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
277         $weeks[] = $week->export($output);
279         // Now chunk up the remaining day. and turn them into weeks.
280         $daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
281         foreach ($daychunks as $days) {
282             $week = new week_exporter($this->calendar, $days, 0, ($daysinweek - count($days)), $this->related);
283             $weeks[] = $week->export($output);
284         }
286         return $weeks;
287     }
289     /**
290      * Get the list of days with the matching date array.
291      *
292      * @return array
293      */
294     protected function get_days() {
295         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
296         $monthdays = $this->related['type']->get_num_days_in_month($date['year'], $date['mon']);
298         $days = [];
299         for ($dayno = 1; $dayno <= $monthdays; $dayno++) {
300             // Get the gregorian representation of the day.
301             $timestamp = $this->related['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
303             $days[] = $this->related['type']->timestamp_to_date_array($timestamp);
304         }
306         return $days;
307     }
309     /**
310      * Returns a list of objects that are related.
311      *
312      * @return array
313      */
314     protected static function define_related() {
315         return [
316             'events' => '\core_calendar\local\event\entities\event_interface[]',
317             'cache' => '\core_calendar\external\events_related_objects_cache',
318             'type' => '\core_calendar\type_base',
319         ];
320     }
322     /**
323      * Get the current month timestamp.
324      *
325      * @return int The month timestamp.
326      */
327     protected function get_month_data() {
328         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
329         $monthtime = $this->related['type']->convert_to_gregorian($date['year'], $date['month'], 1);
331         return make_timestamp($monthtime['year'], $monthtime['month']);
332     }
334     /**
335      * Get the previous month timestamp.
336      *
337      * @return int The previous month timestamp.
338      */
339     protected function get_previous_month_data() {
340         $type = $this->related['type'];
341         $date = $type->timestamp_to_date_array($this->calendar->time);
342         list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
343         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
345         return $type->timestamp_to_date_array($time);
346     }
348     /**
349      * Get the next month timestamp.
350      *
351      * @return int The next month timestamp.
352      */
353     protected function get_next_month_data() {
354         $type = $this->related['type'];
355         $date = $type->timestamp_to_date_array($this->calendar->time);
356         list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
357         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
359         return $type->timestamp_to_date_array($time);
360     }
362     /**
363      * Set whether the navigation should be shown.
364      *
365      * @param   bool    $include
366      * @return  $this
367      */
368     public function set_includenavigation($include) {
369         $this->includenavigation = $include;
371         return $this;
372     }
374     /**
375      * Get the default context for use when adding a new event.
376      *
377      * @return null|\context
378      */
379     protected function get_default_add_context() {
380         if (calendar_user_can_add_event($this->calendar->course)) {
381             return \context_course::instance($this->calendar->course->id);
382         }
384         return null;
385     }