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
/**
18
 * Contains event class for providing the related objects when exporting a list of calendar events.
19
 *
20
 * @package   core_calendar
21
 * @copyright 2017 Ryan Wyllie <ryan@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_calendar\local\event\entities\event_interface;
30
 
31
/**
32
 * Class to providing the related objects when exporting a list of calendar events.
33
 *
34
 * This class is only meant for use with exporters. It attempts to bulk load
35
 * the related objects for a list of events and cache them to avoid having
36
 * to query the database when exporting each individual event.
37
 *
38
 * @package   core_calendar
39
 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class events_related_objects_cache {
43
 
44
    /**
45
     * @var array $events The events for which we need related objects.
46
     */
47
    protected $events;
48
 
49
    /**
50
     * @var array $courses The related courses.
51
     */
52
    protected $courses = null;
53
 
54
    /**
55
     * @var array $groups The related groups.
56
     */
57
    protected $groups = null;
58
 
59
    /**
60
     * @var array $coursemodules The related course modules.
61
     */
62
    protected $coursemodules = [];
63
 
64
    /**
65
     * @var array $moduleinstances The related module instances.
66
     */
67
    protected $moduleinstances = null;
68
 
69
    /**
70
     * Constructor.
71
     *
72
     * @param array $events Array of event_interface events
73
     * @param array $courses Array of courses to populate the cache with
74
     */
75
    public function __construct(array $events, array $courses = null) {
76
        $this->events = $events;
77
 
78
        if (!is_null($courses)) {
79
            $this->courses = [];
80
 
81
            foreach ($courses as $course) {
82
                $this->courses[$course->id] = $course;
83
            }
84
        }
85
    }
86
 
87
    /**
88
     * Get the related course object for a given event.
89
     *
90
     * @param event_interface $event The event object.
91
     * @return \stdClass|null
92
     */
93
    public function get_course(event_interface $event) {
94
        if (is_null($this->courses)) {
95
            $this->load_courses();
96
        }
97
 
98
        if ($course = $event->get_course()) {
99
            $courseid = $course->get('id');
100
            return isset($this->courses[$courseid]) ? $this->courses[$courseid] : null;
101
        } else {
102
            return null;
103
        }
104
    }
105
 
106
    /**
107
     * Get the related context for a given event.
108
     *
109
     * @param event_interface $event The event object.
110
     * @return \context|null
111
     */
112
    public function get_context(event_interface $event) {
113
        global $USER;
114
 
115
        $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
116
        $courseid = $event->get_course() ? $event->get_course()->get('id') : null;
117
        $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
118
        $userid = $event->get_user() ? $event->get_user()->get('id') : null;
119
        $moduleid = $event->get_course_module() ? $event->get_course_module()->get('id') : null;
120
 
121
        if (!empty($categoryid)) {
122
            return \context_coursecat::instance($categoryid);
123
        } else if (!empty($courseid)) {
124
            return \context_course::instance($event->get_course()->get('id'));
125
        } else if (!empty($groupid)) {
126
            $group = $this->get_group($event);
127
            return \context_course::instance($group->courseid);
128
        } else if (!empty($userid) && $userid == $USER->id) {
129
            return \context_user::instance($userid);
130
        } else if (!empty($userid) && $userid != $USER->id && $moduleid && $moduleid > 0) {
131
            $cm = $this->get_course_module($event);
132
            return \context_course::instance($cm->course);
133
        } else {
134
            return \context_user::instance($userid);
135
        }
136
    }
137
 
138
    /**
139
     * Get the related group object for a given event.
140
     *
141
     * @param event_interface $event The event object.
142
     * @return \stdClass|null
143
     */
144
    public function get_group(event_interface $event) {
145
        if (is_null($this->groups)) {
146
            $this->load_groups();
147
        }
148
 
149
        if ($group = $event->get_group()) {
150
            $groupid = $group->get('id');
151
            return isset($this->groups[$groupid]) ? $this->groups[$groupid] : null;
152
        } else {
153
            return null;
154
        }
155
    }
156
 
157
    /**
158
     * Get the related course module for a given event.
159
     *
160
     * @param event_interface $event The event object.
161
     * @return \stdClass|null
162
     */
163
    public function get_course_module(event_interface $event) {
164
        if (!$event->get_course_module()) {
165
            return null;
166
        }
167
 
168
        $id = $event->get_course_module()->get('id');
169
        $name = $event->get_course_module()->get('modname');
170
        $key = $name . '_' . $id;
171
 
172
        if (!isset($this->coursemodules[$key])) {
173
            $this->coursemodules[$key] = get_coursemodule_from_instance($name, $id, 0, false, MUST_EXIST);
174
        }
175
 
176
        return $this->coursemodules[$key];
177
    }
178
 
179
    /**
180
     * Get the related module instance for a given event.
181
     *
182
     * @param event_interface $event The event object.
183
     * @return \stdClass|null
184
     */
185
    public function get_module_instance(event_interface $event) {
186
        if (!$event->get_course_module()) {
187
            return null;
188
        }
189
 
190
        if (is_null($this->moduleinstances)) {
191
            $this->load_module_instances();
192
        }
193
 
194
        $id = $event->get_course_module()->get('instance');
195
        $name = $event->get_course_module()->get('modname');
196
 
197
        if (isset($this->moduleinstances[$name])) {
198
            if (isset($this->moduleinstances[$name][$id])) {
199
                return $this->moduleinstances[$name][$id];
200
            }
201
        }
202
 
203
        return null;
204
    }
205
 
206
    /**
207
     * Load the list of all of the distinct courses required for the
208
     * list of provided events and save the result in memory.
209
     */
210
    protected function load_courses() {
211
        global $DB;
212
 
213
        $courseids = [];
214
        foreach ($this->events as $event) {
215
            if ($course = $event->get_course()) {
216
                $id = $course->get('id');
217
                $courseids[$id] = true;
218
            }
219
        }
220
 
221
        if (empty($courseids)) {
222
            $this->courses = [];
223
            return;
224
        }
225
 
226
        list($idsql, $params) = $DB->get_in_or_equal(array_keys($courseids));
227
        $sql = "SELECT * FROM {course} WHERE id {$idsql}";
228
 
229
        $this->courses = $DB->get_records_sql($sql, $params);
230
    }
231
 
232
    /**
233
     * Load the list of all of the distinct groups required for the
234
     * list of provided events and save the result in memory.
235
     */
236
    protected function load_groups() {
237
        global $DB;
238
 
239
        $groupids = [];
240
        foreach ($this->events as $event) {
241
            if ($group = $event->get_group()) {
242
                $id = $group->get('id');
243
                $groupids[$id] = true;
244
            }
245
        }
246
 
247
        if (empty($groupids)) {
248
            $this->groups = [];
249
            return;
250
        }
251
 
252
        list($idsql, $params) = $DB->get_in_or_equal(array_keys($groupids));
253
        $sql = "SELECT * FROM {groups} WHERE id {$idsql}";
254
 
255
        $this->groups = $DB->get_records_sql($sql, $params);
256
    }
257
 
258
    /**
259
     * Load the list of all of the distinct module instances required for the
260
     * list of provided events and save the result in memory.
261
     */
262
    protected function load_module_instances() {
263
        global $DB;
264
 
265
        $this->moduleinstances = [];
266
        $modulestoload = [];
267
        foreach ($this->events as $event) {
268
            if ($module = $event->get_course_module()) {
269
                $id = $module->get('instance');
270
                $name = $module->get('modname');
271
 
272
                $ids = isset($modulestoload[$name]) ? $modulestoload[$name] : [];
273
                $ids[$id] = true;
274
                $modulestoload[$name] = $ids;
275
            }
276
        }
277
 
278
        if (empty($modulestoload)) {
279
            return;
280
        }
281
 
282
        foreach ($modulestoload as $modulename => $ids) {
283
            list($idsql, $params) = $DB->get_in_or_equal(array_keys($ids));
284
            $sql = "SELECT * FROM {" . $modulename . "} WHERE id {$idsql}";
285
            $this->moduleinstances[$modulename] = $DB->get_records_sql($sql, $params);
286
        }
287
    }
288
}