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
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * This file contains the renderers for the calendar within Moodle
20
 *
21
 * @copyright 2010 Sam Hemelryk
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @package calendar
24
 */
25
 
26
if (!defined('MOODLE_INTERNAL')) {
27
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28
}
29
 
30
/**
31
 * The primary renderer for the calendar.
32
 */
33
class core_calendar_renderer extends plugin_renderer_base {
34
 
35
    /**
36
     * Starts the standard layout for the page
37
     *
38
     * @return string
39
     */
40
    public function start_layout() {
41
        return html_writer::start_tag('div', ['data-region' => 'calendar', 'class' => 'maincalendar']);
42
    }
43
 
44
    /**
45
     * Creates the remainder of the layout
46
     *
47
     * @return string
48
     */
49
    public function complete_layout() {
50
        return html_writer::end_tag('div');
51
    }
52
 
53
    /**
54
     * @deprecated since 4.0 MDL-72810.
55
     */
56
    public function fake_block_threemonths() {
57
        throw new coding_exception(__FUNCTION__ . '() has been removed.');
58
    }
59
 
60
    /**
61
     * Adds a pretent calendar block
62
     *
63
     * @param block_contents $bc
64
     * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
65
     */
66
    public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
67
        $this->page->blocks->add_fake_block($bc, $pos);
68
    }
69
 
70
    /**
71
     * Creates a button to add a new event.
72
     *
73
     * @param int $courseid
74
     * @param int $unused1
75
     * @param int $unused2
76
     * @param int $unused3
77
     * @param int $unused4
78
     * @return string
79
     */
80
    public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
81
        $data = [
82
            'contextid' => (\context_course::instance($courseid))->id,
83
        ];
84
        return $this->render_from_template('core_calendar/add_event_button', $data);
85
    }
86
 
87
    /**
88
     * Displays an event
89
     *
90
     * @deprecated since 3.9
91
     *
92
     * @param calendar_event $event
93
     * @param bool $showactions
94
     * @return string
95
     */
96
    public function event(calendar_event $event, $showactions=true) {
97
        global $CFG;
98
        debugging('This function is no longer used', DEBUG_DEVELOPER);
99
 
100
        $event = calendar_add_event_metadata($event);
101
        $context = $event->context;
102
        $output = '';
103
 
104
        $output .= $this->output->box_start('card-header clearfix');
105
        if (calendar_edit_event_allowed($event) && $showactions) {
106
            if (calendar_delete_event_allowed($event)) {
107
                $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
108
                $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
109
                if (!empty($event->calendarcourseid)) {
110
                    $editlink->param('course', $event->calendarcourseid);
111
                    $deletelink->param('course', $event->calendarcourseid);
112
                }
113
            } else {
114
                $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
115
                $editlink = new moodle_url('/course/mod.php', $params);
116
                $deletelink = null;
117
            }
118
 
119
            $commands  = html_writer::start_tag('div', array('class' => 'commands float-sm-right'));
120
            $commands .= html_writer::start_tag('a', array('href' => $editlink));
121
            $str = get_string('tt_editevent', 'calendar');
122
            $commands .= $this->output->pix_icon('t/edit', $str);
123
            $commands .= html_writer::end_tag('a');
124
            if ($deletelink != null) {
125
                $commands .= html_writer::start_tag('a', array('href' => $deletelink));
126
                $str = get_string('tt_deleteevent', 'calendar');
127
                $commands .= $this->output->pix_icon('t/delete', $str);
128
                $commands .= html_writer::end_tag('a');
129
            }
130
            $commands .= html_writer::end_tag('div');
131
            $output .= $commands;
132
        }
133
        if (!empty($event->icon)) {
134
            $output .= $event->icon;
135
        } else {
136
            $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
137
        }
138
 
139
        if (!empty($event->referer)) {
140
            $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
141
        } else {
142
            $output .= $this->output->heading(
143
                format_string($event->name, false, array('context' => $context)),
144
                3,
145
                array('class' => 'name d-inline-block')
146
            );
147
        }
148
        // Show subscription source if needed.
149
        if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
150
            if (!empty($event->subscription->url)) {
151
                $source = html_writer::link($event->subscription->url,
152
                        get_string('subscriptionsource', 'calendar', $event->subscription->name));
153
            } else {
154
                // File based ical.
155
                $source = get_string('subscriptionsource', 'calendar', $event->subscription->name);
156
            }
157
            $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
158
        }
159
        if (!empty($event->courselink)) {
160
            $output .= html_writer::tag('div', $event->courselink);
161
        }
162
        if (!empty($event->time)) {
163
            $output .= html_writer::tag('span', $event->time, array('class' => 'date float-sm-right mr-1'));
164
        } else {
165
            $attrs = array('class' => 'date float-sm-right mr-1');
166
            $output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
167
        }
168
 
169
        if (!empty($event->actionurl)) {
170
            $actionlink = html_writer::link(new moodle_url($event->actionurl), $event->actionname);
171
            $output .= html_writer::tag('div', $actionlink, ['class' => 'action']);
172
        }
173
 
174
        $output .= $this->output->box_end();
175
        $eventdetailshtml = '';
176
        $eventdetailsclasses = '';
177
 
178
        $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
179
        $eventdetailsclasses .= 'description card-block';
180
        if (isset($event->cssclass)) {
181
            $eventdetailsclasses .= ' '.$event->cssclass;
182
        }
183
 
184
        if (!empty($eventdetailshtml)) {
185
            $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
186
        }
187
 
188
        $eventhtml = html_writer::tag('div', $output, array('class' => 'card'));
189
        return html_writer::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id));
190
    }
191
 
192
    /**
193
     * Displays a course filter selector
194
     *
195
     * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
196
     * @param string $label The label to use for the course select.
197
     * @param int $courseid The id of the course to be selected.
198
     * @param int|null $calendarinstanceid The instance ID of the calendar we're generating this course filter for.
199
     * @return string
200
     */
201
    public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null, int $calendarinstanceid = null) {
202
        global $CFG, $DB;
203
 
204
        if (!isloggedin() or isguestuser()) {
205
            return '';
206
        }
207
 
208
        $contextrecords = [];
209
        $courses = calendar_get_default_courses($courseid, 'id, shortname');
210
 
211
        if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE) {
212
            // We need to pull the context records from the DB to preload them
213
            // below. The calendar_get_default_courses code will actually preload
214
            // the contexts itself however the context cache is capped to a certain
215
            // amount before it starts recycling. Unfortunately that starts to happen
216
            // quite a bit if a user has access to a large number of courses (e.g. admin).
217
            // So in order to avoid hitting the DB for each context as we loop below we
218
            // can load all of the context records and add them to the cache just in time.
219
            $courseids = array_map(function($c) {
220
                return $c->id;
221
            }, $courses);
222
            list($insql, $params) = $DB->get_in_or_equal($courseids);
223
            $contextsql = "SELECT ctx.instanceid, " . context_helper::get_preload_record_columns_sql('ctx') .
224
                          " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
225
            array_unshift($params, CONTEXT_COURSE);
226
            $contextrecords = $DB->get_records_sql($contextsql, $params);
227
        }
228
 
229
        unset($courses[SITEID]);
230
 
231
        $courseoptions = array();
232
        $courseoptions[SITEID] = get_string('fulllistofcourses');
233
        foreach ($courses as $course) {
234
            if (isset($contextrecords[$course->id])) {
235
                context_helper::preload_from_record($contextrecords[$course->id]);
236
            }
237
            $coursecontext = context_course::instance($course->id);
238
            $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
239
        }
240
 
241
        if ($courseid) {
242
            $selected = $courseid;
243
        } else if ($this->page->course->id !== SITEID) {
244
            $selected = $this->page->course->id;
245
        } else {
246
            $selected = '';
247
        }
248
        $courseurl = new moodle_url($returnurl);
249
        $courseurl->remove_params('course');
250
 
251
        $labelattributes = [];
252
        if (empty($label)) {
253
            $label = get_string('listofcourses');
254
            $labelattributes['class'] = 'sr-only';
255
        }
256
 
257
        $filterid = 'calendar-course-filter';
258
        if ($calendarinstanceid) {
259
            $filterid .= "-$calendarinstanceid";
260
        }
261
        $select = html_writer::label($label, $filterid, false, $labelattributes);
262
        $select .= html_writer::select($courseoptions, 'course', $selected, false,
263
                ['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => $filterid]);
264
 
265
        return $select;
266
    }
267
 
268
    /**
269
     * Render the subscriptions header
270
     *
271
     * @return string
272
     */
273
    public function render_subscriptions_header(): string {
274
        $importcalendarbutton = new single_button(new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()),
275
                get_string('importcalendar', 'calendar'), 'get', single_button::BUTTON_PRIMARY);
276
        $importcalendarbutton->class .= ' float-sm-right float-right';
277
        $exportcalendarbutton = new single_button(new moodle_url('/calendar/export.php', calendar_get_export_import_link_params()),
278
                get_string('exportcalendar', 'calendar'), 'get', single_button::BUTTON_PRIMARY);
279
        $exportcalendarbutton->class .= ' float-sm-right float-right';
280
        $output = $this->output->heading(get_string('managesubscriptions', 'calendar'));
281
        $output .= html_writer::start_div('header d-flex flex-wrap mt-5');
282
        $headerattr = [
283
            'class' => 'mr-auto',
284
            'aria-describedby' => 'subscription_details_table',
285
        ];
286
        $output .= html_writer::tag('h3', get_string('yoursubscriptions', 'calendar'), $headerattr);
287
        $output .= $this->output->render($importcalendarbutton);
288
        $output .= $this->output->render($exportcalendarbutton);
289
        $output .= html_writer::end_div();
290
 
291
        return $output;
292
    }
293
 
294
    /**
295
     * Render the subscriptions blank state appearance
296
     *
297
     * @return string
298
     */
299
    public function render_no_calendar_subscriptions(): string {
300
        $output = html_writer::start_div('mt-5');
11 efrain 301
        $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
302
        $output .= get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink);
1 efrain 303
        $output .= html_writer::end_div();
304
 
305
        return $output;
306
    }
307
 
308
    /**
309
     * Renders a table containing information about calendar subscriptions.
310
     *
311
     * @param int $unused
312
     * @param array $subscriptions
313
     * @param string $unused2
314
     * @return string
315
     */
316
    public function subscription_details($unused, $subscriptions, $unused2 = '') {
317
        $table = new html_table();
318
        $table->head  = array(
319
            get_string('colcalendar', 'calendar'),
320
            get_string('collastupdated', 'calendar'),
321
            get_string('eventkind', 'calendar'),
322
            get_string('colpoll', 'calendar'),
323
            get_string('colactions', 'calendar')
324
        );
325
        $table->data  = array();
326
        $table->id = 'subscription_details_table';
327
 
328
        if (empty($subscriptions)) {
11 efrain 329
            $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
330
            $cell = new html_table_cell(get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink));
1 efrain 331
            $cell->colspan = 5;
332
            $table->data[] = new html_table_row(array($cell));
333
        }
334
        $strnever = new lang_string('never', 'calendar');
335
        foreach ($subscriptions as $sub) {
336
            $label = $sub->name;
337
            if (!empty($sub->url)) {
338
                $label = html_writer::link($sub->url, $label);
339
            }
340
            if (empty($sub->lastupdated)) {
341
                $lastupdated = $strnever->out();
342
            } else {
343
                $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
344
            }
345
 
346
            $type = $sub->eventtype . 'events';
347
            $calendarname = new html_table_cell($label);
348
            $calendarname->header = true;
349
 
350
            $tablerow = new html_table_row(array(
351
                $calendarname,
352
                new html_table_cell($lastupdated),
353
                new html_table_cell(get_string($type, 'calendar')),
354
                new html_table_cell($this->render_subscription_update_interval($sub)),
355
                new html_table_cell($this->subscription_action_links())
356
            ));
357
            $tablerow->attributes += [
358
                'data-subid' => $sub->id,
359
                'data-subname' => $sub->name
360
            ];
361
            $table->data[] = $tablerow;
362
        }
363
 
364
        $out  = $this->output->box_start('generalbox calendarsubs');
365
 
366
        $out .= html_writer::table($table);
367
        $out .= $this->output->box_end();
368
 
369
        $this->page->requires->js_call_amd('core_calendar/manage_subscriptions', 'init');
370
        return $out;
371
    }
372
 
373
    /**
374
     * Render subscription update interval form.
375
     *
376
     * @param stdClass $subscription
377
     * @return string
378
     */
379
    protected function render_subscription_update_interval(stdClass $subscription): string {
380
        if (empty($subscription->url)) {
381
            return '';
382
        }
383
 
384
        $tmpl = new \core_calendar\output\refreshintervalcollection($subscription);
385
        return $this->output->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output));
386
    }
387
 
388
    /**
389
     * Creates a form to perform actions on a given subscription.
390
     *
391
     * @return string
392
     */
393
    protected function subscription_action_links(): string {
394
        $html = html_writer::start_tag('div', array('class' => 'btn-group float-left'));
395
        $html .= html_writer::span(html_writer::link('#', get_string('delete'),
396
            ['data-action' => 'delete-subscription']), '');
397
        $html .= html_writer::end_tag('div');
398
        return $html;
399
    }
400
 
401
    /**
402
     * Render the event filter region.
403
     *
404
     * @return  string
405
     */
406
    public function event_filter() {
407
        $data = [
408
            'eventtypes' => calendar_get_filter_types(),
409
        ];
410
        return $this->render_from_template('core_calendar/event_filter', $data);
411
    }
412
 
413
    /**
414
     * Render the calendar import result.
415
     *
416
     * @param array $result Import result
417
     * @return string|null
418
     */
419
    public function render_import_result(array $result): ?string {
420
        $data = [
421
            'eventsimported' => $result['eventsimported'],
422
            'eventsskipped' => $result['eventsskipped'],
423
            'eventsupdated' => $result['eventsupdated'],
424
            'eventsdeleted' => $result['eventsdeleted'],
425
            'haserror' => $result['haserror'],
426
            'errors' => $result['errors']
427
        ];
428
 
429
        return $this->render_from_template('core_calendar/subscription_update_result', $data);
430
    }
431
}