weekly release 3.4dev
[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
RW
29require_once($CFG->dirroot . "/calendar/lib.php");
30
6ad2bfd1 31use \core\external\exporter;
1652934f 32use \core_calendar\local\event\container;
6ad2bfd1
AN
33use \core_calendar\local\event\entities\event_interface;
34use \core_calendar\local\event\entities\action_event_interface;
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_base extends exporter {
46
47 /**
48 * @var event_interface $event
49 */
50 protected $event;
51
52 /**
53 * Constructor.
54 *
55 * @param event_interface $event
56 * @param array $related The related data.
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();
77 $data->visible = $event->is_visible() ? 1 : 0;
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 [
6ad2bfd1
AN
159 'icon' => [
160 'type' => event_icon_exporter::read_properties_definition(),
161 ],
6ad2bfd1
AN
162 'course' => [
163 'type' => course_summary_exporter::read_properties_definition(),
164 'optional' => true,
1652934f
RW
165 ],
166 'canedit' => [
167 'type' => PARAM_BOOL
168 ],
169 'candelete' => [
170 'type' => PARAM_BOOL
171 ],
6ad2bfd1
AN
172 ];
173 }
174
175 /**
176 * Get the additional values to inject while exporting.
177 *
178 * @param renderer_base $output The renderer.
179 * @return array Keys are the property names, values are their values.
180 */
181 protected function get_other_values(renderer_base $output) {
182 $values = [];
183 $event = $this->event;
1652934f 184 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
6ad2bfd1 185 $context = $this->related['context'];
6ad2bfd1
AN
186 $timesort = $event->get_times()->get_sort_time()->getTimestamp();
187 $iconexporter = new event_icon_exporter($event, ['context' => $context]);
188
6ad2bfd1
AN
189 $values['icon'] = $iconexporter->export($output);
190
6ad2bfd1
AN
191 if ($course = $this->related['course']) {
192 $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
193 $values['course'] = $coursesummaryexporter->export($output);
194 }
195
909d0858 196 $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
1652934f
RW
197 $values['candelete'] = calendar_delete_event_allowed($legacyevent);
198
6ad2bfd1
AN
199 return $values;
200 }
201
202 /**
203 * Returns a list of objects that are related.
204 *
205 * @return array
206 */
207 protected static function define_related() {
208 return [
209 'context' => 'context',
210 'course' => 'stdClass?',
211 ];
212 }
213}