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 use \core\external\exporter;
30 use \core_calendar\local\event\entities\event_interface;
31 use \core_calendar\local\event\entities\action_event_interface;
32 use \core_course\external\course_summary_exporter;
36 * Class for displaying a calendar event.
38 * @package core_calendar
39 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class event_exporter extends exporter {
45 * @var event_interface $event
52 * @param event_interface $event
53 * @param array $related The related data.
55 public function __construct(event_interface $event, $related = []) {
56 $this->event = $event;
58 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
59 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
60 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
61 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
63 $data = new \stdClass();
64 $data->id = $event->get_id();
65 $data->name = $event->get_name();
66 $data->description = $event->get_description()->get_value();
67 $data->descriptionformat = $event->get_description()->get_format();
68 $data->groupid = $groupid;
69 $data->userid = $userid;
70 $data->eventtype = $event->get_type();
71 $data->timestart = $starttimestamp;
72 $data->timeduration = $endtimestamp - $starttimestamp;
73 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
74 $data->visible = $event->is_visible();
75 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
77 if ($repeats = $event->get_repeats()) {
78 $data->repeatid = $repeats->get_id();
81 if ($cm = $event->get_course_module()) {
82 $data->modulename = $cm->get('modname');
83 $data->instance = $cm->get('id');
86 parent::__construct($data, $related);
90 * Return the list of properties.
94 protected static function define_properties() {
96 'id' => ['type' => PARAM_INT],
97 'name' => ['type' => PARAM_TEXT],
102 'null' => NULL_ALLOWED
104 'descriptionformat' => [
108 'null' => NULL_ALLOWED
114 'null' => NULL_ALLOWED
120 'null' => NULL_ALLOWED
126 'null' => NULL_ALLOWED
129 'type' => PARAM_TEXT,
132 'null' => NULL_ALLOWED
138 'null' => NULL_ALLOWED
140 'eventtype' => ['type' => PARAM_TEXT],
141 'timestart' => ['type' => PARAM_INT],
142 'timeduration' => ['type' => PARAM_INT],
143 'timesort' => ['type' => PARAM_INT],
144 'visible' => ['type' => PARAM_INT],
145 'timemodified' => ['type' => PARAM_INT],
150 * Return the list of additional properties.
154 protected static function define_other_properties() {
156 'url' => ['type' => PARAM_URL],
158 'type' => event_icon_exporter::read_properties_definition(),
161 'type' => event_action_exporter::read_properties_definition(),
165 'type' => course_summary_exporter::read_properties_definition(),
172 * Get the additional values to inject while exporting.
174 * @param renderer_base $output The renderer.
175 * @return array Keys are the property names, values are their values.
177 protected function get_other_values(renderer_base $output) {
179 $event = $this->event;
180 $context = $this->related['context'];
181 if ($moduleproxy = $event->get_course_module()) {
182 $modulename = $moduleproxy->get('modname');
183 $moduleid = $moduleproxy->get('id');
184 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
186 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
188 require_once($CFG->dirroot.'/course/lib.php');
189 $url = \course_get_url($this->related['course'] ?: SITEID);
191 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
192 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
194 $values['url'] = $url->out(false);
195 $values['icon'] = $iconexporter->export($output);
197 if ($event instanceof action_event_interface) {
199 'context' => $context,
202 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
203 $values['action'] = $actionexporter->export($output);
206 if ($course = $this->related['course']) {
207 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
208 $values['course'] = $coursesummaryexporter->export($output);
215 * Returns a list of objects that are related.
219 protected static function define_related() {
221 'context' => 'context',
222 'course' => 'stdClass?',