MDL-60963 calendar: use related cache for module instances in export
[moodle.git] / calendar / classes / external / week_day_exporter.php
blob49980d502c518fe3c9ae9aa8270b9aaa2ef7a959
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 on month 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 use renderer_base;
30 use moodle_url;
32 /**
33 * Class for displaying the day on month view.
35 * @package core_calendar
36 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class week_day_exporter extends day_exporter {
41 /**
42 * Return the list of properties.
44 * @return array
46 protected static function define_properties() {
47 $return = parent::define_properties();
48 $return = array_merge($return, [
49 // These are additional params.
50 'istoday' => [
51 'type' => PARAM_BOOL,
52 'default' => false,
54 'isweekend' => [
55 'type' => PARAM_BOOL,
56 'default' => false,
58 ]);
60 return $return;
62 /**
63 * Return the list of additional properties.
65 * @return array
67 protected static function define_other_properties() {
68 $return = parent::define_other_properties();
69 $return = array_merge($return, [
70 'popovertitle' => [
71 'type' => PARAM_RAW,
72 'default' => '',
74 ]);
76 return $return;
79 /**
80 * Get the additional values to inject while exporting.
82 * @param renderer_base $output The renderer.
83 * @return array Keys are the property names, values are their values.
85 protected function get_other_values(renderer_base $output) {
86 $timestamp = $this->data[0];
87 // Need to account for user's timezone.
88 $usernow = usergetdate(time());
89 $today = new \DateTimeImmutable();
90 // The start time should use the day's date but the current
91 // time of the day (adjusted for user's timezone).
92 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
93 $usernow['hours'],
94 $usernow['minutes'],
95 $usernow['seconds']
98 $return = parent::get_other_values($output);
100 $url = new moodle_url('/calendar/view.php', [
101 'view' => 'day',
102 'time' => $timestamp,
105 if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
106 $url->param('course', $this->calendar->course->id);
107 } else if ($this->calendar->categoryid) {
108 $url->param('category', $this->calendar->categoryid);
111 $return['viewdaylink'] = $url->out(false);
113 if ($popovertitle = $this->get_popover_title()) {
114 $return['popovertitle'] = $popovertitle;
116 $cache = $this->related['cache'];
117 $eventexporters = array_map(function($event) use ($cache, $output, $url) {
118 $context = $cache->get_context($event);
119 $course = $cache->get_course($event);
120 $moduleinstance = $cache->get_module_instance($event);
121 $exporter = new calendar_event_exporter($event, [
122 'context' => $context,
123 'course' => $course,
124 'moduleinstance' => $moduleinstance,
125 'daylink' => $url,
126 'type' => $this->related['type'],
127 'today' => $this->data[0],
130 return $exporter;
131 }, $this->related['events']);
133 $return['events'] = array_map(function($exporter) use ($output) {
134 return $exporter->export($output);
135 }, $eventexporters);
137 if ($popovertitle = $this->get_popover_title()) {
138 $return['popovertitle'] = $popovertitle;
141 $return['calendareventtypes'] = array_map(function($exporter) {
142 return $exporter->get_calendar_event_type();
143 }, $eventexporters);
144 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
146 $return['haslastdayofevent'] = false;
147 foreach ($return['events'] as $event) {
148 if ($event->islastday) {
149 $return['haslastdayofevent'] = true;
150 break;
154 return $return;
158 * Returns a list of objects that are related.
160 * @return array
162 protected static function define_related() {
163 return [
164 'events' => '\core_calendar\local\event\entities\event_interface[]',
165 'cache' => '\core_calendar\external\events_related_objects_cache',
166 'type' => '\core_calendar\type_base',
171 * Get the title for this popover.
173 * @return string
175 protected function get_popover_title() {
176 $title = null;
178 $userdate = userdate($this->data[0], get_string('strftimedayshort'));
179 if (count($this->related['events'])) {
180 $title = get_string('eventsfor', 'calendar', $userdate);
181 } else if ($this->data['istoday']) {
182 $title = $userdate;
185 if ($this->data['istoday']) {
186 $title = get_string('todayplustitle', 'calendar', $userdate);
189 return $title;