MDL-57502 core_calendar: add calendar event exporters
[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
29use \core\external\exporter;
30use \core_calendar\local\interfaces\event_interface;
31use \core_calendar\local\interfaces\action_event_interface;
32use \core_course\external\course_summary_exporter;
33use \renderer_base;
34
35/**
36 * Class for displaying a calendar event.
37 *
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
41 */
42class event_exporter extends exporter {
43
44 /**
45 * @var event_interface $event
46 */
47 protected $event;
48
49 /**
50 * Constructor.
51 *
52 * @param event_interface $event
53 */
54 public function __construct(event_interface $event, $related = []) {
55 $this->event = $event;
56
57 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
58 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
59 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
60 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
61
62 $data = new \stdClass();
63 $data->id = $event->get_id();
64 $data->name = $event->get_name();
65 $data->description = $event->get_description()->get_value();
66 $data->descriptionformat = $event->get_description()->get_format();
67 $data->groupid = $groupid;
68 $data->userid = $userid;
69 $data->eventtype = $event->get_type();
70 $data->timestart = $starttimestamp;
71 $data->timeduration = $endtimestamp - $starttimestamp;
72 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
73 $data->visible = $event->is_visible();
74 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
75
76 if ($repeats = $event->get_repeats()) {
77 $data->repeatid = $repeats->get_id();
78 }
79
80 if ($cm = $event->get_course_module()) {
81 $data->modulename = $cm->get('modname');
82 $data->instance = $cm->get('id');
83 }
84
85 parent::__construct($data, $related);
86 }
87
88 /**
89 * Return the list of properties.
90 *
91 * @return array
92 */
93 protected static function define_properties() {
94 return [
95 'id' => ['type' => PARAM_INT],
96 'name' => ['type' => PARAM_TEXT],
97 'description' => [
98 'type' => PARAM_RAW,
99 'optional' => true,
100 'default' => null,
101 'null' => NULL_ALLOWED
102 ],
103 'descriptionformat' => [
104 'type' => PARAM_INT,
105 'optional' => true,
106 'default' => null,
107 'null' => NULL_ALLOWED
108 ],
109 'groupid' => [
110 'type' => PARAM_INT,
111 'optional' => true,
112 'default' => null,
113 'null' => NULL_ALLOWED
114 ],
115 'userid' => [
116 'type' => PARAM_INT,
117 'optional' => true,
118 'default' => null,
119 'null' => NULL_ALLOWED
120 ],
121 'repeatid' => [
122 'type' => PARAM_INT,
123 'optional' => true,
124 'default' => null,
125 'null' => NULL_ALLOWED
126 ],
127 'modulename' => [
128 'type' => PARAM_TEXT,
129 'optional' => true,
130 'default' => null,
131 'null' => NULL_ALLOWED
132 ],
133 'instance' => [
134 'type' => PARAM_INT,
135 'optional' => true,
136 'default' => null,
137 'null' => NULL_ALLOWED
138 ],
139 'eventtype' => ['type' => PARAM_TEXT],
140 'timestart' => ['type' => PARAM_INT],
141 'timeduration' => ['type' => PARAM_INT],
142 'timesort' => ['type' => PARAM_INT],
143 'visible' => ['type' => PARAM_INT],
144 'timemodified' => ['type' => PARAM_INT],
145 ];
146 }
147
148 /**
149 * Return the list of additional properties.
150 *
151 * @return array
152 */
153 protected static function define_other_properties() {
154 return [
155 'url' => ['type' => PARAM_URL],
156 'enddate' => ['type' => PARAM_TEXT],
157 'icon' => [
158 'type' => event_icon_exporter::read_properties_definition(),
159 ],
160 'action' => [
161 'type' => event_action_exporter::read_properties_definition(),
162 'optional' => true,
163 ],
164 'course' => [
165 'type' => course_summary_exporter::read_properties_definition(),
166 'optional' => true,
167 ]
168 ];
169 }
170
171 /**
172 * Get the additional values to inject while exporting.
173 *
174 * @param renderer_base $output The renderer.
175 * @return array Keys are the property names, values are their values.
176 */
177 protected function get_other_values(renderer_base $output) {
178 $values = [];
179 $event = $this->event;
180 $context = $this->related['context'];
181 $modulename = $event->get_course_module()->get('modname');
182 $moduleid = $event->get_course_module()->get('id');
183 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
184 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
185 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
186
187 $values['url'] = $url->out(false);
188 $values['enddate'] = userdate($timesort, get_string('strftimerecent'));
189 $values['icon'] = $iconexporter->export($output);
190
191 if ($event instanceof action_event_interface) {
192 $actionexporter = new event_action_exporter($event->get_action(),
193 ['context' => $context]);
194 $values['action'] = $actionexporter->export($output);
195 }
196
197 if ($course = $this->related['course']) {
198 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
199 $values['course'] = $coursesummaryexporter->export($output);
200 }
201
202 return $values;
203 }
204
205 /**
206 * Returns a list of objects that are related.
207 *
208 * @return array
209 */
210 protected static function define_related() {
211 return [
212 'context' => 'context',
213 'course' => 'stdClass?',
214 ];
215 }
216}