MDL-60963 calendar: use related cache for module instances in export
[moodle.git] / calendar / classes / external / day_exporter.php
blob7e888b84d44d983103de6c42fa14b8caa4b4e43a
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.
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 the day view.
20 * @package core_calendar
21 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_calendar\external;
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/calendar/lib.php');
31 use core\external\exporter;
32 use renderer_base;
33 use moodle_url;
35 /**
36 * Class for displaying the day view.
38 * @package core_calendar
39 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class day_exporter extends exporter {
44 /**
45 * @var \calendar_information $calendar The calendar being displayed.
47 protected $calendar;
49 /**
50 * @var moodle_url
52 protected $url;
53 /**
54 * Constructor.
56 * @param \calendar_information $calendar The calendar information for the period being displayed
57 * @param mixed $data Either an stdClass or an array of values.
58 * @param array $related Related objects.
60 public function __construct(\calendar_information $calendar, $data, $related) {
61 $this->calendar = $calendar;
63 $url = new moodle_url('/calendar/view.php', [
64 'view' => 'day',
65 'time' => $calendar->time,
66 ]);
68 if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
69 $url->param('course', $this->calendar->course->id);
70 } else if ($this->calendar->categoryid) {
71 $url->param('category', $this->calendar->categoryid);
74 $this->url = $url;
76 parent::__construct($data, $related);
79 /**
80 * Return the list of properties.
82 * @return array
84 protected static function define_properties() {
85 // These are the default properties as returned by getuserdate()
86 // but without the formatted month and week names.
87 return [
88 'seconds' => [
89 'type' => PARAM_INT,
91 'minutes' => [
92 'type' => PARAM_INT,
94 'hours' => [
95 'type' => PARAM_INT,
97 'mday' => [
98 'type' => PARAM_INT,
100 'wday' => [
101 'type' => PARAM_INT,
103 'year' => [
104 'type' => PARAM_INT,
106 'yday' => [
107 'type' => PARAM_INT,
113 * Return the list of additional properties.
115 * @return array
117 protected static function define_other_properties() {
118 return [
119 'timestamp' => [
120 'type' => PARAM_INT,
122 'neweventtimestamp' => [
123 'type' => PARAM_INT,
125 'viewdaylink' => [
126 'type' => PARAM_URL,
127 'optional' => true,
129 'events' => [
130 'type' => calendar_event_exporter::read_properties_definition(),
131 'multiple' => true,
133 'hasevents' => [
134 'type' => PARAM_BOOL,
135 'default' => false,
137 'calendareventtypes' => [
138 'type' => PARAM_RAW,
139 'multiple' => true,
141 'previousperiod' => [
142 'type' => PARAM_INT,
144 'nextperiod' => [
145 'type' => PARAM_INT,
147 'navigation' => [
148 'type' => PARAM_RAW,
150 'haslastdayofevent' => [
151 'type' => PARAM_BOOL,
152 'default' => false,
158 * Get the additional values to inject while exporting.
160 * @param renderer_base $output The renderer.
161 * @return array Keys are the property names, values are their values.
163 protected function get_other_values(renderer_base $output) {
164 $daytimestamp = $this->calendar->time;
165 $timestamp = $this->data[0];
166 // Need to account for user's timezone.
167 $usernow = usergetdate(time());
168 $today = new \DateTimeImmutable();
169 // The start time should use the day's date but the current
170 // time of the day (adjusted for user's timezone).
171 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
172 $usernow['hours'],
173 $usernow['minutes'],
174 $usernow['seconds']
177 $return = [
178 'timestamp' => $timestamp,
179 'neweventtimestamp' => $neweventstarttime->getTimestamp(),
180 'previousperiod' => $this->get_previous_day_timestamp($daytimestamp),
181 'nextperiod' => $this->get_next_day_timestamp($daytimestamp),
182 'navigation' => $this->get_navigation(),
183 'viewdaylink' => $this->url->out(false),
187 $cache = $this->related['cache'];
188 $eventexporters = array_map(function($event) use ($cache, $output) {
189 $context = $cache->get_context($event);
190 $course = $cache->get_course($event);
191 $moduleinstance = $cache->get_module_instance($event);
192 $exporter = new calendar_event_exporter($event, [
193 'context' => $context,
194 'course' => $course,
195 'moduleinstance' => $moduleinstance,
196 'daylink' => $this->url,
197 'type' => $this->related['type'],
198 'today' => $this->data[0],
201 return $exporter;
202 }, $this->related['events']);
204 $return['events'] = array_map(function($exporter) use ($output) {
205 return $exporter->export($output);
206 }, $eventexporters);
208 $return['hasevents'] = !empty($return['events']);
210 $return['calendareventtypes'] = array_map(function($exporter) {
211 return $exporter->get_calendar_event_type();
212 }, $eventexporters);
213 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
215 $return['haslastdayofevent'] = false;
216 foreach ($return['events'] as $event) {
217 if ($event->islastday) {
218 $return['haslastdayofevent'] = true;
219 break;
223 return $return;
227 * Returns a list of objects that are related.
229 * @return array
231 protected static function define_related() {
232 return [
233 'events' => '\core_calendar\local\event\entities\event_interface[]',
234 'cache' => '\core_calendar\external\events_related_objects_cache',
235 'type' => '\core_calendar\type_base',
240 * Get the previous day timestamp.
242 * @param int $daytimestamp The current day timestamp.
243 * @return int The previous day timestamp.
245 protected function get_previous_day_timestamp($daytimestamp) {
246 return $this->related['type']->get_prev_day($daytimestamp);
250 * Get the next day timestamp.
252 * @param int $daytimestamp The current day timestamp.
253 * @return int The next day timestamp.
255 protected function get_next_day_timestamp($daytimestamp) {
256 return $this->related['type']->get_next_day($daytimestamp);
260 * Get the calendar navigation controls.
262 * @return string The html code to the calendar top navigation.
264 protected function get_navigation() {
265 return calendar_top_controls('day', [
266 'id' => $this->calendar->courseid,
267 'time' => $this->calendar->time,