1 |
efrain |
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 |
namespace core_calendar\external;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once($CFG->dirroot . '/calendar/lib.php');
|
|
|
22 |
|
|
|
23 |
use core\external\exporter;
|
|
|
24 |
use core_date;
|
|
|
25 |
use DateTimeImmutable;
|
|
|
26 |
use renderer_base;
|
|
|
27 |
use moodle_url;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Class for displaying the day view.
|
|
|
31 |
*
|
|
|
32 |
* @package core_calendar
|
|
|
33 |
* @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class day_exporter extends exporter {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @var \calendar_information $calendar The calendar being displayed.
|
|
|
40 |
*/
|
|
|
41 |
protected $calendar;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @var moodle_url
|
|
|
45 |
*/
|
|
|
46 |
protected $url;
|
|
|
47 |
/**
|
|
|
48 |
* Constructor.
|
|
|
49 |
*
|
|
|
50 |
* @param \calendar_information $calendar The calendar information for the period being displayed
|
|
|
51 |
* @param mixed $data Either an stdClass or an array of values.
|
|
|
52 |
* @param array $related Related objects.
|
|
|
53 |
*/
|
|
|
54 |
public function __construct(\calendar_information $calendar, $data, $related) {
|
|
|
55 |
$this->calendar = $calendar;
|
|
|
56 |
|
|
|
57 |
$url = new moodle_url('/calendar/view.php', [
|
|
|
58 |
'view' => 'day',
|
|
|
59 |
'time' => $calendar->time,
|
|
|
60 |
]);
|
|
|
61 |
|
|
|
62 |
if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
|
|
|
63 |
$url->param('course', $this->calendar->course->id);
|
|
|
64 |
} else if ($this->calendar->categoryid) {
|
|
|
65 |
$url->param('category', $this->calendar->categoryid);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$this->url = $url;
|
|
|
69 |
|
|
|
70 |
parent::__construct($data, $related);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Return the list of properties.
|
|
|
75 |
*
|
|
|
76 |
* @return array
|
|
|
77 |
*/
|
|
|
78 |
protected static function define_properties() {
|
|
|
79 |
// These are the default properties as returned by getuserdate()
|
|
|
80 |
// but without the formatted month and week names.
|
|
|
81 |
return [
|
|
|
82 |
'seconds' => [
|
|
|
83 |
'type' => PARAM_INT,
|
|
|
84 |
],
|
|
|
85 |
'minutes' => [
|
|
|
86 |
'type' => PARAM_INT,
|
|
|
87 |
],
|
|
|
88 |
'hours' => [
|
|
|
89 |
'type' => PARAM_INT,
|
|
|
90 |
],
|
|
|
91 |
'mday' => [
|
|
|
92 |
'type' => PARAM_INT,
|
|
|
93 |
],
|
|
|
94 |
'wday' => [
|
|
|
95 |
'type' => PARAM_INT,
|
|
|
96 |
],
|
|
|
97 |
'year' => [
|
|
|
98 |
'type' => PARAM_INT,
|
|
|
99 |
],
|
|
|
100 |
'yday' => [
|
|
|
101 |
'type' => PARAM_INT,
|
|
|
102 |
],
|
|
|
103 |
];
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Return the list of additional properties.
|
|
|
108 |
*
|
|
|
109 |
* @return array
|
|
|
110 |
*/
|
|
|
111 |
protected static function define_other_properties() {
|
|
|
112 |
return [
|
|
|
113 |
'timestamp' => [
|
|
|
114 |
'type' => PARAM_INT,
|
|
|
115 |
],
|
|
|
116 |
'neweventtimestamp' => [
|
|
|
117 |
'type' => PARAM_INT,
|
|
|
118 |
],
|
|
|
119 |
'viewdaylink' => [
|
|
|
120 |
'type' => PARAM_URL,
|
|
|
121 |
'optional' => true,
|
|
|
122 |
],
|
|
|
123 |
'viewdaylinktitle' => [
|
|
|
124 |
'type' => PARAM_RAW,
|
|
|
125 |
'optional' => true,
|
|
|
126 |
],
|
|
|
127 |
'events' => [
|
|
|
128 |
'type' => calendar_event_exporter::read_properties_definition(),
|
|
|
129 |
'multiple' => true,
|
|
|
130 |
],
|
|
|
131 |
'hasevents' => [
|
|
|
132 |
'type' => PARAM_BOOL,
|
|
|
133 |
'default' => false,
|
|
|
134 |
],
|
|
|
135 |
'calendareventtypes' => [
|
|
|
136 |
'type' => PARAM_RAW,
|
|
|
137 |
'multiple' => true,
|
|
|
138 |
],
|
|
|
139 |
'previousperiod' => [
|
|
|
140 |
'type' => PARAM_INT,
|
|
|
141 |
],
|
|
|
142 |
'nextperiod' => [
|
|
|
143 |
'type' => PARAM_INT,
|
|
|
144 |
],
|
|
|
145 |
'haslastdayofevent' => [
|
|
|
146 |
'type' => PARAM_BOOL,
|
|
|
147 |
'default' => false,
|
|
|
148 |
],
|
|
|
149 |
];
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Get the additional values to inject while exporting.
|
|
|
154 |
*
|
|
|
155 |
* @param renderer_base $output The renderer.
|
|
|
156 |
* @return array Keys are the property names, values are their values.
|
|
|
157 |
*/
|
|
|
158 |
protected function get_other_values(renderer_base $output) {
|
|
|
159 |
$daytimestamp = $this->calendar->time;
|
|
|
160 |
$timestamp = $this->data[0];
|
|
|
161 |
// Need to account for user's timezone.
|
|
|
162 |
$usernow = usergetdate(time());
|
|
|
163 |
$today = new DateTimeImmutable(
|
|
|
164 |
timezone: core_date::get_user_timezone_object(),
|
|
|
165 |
);
|
|
|
166 |
|
|
|
167 |
// The start time should use the day's date but the current
|
|
|
168 |
// time of the day (adjusted for user's timezone).
|
|
|
169 |
$neweventstarttime = $today->setTimestamp($timestamp)->setTime(
|
|
|
170 |
$usernow['hours'],
|
|
|
171 |
$usernow['minutes'],
|
|
|
172 |
$usernow['seconds']
|
|
|
173 |
);
|
|
|
174 |
|
|
|
175 |
$return = [
|
|
|
176 |
'timestamp' => $timestamp,
|
|
|
177 |
'neweventtimestamp' => $neweventstarttime->getTimestamp(),
|
|
|
178 |
'previousperiod' => $this->get_previous_day_timestamp($daytimestamp),
|
|
|
179 |
'nextperiod' => $this->get_next_day_timestamp($daytimestamp),
|
|
|
180 |
'viewdaylink' => $this->url->out(false),
|
|
|
181 |
];
|
|
|
182 |
|
|
|
183 |
if ($viewdaylinktitle = $this->get_view_link_title()) {
|
|
|
184 |
$return['viewdaylinktitle'] = $viewdaylinktitle;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
$cache = $this->related['cache'];
|
|
|
189 |
$eventexporters = array_map(function($event) use ($cache, $output) {
|
|
|
190 |
$context = $cache->get_context($event);
|
|
|
191 |
$course = $cache->get_course($event);
|
|
|
192 |
$moduleinstance = $cache->get_module_instance($event);
|
|
|
193 |
$exporter = new calendar_event_exporter($event, [
|
|
|
194 |
'context' => $context,
|
|
|
195 |
'course' => $course,
|
|
|
196 |
'moduleinstance' => $moduleinstance,
|
|
|
197 |
'daylink' => $this->url,
|
|
|
198 |
'type' => $this->related['type'],
|
|
|
199 |
'today' => $this->data[0],
|
|
|
200 |
]);
|
|
|
201 |
|
|
|
202 |
return $exporter;
|
|
|
203 |
}, $this->related['events']);
|
|
|
204 |
|
|
|
205 |
$return['events'] = array_map(function($exporter) use ($output) {
|
|
|
206 |
return $exporter->export($output);
|
|
|
207 |
}, $eventexporters);
|
|
|
208 |
|
|
|
209 |
$return['hasevents'] = !empty($return['events']);
|
|
|
210 |
|
|
|
211 |
$return['calendareventtypes'] = array_map(function($exporter) {
|
|
|
212 |
return $exporter->get_calendar_event_type();
|
|
|
213 |
}, $eventexporters);
|
|
|
214 |
$return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
|
|
|
215 |
|
|
|
216 |
$return['haslastdayofevent'] = false;
|
|
|
217 |
foreach ($return['events'] as $event) {
|
|
|
218 |
if ($event->islastday) {
|
|
|
219 |
$return['haslastdayofevent'] = true;
|
|
|
220 |
break;
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
return $return;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Returns a list of objects that are related.
|
|
|
229 |
*
|
|
|
230 |
* @return array
|
|
|
231 |
*/
|
|
|
232 |
protected static function define_related() {
|
|
|
233 |
return [
|
|
|
234 |
'events' => '\core_calendar\local\event\entities\event_interface[]',
|
|
|
235 |
'cache' => '\core_calendar\external\events_related_objects_cache',
|
|
|
236 |
'type' => '\core_calendar\type_base',
|
|
|
237 |
];
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Get the previous day timestamp.
|
|
|
242 |
*
|
|
|
243 |
* @param int $daytimestamp The current day timestamp.
|
|
|
244 |
* @return int The previous day timestamp.
|
|
|
245 |
*/
|
|
|
246 |
protected function get_previous_day_timestamp($daytimestamp) {
|
|
|
247 |
return $this->related['type']->get_prev_day($daytimestamp);
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Get the next day timestamp.
|
|
|
252 |
*
|
|
|
253 |
* @param int $daytimestamp The current day timestamp.
|
|
|
254 |
* @return int The next day timestamp.
|
|
|
255 |
*/
|
|
|
256 |
protected function get_next_day_timestamp($daytimestamp) {
|
|
|
257 |
return $this->related['type']->get_next_day($daytimestamp);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Get the title for view link.
|
|
|
262 |
*
|
|
|
263 |
* @return string
|
|
|
264 |
*/
|
|
|
265 |
protected function get_view_link_title() {
|
|
|
266 |
$title = null;
|
|
|
267 |
|
|
|
268 |
$userdate = userdate($this->data[0], get_string('strftimedayshort'));
|
|
|
269 |
if ($this->data['istoday']) {
|
|
|
270 |
$title = get_string('todayplustitle', 'calendar', $userdate);
|
|
|
271 |
} else if (count($this->related['events'])) {
|
|
|
272 |
$title = get_string('eventsfor', 'calendar', $userdate);
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return $title;
|
|
|
276 |
}
|
|
|
277 |
}
|