2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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;
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 {
43 * @var \calendar_information $calendar The calendar to be rendered.
48 * @var int $firstdayofweek The first day of the week.
50 protected $firstdayofweek;
53 * @var moodle_url $url The URL for the events page.
58 * @var bool $includenavigation Whether navigation should be included on the output.
60 protected $includenavigation = true;
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', [
75 'time' => $calendar->time,
78 if ($this->calendar->courseid) {
79 $this->url->param('course', $this->calendar->courseid);
82 $related['type'] = $type;
85 'url' => $this->url->out(false),
88 parent::__construct($data, $related);
91 protected static function define_properties() {
100 * Return the list of additional properties.
104 protected static function define_other_properties() {
109 'filter_selector' => [
113 'type' => week_exporter::read_properties_definition(),
117 'type' => day_name_exporter::read_properties_definition(),
121 'type' => PARAM_ALPHA,
124 'type' => date_exporter::read_properties_definition(),
127 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
131 'includenavigation' => [
132 'type' => PARAM_BOOL,
135 'previousperiod' => [
136 'type' => date_exporter::read_properties_definition(),
138 'previousperiodlink' => [
141 'previousperiodname' => [
142 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
147 'type' => date_exporter::read_properties_definition(),
149 'nextperiodname' => [
150 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
154 'nextperiodlink' => [
158 // The left arrow defined by the theme.
162 // The right arrow defined by the theme.
165 'defaulteventcontext' => [
173 * Get the additional values to inject while exporting.
175 * @param renderer_base $output The renderer.
176 * @return array Keys are the property names, values are their values.
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]);
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),
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,
208 if ($context = $this->get_default_add_context()) {
209 $return['defaulteventcontext'] = $context->id;
216 * Get the course filter selector.
218 * @param renderer_base $output
219 * @return string The html code for the course filter selector.
221 protected function get_course_filter_selector(renderer_base $output) {
223 $content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'));
229 * Get the list of day names for display, re-ordered from the first day
232 * @param renderer_base $output
233 * @return day_name_exporter[]
235 protected function get_day_names(renderer_base $output) {
236 $weekdays = $this->related['type']->get_weekdays();
237 $daysinweek = count($weekdays);
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);
251 * Get the list of week days, ordered into weeks and padded according
252 * to the value of the first day of the week.
254 * @param renderer_base $output
255 * @return array The list of weeks.
257 protected function get_weeks(renderer_base $output) {
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);
288 * Get the list of days with the matching date array.
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']);
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);
308 * Returns a list of objects that are related.
312 protected static function define_related() {
314 'events' => '\core_calendar\local\event\entities\event_interface[]',
315 'cache' => '\core_calendar\external\events_related_objects_cache',
316 'type' => '\core_calendar\type_base',
321 * Get the current month timestamp.
323 * @return int The month timestamp.
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']);
333 * Get the previous month timestamp.
335 * @return int The previous month timestamp.
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);
347 * Get the next month timestamp.
349 * @return int The next month timestamp.
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);
361 * Set whether the navigation should be shown.
363 * @param bool $include
366 public function set_includenavigation($include) {
367 $this->includenavigation = $include;
373 * Get the default context for use when adding a new event.
375 * @return null|\context
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);