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;
41 * Class for displaying a calendar event.
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
47 class event_exporter_base extends exporter {
50 * @var event_interface $event
57 * @param event_interface $event
58 * @param array $related The related data.
60 public function __construct(event_interface $event, $related = []) {
61 $this->event = $event;
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;
68 $data = new \stdClass();
69 $data->id = $event->get_id();
70 $data->name = $event->get_name();
71 $data->description = file_rewrite_pluginfile_urls(
72 $event->get_description()->get_value(),
74 $related['context']->id,
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();
89 if ($repeats = $event->get_repeats()) {
90 $data->repeatid = $repeats->get_id();
91 $data->eventcount = $repeats->get_num() + 1;
94 if ($cm = $event->get_course_module()) {
95 $data->modulename = $cm->get('modname');
96 $data->instance = $cm->get('id');
99 parent::__construct($data, $related);
103 * Return the list of properties.
107 protected static function define_properties() {
109 'id' => ['type' => PARAM_INT],
110 'name' => ['type' => PARAM_TEXT],
115 'null' => NULL_ALLOWED
117 'descriptionformat' => [
121 'null' => NULL_ALLOWED
127 'null' => NULL_ALLOWED
133 'null' => NULL_ALLOWED
139 'null' => NULL_ALLOWED
145 'null' => NULL_ALLOWED
148 'type' => PARAM_TEXT,
151 'null' => NULL_ALLOWED
157 'null' => NULL_ALLOWED
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],
169 * Return the list of additional properties.
173 protected static function define_other_properties() {
176 'type' => event_icon_exporter::read_properties_definition(),
179 'type' => course_summary_exporter::read_properties_definition(),
183 'type' => event_subscription_exporter::read_properties_definition(),
211 'null' => NULL_ALLOWED
217 * Get the additional values to inject while exporting.
219 * @param renderer_base $output The renderer.
220 * @return array Keys are the property names, values are their values.
222 protected function get_other_values(renderer_base $output) {
224 $event = $this->event;
225 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
226 $context = $this->related['context'];
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;
234 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
235 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
237 $values['icon'] = $iconexporter->export($output);
239 $subscriptionexporter = new event_subscription_exporter($event);
240 $values['subscription'] = $subscriptionexporter->export($output);
242 if ($course = $this->related['course']) {
243 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
244 $values['course'] = $coursesummaryexporter->export($output);
246 $courseid = (!$course) ? SITEID : $course->id;
248 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
249 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
251 $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
252 $values['deleteurl'] = $deleteurl->out(false);
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,
260 if ($course = $this->related['course']) {
261 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
262 $values['course'] = $coursesummaryexporter->export($output);
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'))]);
273 * Returns a list of objects that are related.
277 protected static function define_related() {
279 'context' => 'context',
280 'course' => 'stdClass?',