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 a calendar event.
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
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_calendar\local\event\container;
30 use \core_course\external\course_summary_exporter;
32 require_once($CFG->dirroot . '/course/lib.php');
34 * Class for displaying a calendar event.
36 * @package core_calendar
37 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class calendar_event_exporter extends event_exporter_base {
43 * Return the list of additional properties.
47 protected static function define_other_properties() {
49 $values = parent::define_other_properties();
50 $values['url'] = ['type' => PARAM_URL];
51 $values['islastday'] = [
55 $values['calendareventtype'] = [
58 $values['popupname'] = [
61 $values['mindaytimestamp'] = [
65 $values['mindayerror'] = [
69 $values['maxdaytimestamp'] = [
73 $values['maxdayerror'] = [
77 $values['draggable'] = [
86 * Get the additional values to inject while exporting.
88 * @param renderer_base $output The renderer.
89 * @return array Keys are the property names, values are their values.
91 protected function get_other_values(renderer_base $output) {
94 $values = parent::get_other_values($output);
95 $event = $this->event;
96 $course = $this->related['course'];
97 $hascourse = !empty($course);
99 // By default all events that can be edited are
101 $values['draggable'] = $values['canedit'];
103 if ($moduleproxy = $event->get_course_module()) {
104 $modulename = $moduleproxy->get('modname');
105 $moduleid = $moduleproxy->get('id');
106 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
108 // Build edit event url for action events.
109 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
110 $editurl = new \moodle_url('/course/mod.php', $params);
111 $values['editurl'] = $editurl->out(false);
112 } else if ($event->get_type() == 'category') {
113 $url = $event->get_category()->get_proxied_instance()->get_view_link();
115 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
116 $url = course_get_url($hascourse ? $course : SITEID);
119 $values['url'] = $url->out(false);
120 $values['islastday'] = false;
121 $today = $this->related['type']->timestamp_to_date_array($this->related['today']);
123 $values['popupname'] = $this->event->get_name();
125 $times = $this->event->get_times();
126 if ($duration = $times->get_duration()) {
127 $enddate = $this->related['type']->timestamp_to_date_array($times->get_end_time()->getTimestamp());
128 $values['islastday'] = true;
129 $values['islastday'] = $values['islastday'] && $enddate['year'] == $today['year'];
130 $values['islastday'] = $values['islastday'] && $enddate['mon'] == $today['mon'];
131 $values['islastday'] = $values['islastday'] && $enddate['mday'] == $today['mday'];
134 $subscription = $this->event->get_subscription();
135 if ($subscription && !empty($subscription->get('id')) && $CFG->calendar_showicalsource) {
137 'name' => $values['popupname'],
138 'source' => $subscription->get('name'),
140 $values['popupname'] = get_string('namewithsource', 'calendar', $a);
142 if ($values['islastday']) {
143 $startdate = $this->related['type']->timestamp_to_date_array($times->get_start_time()->getTimestamp());
145 $samedate = $samedate && $startdate['mon'] == $enddate['mon'];
146 $samedate = $samedate && $startdate['year'] == $enddate['year'];
147 $samedate = $samedate && $startdate['mday'] == $enddate['mday'];
150 $values['popupname'] = get_string('eventendtimewrapped', 'calendar', $values['popupname']);
155 // Include category name into the event name, if applicable.
156 $proxy = $this->event->get_category();
157 if ($proxy && $proxy->get('id')) {
158 $category = $proxy->get_proxied_instance();
159 $eventnameparams = (object) [
160 'name' => $values['popupname'],
161 'category' => $category->get_formatted_name(),
163 $values['popupname'] = get_string('eventnameandcategory', 'calendar', $eventnameparams);
166 // Include course's shortname into the event name, if applicable.
167 if ($hascourse && $course->id !== SITEID) {
168 $eventnameparams = (object) [
169 'name' => $values['popupname'],
170 'course' => $values['course']->shortname,
172 $values['popupname'] = get_string('eventnameandcourse', 'calendar', $eventnameparams);
175 $values['calendareventtype'] = $this->get_calendar_event_type();
177 if ($event->get_course_module()) {
178 $values = array_merge($values, $this->get_module_timestamp_limits($event));
185 * Returns a list of objects that are related.
189 protected static function define_related() {
190 $related = parent::define_related();
191 $related['daylink'] = \moodle_url::class;
192 $related['type'] = '\core_calendar\type_base';
193 $related['today'] = 'int';
199 * Return the normalised event type.
200 * Activity events are normalised to be course events.
204 public function get_calendar_event_type() {
205 if ($this->event->get_course_module()) {
209 return $this->event->get_type();
213 * Return the set of minimum and maximum date timestamp values
214 * for the given event.
216 * @param event_interface $event
219 protected function get_module_timestamp_limits($event) {
223 $mapper = container::get_event_mapper();
224 $starttime = $event->get_times()->get_start_time();
225 $modname = $event->get_course_module()->get('modname');
226 $modid = $event->get_course_module()->get('instance');
227 $moduleinstance = $DB->get_record($modname, ['id' => $modid]);
229 list($min, $max) = component_callback(
231 'core_calendar_get_valid_event_timestart_range',
232 [$mapper->from_event_to_legacy_event($event), $moduleinstance],
236 // The callback will return false for either of the
237 // min or max cutoffs to indicate that there are no
238 // valid timestart values. In which case the event is
240 if ($min === false || $max === false) {
241 return ['draggable' => false];
245 $values = array_merge($values, $this->get_module_timestamp_min_limit($starttime, $min));
249 $values = array_merge($values, $this->get_module_timestamp_max_limit($starttime, $max));
256 * Get the correct minimum midnight day limit based on the event start time
257 * and the module's minimum timestamp limit.
259 * @param DateTimeInterface $starttime The event start time
260 * @param array $min The module's minimum limit for the event
262 protected function get_module_timestamp_min_limit(\DateTimeInterface $starttime, $min) {
263 // We need to check that the minimum valid time is earlier in the
264 // day than the current event time so that if the user drags and drops
265 // the event to this day (which changes the date but not the time) it
266 // will result in a valid time start for the event.
269 // An event that starts on 2017-01-10 08:00 with a minimum cutoff
270 // of 2017-01-05 09:00 means that 2017-01-05 is not a valid start day
271 // for the drag and drop because it would result in the event start time
272 // being set to 2017-01-05 08:00, which is invalid. Instead the minimum
273 // valid start day would be 2017-01-06.
275 $timestamp = $min[0];
276 $errorstring = $min[1];
277 $mindate = (new \DateTimeImmutable())->setTimestamp($timestamp);
278 $minstart = $mindate->setTime(
279 $starttime->format('H'),
280 $starttime->format('i'),
281 $starttime->format('s')
283 $midnight = usergetmidnight($timestamp);
285 if ($mindate <= $minstart) {
286 $values['mindaytimestamp'] = $midnight;
288 $tomorrow = (new \DateTime())->setTimestamp($midnight)->modify('+1 day');
289 $values['mindaytimestamp'] = $tomorrow->getTimestamp();
292 // Get the human readable error message to display if the min day
293 // timestamp is violated.
294 $values['mindayerror'] = $errorstring;
299 * Get the correct maximum midnight day limit based on the event start time
300 * and the module's maximum timestamp limit.
302 * @param DateTimeInterface $starttime The event start time
303 * @param array $max The module's maximum limit for the event
305 protected function get_module_timestamp_max_limit(\DateTimeInterface $starttime, $max) {
306 // We're doing a similar calculation here as we are for the minimum
307 // day timestamp. See the explanation above.
309 $timestamp = $max[0];
310 $errorstring = $max[1];
311 $maxdate = (new \DateTimeImmutable())->setTimestamp($timestamp);
312 $maxstart = $maxdate->setTime(
313 $starttime->format('H'),
314 $starttime->format('i'),
315 $starttime->format('s')
317 $midnight = usergetmidnight($timestamp);
319 if ($maxdate >= $maxstart) {
320 $values['maxdaytimestamp'] = $midnight;
322 $yesterday = (new \DateTime())->setTimestamp($midnight)->modify('-1 day');
323 $values['maxdaytimestamp'] = $yesterday->getTimestamp();
326 // Get the human readable error message to display if the max day
327 // timestamp is violated.
328 $values['maxdayerror'] = $errorstring;