Merge branch 'MDL-43176' of https://github.com/NeillM/moodle
[moodle.git] / calendar / classes / external / event_exporter_base.php
CommitLineData
6ad2bfd1
AN
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
1652934f 29require_once($CFG->dirroot . "/calendar/lib.php");
ea5f7707 30require_once($CFG->libdir . "/filelib.php");
1652934f 31
6ad2bfd1 32use \core\external\exporter;
1652934f 33use \core_calendar\local\event\container;
6ad2bfd1
AN
34use \core_calendar\local\event\entities\event_interface;
35use \core_calendar\local\event\entities\action_event_interface;
36use \core_course\external\course_summary_exporter;
37use \renderer_base;
38
39/**
40 * Class for displaying a calendar event.
41 *
42 * @package core_calendar
43 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class event_exporter_base extends exporter {
47
48 /**
49 * @var event_interface $event
50 */
51 protected $event;
52
53 /**
54 * Constructor.
55 *
56 * @param event_interface $event
57 * @param array $related The related data.
58 */
59 public function __construct(event_interface $event, $related = []) {
60 $this->event = $event;
61
62 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
63 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
64 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
65 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
66
67 $data = new \stdClass();
68 $data->id = $event->get_id();
69 $data->name = $event->get_name();
ea5f7707
RW
70 $data->description = file_rewrite_pluginfile_urls(
71 $event->get_description()->get_value(),
72 'pluginfile.php',
73 $related['context']->id,
74 'calendar',
75 'event_description',
76 $event->get_id()
77 );
6ad2bfd1
AN
78 $data->descriptionformat = $event->get_description()->get_format();
79 $data->groupid = $groupid;
80 $data->userid = $userid;
81 $data->eventtype = $event->get_type();
82 $data->timestart = $starttimestamp;
83 $data->timeduration = $endtimestamp - $starttimestamp;
84 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
85 $data->visible = $event->is_visible() ? 1 : 0;
86 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
87
88 if ($repeats = $event->get_repeats()) {
89 $data->repeatid = $repeats->get_id();
2dbfb484 90 $data->eventcount = $repeats->get_num() + 1;
6ad2bfd1
AN
91 }
92
93 if ($cm = $event->get_course_module()) {
94 $data->modulename = $cm->get('modname');
95 $data->instance = $cm->get('id');
96 }
97
98 parent::__construct($data, $related);
99 }
100
101 /**
102 * Return the list of properties.
103 *
104 * @return array
105 */
106 protected static function define_properties() {
107 return [
108 'id' => ['type' => PARAM_INT],
109 'name' => ['type' => PARAM_TEXT],
110 'description' => [
111 'type' => PARAM_RAW,
112 'optional' => true,
113 'default' => null,
114 'null' => NULL_ALLOWED
115 ],
116 'descriptionformat' => [
117 'type' => PARAM_INT,
118 'optional' => true,
119 'default' => null,
120 'null' => NULL_ALLOWED
121 ],
122 'groupid' => [
123 'type' => PARAM_INT,
124 'optional' => true,
125 'default' => null,
126 'null' => NULL_ALLOWED
127 ],
128 'userid' => [
129 'type' => PARAM_INT,
130 'optional' => true,
131 'default' => null,
132 'null' => NULL_ALLOWED
133 ],
134 'repeatid' => [
135 'type' => PARAM_INT,
136 'optional' => true,
137 'default' => null,
138 'null' => NULL_ALLOWED
139 ],
2dbfb484
AN
140 'eventcount' => [
141 'type' => PARAM_INT,
142 'optional' => true,
143 'default' => null,
144 'null' => NULL_ALLOWED
145 ],
6ad2bfd1
AN
146 'modulename' => [
147 'type' => PARAM_TEXT,
148 'optional' => true,
149 'default' => null,
150 'null' => NULL_ALLOWED
151 ],
152 'instance' => [
153 'type' => PARAM_INT,
154 'optional' => true,
155 'default' => null,
156 'null' => NULL_ALLOWED
157 ],
158 'eventtype' => ['type' => PARAM_TEXT],
159 'timestart' => ['type' => PARAM_INT],
160 'timeduration' => ['type' => PARAM_INT],
161 'timesort' => ['type' => PARAM_INT],
162 'visible' => ['type' => PARAM_INT],
163 'timemodified' => ['type' => PARAM_INT],
164 ];
165 }
166
167 /**
168 * Return the list of additional properties.
169 *
170 * @return array
171 */
172 protected static function define_other_properties() {
173 return [
6ad2bfd1
AN
174 'icon' => [
175 'type' => event_icon_exporter::read_properties_definition(),
176 ],
6ad2bfd1
AN
177 'course' => [
178 'type' => course_summary_exporter::read_properties_definition(),
179 'optional' => true,
1652934f
RW
180 ],
181 'canedit' => [
182 'type' => PARAM_BOOL
183 ],
184 'candelete' => [
185 'type' => PARAM_BOOL
186 ],
6ad2bfd1
AN
187 ];
188 }
189
190 /**
191 * Get the additional values to inject while exporting.
192 *
193 * @param renderer_base $output The renderer.
194 * @return array Keys are the property names, values are their values.
195 */
196 protected function get_other_values(renderer_base $output) {
197 $values = [];
198 $event = $this->event;
1652934f 199 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
6ad2bfd1 200 $context = $this->related['context'];
6ad2bfd1
AN
201 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
202 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
203
6ad2bfd1
AN
204 $values['icon'] = $iconexporter->export($output);
205
6ad2bfd1
AN
206 if ($course = $this->related['course']) {
207 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
208 $values['course'] = $coursesummaryexporter->export($output);
209 }
210
909d0858 211 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
1652934f
RW
212 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
213
6ad2bfd1
AN
214 return $values;
215 }
216
217 /**
218 * Returns a list of objects that are related.
219 *
220 * @return array
221 */
222 protected static function define_related() {
223 return [
224 'context' => 'context',
225 'course' => 'stdClass?',
226 ];
227 }
228}