MDL-65318 core_calendar: make event details consistent across pages
[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
6fb75156
SL
29require_once($CFG->dirroot . "/calendar/lib.php");
30
53cbac18 31use \core_calendar\local\event\entities\action_event_interface;
6fb75156 32use \core_calendar\local\event\container;
984355ce
RW
33use \core_course\external\course_summary_exporter;
34use \renderer_base;
35
36/**
37 * Class for displaying a calendar event.
38 *
39 * @package core_calendar
40 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
6ad2bfd1 43class event_exporter extends event_exporter_base {
984355ce
RW
44
45 /**
46 * Return the list of additional properties.
47 *
48 * @return array
49 */
50 protected static function define_other_properties() {
6ad2bfd1 51 $values = parent::define_other_properties();
0085b0ea 52
6ad2bfd1
AN
53 $values['url'] = ['type' => PARAM_URL];
54 $values['action'] = [
55 'type' => event_action_exporter::read_properties_definition(),
56 'optional' => true,
984355ce 57 ];
a67bbe50 58 $values['calendareventtype'] = ['type' => PARAM_TEXT];
6ad2bfd1 59 return $values;
984355ce
RW
60 }
61
62 /**
63 * Get the additional values to inject while exporting.
64 *
65 * @param renderer_base $output The renderer.
66 * @return array Keys are the property names, values are their values.
67 */
68 protected function get_other_values(renderer_base $output) {
6ad2bfd1
AN
69 $values = parent::get_other_values($output);
70
00d2d344
SL
71 global $CFG;
72 require_once($CFG->dirroot.'/course/lib.php');
73
984355ce
RW
74 $event = $this->event;
75 $context = $this->related['context'];
a67bbe50 76 $values['calendareventtype'] = $event->get_type();
cd2c4b85
MG
77 if ($moduleproxy = $event->get_course_module()) {
78 $modulename = $moduleproxy->get('modname');
79 $moduleid = $moduleproxy->get('id');
80 $url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
c34e2002 81
c34e2002
SL
82 // Build edit event url for action events.
83 $params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
84 $editurl = new \moodle_url('/course/mod.php', $params);
85 $values['editurl'] = $editurl->out(false);
a67bbe50
SL
86 // Activity events are normalised to "look" like course events.
87 $values['calendareventtype'] = 'course';
0085b0ea 88 } else if ($event->get_type() == 'category') {
0085b0ea 89 $url = $event->get_category()->get_proxied_instance()->get_view_link();
00d2d344 90 } else if ($event->get_type() == 'course') {
00d2d344 91 $url = \course_get_url($this->related['course'] ?: SITEID);
cd2c4b85
MG
92 } else {
93 // TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
cd2c4b85
MG
94 $url = \course_get_url($this->related['course'] ?: SITEID);
95 }
984355ce 96 $values['url'] = $url->out(false);
984355ce 97
d2111dbc
TS
98 // Override default formatted time to make sure the date portion of the time is always rendered.
99 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
100 $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false);
101
984355ce 102 if ($event instanceof action_event_interface) {
6b7b39fe
JP
103 $actionrelated = [
104 'context' => $context,
105 'event' => $event
106 ];
107 $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
984355ce
RW
108 $values['action'] = $actionexporter->export($output);
109 }
110
4df03a27 111
a1e166e7 112
6ad2bfd1 113 return $values;
984355ce
RW
114 }
115}