MDL-59890 coursecat: Add helper to fetch nested view of coursecat
[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, [
70 'timestamp' => [
71 'type' => PARAM_INT,
72 ],
73 'neweventtimestamp' => [
74 'type' => PARAM_INT,
75 ],
76 'viewdaylink' => [
77 'type' => PARAM_URL,
78 'optional' => true,
79 ],
80 'calendareventtypes' => [
81 'type' => PARAM_RAW,
82 'multiple' => true,
83 ],
84 'popovertitle' => [
85 'type' => PARAM_RAW,
86 'default' => '',
87 ],
88 'haslastdayofevent' => [
89 'type' => PARAM_BOOL,
90 'default' => false,
91 ],
92 ]);
93
94 return $return;
95 }
96
97 /**
98 * Get the additional values to inject while exporting.
99 *
100 * @param renderer_base $output The renderer.
101 * @return array Keys are the property names, values are their values.
102 */
103 protected function get_other_values(renderer_base $output) {
104 $timestamp = $this->data[0];
105 // Need to account for user's timezone.
106 $usernow = usergetdate(time());
107 $today = new \DateTimeImmutable();
108 // The start time should use the day's date but the current
109 // time of the day (adjusted for user's timezone).
110 $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
111 $usernow['hours'],
112 $usernow['minutes'],
113 $usernow['seconds']
114 );
115
146d3713 116 $return = parent::get_other_values($output);
fb9be271
AN
117
118 $url = new moodle_url('/calendar/view.php', [
119 'view' => 'day',
120 'time' => $timestamp,
121 'course' => $this->calendar->course->id,
146d3713 122 ]);
fb9be271 123
146d3713
SL
124 $return['viewdaylink'] = $url->out(false);
125 if ($popovertitle = $this->get_popover_title()) {
126 $return['popovertitle'] = $popovertitle;
127 }
fb9be271
AN
128 $cache = $this->related['cache'];
129 $eventexporters = array_map(function($event) use ($cache, $output, $url) {
130 $context = $cache->get_context($event);
131 $course = $cache->get_course($event);
132 $exporter = new calendar_event_exporter($event, [
133 'context' => $context,
134 'course' => $course,
135 'daylink' => $url,
136 'type' => $this->related['type'],
137 'today' => $this->data[0],
138 ]);
139
140 return $exporter;
141 }, $this->related['events']);
142
143 $return['events'] = array_map(function($exporter) use ($output) {
144 return $exporter->export($output);
145 }, $eventexporters);
146
147 if ($popovertitle = $this->get_popover_title()) {
148 $return['popovertitle'] = $popovertitle;
149 }
150
151 $return['calendareventtypes'] = array_map(function($exporter) {
152 return $exporter->get_calendar_event_type();
153 }, $eventexporters);
154 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
155
156 $return['haslastdayofevent'] = false;
157 foreach ($return['events'] as $event) {
158 if ($event->islastday) {
159 $return['haslastdayofevent'] = true;
160 break;
161 }
162 }
163
164 return $return;
165 }
166
167 /**
168 * Returns a list of objects that are related.
169 *
170 * @return array
171 */
172 protected static function define_related() {
173 return [
174 'events' => '\core_calendar\local\event\entities\event_interface[]',
175 'cache' => '\core_calendar\external\events_related_objects_cache',
176 'type' => '\core_calendar\type_base',
177 ];
178 }
179
180 /**
181 * Get the title for this popover.
182 *
183 * @return string
184 */
185 protected function get_popover_title() {
186 $title = null;
187
188 $userdate = userdate($this->data[0], get_string('strftimedayshort'));
189 if (count($this->related['events'])) {
190 $title = get_string('eventsfor', 'calendar', $userdate);
191 } else if ($this->data['istoday']) {
192 $title = $userdate;
193 }
194
195 if ($this->data['istoday']) {
196 $title = get_string('todayplustitle', 'calendar', $userdate);
197 }
198
199 return $title;
200 }
201}