2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
;
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
{
45 * @var \calendar_information $calendar The calendar being displayed.
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', [
65 'time' => $calendar->time
,
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
);
76 parent
::__construct($data, $related);
80 * Return the list of properties.
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.
113 * Return the list of additional properties.
117 protected static function define_other_properties() {
122 'neweventtimestamp' => [
130 'type' => calendar_event_exporter
::read_properties_definition(),
134 'type' => PARAM_BOOL
,
137 'calendareventtypes' => [
141 'previousperiod' => [
150 'haslastdayofevent' => [
151 'type' => PARAM_BOOL
,
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(
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,
195 'moduleinstance' => $moduleinstance,
196 'daylink' => $this->url
,
197 'type' => $this->related
['type'],
198 'today' => $this->data
[0],
202 }, $this->related
['events']);
204 $return['events'] = array_map(function($exporter) use ($output) {
205 return $exporter->export($output);
208 $return['hasevents'] = !empty($return['events']);
210 $return['calendareventtypes'] = array_map(function($exporter) {
211 return $exporter->get_calendar_event_type();
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;
227 * Returns a list of objects that are related.
231 protected static function define_related() {
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
,