MDL-60963 calendar: use related cache for module instances in export
[moodle.git] / calendar / classes / external / calendar_upcoming_exporter.php
blobab05aefecca4fc0f821e21f8632b3f54612b435e
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.
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/>.
17 /**
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;
30 use renderer_base;
31 use moodle_url;
32 use \core_calendar\local\event\container;
34 /**
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 {
42 /**
43 * @var \calendar_information $calendar The calendar to be rendered.
45 protected $calendar;
47 /**
48 * @var moodle_url $url The URL for the upcoming view page.
50 protected $url;
52 /**
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);
64 /**
65 * Return the list of additional properties.
67 * @return array
69 protected static function define_other_properties() {
70 return [
71 'events' => [
72 'type' => calendar_event_exporter::read_properties_definition(),
73 'multiple' => true,
75 'defaulteventcontext' => [
76 'type' => PARAM_INT,
77 'default' => 0,
79 'filter_selector' => [
80 'type' => PARAM_RAW,
82 'courseid' => [
83 'type' => PARAM_INT,
85 'categoryid' => [
86 'type' => PARAM_INT,
87 'optional' => true,
88 'default' => 0,
90 'isloggedin' => [
91 'type' => PARAM_BOOL,
96 /**
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,
111 $this->url = $url;
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,
119 'course' => $course,
120 'moduleinstance' => $moduleinstance,
121 'daylink' => $url,
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);
133 return $data;
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;
146 return $return;
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);
159 return null;
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.
176 * @return array
178 protected static function define_related() {
179 return [
180 'events' => '\core_calendar\local\event\entities\event_interface[]',
181 'cache' => '\core_calendar\external\events_related_objects_cache',
182 'type' => '\core_calendar\type_base',