b71ccc85a3a93c778cb475a5f07e152d0e558b9f
[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             'defaulteventcontext' => [
166                 'type' => PARAM_INT,
167                 'default' => null,
168             ],
169         ];
170     }
172     /**
173      * Get the additional values to inject while exporting.
174      *
175      * @param renderer_base $output The renderer.
176      * @return array Keys are the property names, values are their values.
177      */
178     protected function get_other_values(renderer_base $output) {
179         $previousperiod = $this->get_previous_month_data();
180         $nextperiod = $this->get_next_month_data();
181         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
183         $nextperiodlink = new moodle_url($this->url);
184         $nextperiodlink->param('time', $nextperiod[0]);
186         $previousperiodlink = new moodle_url($this->url);
187         $previousperiodlink->param('time', $previousperiod[0]);
189         $return = [
190             'courseid' => $this->calendar->courseid,
191             'filter_selector' => $this->get_course_filter_selector($output),
192             'weeks' => $this->get_weeks($output),
193             'daynames' => $this->get_day_names($output),
194             'view' => 'month',
195             'date' => (new date_exporter($date))->export($output),
196             'periodname' => userdate($this->calendar->time, get_string('strftimemonthyear')),
197             'previousperiod' => (new date_exporter($previousperiod))->export($output),
198             'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
199             'previousperiodlink' => $previousperiodlink->out(false),
200             'nextperiod' => (new date_exporter($nextperiod))->export($output),
201             'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
202             'nextperiodlink' => $nextperiodlink->out(false),
203             'larrow' => $output->larrow(),
204             'rarrow' => $output->rarrow(),
205             'includenavigation' => $this->includenavigation,
206         ];
208         if ($context = $this->get_default_add_context()) {
209             $return['defaulteventcontext'] = $context->id;
210         }
212         return $return;
213     }
215     /**
216      * Get the course filter selector.
217      *
218      * @param renderer_base $output
219      * @return string The html code for the course filter selector.
220      */
221     protected function get_course_filter_selector(renderer_base $output) {
222         $content = '';
223         $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'));
225         return $content;
226     }
228     /**
229      * Get the list of day names for display, re-ordered from the first day
230      * of the week.
231      *
232      * @param   renderer_base $output
233      * @return  day_name_exporter[]
234      */
235     protected function get_day_names(renderer_base $output) {
236         $weekdays = $this->related['type']->get_weekdays();
237         $daysinweek = count($weekdays);
239         $daynames = [];
240         for ($i = 0; $i < $daysinweek; $i++) {
241             // Bump the currentdayno and ensure it loops.
242             $dayno = ($i + $this->firstdayofweek + $daysinweek) % $daysinweek;
243             $dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
244             $daynames[] = $dayname->export($output);
245         }
247         return $daynames;
248     }
250     /**
251      * Get the list of week days, ordered into weeks and padded according
252      * to the value of the first day of the week.
253      *
254      * @param renderer_base $output
255      * @return array The list of weeks.
256      */
257     protected function get_weeks(renderer_base $output) {
258         $weeks = [];
259         $alldays = $this->get_days();
261         $daysinweek = count($this->related['type']->get_weekdays());
263         // Calculate which day number is the first, and last day of the week.
264         $firstdayofweek = $this->firstdayofweek;
265         $lastdayofweek = ($firstdayofweek + $daysinweek - 1) % $daysinweek;
267         // The first week is special as it may have padding at the beginning.
268         $day = reset($alldays);
269         $firstdayno = $day['wday'];
271         $prepadding = ($firstdayno + $daysinweek - 1) % $daysinweek;
272         $daysinfirstweek = $daysinweek - $prepadding;
273         $days = array_slice($alldays, 0, $daysinfirstweek);
274         $week = new week_exporter($this->calendar, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
275         $weeks[] = $week->export($output);
277         // Now chunk up the remaining day. and turn them into weeks.
278         $daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
279         foreach ($daychunks as $days) {
280             $week = new week_exporter($this->calendar, $days, 0, ($daysinweek - count($days)), $this->related);
281             $weeks[] = $week->export($output);
282         }
284         return $weeks;
285     }
287     /**
288      * Get the list of days with the matching date array.
289      *
290      * @return array
291      */
292     protected function get_days() {
293         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
294         $monthdays = $this->related['type']->get_num_days_in_month($date['year'], $date['mon']);
296         $days = [];
297         for ($dayno = 1; $dayno <= $monthdays; $dayno++) {
298             // Get the gregorian representation of the day.
299             $timestamp = $this->related['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
301             $days[] = $this->related['type']->timestamp_to_date_array($timestamp);
302         }
304         return $days;
305     }
307     /**
308      * Returns a list of objects that are related.
309      *
310      * @return array
311      */
312     protected static function define_related() {
313         return [
314             'events' => '\core_calendar\local\event\entities\event_interface[]',
315             'cache' => '\core_calendar\external\events_related_objects_cache',
316             'type' => '\core_calendar\type_base',
317         ];
318     }
320     /**
321      * Get the current month timestamp.
322      *
323      * @return int The month timestamp.
324      */
325     protected function get_month_data() {
326         $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
327         $monthtime = $this->related['type']->convert_to_gregorian($date['year'], $date['month'], 1);
329         return make_timestamp($monthtime['year'], $monthtime['month']);
330     }
332     /**
333      * Get the previous month timestamp.
334      *
335      * @return int The previous month timestamp.
336      */
337     protected function get_previous_month_data() {
338         $type = $this->related['type'];
339         $date = $type->timestamp_to_date_array($this->calendar->time);
340         list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
341         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
343         return $type->timestamp_to_date_array($time);
344     }
346     /**
347      * Get the next month timestamp.
348      *
349      * @return int The next month timestamp.
350      */
351     protected function get_next_month_data() {
352         $type = $this->related['type'];
353         $date = $type->timestamp_to_date_array($this->calendar->time);
354         list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
355         $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
357         return $type->timestamp_to_date_array($time);
358     }
360     /**
361      * Set whether the navigation should be shown.
362      *
363      * @param   bool    $include
364      * @return  $this
365      */
366     public function set_includenavigation($include) {
367         $this->includenavigation = $include;
369         return $this;
370     }
372     /**
373      * Get the default context for use when adding a new event.
374      *
375      * @return null|\context
376      */
377     protected function get_default_add_context() {
378         if (calendar_user_can_add_event($this->calendar->course)) {
379             return \context_course::instance($this->calendar->course->id);
380         }
382         return null;
383     }