MDL-65262 core_calendar: change location param type
[moodle.git] / calendar / classes / external / event_exporter_base.php
CommitLineData
6ad2bfd1
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 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
1652934f 29require_once($CFG->dirroot . "/calendar/lib.php");
ea5f7707 30require_once($CFG->libdir . "/filelib.php");
1652934f 31
6ad2bfd1 32use \core\external\exporter;
1652934f 33use \core_calendar\local\event\container;
6ad2bfd1
AN
34use \core_calendar\local\event\entities\event_interface;
35use \core_calendar\local\event\entities\action_event_interface;
36use \core_course\external\course_summary_exporter;
5ba6507e 37use \core\external\coursecat_summary_exporter;
6ad2bfd1 38use \renderer_base;
8afe9f8a 39use moodle_url;
6ad2bfd1
AN
40
41/**
42 * Class for displaying a calendar event.
43 *
44 * @package core_calendar
45 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 */
48class event_exporter_base extends exporter {
49
50 /**
51 * @var event_interface $event
52 */
53 protected $event;
54
55 /**
56 * Constructor.
57 *
58 * @param event_interface $event
59 * @param array $related The related data.
60 */
61 public function __construct(event_interface $event, $related = []) {
62 $this->event = $event;
63
64 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
65 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
66 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
67 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
5ba6507e 68 $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
6ad2bfd1
AN
69
70 $data = new \stdClass();
71 $data->id = $event->get_id();
72 $data->name = $event->get_name();
ea5f7707
RW
73 $data->description = file_rewrite_pluginfile_urls(
74 $event->get_description()->get_value(),
75 'pluginfile.php',
76 $related['context']->id,
77 'calendar',
78 'event_description',
79 $event->get_id()
80 );
6ad2bfd1 81 $data->descriptionformat = $event->get_description()->get_format();
9a3ee0c3 82 $data->location = external_format_text($event->get_location(), FORMAT_PLAIN, $related['context']->id)[0];
6ad2bfd1
AN
83 $data->groupid = $groupid;
84 $data->userid = $userid;
5ba6507e 85 $data->categoryid = $categoryid;
6ad2bfd1
AN
86 $data->eventtype = $event->get_type();
87 $data->timestart = $starttimestamp;
88 $data->timeduration = $endtimestamp - $starttimestamp;
89 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
90 $data->visible = $event->is_visible() ? 1 : 0;
91 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
92
93 if ($repeats = $event->get_repeats()) {
94 $data->repeatid = $repeats->get_id();
2dbfb484 95 $data->eventcount = $repeats->get_num() + 1;
6ad2bfd1
AN
96 }
97
98 if ($cm = $event->get_course_module()) {
99 $data->modulename = $cm->get('modname');
100 $data->instance = $cm->get('id');
101 }
102
103 parent::__construct($data, $related);
104 }
105
106 /**
107 * Return the list of properties.
108 *
109 * @return array
110 */
111 protected static function define_properties() {
112 return [
113 'id' => ['type' => PARAM_INT],
114 'name' => ['type' => PARAM_TEXT],
115 'description' => [
116 'type' => PARAM_RAW,
117 'optional' => true,
118 'default' => null,
119 'null' => NULL_ALLOWED
120 ],
121 'descriptionformat' => [
122 'type' => PARAM_INT,
123 'optional' => true,
124 'default' => null,
125 'null' => NULL_ALLOWED
126 ],
ef4e04ee 127 'location' => [
a8dcc212 128 'type' => PARAM_RAW,
ef4e04ee
MH
129 'optional' => true,
130 'default' => null,
131 'null' => NULL_ALLOWED
132 ],
5ba6507e
AN
133 'categoryid' => [
134 'type' => PARAM_INT,
135 'optional' => true,
136 'default' => null,
137 'null' => NULL_ALLOWED
138 ],
6ad2bfd1
AN
139 'groupid' => [
140 'type' => PARAM_INT,
141 'optional' => true,
142 'default' => null,
143 'null' => NULL_ALLOWED
144 ],
145 'userid' => [
146 'type' => PARAM_INT,
147 'optional' => true,
148 'default' => null,
149 'null' => NULL_ALLOWED
150 ],
151 'repeatid' => [
152 'type' => PARAM_INT,
153 'optional' => true,
154 'default' => null,
155 'null' => NULL_ALLOWED
156 ],
2dbfb484
AN
157 'eventcount' => [
158 'type' => PARAM_INT,
159 'optional' => true,
160 'default' => null,
161 'null' => NULL_ALLOWED
162 ],
6ad2bfd1
AN
163 'modulename' => [
164 'type' => PARAM_TEXT,
165 'optional' => true,
166 'default' => null,
167 'null' => NULL_ALLOWED
168 ],
169 'instance' => [
170 'type' => PARAM_INT,
171 'optional' => true,
172 'default' => null,
173 'null' => NULL_ALLOWED
174 ],
175 'eventtype' => ['type' => PARAM_TEXT],
176 'timestart' => ['type' => PARAM_INT],
177 'timeduration' => ['type' => PARAM_INT],
178 'timesort' => ['type' => PARAM_INT],
179 'visible' => ['type' => PARAM_INT],
180 'timemodified' => ['type' => PARAM_INT],
181 ];
182 }
183
184 /**
185 * Return the list of additional properties.
186 *
187 * @return array
188 */
189 protected static function define_other_properties() {
190 return [
6ad2bfd1
AN
191 'icon' => [
192 'type' => event_icon_exporter::read_properties_definition(),
193 ],
5ba6507e
AN
194 'category' => [
195 'type' => coursecat_summary_exporter::read_properties_definition(),
196 'optional' => true,
197 ],
6ad2bfd1
AN
198 'course' => [
199 'type' => course_summary_exporter::read_properties_definition(),
200 'optional' => true,
1652934f 201 ],
8afe9f8a
SL
202 'subscription' => [
203 'type' => event_subscription_exporter::read_properties_definition(),
204 'optional' => true,
205 ],
1652934f
RW
206 'canedit' => [
207 'type' => PARAM_BOOL
208 ],
209 'candelete' => [
210 'type' => PARAM_BOOL
211 ],
5a35675e
SL
212 'deleteurl' => [
213 'type' => PARAM_URL
214 ],
215 'editurl' => [
216 'type' => PARAM_URL
217 ],
b9a4c3df
SL
218 'viewurl' => [
219 'type' => PARAM_URL
220 ],
5a35675e
SL
221 'formattedtime' => [
222 'type' => PARAM_RAW,
63b13467
SL
223 ],
224 'isactionevent' => [
225 'type' => PARAM_BOOL
226 ],
227 'iscourseevent' => [
228 'type' => PARAM_BOOL
229 ],
d0e56d84
AN
230 'iscategoryevent' => [
231 'type' => PARAM_BOOL
232 ],
63b13467
SL
233 'groupname' => [
234 'type' => PARAM_RAW,
235 'optional' => true,
236 'default' => null,
237 'null' => NULL_ALLOWED
238 ],
6ad2bfd1
AN
239 ];
240 }
241
242 /**
243 * Get the additional values to inject while exporting.
244 *
245 * @param renderer_base $output The renderer.
246 * @return array Keys are the property names, values are their values.
247 */
248 protected function get_other_values(renderer_base $output) {
249 $values = [];
250 $event = $this->event;
1652934f 251 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
6ad2bfd1 252 $context = $this->related['context'];
6b024885 253 $course = $this->related['course'];
63b13467
SL
254 $values['isactionevent'] = false;
255 $values['iscourseevent'] = false;
d0e56d84 256 $values['iscategoryevent'] = false;
63b13467 257 if ($moduleproxy = $event->get_course_module()) {
baedf066
SL
258 // We need a separate property to flag if an event is action event.
259 // That's required because canedit return true but action action events cannot be edited on the calendar UI.
260 // But they are considered editable because you can drag and drop the event on the month view.
63b13467
SL
261 $values['isactionevent'] = true;
262 } else if ($event->get_type() == 'course') {
263 $values['iscourseevent'] = true;
d0e56d84
AN
264 } else if ($event->get_type() == 'category') {
265 $values['iscategoryevent'] = true;
63b13467 266 }
6ad2bfd1
AN
267 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
268 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
269
6ad2bfd1
AN
270 $values['icon'] = $iconexporter->export($output);
271
8afe9f8a
SL
272 $subscriptionexporter = new event_subscription_exporter($event);
273 $values['subscription'] = $subscriptionexporter->export($output);
274
5ba6507e
AN
275 $proxy = $this->event->get_category();
276 if ($proxy && $proxy->get('id')) {
277 $category = $proxy->get_proxied_instance();
278 $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
279 $values['category'] = $categorysummaryexporter->export($output);
280 }
281
6b024885 282 if ($course) {
6ad2bfd1
AN
283 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
284 $values['course'] = $coursesummaryexporter->export($output);
285 }
6b024885 286
5a35675e 287 $courseid = (!$course) ? SITEID : $course->id;
6ad2bfd1 288
909d0858 289 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
1652934f
RW
290 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
291
5a35675e
SL
292 $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
293 $values['deleteurl'] = $deleteurl->out(false);
294
295 $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
296 'course' => $courseid]);
297 $values['editurl'] = $editurl->out(false);
b9a4c3df
SL
298 $viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
299 'time' => $timesort]);
300 $viewurl->set_anchor('event_' . $event->get_id());
301 $values['viewurl'] = $viewurl->out(false);
5a35675e
SL
302 $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
303 $timesort);
304
63b13467
SL
305 if ($group = $event->get_group()) {
306 $values['groupname'] = format_string($group->get('name'), true,
307 ['context' => \context_course::instance($event->get_course()->get('id'))]);
308 }
6b024885 309
6ad2bfd1
AN
310 return $values;
311 }
312
313 /**
314 * Returns a list of objects that are related.
315 *
316 * @return array
317 */
318 protected static function define_related() {
319 return [
320 'context' => 'context',
321 'course' => 'stdClass?',
322 ];
323 }
324}