MDL-60959 calendar: remove duplicate course export in event exporter
[moodle.git] / calendar / classes / external / week_day_exporter.php
CommitLineData
fb9be271
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 the day on month view.
19 *
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
23 */
24
25namespace core_calendar\external;
26
27defined('MOODLE_INTERNAL') || die();
28
29use renderer_base;
30use moodle_url;
31
32/**
33 * Class for displaying the day on month view.
34 *
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
38 */
39class week_day_exporter extends day_exporter {
40
146d3713
SL
41 /**
42 * Return the list of properties.
43 *
44 * @return array
45 */
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,
53 ],
54 'isweekend' => [
55 'type' => PARAM_BOOL,
56 'default' => false,
57 ],
58 ]);
59
60 return $return;
61 }
fb9be271
AN
62 /**
63 * Return the list of additional properties.
64 *
65 * @return array
66 */
67 protected static function define_other_properties() {
68 $return = parent::define_other_properties();
69 $return = array_merge($return, [
fb9be271
AN
70 'popovertitle' => [
71 'type' => PARAM_RAW,
72 'default' => '',
73 ],
fb9be271
AN
74 ]);
75
76 return $return;
77 }
78
79 /**
80 * Get the additional values to inject while exporting.
81 *
82 * @param renderer_base $output The renderer.
83 * @return array Keys are the property names, values are their values.
84 */
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']
96 );
97
146d3713 98 $return = parent::get_other_values($output);
fb9be271
AN
99
100 $url = new moodle_url('/calendar/view.php', [
101 'view' => 'day',
102 'time' => $timestamp,
5ba6507e
AN
103 ]);
104
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);
109 }
fb9be271 110
146d3713 111 $return['viewdaylink'] = $url->out(false);
5ba6507e 112
146d3713
SL
113 if ($popovertitle = $this->get_popover_title()) {
114 $return['popovertitle'] = $popovertitle;
115 }
fb9be271
AN
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 $exporter = new calendar_event_exporter($event, [
121 'context' => $context,
122 'course' => $course,
123 'daylink' => $url,
124 'type' => $this->related['type'],
125 'today' => $this->data[0],
126 ]);
127
128 return $exporter;
129 }, $this->related['events']);
130
131 $return['events'] = array_map(function($exporter) use ($output) {
132 return $exporter->export($output);
133 }, $eventexporters);
134
135 if ($popovertitle = $this->get_popover_title()) {
136 $return['popovertitle'] = $popovertitle;
137 }
138
139 $return['calendareventtypes'] = array_map(function($exporter) {
140 return $exporter->get_calendar_event_type();
141 }, $eventexporters);
142 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
143
144 $return['haslastdayofevent'] = false;
145 foreach ($return['events'] as $event) {
146 if ($event->islastday) {
147 $return['haslastdayofevent'] = true;
148 break;
149 }
150 }
151
152 return $return;
153 }
154
155 /**
156 * Returns a list of objects that are related.
157 *
158 * @return array
159 */
160 protected static function define_related() {
161 return [
162 'events' => '\core_calendar\local\event\entities\event_interface[]',
163 'cache' => '\core_calendar\external\events_related_objects_cache',
164 'type' => '\core_calendar\type_base',
165 ];
166 }
167
168 /**
169 * Get the title for this popover.
170 *
171 * @return string
172 */
173 protected function get_popover_title() {
174 $title = null;
175
176 $userdate = userdate($this->data[0], get_string('strftimedayshort'));
177 if (count($this->related['events'])) {
178 $title = get_string('eventsfor', 'calendar', $userdate);
179 } else if ($this->data['istoday']) {
180 $title = $userdate;
181 }
182
183 if ($this->data['istoday']) {
184 $title = get_string('todayplustitle', 'calendar', $userdate);
185 }
186
187 return $title;
188 }
189}