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 Simey Lameze <simey@moodle.com>
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 core\external\exporter
;
32 use \core_calendar\local\event\container
;
35 * Class for displaying the day view.
37 * @package core_calendar
38 * @copyright 2017 Simey Lameze <simey@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class calendar_day_exporter
extends exporter
{
43 * @var \calendar_information $calendar The calendar to be rendered.
48 * @var moodle_url $url The URL for the day view page.
53 * Constructor for day exporter.
55 * @param \calendar_information $calendar The calendar being represented.
56 * @param array $related The related information
58 public function __construct(\calendar_information
$calendar, $related) {
59 $this->calendar
= $calendar;
61 parent
::__construct([], $related);
65 * Return the list of additional properties.
69 protected static function define_other_properties() {
72 'type' => calendar_event_exporter
::read_properties_definition(),
75 'defaulteventcontext' => [
79 'filter_selector' => [
90 'neweventtimestamp' => [
94 'type' => date_exporter
::read_properties_definition(),
97 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
101 'previousperiod' => [
102 'type' => date_exporter
::read_properties_definition(),
104 'previousperiodlink' => [
107 'previousperiodname' => [
108 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
113 'type' => date_exporter
::read_properties_definition(),
115 'nextperiodname' => [
116 // Note: We must use RAW here because the calendar type returns the formatted month name based on a
120 'nextperiodlink' => [
124 // The left arrow defined by the theme.
128 // The right arrow defined by the theme.
135 * Get the additional values to inject while exporting.
137 * @param renderer_base $output The renderer.
138 * @return array Keys are the property names, values are their values.
140 protected function get_other_values(renderer_base
$output) {
141 $timestamp = $this->calendar
->time
;
143 $cache = $this->related
['cache'];
144 $url = new moodle_url('/calendar/view.php', [
146 'time' => $timestamp,
148 if ($this->calendar
->course
&& SITEID
!== $this->calendar
->course
->id
) {
149 $url->param('course', $this->calendar
->course
->id
);
150 } else if ($this->calendar
->categoryid
) {
151 $url->param('category', $this->calendar
->categoryid
);
154 $return['events'] = array_map(function($event) use ($cache, $output, $url) {
155 $context = $cache->get_context($event);
156 $course = $cache->get_course($event);
157 $moduleinstance = $cache->get_module_instance($event);
158 $exporter = new calendar_event_exporter($event, [
159 'context' => $context,
161 'moduleinstance' => $moduleinstance,
163 'type' => $this->related
['type'],
164 'today' => $this->calendar
->time
,
167 $data = $exporter->export($output);
169 // We need to override default formatted time because it differs from day view.
170 // Formatted time for day view adds a link to the day view.
171 $legacyevent = container
::get_event_mapper()->from_event_to_legacy_event($event);
172 $data->formattedtime
= calendar_format_event_time($legacyevent, time(), null);
175 }, $this->related
['events']);
177 if ($context = $this->get_default_add_context()) {
178 $return['defaulteventcontext'] = $context->id
;
181 if ($this->calendar
->categoryid
) {
182 $return['categoryid'] = $this->calendar
->categoryid
;
185 $return['filter_selector'] = $this->get_course_filter_selector($output);
186 $return['courseid'] = $this->calendar
->courseid
;
188 $previousperiod = $this->get_previous_day_data();
189 $nextperiod = $this->get_next_day_data();
190 $date = $this->related
['type']->timestamp_to_date_array($this->calendar
->time
);
192 $nextperiodlink = new moodle_url($this->url
);
193 $nextperiodlink->param('time', $nextperiod[0]);
195 $previousperiodlink = new moodle_url($this->url
);
196 $previousperiodlink->param('time', $previousperiod[0]);
198 $days = calendar_get_days();
199 $return['date'] = (new date_exporter($date))->export($output);
200 $return['periodname'] = userdate($this->calendar
->time
, get_string('strftimedaydate'));
201 $return['previousperiod'] = (new date_exporter($previousperiod))->export($output);
202 $return['previousperiodname'] = $days[$previousperiod['wday']]['fullname'];
203 $return['previousperiodlink'] = $previousperiodlink->out(false);
204 $return['nextperiod'] = (new date_exporter($nextperiod))->export($output);
205 $return['nextperiodname'] = $days[$nextperiod['wday']]['fullname'];
206 $return['nextperiodlink'] = $nextperiodlink->out(false);
207 $return['larrow'] = $output->larrow();
208 $return['rarrow'] = $output->rarrow();
210 // Need to account for user's timezone.
211 $usernow = usergetdate(time());
212 $today = new \
DateTimeImmutable();
213 $neweventtimestamp = $today->setTimestamp($date[0])->setTime(
218 $return['neweventtimestamp'] = $neweventtimestamp->getTimestamp();
224 * Get the default context for use when adding a new event.
226 * @return null|\context
228 protected function get_default_add_context() {
229 if (calendar_user_can_add_event($this->calendar
->course
)) {
230 return \context_course
::instance($this->calendar
->course
->id
);
237 * Get the course filter selector.
239 * @param renderer_base $output
240 * @return string The html code for the course filter selector.
242 protected function get_course_filter_selector(renderer_base
$output) {
243 $langstr = get_string('dayviewfor', 'calendar');
244 return $output->course_filter_selector($this->url
, $langstr, $this->calendar
->course
->id
);
248 * Returns a list of objects that are related.
252 protected static function define_related() {
254 'events' => '\core_calendar\local\event\entities\event_interface[]',
255 'cache' => '\core_calendar\external\events_related_objects_cache',
256 'type' => '\core_calendar\type_base',
261 * Get the previous day timestamp.
263 * @return int The previous day timestamp.
265 protected function get_previous_day_data() {
266 $type = $this->related
['type'];
267 $time = $type->get_prev_day($this->calendar
->time
);
269 return $type->timestamp_to_date_array($time);
273 * Get the next day timestamp.
275 * @return int The next day timestamp.
277 protected function get_next_day_data() {
278 $type = $this->related
['type'];
279 $time = $type->get_next_day($this->calendar
->time
);
281 return $type->timestamp_to_date_array($time);