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 a calendar event.
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
require_once($CFG->dirroot . "/calendar/lib.php");
30
require_once($CFG->libdir . "/filelib.php");
31
 
1441 ariadna 32
use core\external\exporter;
33
use core_calendar\local\event\container;
34
use core_calendar\local\event\entities\event_interface;
35
use core_calendar\local\event\entities\action_event_interface;
36
use core_calendar\output\humantimeperiod;
37
use core_course\external\course_summary_exporter;
38
use core\external\coursecat_summary_exporter;
39
use renderer_base;
40
use core\url;
1 efrain 41
 
42
/**
43
 * Class for displaying a calendar event.
44
 *
45
 * @package   core_calendar
46
 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
class event_exporter_base extends exporter {
50
 
51
    /**
52
     * @var event_interface $event
53
     */
54
    protected $event;
55
 
56
    /**
57
     * Constructor.
58
     *
59
     * @param event_interface $event
60
     * @param array $related The related data.
61
     */
62
    public function __construct(event_interface $event, $related = []) {
63
        $this->event = $event;
64
 
65
        $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
66
        $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
67
        $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
68
        $userid = $event->get_user() ? $event->get_user()->get('id') : null;
69
        $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
70
 
71
        $data = new \stdClass();
72
        $data->id = $event->get_id();
73
        $data->name = $event->get_name();
74
        $data->description = file_rewrite_pluginfile_urls(
75
            $event->get_description()->get_value(),
76
            'pluginfile.php',
77
            $related['context']->id,
78
            'calendar',
79
            'event_description',
80
            $event->get_id()
81
        );
82
        $data->descriptionformat = $event->get_description()->get_format();
83
        $data->location = \core_external\util::format_text($event->get_location(), FORMAT_PLAIN, $related['context']->id)[0];
84
        $data->groupid = $groupid;
85
        $data->userid = $userid;
86
        $data->categoryid = $categoryid;
87
        $data->eventtype = $event->get_type();
88
        $data->timestart = $starttimestamp;
89
        $data->timeduration = $endtimestamp - $starttimestamp;
90
        $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
91
        $data->timeusermidnight = $event->get_times()->get_usermidnight_time()->getTimestamp();
92
        $data->visible = $event->is_visible() ? 1 : 0;
93
        $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
94
        $data->component = $event->get_component();
95
        $data->overdue = $data->timesort < time();
96
 
97
        if ($repeats = $event->get_repeats()) {
98
            $data->repeatid = $repeats->get_id();
99
            $data->eventcount = $repeats->get_num() + 1;
100
        }
101
 
102
        if ($cm = $event->get_course_module()) {
103
            $data->modulename = $cm->get('modname');
104
            $data->instance = $cm->get('id');
105
            $data->activityname = $cm->get('name');
106
 
107
            $component = 'mod_' . $data->modulename;
108
            if (!component_callback_exists($component, 'core_calendar_get_event_action_string')) {
109
                $modulename = get_string('modulename', $data->modulename);
110
                $data->activitystr = get_string('requiresaction', 'calendar', $modulename);
111
            } else {
112
                $data->activitystr = component_callback(
113
                    $component,
114
                    'core_calendar_get_event_action_string',
115
                    [$event->get_type()]
116
                );
117
            }
118
        }
119
 
120
        parent::__construct($data, $related);
121
    }
122
 
123
    /**
124
     * Return the list of properties.
125
     *
126
     * @return array
127
     */
128
    protected static function define_properties() {
129
        return [
130
            'id' => ['type' => PARAM_INT],
131
            'name' => ['type' => PARAM_TEXT],
132
            'description' => [
133
                'type' => PARAM_RAW,
134
                'optional' => true,
135
                'default' => null,
136
                'null' => NULL_ALLOWED
137
            ],
138
            'descriptionformat' => [
139
                'type' => PARAM_INT,
140
                'optional' => true,
141
                'default' => null,
142
                'null' => NULL_ALLOWED
143
            ],
144
            'location' => [
145
                'type' => PARAM_RAW,
146
                'optional' => true,
147
                'default' => null,
148
                'null' => NULL_ALLOWED
149
            ],
150
            'categoryid' => [
151
                'type' => PARAM_INT,
152
                'optional' => true,
153
                'default' => null,
154
                'null' => NULL_ALLOWED
155
            ],
156
            'groupid' => [
157
                'type' => PARAM_INT,
158
                'optional' => true,
159
                'default' => null,
160
                'null' => NULL_ALLOWED
161
            ],
162
            'userid' => [
163
                'type' => PARAM_INT,
164
                'optional' => true,
165
                'default' => null,
166
                'null' => NULL_ALLOWED
167
            ],
168
            'repeatid' => [
169
                'type' => PARAM_INT,
170
                'optional' => true,
171
                'default' => null,
172
                'null' => NULL_ALLOWED
173
            ],
174
            'eventcount' => [
175
                'type' => PARAM_INT,
176
                'optional' => true,
177
                'default' => null,
178
                'null' => NULL_ALLOWED
179
            ],
180
            'component' => [
181
                'type' => PARAM_COMPONENT,
182
                'optional' => true,
183
                'default' => null,
184
                'null' => NULL_ALLOWED
185
            ],
186
            'modulename' => [
187
                'type' => PARAM_TEXT,
188
                'optional' => true,
189
                'default' => null,
190
                'null' => NULL_ALLOWED
191
            ],
192
            'activityname' => [
193
                'type' => PARAM_TEXT,
194
                'optional' => true,
195
                'default' => null,
196
                'null' => NULL_ALLOWED
197
            ],
198
            'activitystr' => [
199
                'type' => PARAM_TEXT,
200
                'optional' => true,
201
                'default' => null,
202
                'null' => NULL_ALLOWED
203
            ],
204
            'instance' => [
205
                'type' => PARAM_INT,
206
                'optional' => true,
207
                'default' => null,
208
                'null' => NULL_ALLOWED
209
            ],
210
            'eventtype' => ['type' => PARAM_TEXT],
211
            'timestart' => ['type' => PARAM_INT],
212
            'timeduration' => ['type' => PARAM_INT],
213
            'timesort' => ['type' => PARAM_INT],
214
            'timeusermidnight' => ['type' => PARAM_INT],
215
            'visible' => ['type' => PARAM_INT],
216
            'timemodified' => ['type' => PARAM_INT],
217
            'overdue' => [
218
                'type' => PARAM_BOOL,
219
                'optional' => true,
220
                'default' => false,
221
                'null' => NULL_ALLOWED
222
            ],
223
        ];
224
    }
225
 
226
    /**
227
     * Return the list of additional properties.
228
     *
229
     * @return array
230
     */
231
    protected static function define_other_properties() {
232
        return [
233
            'icon' => [
234
                'type' => event_icon_exporter::read_properties_definition(),
235
            ],
236
            'category' => [
237
                'type' => coursecat_summary_exporter::read_properties_definition(),
238
                'optional' => true,
239
            ],
240
            'course' => [
241
                'type' => course_summary_exporter::read_properties_definition(),
242
                'optional' => true,
243
            ],
244
            'subscription' => [
245
                'type' => event_subscription_exporter::read_properties_definition(),
246
                'optional' => true,
247
            ],
248
            'canedit' => [
249
                'type' => PARAM_BOOL
250
            ],
251
            'candelete' => [
252
                'type' => PARAM_BOOL
253
            ],
254
            'deleteurl' => [
255
                'type' => PARAM_URL
256
            ],
257
            'editurl' => [
258
                'type' => PARAM_URL
259
            ],
260
            'viewurl' => [
261
                'type' => PARAM_URL
262
            ],
263
            'formattedtime' => [
264
                'type' => PARAM_RAW,
265
            ],
266
            'formattedlocation' => [
267
                'type' => PARAM_RAW,
268
            ],
269
            'isactionevent' => [
270
                'type' => PARAM_BOOL
271
            ],
272
            'iscourseevent' => [
273
                'type' => PARAM_BOOL
274
            ],
275
            'iscategoryevent' => [
276
                'type' => PARAM_BOOL
277
            ],
278
            'groupname' => [
279
                'type' => PARAM_RAW,
280
                'optional' => true,
281
                'default' => null,
282
                'null' => NULL_ALLOWED
283
            ],
284
            'normalisedeventtype' => [
285
                'type' => PARAM_TEXT
286
            ],
287
            'normalisedeventtypetext' => [
288
                'type' => PARAM_TEXT
289
            ],
290
            'action' => [
291
                'type' => event_action_exporter::read_properties_definition(),
292
                'optional' => true,
293
            ],
294
            'purpose' => [
295
                'type' => PARAM_TEXT
296
            ],
297
            'branded' => [
298
                'type' => PARAM_BOOL,
299
                'optional' => true,
300
            ],
301
        ];
302
    }
303
 
304
    /**
305
     * Get the additional values to inject while exporting.
306
     *
307
     * @param renderer_base $output The renderer.
308
     * @return array Keys are the property names, values are their values.
309
     */
310
    protected function get_other_values(renderer_base $output) {
311
        $values = [];
312
        $event = $this->event;
313
        $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
314
        $context = $this->related['context'];
315
        $course = $this->related['course'];
316
        $values['isactionevent'] = false;
317
        $values['iscourseevent'] = false;
318
        $values['iscategoryevent'] = false;
319
        $values['normalisedeventtype'] = $event->get_type();
320
        if ($moduleproxy = $event->get_course_module()) {
321
            // We need a separate property to flag if an event is action event.
322
            // That's required because canedit return true but action action events cannot be edited on the calendar UI.
323
            // But they are considered editable because you can drag and drop the event on the month view.
324
            $values['isactionevent'] = true;
325
            // Activity events are normalised to "look" like course events.
326
            $values['normalisedeventtype'] = 'course';
327
        } else if ($event->get_type() == 'course') {
328
            $values['iscourseevent'] = true;
329
        } else if ($event->get_type() == 'category') {
330
            $values['iscategoryevent'] = true;
331
        }
332
        $timesort = $event->get_times()->get_sort_time()->getTimestamp();
333
        $iconexporter = new event_icon_exporter($event, ['context' => $context]);
334
        $identifier = 'type' . $values['normalisedeventtype'];
335
        $stringexists = get_string_manager()->string_exists($identifier, 'calendar');
336
        if (!$stringexists) {
337
            // Property normalisedeventtype is used to build the name of the CSS class for the events.
338
            $values['normalisedeventtype'] = 'other';
339
        }
340
        $values['normalisedeventtypetext'] = $stringexists ? get_string($identifier, 'calendar') : '';
341
 
342
        $purpose = 'none';
343
        $isbranded = false;
344
        if ($moduleproxy) {
345
            $purpose = plugin_supports('mod', $moduleproxy->get('modname'), FEATURE_MOD_PURPOSE, 'none');
346
            $isbranded = component_callback('mod_' . $moduleproxy->get('modname'), 'is_branded') !== null ? : false;
347
        }
348
        $values['purpose'] = $purpose;
349
        $values['branded'] = $isbranded;
350
 
351
        $values['icon'] = $iconexporter->export($output);
352
 
353
        $subscriptionexporter = new event_subscription_exporter($event);
354
        $values['subscription'] = $subscriptionexporter->export($output);
355
 
356
        $proxy = $this->event->get_category();
357
        if ($proxy && $proxy->get('id')) {
358
            $category = $proxy->get_proxied_instance();
359
            $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
360
            $values['category'] = $categorysummaryexporter->export($output);
361
        }
362
 
363
        if ($course && $course->id != SITEID) {
364
            $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
365
            $values['course'] = $coursesummaryexporter->export($output);
366
        }
367
 
368
        $courseid = (!$course) ? SITEID : $course->id;
369
 
370
        $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
371
        $values['candelete'] = calendar_delete_event_allowed($legacyevent);
372
 
1441 ariadna 373
        $deleteurl = new url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
1 efrain 374
        $values['deleteurl'] = $deleteurl->out(false);
375
 
1441 ariadna 376
        $editurl = new url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
1 efrain 377
                'course' => $courseid]);
378
        $values['editurl'] = $editurl->out(false);
1441 ariadna 379
        $viewurl = new url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
1 efrain 380
                'time' => $timesort]);
381
        $viewurl->set_anchor('event_' . $event->get_id());
382
        $values['viewurl'] = $viewurl->out(false);
1441 ariadna 383
        $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
384
        $humanperiod = humantimeperiod::create_from_timestamp(
385
            starttimestamp: $legacyevent->timestart,
386
            endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
387
            link: new url(CALENDAR_URL . 'view.php'),
388
        );
389
        $values['formattedtime'] = $output->render($humanperiod);
1 efrain 390
        $values['formattedlocation'] = calendar_format_event_location($legacyevent);
391
 
392
        if ($group = $event->get_group()) {
393
            $values['groupname'] = format_string($group->get('name'), true,
394
                ['context' => \context_course::instance($event->get_course()->get('id'))]);
395
        }
396
 
397
        if ($event instanceof action_event_interface) {
398
            // Export event action if applicable.
399
            $actionrelated = [
400
                'context' => $this->related['context'],
401
                'event' => $event
402
            ];
403
            $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
404
            $values['action'] = $actionexporter->export($output);
405
        }
406
 
407
        return $values;
408
    }
409
 
410
    /**
411
     * Returns a list of objects that are related.
412
     *
413
     * @return array
414
     */
415
    protected static function define_related() {
416
        return [
417
            'context' => 'context',
418
            'course' => 'stdClass?',
419
        ];
420
    }
421
}