MDL-59674 core_calendar: add group name to summary template
[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
29use \core\external\exporter;
30use \core_calendar\local\event\entities\event_interface;
31use \core_calendar\local\event\entities\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_base extends exporter {
43
44 /**
45 * @var event_interface $event
46 */
47 protected $event;
48
49 /**
50 * Constructor.
51 *
52 * @param event_interface $event
53 * @param array $related The related data.
54 */
55 public function __construct(event_interface $event, $related = []) {
56 $this->event = $event;
57
58 $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
59 $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
60 $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
61 $userid = $event->get_user() ? $event->get_user()->get('id') : null;
62
63 $data = new \stdClass();
64 $data->id = $event->get_id();
65 $data->name = $event->get_name();
66 $data->description = $event->get_description()->get_value();
67 $data->descriptionformat = $event->get_description()->get_format();
68 $data->groupid = $groupid;
69 $data->userid = $userid;
70 $data->eventtype = $event->get_type();
71 $data->timestart = $starttimestamp;
72 $data->timeduration = $endtimestamp - $starttimestamp;
73 $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
74 $data->visible = $event->is_visible() ? 1 : 0;
75 $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
76
77 if ($repeats = $event->get_repeats()) {
78 $data->repeatid = $repeats->get_id();
79 }
80
81 if ($cm = $event->get_course_module()) {
82 $data->modulename = $cm->get('modname');
83 $data->instance = $cm->get('id');
84 }
85
86 parent::__construct($data, $related);
87 }
88
89 /**
90 * Return the list of properties.
91 *
92 * @return array
93 */
94 protected static function define_properties() {
95 return [
96 'id' => ['type' => PARAM_INT],
97 'name' => ['type' => PARAM_TEXT],
98 'description' => [
99 'type' => PARAM_RAW,
100 'optional' => true,
101 'default' => null,
102 'null' => NULL_ALLOWED
103 ],
104 'descriptionformat' => [
105 'type' => PARAM_INT,
106 'optional' => true,
107 'default' => null,
108 'null' => NULL_ALLOWED
109 ],
110 'groupid' => [
111 'type' => PARAM_INT,
112 'optional' => true,
113 'default' => null,
114 'null' => NULL_ALLOWED
115 ],
116 'userid' => [
117 'type' => PARAM_INT,
118 'optional' => true,
119 'default' => null,
120 'null' => NULL_ALLOWED
121 ],
122 'repeatid' => [
123 'type' => PARAM_INT,
124 'optional' => true,
125 'default' => null,
126 'null' => NULL_ALLOWED
127 ],
128 'modulename' => [
129 'type' => PARAM_TEXT,
130 'optional' => true,
131 'default' => null,
132 'null' => NULL_ALLOWED
133 ],
134 'instance' => [
135 'type' => PARAM_INT,
136 'optional' => true,
137 'default' => null,
138 'null' => NULL_ALLOWED
139 ],
140 'eventtype' => ['type' => PARAM_TEXT],
141 'timestart' => ['type' => PARAM_INT],
142 'timeduration' => ['type' => PARAM_INT],
143 'timesort' => ['type' => PARAM_INT],
144 'visible' => ['type' => PARAM_INT],
145 'timemodified' => ['type' => PARAM_INT],
146 ];
147 }
148
149 /**
150 * Return the list of additional properties.
151 *
152 * @return array
153 */
154 protected static function define_other_properties() {
155 return [
156 'url' => ['type' => PARAM_URL],
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 if ($moduleproxy = $event->get_course_module()) {
182 $modulename = $moduleproxy->get('modname');
183 $moduleid = $moduleproxy->get('id');
184 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
185 } else {
186 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
187 global $CFG;
188 require_once($CFG->dirroot.'/course/lib.php');
189 $url = \course_get_url($this->related['course'] ?: SITEID);
190 }
191 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
192 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
193
194 $values['url'] = $url->out(false);
195 $values['icon'] = $iconexporter->export($output);
196
197 if ($event instanceof action_event_interface) {
198 $actionrelated = [
199 'context' => $context,
200 'event' => $event
201 ];
202 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
203 $values['action'] = $actionexporter->export($output);
204 }
205
206 if ($course = $this->related['course']) {
207 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
208 $values['course'] = $coursesummaryexporter->export($output);
209 }
210
211 return $values;
212 }
213
214 /**
215 * Returns a list of objects that are related.
216 *
217 * @return array
218 */
219 protected static function define_related() {
220 return [
221 'context' => 'context',
222 'course' => 'stdClass?',
223 ];
224 }
225}