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
/**
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
 
25
namespace core_calendar\external;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core\external\exporter;
1441 ariadna 30
use core_calendar\output\humantimeperiod;
1 efrain 31
use renderer_base;
1441 ariadna 32
use core\url;
33
use core_calendar\local\event\container;
1 efrain 34
 
35
/**
36
 * Class for displaying the day view.
37
 *
38
 * @package   core_calendar
39
 * @copyright 2017 Simey Lameze <simey@moodle.com>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class calendar_upcoming_exporter extends exporter {
43
    /**
44
     * @var \calendar_information $calendar The calendar to be rendered.
45
     */
46
    protected $calendar;
47
 
48
    /**
1441 ariadna 49
     * @var url $url The URL for the upcoming view page.
1 efrain 50
     */
51
    protected $url;
52
 
53
    /**
54
     * Constructor for upcoming exporter.
55
     *
56
     * @param \calendar_information $calendar The calendar being represented.
57
     * @param array $related The related information
58
     */
59
    public function __construct(\calendar_information $calendar, $related) {
60
        $this->calendar = $calendar;
61
 
62
        parent::__construct([], $related);
63
    }
64
 
65
    /**
66
     * Return the list of additional properties.
67
     *
68
     * @return array
69
     */
70
    protected static function define_other_properties() {
71
        return [
72
            'events' => [
73
                'type' => calendar_event_exporter::read_properties_definition(),
74
                'multiple' => true,
75
            ],
76
            'defaulteventcontext' => [
77
                'type' => PARAM_INT,
78
                'default' => 0,
79
            ],
80
            'filter_selector' => [
81
                'type' => PARAM_RAW,
82
            ],
83
            'courseid' => [
84
                'type' => PARAM_INT,
85
            ],
86
            'categoryid' => [
87
                'type' => PARAM_INT,
88
                'optional' => true,
89
                'default' => 0,
90
            ],
91
            'isloggedin' => [
92
                'type' => PARAM_BOOL,
93
            ],
94
            'date' => [
95
                'type' => date_exporter::read_properties_definition(),
96
            ],
97
        ];
98
    }
99
 
100
    /**
101
     * Get the additional values to inject while exporting.
102
     *
103
     * @param renderer_base $output The renderer.
104
     * @return array Keys are the property names, values are their values.
105
     */
106
    protected function get_other_values(renderer_base $output) {
107
        $timestamp = $this->calendar->time;
108
 
109
        $cache = $this->related['cache'];
1441 ariadna 110
        $url = new url('/calendar/view.php', [
1 efrain 111
            'view' => 'upcoming',
112
            'time' => $timestamp,
113
            'course' => $this->calendar->course->id,
114
        ]);
115
        $this->url = $url;
116
        $return['isloggedin'] = isloggedin();
117
        $return['events'] = array_map(function($event) use ($cache, $output, $url) {
118
            $context = $cache->get_context($event);
119
            $course = $cache->get_course($event);
120
            $moduleinstance = $cache->get_module_instance($event);
121
            $exporter = new calendar_event_exporter($event, [
122
                'context' => $context,
123
                'course' => $course,
124
                'moduleinstance' => $moduleinstance,
125
                'daylink' => $url,
126
                'type' => $this->related['type'],
127
                'today' => $this->calendar->time,
128
            ]);
129
 
130
            $data = $exporter->export($output);
131
 
132
            // We need to override default formatted time because it differs from day view.
133
            // Formatted time for upcoming view adds a link to the day view.
134
            $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
1441 ariadna 135
            $humanperiod = humantimeperiod::create_from_timestamp(
136
                starttimestamp: $legacyevent->timestart,
137
                endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
138
                link: new url(CALENDAR_URL . 'view.php'),
139
            );
140
            $data->formattedtime = $output->render($humanperiod);
1 efrain 141
 
142
            return $data;
143
        }, $this->related['events']);
144
 
145
        if ($context = $this->get_default_add_context()) {
146
            $return['defaulteventcontext'] = $context->id;
147
        }
148
        $return['filter_selector'] = $this->get_course_filter_selector($output);
149
        $return['courseid'] = $this->calendar->courseid;
150
        $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
151
        $return['date'] = (new date_exporter($date))->export($output);
152
        if ($this->calendar->categoryid) {
153
            $return['categoryid'] = $this->calendar->categoryid;
154
        }
155
 
156
        return $return;
157
    }
158
 
159
    /**
160
     * Get the default context for use when adding a new event.
161
     *
162
     * @return null|\context
163
     */
164
    protected function get_default_add_context() {
165
        if (calendar_user_can_add_event($this->calendar->course)) {
166
            return \context_course::instance($this->calendar->course->id);
167
        }
168
 
169
        return null;
170
    }
171
 
172
    /**
173
     * Get the course filter selector.
174
     *
175
     * @param renderer_base $output
176
     * @return string The html code for the course filter selector.
177
     */
178
    protected function get_course_filter_selector(renderer_base $output) {
179
        return $output->course_filter_selector($this->url, '', $this->calendar->course->id);
180
    }
181
 
182
    /**
183
     * Returns a list of objects that are related.
184
     *
185
     * @return array
186
     */
187
    protected static function define_related() {
188
        return [
189
            'events' => '\core_calendar\local\event\entities\event_interface[]',
190
            'cache' => '\core_calendar\external\events_related_objects_cache',
191
            'type' => '\core_calendar\type_base',
192
        ];
193
    }
194
}