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 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();
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 {
42 * Return the list of properties.
46 protected static function define_properties() {
47 $return = parent::define_properties();
48 $return = array_merge($return, [
49 // These are additional params.
63 * Return the list of additional properties.
67 protected static function define_other_properties() {
68 $return = parent::define_other_properties();
69 $return = array_merge($return, [
73 'neweventtimestamp' => [
80 'calendareventtypes' => [
88 'haslastdayofevent' => [
98 * Get the additional values to inject while exporting.
100 * @param renderer_base $output The renderer.
101 * @return array Keys are the property names, values are their values.
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(
116 $return = parent::get_other_values($output);
118 $url = new moodle_url('/calendar/view.php', [
120 'time' => $timestamp,
121 'course' => $this->calendar->course->id,
124 $return['viewdaylink'] = $url->out(false);
125 if ($popovertitle = $this->get_popover_title()) {
126 $return['popovertitle'] = $popovertitle;
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,
136 'type' => $this->related['type'],
137 'today' => $this->data[0],
141 }, $this->related['events']);
143 $return['events'] = array_map(function($exporter) use ($output) {
144 return $exporter->export($output);
147 if ($popovertitle = $this->get_popover_title()) {
148 $return['popovertitle'] = $popovertitle;
151 $return['calendareventtypes'] = array_map(function($exporter) {
152 return $exporter->get_calendar_event_type();
154 $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
156 $return['haslastdayofevent'] = false;
157 foreach ($return['events'] as $event) {
158 if ($event->islastday) {
159 $return['haslastdayofevent'] = true;
168 * Returns a list of objects that are related.
172 protected static function define_related() {
174 'events' => '\core_calendar\local\event\entities\event_interface[]',
175 'cache' => '\core_calendar\external\events_related_objects_cache',
176 'type' => '\core_calendar\type_base',
181 * Get the title for this popover.
185 protected function get_popover_title() {
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']) {
195 if ($this->data['istoday']) {
196 $title = get_string('todayplustitle', 'calendar', $userdate);