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 require_once($CFG->dirroot . "/calendar/lib.php");
30 require_once($CFG->libdir . "/filelib.php");
32 use \core\external\exporter;
33 use \core_calendar\local\event\container;
34 use \core_calendar\local\event\entities\event_interface;
35 use \core_calendar\local\event\entities\action_event_interface;
36 use \core_course\external\course_summary_exporter;
37 use \core\external\coursecat_summary_exporter;
42 * Class for displaying a calendar event.
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
48 class event_exporter_base extends exporter {
51 * @var event_interface $event
58 * @param event_interface $event
59 * @param array $related The related data.
61 public function __construct(event_interface $event, $related = []) {
62 $this->event = $event;
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;
68 $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
70 $data = new \stdClass();
71 $data->id = $event->get_id();
72 $data->name = $event->get_name();
73 $data->description = file_rewrite_pluginfile_urls(
74 $event->get_description()->get_value(),
76 $related['context']->id,
81 $data->descriptionformat = $event->get_description()->get_format();
82 $data->groupid = $groupid;
83 $data->userid = $userid;
84 $data->categoryid = $categoryid;
85 $data->eventtype = $event->get_type();
86 $data->timestart = $starttimestamp;
87 $data->timeduration = $endtimestamp - $starttimestamp;
88 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
89 $data->visible = $event->is_visible() ? 1 : 0;
90 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
92 if ($repeats = $event->get_repeats()) {
93 $data->repeatid = $repeats->get_id();
94 $data->eventcount = $repeats->get_num() + 1;
97 if ($cm = $event->get_course_module()) {
98 $data->modulename = $cm->get('modname');
99 $data->instance = $cm->get('id');
102 parent::__construct($data, $related);
106 * Return the list of properties.
110 protected static function define_properties() {
112 'id' => ['type' => PARAM_INT],
113 'name' => ['type' => PARAM_TEXT],
118 'null' => NULL_ALLOWED
120 'descriptionformat' => [
124 'null' => NULL_ALLOWED
130 'null' => NULL_ALLOWED
136 'null' => NULL_ALLOWED
142 'null' => NULL_ALLOWED
148 'null' => NULL_ALLOWED
154 'null' => NULL_ALLOWED
157 'type' => PARAM_TEXT,
160 'null' => NULL_ALLOWED
166 'null' => NULL_ALLOWED
168 'eventtype' => ['type' => PARAM_TEXT],
169 'timestart' => ['type' => PARAM_INT],
170 'timeduration' => ['type' => PARAM_INT],
171 'timesort' => ['type' => PARAM_INT],
172 'visible' => ['type' => PARAM_INT],
173 'timemodified' => ['type' => PARAM_INT],
178 * Return the list of additional properties.
182 protected static function define_other_properties() {
185 'type' => event_icon_exporter::read_properties_definition(),
188 'type' => coursecat_summary_exporter::read_properties_definition(),
192 'type' => course_summary_exporter::read_properties_definition(),
196 'type' => event_subscription_exporter::read_properties_definition(),
223 'iscategoryevent' => [
230 'null' => NULL_ALLOWED
236 * Get the additional values to inject while exporting.
238 * @param renderer_base $output The renderer.
239 * @return array Keys are the property names, values are their values.
241 protected function get_other_values(renderer_base $output) {
243 $event = $this->event;
244 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
245 $context = $this->related['context'];
246 $course = $this->related['course'];
247 $values['isactionevent'] = false;
248 $values['iscourseevent'] = false;
249 $values['iscategoryevent'] = false;
250 if ($moduleproxy = $event->get_course_module()) {
251 $values['isactionevent'] = true;
252 } else if ($event->get_type() == 'course') {
253 $values['iscourseevent'] = true;
254 } else if ($event->get_type() == 'category') {
255 $values['iscategoryevent'] = true;
257 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
258 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
260 $values['icon'] = $iconexporter->export($output);
262 $subscriptionexporter = new event_subscription_exporter($event);
263 $values['subscription'] = $subscriptionexporter->export($output);
265 $proxy = $this->event->get_category();
266 if ($proxy && $proxy->get('id')) {
267 $category = $proxy->get_proxied_instance();
268 $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
269 $values['category'] = $categorysummaryexporter->export($output);
273 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
274 $values['course'] = $coursesummaryexporter->export($output);
277 $courseid = (!$course) ? SITEID : $course->id;
279 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
280 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
282 $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
283 $values['deleteurl'] = $deleteurl->out(false);
285 $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
286 'course' => $courseid]);
287 $values['editurl'] = $editurl->out(false);
288 $viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
289 'time' => $timesort]);
290 $viewurl->set_anchor('event_' . $event->get_id());
291 $values['viewurl'] = $viewurl->out(false);
292 $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
295 if ($group = $event->get_group()) {
296 $values['groupname'] = format_string($group->get('name'), true,
297 ['context' => \context_course::instance($event->get_course()->get('id'))]);
304 * Returns a list of objects that are related.
308 protected static function define_related() {
310 'context' => 'context',
311 'course' => 'stdClass?',