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_course\external\course_summary_exporter;
31 require_once($CFG->dirroot . '/course/lib.php');
33 * Class for displaying a calendar event.
35 * @package core_calendar
36 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class calendar_event_exporter extends event_exporter_base {
42 * Return the list of additional properties.
46 protected static function define_other_properties() {
48 $values = parent::define_other_properties();
49 $values['url'] = ['type' => PARAM_URL];
50 $values['islastday'] = [
54 $values['calendareventtype'] = [
57 $values['popupname'] = [
65 * Get the additional values to inject while exporting.
67 * @param renderer_base $output The renderer.
68 * @return array Keys are the property names, values are their values.
70 protected function get_other_values(renderer_base $output) {
73 $values = parent::get_other_values($output);
74 $event = $this->event;
76 if ($moduleproxy = $event->get_course_module()) {
77 $modulename = $moduleproxy->get('modname');
78 $moduleid = $moduleproxy->get('id');
79 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
81 // Build edit event url for action events.
82 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
83 $editurl = new \moodle_url('/course/mod.php', $params);
84 $values['editurl'] = $editurl->out(false);
85 } else if ($event->get_type() == 'course') {
86 $url = course_get_url($event->get_course()->get('id') ?: SITEID);
88 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
89 $course = $event->get_course()->get('id') ?: SITEID;
91 $url = course_get_url($course);
93 $values['url'] = $url->out(false);
94 $values['islastday'] = false;
95 $today = $this->related['type']->timestamp_to_date_array($this->related['today']);
97 $values['popupname'] = $this->event->get_name();
99 $times = $this->event->get_times();
100 if ($duration = $times->get_duration()) {
101 $enddate = $this->related['type']->timestamp_to_date_array($times->get_end_time()->getTimestamp());
102 $values['islastday'] = true;
103 $values['islastday'] = $values['islastday'] && $enddate['year'] == $today['year'];
104 $values['islastday'] = $values['islastday'] && $enddate['mon'] == $today['mon'];
105 $values['islastday'] = $values['islastday'] && $enddate['mday'] == $today['mday'];
108 $subscription = $this->event->get_subscription();
109 if ($subscription && !empty($subscription->get('id')) && $CFG->calendar_showicalsource) {
111 'name' => $values['popupname'],
112 'source' => $subscription->get('name'),
114 $values['popupname'] = get_string('namewithsource', 'calendar', $a);
116 if ($values['islastday']) {
117 $startdate = $this->related['type']->timestamp_to_date_array($times->get_start_time()->getTimestamp());
119 $samedate = $samedate && $startdate['mon'] == $enddate['mon'];
120 $samedate = $samedate && $startdate['year'] == $enddate['year'];
121 $samedate = $samedate && $startdate['mday'] == $enddate['mday'];
124 $values['popupname'] = get_string('eventendtimewrapped', 'calendar', $values['popupname']);
129 // Include category name into the event name, if applicable.
130 $proxy = $this->event->get_category();
131 if ($proxy && $proxy->get('id')) {
132 $category = $proxy->get_proxied_instance();
133 $eventnameparams = (object) [
134 'name' => $values['popupname'],
135 'category' => $category->get_formatted_name(),
137 $values['popupname'] = get_string('eventnameandcategory', 'calendar', $eventnameparams);
140 // Include course's shortname into the event name, if applicable.
141 $course = $this->event->get_course();
142 if ($course && $course->get('id') && $course->get('id') !== SITEID) {
143 $eventnameparams = (object) [
144 'name' => $values['popupname'],
145 'course' => format_string($course->get('shortname'), true, [
146 'context' => $this->related['context'],
149 $values['popupname'] = get_string('eventnameandcourse', 'calendar', $eventnameparams);
152 $values['calendareventtype'] = $this->get_calendar_event_type();
158 * Returns a list of objects that are related.
162 protected static function define_related() {
163 $related = parent::define_related();
164 $related['daylink'] = \moodle_url::class;
165 $related['type'] = '\core_calendar\type_base';
166 $related['today'] = 'int';
172 * Return the normalised event type.
173 * Activity events are normalised to be course events.
177 public function get_calendar_event_type() {
178 $type = $this->event->get_type();
179 if ($type == 'open' || $type == 'close') {