MDL-59890 coursecat: Add helper to fetch nested view of coursecat
[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;
37use \renderer_base;
8afe9f8a 38use moodle_url;
6ad2bfd1
AN
39
40/**
41 * Class for displaying a calendar event.
42 *
43 * @package core_calendar
44 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47class event_exporter_base extends exporter {
48
49 /**
50 * @var event_interface $event
51 */
52 protected $event;
53
54 /**
55 * Constructor.
56 *
57 * @param event_interface $event
58 * @param array $related The related data.
59 */
60 public function __construct(event_interface $event, $related = []) {
61 $this->event = $event;
62
63 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
64 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
65 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
66 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
67
68 $data = new \stdClass();
69 $data->id = $event->get_id();
70 $data->name = $event->get_name();
ea5f7707
RW
71 $data->description = file_rewrite_pluginfile_urls(
72 $event->get_description()->get_value(),
73 'pluginfile.php',
74 $related['context']->id,
75 'calendar',
76 'event_description',
77 $event->get_id()
78 );
6ad2bfd1
AN
79 $data->descriptionformat = $event->get_description()->get_format();
80 $data->groupid = $groupid;
81 $data->userid = $userid;
82 $data->eventtype = $event->get_type();
83 $data->timestart = $starttimestamp;
84 $data->timeduration = $endtimestamp - $starttimestamp;
85 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
86 $data->visible = $event->is_visible() ? 1 : 0;
87 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
88
89 if ($repeats = $event->get_repeats()) {
90 $data->repeatid = $repeats->get_id();
2dbfb484 91 $data->eventcount = $repeats->get_num() + 1;
6ad2bfd1
AN
92 }
93
94 if ($cm = $event->get_course_module()) {
95 $data->modulename = $cm->get('modname');
96 $data->instance = $cm->get('id');
97 }
98
99 parent::__construct($data, $related);
100 }
101
102 /**
103 * Return the list of properties.
104 *
105 * @return array
106 */
107 protected static function define_properties() {
108 return [
109 'id' => ['type' => PARAM_INT],
110 'name' => ['type' => PARAM_TEXT],
111 'description' => [
112 'type' => PARAM_RAW,
113 'optional' => true,
114 'default' => null,
115 'null' => NULL_ALLOWED
116 ],
117 'descriptionformat' => [
118 'type' => PARAM_INT,
119 'optional' => true,
120 'default' => null,
121 'null' => NULL_ALLOWED
122 ],
123 'groupid' => [
124 'type' => PARAM_INT,
125 'optional' => true,
126 'default' => null,
127 'null' => NULL_ALLOWED
128 ],
129 'userid' => [
130 'type' => PARAM_INT,
131 'optional' => true,
132 'default' => null,
133 'null' => NULL_ALLOWED
134 ],
135 'repeatid' => [
136 'type' => PARAM_INT,
137 'optional' => true,
138 'default' => null,
139 'null' => NULL_ALLOWED
140 ],
2dbfb484
AN
141 'eventcount' => [
142 'type' => PARAM_INT,
143 'optional' => true,
144 'default' => null,
145 'null' => NULL_ALLOWED
146 ],
6ad2bfd1
AN
147 'modulename' => [
148 'type' => PARAM_TEXT,
149 'optional' => true,
150 'default' => null,
151 'null' => NULL_ALLOWED
152 ],
153 'instance' => [
154 'type' => PARAM_INT,
155 'optional' => true,
156 'default' => null,
157 'null' => NULL_ALLOWED
158 ],
159 'eventtype' => ['type' => PARAM_TEXT],
160 'timestart' => ['type' => PARAM_INT],
161 'timeduration' => ['type' => PARAM_INT],
162 'timesort' => ['type' => PARAM_INT],
163 'visible' => ['type' => PARAM_INT],
164 'timemodified' => ['type' => PARAM_INT],
165 ];
166 }
167
168 /**
169 * Return the list of additional properties.
170 *
171 * @return array
172 */
173 protected static function define_other_properties() {
174 return [
6ad2bfd1
AN
175 'icon' => [
176 'type' => event_icon_exporter::read_properties_definition(),
177 ],
6ad2bfd1
AN
178 'course' => [
179 'type' => course_summary_exporter::read_properties_definition(),
180 'optional' => true,
1652934f 181 ],
8afe9f8a
SL
182 'subscription' => [
183 'type' => event_subscription_exporter::read_properties_definition(),
184 'optional' => true,
185 ],
1652934f
RW
186 'canedit' => [
187 'type' => PARAM_BOOL
188 ],
189 'candelete' => [
190 'type' => PARAM_BOOL
191 ],
5a35675e
SL
192 'deleteurl' => [
193 'type' => PARAM_URL
194 ],
195 'editurl' => [
196 'type' => PARAM_URL
197 ],
198 'formattedtime' => [
199 'type' => PARAM_RAW,
63b13467
SL
200 ],
201 'isactionevent' => [
202 'type' => PARAM_BOOL
203 ],
204 'iscourseevent' => [
205 'type' => PARAM_BOOL
206 ],
207 'groupname' => [
208 'type' => PARAM_RAW,
209 'optional' => true,
210 'default' => null,
211 'null' => NULL_ALLOWED
212 ],
6ad2bfd1
AN
213 ];
214 }
215
216 /**
217 * Get the additional values to inject while exporting.
218 *
219 * @param renderer_base $output The renderer.
220 * @return array Keys are the property names, values are their values.
221 */
222 protected function get_other_values(renderer_base $output) {
223 $values = [];
224 $event = $this->event;
1652934f 225 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
6ad2bfd1 226 $context = $this->related['context'];
63b13467
SL
227 $values['isactionevent'] = false;
228 $values['iscourseevent'] = false;
229 if ($moduleproxy = $event->get_course_module()) {
230 $values['isactionevent'] = true;
231 } else if ($event->get_type() == 'course') {
232 $values['iscourseevent'] = true;
233 }
6ad2bfd1
AN
234 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
235 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
236
6ad2bfd1
AN
237 $values['icon'] = $iconexporter->export($output);
238
8afe9f8a
SL
239 $subscriptionexporter = new event_subscription_exporter($event);
240 $values['subscription'] = $subscriptionexporter->export($output);
241
6ad2bfd1
AN
242 if ($course = $this->related['course']) {
243 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
244 $values['course'] = $coursesummaryexporter->export($output);
245 }
5a35675e 246 $courseid = (!$course) ? SITEID : $course->id;
6ad2bfd1 247
909d0858 248 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
1652934f
RW
249 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
250
5a35675e
SL
251 $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
252 $values['deleteurl'] = $deleteurl->out(false);
253
254 $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
255 'course' => $courseid]);
256 $values['editurl'] = $editurl->out(false);
257 $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
258 $timesort);
259
63b13467
SL
260 if ($course = $this->related['course']) {
261 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
262 $values['course'] = $coursesummaryexporter->export($output);
263 }
264
265 if ($group = $event->get_group()) {
266 $values['groupname'] = format_string($group->get('name'), true,
267 ['context' => \context_course::instance($event->get_course()->get('id'))]);
268 }
6ad2bfd1
AN
269 return $values;
270 }
271
272 /**
273 * Returns a list of objects that are related.
274 *
275 * @return array
276 */
277 protected static function define_related() {
278 return [
279 'context' => 'context',
280 'course' => 'stdClass?',
281 ];
282 }
283}