Proyectos de Subversion Moodle

Rev

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