Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_calendar\external;
18
 
19
use core\external\exporter;
20
use core_date;
21
use DateTimeImmutable;
22
use renderer_base;
23
use moodle_url;
24
use core_calendar\local\event\container;
25
 
26
/**
27
 * Class for displaying the day view.
28
 *
29
 * @package   core_calendar
30
 * @copyright 2017 Simey Lameze <simey@moodle.com>
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class calendar_day_exporter extends exporter {
34
    /**
35
     * @var \calendar_information $calendar The calendar to be rendered.
36
     */
37
    protected $calendar;
38
 
39
    /**
40
     * @var moodle_url $url The URL for the day view page.
41
     */
42
    protected $url;
43
 
44
    /**
45
     * Constructor for day exporter.
46
     *
47
     * @param \calendar_information $calendar The calendar being represented.
48
     * @param array $related The related information
49
     */
50
    public function __construct(\calendar_information $calendar, $related) {
51
        $this->calendar = $calendar;
52
 
53
        parent::__construct([], $related);
54
    }
55
 
56
    /**
57
     * Return the list of additional properties.
58
     *
59
     * @return array
60
     */
61
    protected static function define_other_properties() {
62
        return [
63
            'events' => [
64
                'type' => calendar_event_exporter::read_properties_definition(),
65
                'multiple' => true,
66
            ],
67
            'defaulteventcontext' => [
68
                'type' => PARAM_INT,
69
                'default' => 0,
70
            ],
71
            'filter_selector' => [
72
                'type' => PARAM_RAW,
73
            ],
74
            'courseid' => [
75
                'type' => PARAM_INT,
76
            ],
77
            'categoryid' => [
78
                'type' => PARAM_INT,
79
                'optional' => true,
80
                'default' => 0,
81
            ],
82
            'neweventtimestamp' => [
83
                'type' => PARAM_INT,
84
            ],
85
            'date' => [
86
                'type' => date_exporter::read_properties_definition(),
87
            ],
88
            'periodname' => [
89
                // Note: We must use RAW here because the calendar type returns the formatted month name based on a
90
                // calendar format.
91
                'type' => PARAM_RAW,
92
            ],
93
            'previousperiod' => [
94
                'type' => date_exporter::read_properties_definition(),
95
            ],
96
            'previousperiodlink' => [
97
                'type' => PARAM_URL,
98
            ],
99
            'previousperiodname' => [
100
                // Note: We must use RAW here because the calendar type returns the formatted month name based on a
101
                // calendar format.
102
                'type' => PARAM_RAW,
103
            ],
104
            'nextperiod' => [
105
                'type' => date_exporter::read_properties_definition(),
106
            ],
107
            'nextperiodname' => [
108
                // Note: We must use RAW here because the calendar type returns the formatted month name based on a
109
                // calendar format.
110
                'type' => PARAM_RAW,
111
            ],
112
            'nextperiodlink' => [
113
                'type' => PARAM_URL,
114
            ],
115
            'larrow' => [
116
                // The left arrow defined by the theme.
117
                'type' => PARAM_RAW,
118
            ],
119
            'rarrow' => [
120
                // The right arrow defined by the theme.
121
                'type' => PARAM_RAW,
122
            ],
123
        ];
124
    }
125
 
126
    /**
127
     * Get the additional values to inject while exporting.
128
     *
129
     * @param renderer_base $output The renderer.
130
     * @return array Keys are the property names, values are their values.
131
     */
132
    protected function get_other_values(renderer_base $output) {
133
        $timestamp = $this->calendar->time;
134
 
135
        $cache = $this->related['cache'];
136
        $url = new moodle_url('/calendar/view.php', [
137
            'view' => 'day',
138
            'time' => $timestamp,
139
        ]);
140
        if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
141
            $url->param('course', $this->calendar->course->id);
142
        } else if ($this->calendar->categoryid) {
143
            $url->param('category', $this->calendar->categoryid);
144
        }
145
        $this->url = $url;
146
        $return['events'] = array_map(function($event) use ($cache, $output, $url) {
147
            $context = $cache->get_context($event);
148
            $course = $cache->get_course($event);
149
            $moduleinstance = $cache->get_module_instance($event);
150
            $exporter = new calendar_event_exporter($event, [
151
                'context' => $context,
152
                'course' => $course,
153
                'moduleinstance' => $moduleinstance,
154
                'daylink' => $url,
155
                'type' => $this->related['type'],
156
                'today' => $this->calendar->time,
157
            ]);
158
 
159
            $data = $exporter->export($output);
160
 
161
            // We need to override default formatted time because it differs from day view.
162
            // Formatted time for day view adds a link to the day view.
163
            $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
164
            $data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
165
 
166
            return $data;
167
        }, $this->related['events']);
168
 
169
        if ($context = $this->get_default_add_context()) {
170
            $return['defaulteventcontext'] = $context->id;
171
        }
172
 
173
        if ($this->calendar->categoryid) {
174
            $return['categoryid'] = $this->calendar->categoryid;
175
        }
176
 
177
        $return['filter_selector'] = $this->get_course_filter_selector($output);
178
        $return['courseid'] = $this->calendar->courseid;
179
 
180
        $previousperiod = $this->get_previous_day_data();
181
        $nextperiod = $this->get_next_day_data();
182
        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
183
 
184
        $nextperiodlink = new moodle_url($this->url);
185
        $nextperiodlink->param('time', $nextperiod[0]);
186
 
187
        $previousperiodlink = new moodle_url($this->url);
188
        $previousperiodlink->param('time', $previousperiod[0]);
189
 
190
        $days = calendar_get_days();
191
        $return['date'] = (new date_exporter($date))->export($output);
192
        $return['periodname'] = userdate($this->calendar->time, get_string('strftimedaydate'));
193
        $return['previousperiod'] = (new date_exporter($previousperiod))->export($output);
194
        $return['previousperiodname'] = $days[$previousperiod['wday']]['fullname'];
195
        $return['previousperiodlink'] = $previousperiodlink->out(false);
196
        $return['nextperiod'] = (new date_exporter($nextperiod))->export($output);
197
        $return['nextperiodname'] = $days[$nextperiod['wday']]['fullname'];
198
        $return['nextperiodlink'] = $nextperiodlink->out(false);
199
        $return['larrow'] = $output->larrow();
200
        $return['rarrow'] = $output->rarrow();
201
 
202
        // Need to account for user's timezone.
203
        $usernow = usergetdate(time());
204
        $today = new DateTimeImmutable(
205
            timezone: core_date::get_user_timezone_object(),
206
        );
207
 
208
        // The start time should use the day's date but the current
209
        // time of the day (adjusted for user's timezone).
210
        $neweventtimestamp = $today->setTimestamp($date[0])->setTime(
211
            $usernow['hours'],
212
            $usernow['minutes'],
213
            $usernow['seconds']
214
        );
215
        $return['neweventtimestamp'] = $neweventtimestamp->getTimestamp();
216
 
217
        return $return;
218
    }
219
 
220
    /**
221
     * Get the default context for use when adding a new event.
222
     *
223
     * @return null|\context
224
     */
225
    protected function get_default_add_context() {
226
        if (calendar_user_can_add_event($this->calendar->course)) {
227
            return \context_course::instance($this->calendar->course->id);
228
        }
229
 
230
        return null;
231
    }
232
 
233
    /**
234
     * Get the course filter selector.
235
     *
236
     * @param renderer_base $output
237
     * @return string The html code for the course filter selector.
238
     */
239
    protected function get_course_filter_selector(renderer_base $output) {
240
        return $output->course_filter_selector($this->url, '', $this->calendar->course->id);
241
    }
242
 
243
    /**
244
     * Returns a list of objects that are related.
245
     *
246
     * @return array
247
     */
248
    protected static function define_related() {
249
        return [
250
            'events' => '\core_calendar\local\event\entities\event_interface[]',
251
            'cache' => '\core_calendar\external\events_related_objects_cache',
252
            'type' => '\core_calendar\type_base',
253
        ];
254
    }
255
 
256
    /**
257
     * Get the previous day timestamp.
258
     *
259
     * @return int The previous day timestamp.
260
     */
261
    protected function get_previous_day_data() {
262
        $type = $this->related['type'];
263
        $time = $type->get_prev_day($this->calendar->time);
264
 
265
        return $type->timestamp_to_date_array($time);
266
    }
267
 
268
    /**
269
     * Get the next day timestamp.
270
     *
271
     * @return int The next day timestamp.
272
     */
273
    protected function get_next_day_data() {
274
        $type = $this->related['type'];
275
        $time = $type->get_next_day($this->calendar->time);
276
 
277
        return $type->timestamp_to_date_array($time);
278
    }
279
}