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 upcoming 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_upcoming_exporter
extends exporter
{
43 * @var \calendar_information $calendar The calendar to be rendered.
48 * @var moodle_url $url The URL for the upcoming view page.
53 * Constructor for upcoming 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' => [
97 * Get the additional values to inject while exporting.
99 * @param renderer_base $output The renderer.
100 * @return array Keys are the property names, values are their values.
102 protected function get_other_values(renderer_base
$output) {
103 $timestamp = $this->calendar
->time
;
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
,
112 $return['isloggedin'] = isloggedin();
113 $return['events'] = array_map(function($event) use ($cache, $output, $url) {
114 $context = $cache->get_context($event);
115 $course = $cache->get_course($event);
116 $moduleinstance = $cache->get_module_instance($event);
117 $exporter = new calendar_event_exporter($event, [
118 'context' => $context,
120 'moduleinstance' => $moduleinstance,
122 'type' => $this->related
['type'],
123 'today' => $this->calendar
->time
,
126 $data = $exporter->export($output);
128 // We need to override default formatted time because it differs from day view.
129 // Formatted time for upcoming view adds a link to the day view.
130 $legacyevent = container
::get_event_mapper()->from_event_to_legacy_event($event);
131 $data->formattedtime
= calendar_format_event_time($legacyevent, time(), null);
134 }, $this->related
['events']);
136 if ($context = $this->get_default_add_context()) {
137 $return['defaulteventcontext'] = $context->id
;
139 $return['filter_selector'] = $this->get_course_filter_selector($output);
140 $return['courseid'] = $this->calendar
->courseid
;
142 if ($this->calendar
->categoryid
) {
143 $return['categoryid'] = $this->calendar
->categoryid
;
150 * Get the default context for use when adding a new event.
152 * @return null|\context
154 protected function get_default_add_context() {
155 if (calendar_user_can_add_event($this->calendar
->course
)) {
156 return \context_course
::instance($this->calendar
->course
->id
);
163 * Get the course filter selector.
165 * @param renderer_base $output
166 * @return string The html code for the course filter selector.
168 protected function get_course_filter_selector(renderer_base
$output) {
169 $langstr = get_string('upcomingeventsfor', 'calendar');
170 return $output->course_filter_selector($this->url
, $langstr, $this->calendar
->course
->id
);
174 * Returns a list of objects that are related.
178 protected static function define_related() {
180 'events' => '\core_calendar\local\event\entities\event_interface[]',
181 'cache' => '\core_calendar\external\events_related_objects_cache',
182 'type' => '\core_calendar\type_base',