MDL-59392 calendar: Split day exporter to week exporter
[moodle.git] / calendar / classes / external / week_day_exporter.php
CommitLineData
fb9be271
AN
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/>.
16
17/**
18 * Contains event class for displaying the day on 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 */
24
25namespace core_calendar\external;
26
27defined('MOODLE_INTERNAL') || die();
28
29use renderer_base;
30use moodle_url;
31
32/**
33 * Class for displaying the day on month view.
34 *
35 * @package core_calendar
36 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class week_day_exporter extends day_exporter {
40
41 /**
42 * Return the list of additional properties.
43 *
44 * @return array
45 */
46 protected static function define_other_properties() {
47 $return = parent::define_other_properties();
48 $return = array_merge($return, [
49 'timestamp' => [
50 'type' => PARAM_INT,
51 ],
52 'neweventtimestamp' => [
53 'type' => PARAM_INT,
54 ],
55 'viewdaylink' => [
56 'type' => PARAM_URL,
57 'optional' => true,
58 ],
59 'calendareventtypes' => [
60 'type' => PARAM_RAW,
61 'multiple' => true,
62 ],
63 'popovertitle' => [
64 'type' => PARAM_RAW,
65 'default' => '',
66 ],
67 'haslastdayofevent' => [
68 'type' => PARAM_BOOL,
69 'default' => false,
70 ],
71 ]);
72
73 return $return;
74 }
75
76 /**
77 * Get the additional values to inject while exporting.
78 *
79 * @param renderer_base $output The renderer.
80 * @return array Keys are the property names, values are their values.
81 */
82 protected function get_other_values(renderer_base $output) {
83 $timestamp = $this->data[0];
84 // Need to account for user's timezone.
85 $usernow = usergetdate(time());
86 $today = new \DateTimeImmutable();
87 // The start time should use the day's date but the current
88 // time of the day (adjusted for user's timezone).
89 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
90 $usernow['hours'],
91 $usernow['minutes'],
92 $usernow['seconds']
93 );
94
95 $return = [
96 'timestamp' => $timestamp,
97 'neweventtimestamp' => $neweventstarttime->getTimestamp()
98 ];
99
100 $url = new moodle_url('/calendar/view.php', [
101 'view' => 'day',
102 'time' => $timestamp,
103 'course' => $this->calendar->course->id,
104 ]);
105 $return['viewdaylink'] = $url->out(false);
106
107 $cache = $this->related['cache'];
108 $eventexporters = array_map(function($event) use ($cache, $output, $url) {
109 $context = $cache->get_context($event);
110 $course = $cache->get_course($event);
111 $exporter = new calendar_event_exporter($event, [
112 'context' => $context,
113 'course' => $course,
114 'daylink' => $url,
115 'type' => $this->related['type'],
116 'today' => $this->data[0],
117 ]);
118
119 return $exporter;
120 }, $this->related['events']);
121
122 $return['events'] = array_map(function($exporter) use ($output) {
123 return $exporter->export($output);
124 }, $eventexporters);
125
126 if ($popovertitle = $this->get_popover_title()) {
127 $return['popovertitle'] = $popovertitle;
128 }
129
130 $return['calendareventtypes'] = array_map(function($exporter) {
131 return $exporter->get_calendar_event_type();
132 }, $eventexporters);
133 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
134
135 $return['haslastdayofevent'] = false;
136 foreach ($return['events'] as $event) {
137 if ($event->islastday) {
138 $return['haslastdayofevent'] = true;
139 break;
140 }
141 }
142
143 return $return;
144 }
145
146 /**
147 * Returns a list of objects that are related.
148 *
149 * @return array
150 */
151 protected static function define_related() {
152 return [
153 'events' => '\core_calendar\local\event\entities\event_interface[]',
154 'cache' => '\core_calendar\external\events_related_objects_cache',
155 'type' => '\core_calendar\type_base',
156 ];
157 }
158
159 /**
160 * Get the title for this popover.
161 *
162 * @return string
163 */
164 protected function get_popover_title() {
165 $title = null;
166
167 $userdate = userdate($this->data[0], get_string('strftimedayshort'));
168 if (count($this->related['events'])) {
169 $title = get_string('eventsfor', 'calendar', $userdate);
170 } else if ($this->data['istoday']) {
171 $title = $userdate;
172 }
173
174 if ($this->data['istoday']) {
175 $title = get_string('todayplustitle', 'calendar', $userdate);
176 }
177
178 return $title;
179 }
180}