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 * @var bool $initialeventsloaded Whether the events have been loaded for this month.
65 protected $initialeventsloaded = true;
68 * Constructor for month_exporter.
70 * @param \calendar_information $calendar The calendar being represented
71 * @param \core_calendar\type_base $type The calendar type (e.g. Gregorian)
72 * @param array $related The related information
74 public function __construct(\calendar_information
$calendar, \core_calendar\type_base
$type, $related) {
75 $this->calendar
= $calendar;
76 $this->firstdayofweek
= $type->get_starting_weekday();
78 $this->url
= new moodle_url('/calendar/view.php', [
80 'time' => $calendar->time
,
83 if ($this->calendar
->course
&& SITEID
!== $this->calendar
->course
->id
) {
84 $this->url
->param('course', $this->calendar
->course
->id
);
85 } else if ($this->calendar
->categoryid
) {
86 $this->url
->param('category', $this->calendar
->categoryid
);
89 $related['type'] = $type;
92 'url' => $this->url
->out(false),
95 parent
::__construct($data, $related);
98 protected static function define_properties() {
107 * Return the list of additional properties.
111 protected static function define_other_properties() {
121 'filter_selector' => [
125 'type' => week_exporter
::read_properties_definition(),
129 'type' => day_name_exporter
::read_properties_definition(),
133 'type' => PARAM_ALPHA
,
136 'type' => date_exporter
::read_properties_definition(),
139 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
143 'includenavigation' => [
144 'type' => PARAM_BOOL
,
147 // Tracks whether the first set of events have been loaded and provided
149 'initialeventsloaded' => [
150 'type' => PARAM_BOOL
,
153 'previousperiod' => [
154 'type' => date_exporter
::read_properties_definition(),
156 'previousperiodlink' => [
159 'previousperiodname' => [
160 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
165 'type' => date_exporter
::read_properties_definition(),
167 'nextperiodname' => [
168 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
172 'nextperiodlink' => [
176 // The left arrow defined by the theme.
180 // The right arrow defined by the theme.
183 'defaulteventcontext' => [
191 * Get the additional values to inject while exporting.
193 * @param renderer_base $output The renderer.
194 * @return array Keys are the property names, values are their values.
196 protected function get_other_values(renderer_base
$output) {
197 $previousperiod = $this->get_previous_month_data();
198 $nextperiod = $this->get_next_month_data();
199 $date = $this->related
['type']->timestamp_to_date_array($this->calendar
->time
);
201 $nextperiodlink = new moodle_url($this->url
);
202 $nextperiodlink->param('time', $nextperiod[0]);
204 $previousperiodlink = new moodle_url($this->url
);
205 $previousperiodlink->param('time', $previousperiod[0]);
208 'courseid' => $this->calendar
->courseid
,
209 'filter_selector' => $this->get_course_filter_selector($output),
210 'weeks' => $this->get_weeks($output),
211 'daynames' => $this->get_day_names($output),
213 'date' => (new date_exporter($date))->export($output),
214 'periodname' => userdate($this->calendar
->time
, get_string('strftimemonthyear')),
215 'previousperiod' => (new date_exporter($previousperiod))->export($output),
216 'previousperiodname' => userdate($previousperiod[0], get_string('strftimemonthyear')),
217 'previousperiodlink' => $previousperiodlink->out(false),
218 'nextperiod' => (new date_exporter($nextperiod))->export($output),
219 'nextperiodname' => userdate($nextperiod[0], get_string('strftimemonthyear')),
220 'nextperiodlink' => $nextperiodlink->out(false),
221 'larrow' => $output->larrow(),
222 'rarrow' => $output->rarrow(),
223 'includenavigation' => $this->includenavigation
,
224 'initialeventsloaded' => $this->initialeventsloaded
,
227 if ($context = $this->get_default_add_context()) {
228 $return['defaulteventcontext'] = $context->id
;
231 if ($this->calendar
->categoryid
) {
232 $return['categoryid'] = $this->calendar
->categoryid
;
239 * Get the course filter selector.
241 * @param renderer_base $output
242 * @return string The html code for the course filter selector.
244 protected function get_course_filter_selector(renderer_base
$output) {
246 $content .= $output->course_filter_selector($this->url
, get_string('detailedmonthviewfor', 'calendar'),
247 $this->calendar
->course
->id
);
253 * Get the list of day names for display, re-ordered from the first day
256 * @param renderer_base $output
257 * @return day_name_exporter[]
259 protected function get_day_names(renderer_base
$output) {
260 $weekdays = $this->related
['type']->get_weekdays();
261 $daysinweek = count($weekdays);
264 for ($i = 0; $i < $daysinweek; $i++
) {
265 // Bump the currentdayno and ensure it loops.
266 $dayno = ($i +
$this->firstdayofweek +
$daysinweek) %
$daysinweek;
267 $dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
268 $daynames[] = $dayname->export($output);
275 * Get the list of week days, ordered into weeks and padded according
276 * to the value of the first day of the week.
278 * @param renderer_base $output
279 * @return array The list of weeks.
281 protected function get_weeks(renderer_base
$output) {
283 $alldays = $this->get_days();
285 $daysinweek = count($this->related
['type']->get_weekdays());
287 // Calculate which day number is the first, and last day of the week.
288 $firstdayofweek = $this->firstdayofweek
;
290 // The first week is special as it may have padding at the beginning.
291 $day = reset($alldays);
292 $firstdayno = $day['wday'];
294 $prepadding = ($firstdayno +
$daysinweek - $firstdayofweek) %
$daysinweek;
295 $daysinfirstweek = $daysinweek - $prepadding;
296 $days = array_slice($alldays, 0, $daysinfirstweek);
297 $week = new week_exporter($this->calendar
, $days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related
);
298 $weeks[] = $week->export($output);
300 // Now chunk up the remaining day. and turn them into weeks.
301 $daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
302 foreach ($daychunks as $days) {
303 $week = new week_exporter($this->calendar
, $days, 0, ($daysinweek - count($days)), $this->related
);
304 $weeks[] = $week->export($output);
311 * Get the list of days with the matching date array.
315 protected function get_days() {
316 $date = $this->related
['type']->timestamp_to_date_array($this->calendar
->time
);
317 $monthdays = $this->related
['type']->get_num_days_in_month($date['year'], $date['mon']);
320 for ($dayno = 1; $dayno <= $monthdays; $dayno++
) {
321 // Get the gregorian representation of the day.
322 $timestamp = $this->related
['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
324 $days[] = $this->related
['type']->timestamp_to_date_array($timestamp);
331 * Returns a list of objects that are related.
335 protected static function define_related() {
337 'events' => '\core_calendar\local\event\entities\event_interface[]',
338 'cache' => '\core_calendar\external\events_related_objects_cache',
339 'type' => '\core_calendar\type_base',
344 * Get the current month timestamp.
346 * @return int The month timestamp.
348 protected function get_month_data() {
349 $date = $this->related
['type']->timestamp_to_date_array($this->calendar
->time
);
350 $monthtime = $this->related
['type']->convert_to_gregorian($date['year'], $date['month'], 1);
352 return make_timestamp($monthtime['year'], $monthtime['month']);
356 * Get the previous month timestamp.
358 * @return int The previous month timestamp.
360 protected function get_previous_month_data() {
361 $type = $this->related
['type'];
362 $date = $type->timestamp_to_date_array($this->calendar
->time
);
363 list($date['mon'], $date['year']) = $type->get_prev_month($date['year'], $date['mon']);
364 $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
366 return $type->timestamp_to_date_array($time);
370 * Get the next month timestamp.
372 * @return int The next month timestamp.
374 protected function get_next_month_data() {
375 $type = $this->related
['type'];
376 $date = $type->timestamp_to_date_array($this->calendar
->time
);
377 list($date['mon'], $date['year']) = $type->get_next_month($date['year'], $date['mon']);
378 $time = $type->convert_to_timestamp($date['year'], $date['mon'], 1);
380 return $type->timestamp_to_date_array($time);
384 * Set whether the navigation should be shown.
386 * @param bool $include
389 public function set_includenavigation($include) {
390 $this->includenavigation
= $include;
396 * Set whether the initial events have already been loaded and
397 * provided to the exporter.
399 * @param bool $loaded
402 public function set_initialeventsloaded(bool $loaded) {
403 $this->initialeventsloaded
= $loaded;
409 * Get the default context for use when adding a new event.
411 * @return null|\context
413 protected function get_default_add_context() {
414 if (calendar_user_can_add_event($this->calendar
->course
)) {
415 return \context_course
::instance($this->calendar
->course
->id
);