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");
31 use \core\external\exporter;
32 use \core_calendar\local\event\entities\event_interface;
33 use \core_calendar\local\event\entities\action_event_interface;
34 use \core_calendar\local\event\container;
35 use \core_course\external\course_summary_exporter;
39 * Class for displaying a calendar event.
41 * @package core_calendar
42 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class event_exporter extends exporter {
48 * @var event_interface $event
55 * @param event_interface $event
56 * @param array $related The related data.
58 public function __construct(event_interface $event, $related = []) {
59 $this->event = $event;
61 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
62 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
63 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
64 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
66 $data = new \stdClass();
67 $data->id = $event->get_id();
68 $data->name = $event->get_name();
69 $data->description = $event->get_description()->get_value();
70 $data->descriptionformat = $event->get_description()->get_format();
71 $data->groupid = $groupid;
72 $data->userid = $userid;
73 $data->eventtype = $event->get_type();
74 $data->timestart = $starttimestamp;
75 $data->timeduration = $endtimestamp - $starttimestamp;
76 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
77 $data->visible = $event->is_visible() ? 1 : 0;
78 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
80 if ($repeats = $event->get_repeats()) {
81 $data->repeatid = $repeats->get_id();
84 if ($cm = $event->get_course_module()) {
85 $data->modulename = $cm->get('modname');
86 $data->instance = $cm->get('id');
89 parent::__construct($data, $related);
93 * Return the list of properties.
97 protected static function define_properties() {
99 'id' => ['type' => PARAM_INT],
100 'name' => ['type' => PARAM_TEXT],
105 'null' => NULL_ALLOWED
107 'descriptionformat' => [
111 'null' => NULL_ALLOWED
117 'null' => NULL_ALLOWED
123 'null' => NULL_ALLOWED
129 'null' => NULL_ALLOWED
132 'type' => PARAM_TEXT,
135 'null' => NULL_ALLOWED
141 'null' => NULL_ALLOWED
143 'eventtype' => ['type' => PARAM_TEXT],
144 'timestart' => ['type' => PARAM_INT],
145 'timeduration' => ['type' => PARAM_INT],
146 'timesort' => ['type' => PARAM_INT],
147 'visible' => ['type' => PARAM_INT],
148 'timemodified' => ['type' => PARAM_INT],
153 * Return the list of additional properties.
157 protected static function define_other_properties() {
159 'url' => ['type' => PARAM_URL],
165 'type' => event_icon_exporter::read_properties_definition(),
168 'type' => event_action_exporter::read_properties_definition(),
172 'type' => course_summary_exporter::read_properties_definition(),
175 'canedit' => ['type' => PARAM_BOOL],
176 'displayeventsource' => ['type' => PARAM_BOOL],
181 'null' => NULL_ALLOWED
183 'isactionevent' => ['type' => PARAM_BOOL],
184 'candelete' => ['type' => PARAM_BOOL]
189 * Get the additional values to inject while exporting.
191 * @param renderer_base $output The renderer.
192 * @return array Keys are the property names, values are their values.
194 protected function get_other_values(renderer_base $output) {
196 $event = $this->event;
197 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
199 $context = $this->related['context'];
200 $values['isactionevent'] = false;
201 if ($moduleproxy = $event->get_course_module()) {
202 $modulename = $moduleproxy->get('modname');
203 $moduleid = $moduleproxy->get('id');
204 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
206 $values['isactionevent'] = true;
208 // Build edit event url for action events.
209 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
210 $editurl = new \moodle_url('/course/mod.php', $params);
211 $values['editurl'] = $editurl->out(false);
213 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
215 require_once($CFG->dirroot.'/course/lib.php');
216 $url = \course_get_url($this->related['course'] ?: SITEID);
218 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
219 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
221 $values['url'] = $url->out(false);
222 $values['icon'] = $iconexporter->export($output);
224 if ($event instanceof action_event_interface) {
226 'context' => $context,
229 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
230 $values['action'] = $actionexporter->export($output);
233 if ($course = $this->related['course']) {
234 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
235 $values['course'] = $coursesummaryexporter->export($output);
238 $values['canedit'] = calendar_edit_event_allowed($legacyevent);
239 $values['candelete'] = (!$values['isactionevent'] && $values['canedit']);
241 // Handle event subscription.
242 $values['subscription'] = null;
243 $values['displayeventsource'] = false;
244 if (!empty($legacyevent->subscriptionid)) {
245 $subscription = calendar_get_subscription($legacyevent->subscriptionid);
246 if (!empty($subscription) && $CFG->calendar_showicalsource) {
247 $values['displayeventsource'] = true;
248 $subscriptiondata = new \stdClass();
249 if (!empty($subscription->url)) {
250 $subscriptiondata->url = $subscription->url;
252 $subscriptiondata->name = $subscription->name;
253 $values['subscription'] = json_encode($subscriptiondata);
260 * Returns a list of objects that are related.
264 protected static function define_related() {
266 'context' => 'context',
267 'course' => 'stdClass?',