MDL-59388 calendar: Add data-time to calendar navigation links
[moodle.git] / calendar / classes / external / event_exporter.php
CommitLineData
984355ce
RW
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 a calendar event.
19 *
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
23 */
24
25namespace core_calendar\external;
26
27defined('MOODLE_INTERNAL') || die();
28
6fb75156
SL
29require_once($CFG->dirroot . "/calendar/lib.php");
30
984355ce 31use \core\external\exporter;
42e76c3f 32use \core_calendar\local\event\entities\event_interface;
53cbac18 33use \core_calendar\local\event\entities\action_event_interface;
6fb75156 34use \core_calendar\local\event\container;
984355ce
RW
35use \core_course\external\course_summary_exporter;
36use \renderer_base;
37
38/**
39 * Class for displaying a calendar event.
40 *
41 * @package core_calendar
42 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class event_exporter extends exporter {
46
47 /**
48 * @var event_interface $event
49 */
50 protected $event;
51
52 /**
53 * Constructor.
54 *
55 * @param event_interface $event
20592f5f 56 * @param array $related The related data.
984355ce
RW
57 */
58 public function __construct(event_interface $event, $related = []) {
59 $this->event = $event;
60
61 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
62 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
63 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
64 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
65
66 $data = new \stdClass();
67 $data->id = $event->get_id();
68 $data->name = $event->get_name();
69 $data->description = $event->get_description()->get_value();
70 $data->descriptionformat = $event->get_description()->get_format();
71 $data->groupid = $groupid;
72 $data->userid = $userid;
73 $data->eventtype = $event->get_type();
74 $data->timestart = $starttimestamp;
75 $data->timeduration = $endtimestamp - $starttimestamp;
76 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
8e02018f 77 $data->visible = $event->is_visible() ? 1 : 0;
984355ce
RW
78 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
79
80 if ($repeats = $event->get_repeats()) {
81 $data->repeatid = $repeats->get_id();
82 }
83
84 if ($cm = $event->get_course_module()) {
85 $data->modulename = $cm->get('modname');
86 $data->instance = $cm->get('id');
87 }
88
89 parent::__construct($data, $related);
90 }
91
92 /**
93 * Return the list of properties.
94 *
95 * @return array
96 */
97 protected static function define_properties() {
98 return [
99 'id' => ['type' => PARAM_INT],
100 'name' => ['type' => PARAM_TEXT],
101 'description' => [
102 'type' => PARAM_RAW,
103 'optional' => true,
104 'default' => null,
105 'null' => NULL_ALLOWED
106 ],
107 'descriptionformat' => [
108 'type' => PARAM_INT,
109 'optional' => true,
110 'default' => null,
111 'null' => NULL_ALLOWED
112 ],
113 'groupid' => [
114 'type' => PARAM_INT,
115 'optional' => true,
116 'default' => null,
117 'null' => NULL_ALLOWED
118 ],
119 'userid' => [
120 'type' => PARAM_INT,
121 'optional' => true,
122 'default' => null,
123 'null' => NULL_ALLOWED
124 ],
125 'repeatid' => [
126 'type' => PARAM_INT,
127 'optional' => true,
128 'default' => null,
129 'null' => NULL_ALLOWED
130 ],
131 'modulename' => [
132 'type' => PARAM_TEXT,
133 'optional' => true,
134 'default' => null,
135 'null' => NULL_ALLOWED
136 ],
137 'instance' => [
138 'type' => PARAM_INT,
139 'optional' => true,
140 'default' => null,
141 'null' => NULL_ALLOWED
142 ],
143 'eventtype' => ['type' => PARAM_TEXT],
144 'timestart' => ['type' => PARAM_INT],
145 'timeduration' => ['type' => PARAM_INT],
146 'timesort' => ['type' => PARAM_INT],
147 'visible' => ['type' => PARAM_INT],
148 'timemodified' => ['type' => PARAM_INT],
149 ];
150 }
151
152 /**
153 * Return the list of additional properties.
154 *
155 * @return array
156 */
157 protected static function define_other_properties() {
158 return [
159 'url' => ['type' => PARAM_URL],
c34e2002
SL
160 'editurl' => [
161 'type' => PARAM_URL,
162 'optional' => true
163 ],
984355ce
RW
164 'icon' => [
165 'type' => event_icon_exporter::read_properties_definition(),
166 ],
167 'action' => [
168 'type' => event_action_exporter::read_properties_definition(),
169 'optional' => true,
170 ],
171 'course' => [
172 'type' => course_summary_exporter::read_properties_definition(),
173 'optional' => true,
6fb75156
SL
174 ],
175 'canedit' => ['type' => PARAM_BOOL],
176 'displayeventsource' => ['type' => PARAM_BOOL],
177 'subscription' => [
178 'type' => PARAM_RAW,
179 'optional' => true,
180 'default' => null,
181 'null' => NULL_ALLOWED
4df03a27
SL
182 ],
183 'isactionevent' => ['type' => PARAM_BOOL],
184 'candelete' => ['type' => PARAM_BOOL]
984355ce
RW
185 ];
186 }
187
188 /**
189 * Get the additional values to inject while exporting.
190 *
191 * @param renderer_base $output The renderer.
192 * @return array Keys are the property names, values are their values.
193 */
194 protected function get_other_values(renderer_base $output) {
195 $values = [];
196 $event = $this->event;
6fb75156
SL
197 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
198
984355ce 199 $context = $this->related['context'];
4df03a27 200 $values['isactionevent'] = false;
cd2c4b85
MG
201 if ($moduleproxy = $event->get_course_module()) {
202 $modulename = $moduleproxy->get('modname');
203 $moduleid = $moduleproxy->get('id');
204 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
c34e2002 205
4df03a27 206 $values['isactionevent'] = true;
c34e2002
SL
207
208 // Build edit event url for action events.
209 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
210 $editurl = new \moodle_url('/course/mod.php', $params);
211 $values['editurl'] = $editurl->out(false);
cd2c4b85
MG
212 } else {
213 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
214 global $CFG;
215 require_once($CFG->dirroot.'/course/lib.php');
216 $url = \course_get_url($this->related['course'] ?: SITEID);
217 }
984355ce 218 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
984355ce
RW
219 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
220
221 $values['url'] = $url->out(false);
984355ce
RW
222 $values['icon'] = $iconexporter->export($output);
223
224 if ($event instanceof action_event_interface) {
6b7b39fe
JP
225 $actionrelated = [
226 'context' => $context,
227 'event' => $event
228 ];
229 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
984355ce
RW
230 $values['action'] = $actionexporter->export($output);
231 }
232
233 if ($course = $this->related['course']) {
234 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
235 $values['course'] = $coursesummaryexporter->export($output);
236 }
4df03a27 237
6fb75156 238 $values['canedit'] = calendar_edit_event_allowed($legacyevent);
436d39ba 239 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
6fb75156
SL
240
241 // Handle event subscription.
242 $values['subscription'] = null;
243 $values['displayeventsource'] = false;
244 if (!empty($legacyevent->subscriptionid)) {
245 $subscription = calendar_get_subscription($legacyevent->subscriptionid);
246 if (!empty($subscription) && $CFG->calendar_showicalsource) {
247 $values['displayeventsource'] = true;
248 $subscriptiondata = new \stdClass();
249 if (!empty($subscription->url)) {
250 $subscriptiondata->url = $subscription->url;
251 }
252 $subscriptiondata->name = $subscription->name;
253 $values['subscription'] = json_encode($subscriptiondata);
254 }
255 }
984355ce
RW
256 return $values;
257 }
258
259 /**
260 * Returns a list of objects that are related.
261 *
262 * @return array
263 */
264 protected static function define_related() {
265 return [
266 'context' => 'context',
267 'course' => 'stdClass?',
268 ];
269 }
270}