MDL-59671 core_calendar: add visibility logic to template
[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
984355ce 31use \core\external\exporter;
42e76c3f 32use \core_calendar\local\event\entities\event_interface;
53cbac18 33use \core_calendar\local\event\entities\action_event_interface;
6fb75156 34use \core_calendar\local\event\container;
984355ce
RW
35use \core_course\external\course_summary_exporter;
36use \renderer_base;
37
38/**
39 * Class for displaying a calendar event.
40 *
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
44 */
45class event_exporter extends exporter {
46
47 /**
48 * @var event_interface $event
49 */
50 protected $event;
51
52 /**
53 * Constructor.
54 *
55 * @param event_interface $event
20592f5f 56 * @param array $related The related data.
984355ce
RW
57 */
58 public function __construct(event_interface $event, $related = []) {
59 $this->event = $event;
60
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;
65
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();
8e02018f 77 $data->visible = $event->is_visible() ? 1 : 0;
984355ce
RW
78 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
79
80 if ($repeats = $event->get_repeats()) {
81 $data->repeatid = $repeats->get_id();
82 }
83
84 if ($cm = $event->get_course_module()) {
85 $data->modulename = $cm->get('modname');
86 $data->instance = $cm->get('id');
87 }
88
89 parent::__construct($data, $related);
90 }
91
92 /**
93 * Return the list of properties.
94 *
95 * @return array
96 */
97 protected static function define_properties() {
98 return [
99 'id' => ['type' => PARAM_INT],
100 'name' => ['type' => PARAM_TEXT],
101 'description' => [
102 'type' => PARAM_RAW,
103 'optional' => true,
104 'default' => null,
105 'null' => NULL_ALLOWED
106 ],
107 'descriptionformat' => [
108 'type' => PARAM_INT,
109 'optional' => true,
110 'default' => null,
111 'null' => NULL_ALLOWED
112 ],
113 'groupid' => [
114 'type' => PARAM_INT,
115 'optional' => true,
116 'default' => null,
117 'null' => NULL_ALLOWED
118 ],
119 'userid' => [
120 'type' => PARAM_INT,
121 'optional' => true,
122 'default' => null,
123 'null' => NULL_ALLOWED
124 ],
125 'repeatid' => [
126 'type' => PARAM_INT,
127 'optional' => true,
128 'default' => null,
129 'null' => NULL_ALLOWED
130 ],
131 'modulename' => [
132 'type' => PARAM_TEXT,
133 'optional' => true,
134 'default' => null,
135 'null' => NULL_ALLOWED
136 ],
137 'instance' => [
138 'type' => PARAM_INT,
139 'optional' => true,
140 'default' => null,
141 'null' => NULL_ALLOWED
142 ],
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],
149 ];
150 }
151
152 /**
153 * Return the list of additional properties.
154 *
155 * @return array
156 */
157 protected static function define_other_properties() {
158 return [
159 'url' => ['type' => PARAM_URL],
984355ce
RW
160 'icon' => [
161 'type' => event_icon_exporter::read_properties_definition(),
162 ],
163 'action' => [
164 'type' => event_action_exporter::read_properties_definition(),
165 'optional' => true,
166 ],
167 'course' => [
168 'type' => course_summary_exporter::read_properties_definition(),
169 'optional' => true,
6fb75156
SL
170 ],
171 'canedit' => ['type' => PARAM_BOOL],
172 'displayeventsource' => ['type' => PARAM_BOOL],
173 'subscription' => [
174 'type' => PARAM_RAW,
175 'optional' => true,
176 'default' => null,
177 'null' => NULL_ALLOWED
4df03a27
SL
178 ],
179 'isactionevent' => ['type' => PARAM_BOOL],
180 'candelete' => ['type' => PARAM_BOOL]
984355ce
RW
181 ];
182 }
183
184 /**
185 * Get the additional values to inject while exporting.
186 *
187 * @param renderer_base $output The renderer.
188 * @return array Keys are the property names, values are their values.
189 */
190 protected function get_other_values(renderer_base $output) {
191 $values = [];
192 $event = $this->event;
6fb75156
SL
193 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
194
984355ce 195 $context = $this->related['context'];
4df03a27 196 $values['isactionevent'] = false;
cd2c4b85
MG
197 if ($moduleproxy = $event->get_course_module()) {
198 $modulename = $moduleproxy->get('modname');
199 $moduleid = $moduleproxy->get('id');
200 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
4df03a27 201 $values['isactionevent'] = true;
cd2c4b85
MG
202 } else {
203 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
204 global $CFG;
205 require_once($CFG->dirroot.'/course/lib.php');
206 $url = \course_get_url($this->related['course'] ?: SITEID);
207 }
984355ce 208 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
984355ce
RW
209 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
210
211 $values['url'] = $url->out(false);
984355ce
RW
212 $values['icon'] = $iconexporter->export($output);
213
214 if ($event instanceof action_event_interface) {
6b7b39fe
JP
215 $actionrelated = [
216 'context' => $context,
217 'event' => $event
218 ];
219 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
984355ce
RW
220 $values['action'] = $actionexporter->export($output);
221 }
222
223 if ($course = $this->related['course']) {
224 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
225 $values['course'] = $coursesummaryexporter->export($output);
226 }
4df03a27 227
6fb75156 228 $values['canedit'] = calendar_edit_event_allowed($legacyevent);
4df03a27 229 $values['candelete'] = (!$values['isactionevent'] && $values['canedit']);
6fb75156
SL
230
231 // Handle event subscription.
232 $values['subscription'] = null;
233 $values['displayeventsource'] = false;
234 if (!empty($legacyevent->subscriptionid)) {
235 $subscription = calendar_get_subscription($legacyevent->subscriptionid);
236 if (!empty($subscription) && $CFG->calendar_showicalsource) {
237 $values['displayeventsource'] = true;
238 $subscriptiondata = new \stdClass();
239 if (!empty($subscription->url)) {
240 $subscriptiondata->url = $subscription->url;
241 }
242 $subscriptiondata->name = $subscription->name;
243 $values['subscription'] = json_encode($subscriptiondata);
244 }
245 }
984355ce
RW
246 return $values;
247 }
248
249 /**
250 * Returns a list of objects that are related.
251 *
252 * @return array
253 */
254 protected static function define_related() {
255 return [
256 'context' => 'context',
257 'course' => 'stdClass?',
258 ];
259 }
260}