MDL-60766 block_calendar_upcoming: stop summary modal not logged in
[moodle.git] / calendar / classes / external / calendar_upcoming_exporter.php
CommitLineData
84ad7386
SL
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 upcoming view.
19 *
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
23 */
24
25namespace core_calendar\external;
26
27defined('MOODLE_INTERNAL') || die();
28
29use core\external\exporter;
30use renderer_base;
31use moodle_url;
32use \core_calendar\local\event\container;
33
34/**
35 * Class for displaying the day view.
36 *
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
40 */
41class calendar_upcoming_exporter extends exporter {
42 /**
43 * @var \calendar_information $calendar The calendar to be rendered.
44 */
45 protected $calendar;
46
47 /**
48 * @var moodle_url $url The URL for the upcoming view page.
49 */
50 protected $url;
51
52 /**
53 * Constructor for upcoming exporter.
54 *
55 * @param \calendar_information $calendar The calendar being represented.
56 * @param array $related The related information
57 */
58 public function __construct(\calendar_information $calendar, $related) {
59 $this->calendar = $calendar;
60
61 parent::__construct([], $related);
62 }
63
64 /**
65 * Return the list of additional properties.
66 *
67 * @return array
68 */
69 protected static function define_other_properties() {
70 return [
71 'events' => [
72 'type' => calendar_event_exporter::read_properties_definition(),
73 'multiple' => true,
74 ],
75 'defaulteventcontext' => [
76 'type' => PARAM_INT,
0d049757 77 'default' => 0,
84ad7386
SL
78 ],
79 'filter_selector' => [
80 'type' => PARAM_RAW,
81 ],
82 'courseid' => [
83 'type' => PARAM_INT,
84 ],
d523cbcb
AN
85 'categoryid' => [
86 'type' => PARAM_INT,
87 'optional' => true,
88 'default' => 0,
89 ],
08b75584
RW
90 'isloggedin' => [
91 'type' => PARAM_BOOL,
92 ],
84ad7386
SL
93 ];
94 }
95
96 /**
97 * Get the additional values to inject while exporting.
98 *
99 * @param renderer_base $output The renderer.
100 * @return array Keys are the property names, values are their values.
101 */
102 protected function get_other_values(renderer_base $output) {
103 $timestamp = $this->calendar->time;
104
105 $cache = $this->related['cache'];
106 $url = new moodle_url('/calendar/view.php', [
107 'view' => 'upcoming',
108 'time' => $timestamp,
109 'course' => $this->calendar->course->id,
110 ]);
111 $this->url = $url;
08b75584 112 $return['isloggedin'] = isloggedin();
84ad7386
SL
113 $return['events'] = array_map(function($event) use ($cache, $output, $url) {
114 $context = $cache->get_context($event);
115 $course = $cache->get_course($event);
116 $exporter = new calendar_event_exporter($event, [
117 'context' => $context,
118 'course' => $course,
119 'daylink' => $url,
120 'type' => $this->related['type'],
121 'today' => $this->calendar->time,
122 ]);
123
124 $data = $exporter->export($output);
125
126 // We need to override default formatted time because it differs from day view.
127 // Formatted time for upcoming view adds a link to the day view.
128 $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
129 $data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
130
131 return $data;
132 }, $this->related['events']);
133
134 if ($context = $this->get_default_add_context()) {
135 $return['defaulteventcontext'] = $context->id;
136 }
137 $return['filter_selector'] = $this->get_course_filter_selector($output);
138 $return['courseid'] = $this->calendar->courseid;
d523cbcb
AN
139
140 if ($this->calendar->categoryid) {
141 $return['categoryid'] = $this->calendar->categoryid;
142 }
143
84ad7386
SL
144 return $return;
145 }
146
147 /**
148 * Get the default context for use when adding a new event.
149 *
150 * @return null|\context
151 */
152 protected function get_default_add_context() {
153 if (calendar_user_can_add_event($this->calendar->course)) {
154 return \context_course::instance($this->calendar->course->id);
155 }
156
157 return null;
158 }
159
160 /**
161 * Get the course filter selector.
162 *
163 * @param renderer_base $output
164 * @return string The html code for the course filter selector.
165 */
166 protected function get_course_filter_selector(renderer_base $output) {
167 $langstr = get_string('upcomingeventsfor', 'calendar');
72c4cacf 168 return $output->course_filter_selector($this->url, $langstr, $this->calendar->course->id);
84ad7386
SL
169 }
170
171 /**
172 * Returns a list of objects that are related.
173 *
174 * @return array
175 */
176 protected static function define_related() {
177 return [
178 'events' => '\core_calendar\local\event\entities\event_interface[]',
179 'cache' => '\core_calendar\external\events_related_objects_cache',
180 'type' => '\core_calendar\type_base',
181 ];
182 }
183}