MDL-60959 calendar: remove duplicate course export in event exporter
[moodle.git] / calendar / classes / external / event_exporter_base.php
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/>.
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  */
25 namespace core_calendar\external;
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . "/calendar/lib.php");
30 require_once($CFG->libdir . "/filelib.php");
32 use \core\external\exporter;
33 use \core_calendar\local\event\container;
34 use \core_calendar\local\event\entities\event_interface;
35 use \core_calendar\local\event\entities\action_event_interface;
36 use \core_course\external\course_summary_exporter;
37 use \core\external\coursecat_summary_exporter;
38 use \renderer_base;
39 use moodle_url;
41 /**
42  * Class for displaying a calendar event.
43  *
44  * @package   core_calendar
45  * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
46  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47  */
48 class event_exporter_base extends exporter {
50     /**
51      * @var event_interface $event
52      */
53     protected $event;
55     /**
56      * Constructor.
57      *
58      * @param event_interface $event
59      * @param array $related The related data.
60      */
61     public function __construct(event_interface $event, $related = []) {
62         $this->event = $event;
64         $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
65         $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
66         $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
67         $userid = $event->get_user() ? $event->get_user()->get('id') : null;
68         $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
70         $data = new \stdClass();
71         $data->id = $event->get_id();
72         $data->name = $event->get_name();
73         $data->description = file_rewrite_pluginfile_urls(
74             $event->get_description()->get_value(),
75             'pluginfile.php',
76             $related['context']->id,
77             'calendar',
78             'event_description',
79             $event->get_id()
80         );
81         $data->descriptionformat = $event->get_description()->get_format();
82         $data->groupid = $groupid;
83         $data->userid = $userid;
84         $data->categoryid = $categoryid;
85         $data->eventtype = $event->get_type();
86         $data->timestart = $starttimestamp;
87         $data->timeduration = $endtimestamp - $starttimestamp;
88         $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
89         $data->visible = $event->is_visible() ? 1 : 0;
90         $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
92         if ($repeats = $event->get_repeats()) {
93             $data->repeatid = $repeats->get_id();
94             $data->eventcount = $repeats->get_num() + 1;
95         }
97         if ($cm = $event->get_course_module()) {
98             $data->modulename = $cm->get('modname');
99             $data->instance = $cm->get('id');
100         }
102         parent::__construct($data, $related);
103     }
105     /**
106      * Return the list of properties.
107      *
108      * @return array
109      */
110     protected static function define_properties() {
111         return [
112             'id' => ['type' => PARAM_INT],
113             'name' => ['type' => PARAM_TEXT],
114             'description' => [
115                 'type' => PARAM_RAW,
116                 'optional' => true,
117                 'default' => null,
118                 'null' => NULL_ALLOWED
119             ],
120             'descriptionformat' => [
121                 'type' => PARAM_INT,
122                 'optional' => true,
123                 'default' => null,
124                 'null' => NULL_ALLOWED
125             ],
126             'categoryid' => [
127                 'type' => PARAM_INT,
128                 'optional' => true,
129                 'default' => null,
130                 'null' => NULL_ALLOWED
131             ],
132             'groupid' => [
133                 'type' => PARAM_INT,
134                 'optional' => true,
135                 'default' => null,
136                 'null' => NULL_ALLOWED
137             ],
138             'userid' => [
139                 'type' => PARAM_INT,
140                 'optional' => true,
141                 'default' => null,
142                 'null' => NULL_ALLOWED
143             ],
144             'repeatid' => [
145                 'type' => PARAM_INT,
146                 'optional' => true,
147                 'default' => null,
148                 'null' => NULL_ALLOWED
149             ],
150             'eventcount' => [
151                 'type' => PARAM_INT,
152                 'optional' => true,
153                 'default' => null,
154                 'null' => NULL_ALLOWED
155             ],
156             'modulename' => [
157                 'type' => PARAM_TEXT,
158                 'optional' => true,
159                 'default' => null,
160                 'null' => NULL_ALLOWED
161             ],
162             'instance' => [
163                 'type' => PARAM_INT,
164                 'optional' => true,
165                 'default' => null,
166                 'null' => NULL_ALLOWED
167             ],
168             'eventtype' => ['type' => PARAM_TEXT],
169             'timestart' => ['type' => PARAM_INT],
170             'timeduration' => ['type' => PARAM_INT],
171             'timesort' => ['type' => PARAM_INT],
172             'visible' => ['type' => PARAM_INT],
173             'timemodified' => ['type' => PARAM_INT],
174         ];
175     }
177     /**
178      * Return the list of additional properties.
179      *
180      * @return array
181      */
182     protected static function define_other_properties() {
183         return [
184             'icon' => [
185                 'type' => event_icon_exporter::read_properties_definition(),
186             ],
187             'category' => [
188                 'type' => coursecat_summary_exporter::read_properties_definition(),
189                 'optional' => true,
190             ],
191             'course' => [
192                 'type' => course_summary_exporter::read_properties_definition(),
193                 'optional' => true,
194             ],
195             'subscription' => [
196                 'type' => event_subscription_exporter::read_properties_definition(),
197                 'optional' => true,
198             ],
199             'canedit' => [
200                 'type' => PARAM_BOOL
201             ],
202             'candelete' => [
203                 'type' => PARAM_BOOL
204             ],
205             'deleteurl' => [
206                 'type' => PARAM_URL
207             ],
208             'editurl' => [
209                 'type' => PARAM_URL
210             ],
211             'viewurl' => [
212                 'type' => PARAM_URL
213             ],
214             'formattedtime' => [
215                 'type' => PARAM_RAW,
216             ],
217             'isactionevent' => [
218                 'type' => PARAM_BOOL
219             ],
220             'iscourseevent' => [
221                 'type' => PARAM_BOOL
222             ],
223             'iscategoryevent' => [
224                 'type' => PARAM_BOOL
225             ],
226             'groupname' => [
227                 'type' => PARAM_RAW,
228                 'optional' => true,
229                 'default' => null,
230                 'null' => NULL_ALLOWED
231             ],
232         ];
233     }
235     /**
236      * Get the additional values to inject while exporting.
237      *
238      * @param renderer_base $output The renderer.
239      * @return array Keys are the property names, values are their values.
240      */
241     protected function get_other_values(renderer_base $output) {
242         $values = [];
243         $event = $this->event;
244         $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
245         $context = $this->related['context'];
246         $course = $this->related['course'];
247         $values['isactionevent'] = false;
248         $values['iscourseevent'] = false;
249         $values['iscategoryevent'] = false;
250         if ($moduleproxy = $event->get_course_module()) {
251             $values['isactionevent'] = true;
252         } else if ($event->get_type() == 'course') {
253             $values['iscourseevent'] = true;
254         } else if ($event->get_type() == 'category') {
255             $values['iscategoryevent'] = true;
256         }
257         $timesort = $event->get_times()->get_sort_time()->getTimestamp();
258         $iconexporter = new event_icon_exporter($event, ['context' => $context]);
260         $values['icon'] = $iconexporter->export($output);
262         $subscriptionexporter = new event_subscription_exporter($event);
263         $values['subscription'] = $subscriptionexporter->export($output);
265         $proxy = $this->event->get_category();
266         if ($proxy && $proxy->get('id')) {
267             $category = $proxy->get_proxied_instance();
268             $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
269             $values['category'] = $categorysummaryexporter->export($output);
270         }
272         if ($course) {
273             $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
274             $values['course'] = $coursesummaryexporter->export($output);
275         }
277         $courseid = (!$course) ? SITEID : $course->id;
279         $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
280         $values['candelete'] = calendar_delete_event_allowed($legacyevent);
282         $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
283         $values['deleteurl'] = $deleteurl->out(false);
285         $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
286                 'course' => $courseid]);
287         $values['editurl'] = $editurl->out(false);
288         $viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
289                 'time' => $timesort]);
290         $viewurl->set_anchor('event_' . $event->get_id());
291         $values['viewurl'] = $viewurl->out(false);
292         $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
293                 $timesort);
295         if ($group = $event->get_group()) {
296             $values['groupname'] = format_string($group->get('name'), true,
297                 ['context' => \context_course::instance($event->get_course()->get('id'))]);
298         }
300         return $values;
301     }
303     /**
304      * Returns a list of objects that are related.
305      *
306      * @return array
307      */
308     protected static function define_related() {
309         return [
310             'context' => 'context',
311             'course' => 'stdClass?',
312         ];
313     }