weekly release 3.4dev
[moodle.git] / calendar / classes / external / event_exporter.php
CommitLineData
984355ce
RW
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
6fb75156
SL
29require_once($CFG->dirroot . "/calendar/lib.php");
30
53cbac18 31use \core_calendar\local\event\entities\action_event_interface;
6fb75156 32use \core_calendar\local\event\container;
984355ce
RW
33use \core_course\external\course_summary_exporter;
34use \renderer_base;
35
36/**
37 * Class for displaying a calendar event.
38 *
39 * @package core_calendar
40 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
6ad2bfd1 43class event_exporter extends event_exporter_base {
984355ce
RW
44
45 /**
46 * Return the list of additional properties.
47 *
48 * @return array
49 */
50 protected static function define_other_properties() {
6ad2bfd1
AN
51
52 $values = parent::define_other_properties();
53
54 $values['canedit'] = ['type' => PARAM_BOOL];
55 $values['displayeventsource'] = ['type' => PARAM_BOOL];
56 $values['subscription'] = [
57 'type' => PARAM_RAW,
58 'optional' => true,
59 'default' => null,
60 'null' => NULL_ALLOWED
61 ];
62 $values['isactionevent'] = ['type' => PARAM_BOOL];
63 $values['candelete'] = ['type' => PARAM_BOOL];
64 $values['url'] = ['type' => PARAM_URL];
65 $values['action'] = [
66 'type' => event_action_exporter::read_properties_definition(),
67 'optional' => true,
984355ce 68 ];
6ad2bfd1
AN
69
70 return $values;
984355ce
RW
71 }
72
73 /**
74 * Get the additional values to inject while exporting.
75 *
76 * @param renderer_base $output The renderer.
77 * @return array Keys are the property names, values are their values.
78 */
79 protected function get_other_values(renderer_base $output) {
6ad2bfd1
AN
80 $values = parent::get_other_values($output);
81
984355ce 82 $event = $this->event;
6fb75156
SL
83 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
84
984355ce 85 $context = $this->related['context'];
4df03a27 86 $values['isactionevent'] = false;
6ad2bfd1 87
cd2c4b85
MG
88 if ($moduleproxy = $event->get_course_module()) {
89 $modulename = $moduleproxy->get('modname');
90 $moduleid = $moduleproxy->get('id');
91 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
c34e2002 92
4df03a27 93 $values['isactionevent'] = true;
c34e2002
SL
94
95 // Build edit event url for action events.
96 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
97 $editurl = new \moodle_url('/course/mod.php', $params);
98 $values['editurl'] = $editurl->out(false);
cd2c4b85
MG
99 } else {
100 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
101 global $CFG;
102 require_once($CFG->dirroot.'/course/lib.php');
103 $url = \course_get_url($this->related['course'] ?: SITEID);
104 }
984355ce
RW
105
106 $values['url'] = $url->out(false);
984355ce
RW
107
108 if ($event instanceof action_event_interface) {
6b7b39fe
JP
109 $actionrelated = [
110 'context' => $context,
111 'event' => $event
112 ];
113 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
984355ce
RW
114 $values['action'] = $actionexporter->export($output);
115 }
116
117 if ($course = $this->related['course']) {
118 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
119 $values['course'] = $coursesummaryexporter->export($output);
120 }
4df03a27 121
6fb75156 122 $values['canedit'] = calendar_edit_event_allowed($legacyevent);
436d39ba 123 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
6fb75156
SL
124
125 // Handle event subscription.
126 $values['subscription'] = null;
127 $values['displayeventsource'] = false;
128 if (!empty($legacyevent->subscriptionid)) {
129 $subscription = calendar_get_subscription($legacyevent->subscriptionid);
130 if (!empty($subscription) && $CFG->calendar_showicalsource) {
131 $values['displayeventsource'] = true;
132 $subscriptiondata = new \stdClass();
133 if (!empty($subscription->url)) {
134 $subscriptiondata->url = $subscription->url;
135 }
136 $subscriptiondata->name = $subscription->name;
137 $values['subscription'] = json_encode($subscriptiondata);
138 }
139 }
984355ce 140
6ad2bfd1 141 return $values;
984355ce
RW
142 }
143}