Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 * This file contains the renderers for the calendar within Moodle
19
 *
20
 * @copyright 2010 Sam Hemelryk
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @package calendar
23
 */
24
 
25
if (!defined('MOODLE_INTERNAL')) {
26
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
27
}
28
 
29
/**
30
 * The primary renderer for the calendar.
31
 */
32
class core_calendar_renderer extends plugin_renderer_base {
33
 
34
    /**
35
     * Starts the standard layout for the page
36
     *
37
     * @return string
38
     */
39
    public function start_layout() {
40
        return html_writer::start_tag('div', ['data-region' => 'calendar', 'class' => 'maincalendar']);
41
    }
42
 
43
    /**
44
     * Creates the remainder of the layout
45
     *
46
     * @return string
47
     */
48
    public function complete_layout() {
49
        return html_writer::end_tag('div');
50
    }
51
 
52
    /**
53
     * Adds a pretent calendar block
54
     *
55
     * @param block_contents $bc
56
     * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
57
     */
58
    public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
59
        $this->page->blocks->add_fake_block($bc, $pos);
60
    }
61
 
62
    /**
63
     * Creates a button to add a new event.
64
     *
65
     * @param int $courseid
66
     * @param int $unused1
67
     * @param int $unused2
68
     * @param int $unused3
69
     * @param int $unused4
70
     * @return string
71
     */
72
    public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
73
        $data = [
74
            'contextid' => (\context_course::instance($courseid))->id,
75
        ];
76
        return $this->render_from_template('core_calendar/add_event_button', $data);
77
    }
78
 
79
    /**
1441 ariadna 80
     * @deprecated 3.9
1 efrain 81
     */
1441 ariadna 82
    #[\core\attribute\deprecated(
83
        replacement: 'event no longer used',
84
        since: '3.9',
85
        mdl: 'MDL-58866',
86
        final: true,
87
    )]
88
    public function event() {
89
        \core\deprecation::emit_deprecation(__FUNCTION__);
1 efrain 90
    }
91
 
92
    /**
93
     * Displays a course filter selector
94
     *
95
     * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
96
     * @param string $label The label to use for the course select.
97
     * @param int $courseid The id of the course to be selected.
98
     * @param int|null $calendarinstanceid The instance ID of the calendar we're generating this course filter for.
99
     * @return string
100
     */
1441 ariadna 101
    public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null, ?int $calendarinstanceid = null) {
1 efrain 102
        global $CFG, $DB;
103
 
104
        if (!isloggedin() or isguestuser()) {
105
            return '';
106
        }
107
 
108
        $contextrecords = [];
1441 ariadna 109
        $courses = calendar_get_default_courses($courseid, 'id, shortname, fullname');
1 efrain 110
 
111
        if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE) {
112
            // We need to pull the context records from the DB to preload them
113
            // below. The calendar_get_default_courses code will actually preload
114
            // the contexts itself however the context cache is capped to a certain
115
            // amount before it starts recycling. Unfortunately that starts to happen
116
            // quite a bit if a user has access to a large number of courses (e.g. admin).
117
            // So in order to avoid hitting the DB for each context as we loop below we
118
            // can load all of the context records and add them to the cache just in time.
119
            $courseids = array_map(function($c) {
120
                return $c->id;
121
            }, $courses);
122
            list($insql, $params) = $DB->get_in_or_equal($courseids);
123
            $contextsql = "SELECT ctx.instanceid, " . context_helper::get_preload_record_columns_sql('ctx') .
124
                          " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
125
            array_unshift($params, CONTEXT_COURSE);
126
            $contextrecords = $DB->get_records_sql($contextsql, $params);
127
        }
128
 
129
        unset($courses[SITEID]);
130
 
131
        $courseoptions = array();
132
        $courseoptions[SITEID] = get_string('fulllistofcourses');
133
        foreach ($courses as $course) {
134
            if (isset($contextrecords[$course->id])) {
135
                context_helper::preload_from_record($contextrecords[$course->id]);
136
            }
1441 ariadna 137
 
138
            // Limit the displayed course name to prevent the dropdown from getting too wide.
139
            $coursename = format_string(get_course_display_name_for_list($course), true, [
140
                'context' => \core\context\course::instance($course->id),
141
            ]);
142
            $courseoptions[$course->id] = shorten_text($coursename, 50, true);
1 efrain 143
        }
144
 
145
        if ($courseid) {
146
            $selected = $courseid;
147
        } else if ($this->page->course->id !== SITEID) {
148
            $selected = $this->page->course->id;
149
        } else {
150
            $selected = '';
151
        }
152
        $courseurl = new moodle_url($returnurl);
153
        $courseurl->remove_params('course');
154
 
155
        $labelattributes = [];
156
        if (empty($label)) {
157
            $label = get_string('listofcourses');
1441 ariadna 158
            $labelattributes['class'] = 'visually-hidden';
1 efrain 159
        }
160
 
161
        $filterid = 'calendar-course-filter';
162
        if ($calendarinstanceid) {
163
            $filterid .= "-$calendarinstanceid";
164
        }
165
        $select = html_writer::label($label, $filterid, false, $labelattributes);
166
        $select .= html_writer::select($courseoptions, 'course', $selected, false,
1441 ariadna 167
                ['class' => 'cal_courses_flt ms-1 me-auto me-2 mb-2', 'id' => $filterid]);
1 efrain 168
 
169
        return $select;
170
    }
171
 
172
    /**
173
     * Render the subscriptions header
174
     *
175
     * @return string
176
     */
177
    public function render_subscriptions_header(): string {
178
        $importcalendarbutton = new single_button(new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()),
179
                get_string('importcalendar', 'calendar'), 'get', single_button::BUTTON_PRIMARY);
1441 ariadna 180
        $importcalendarbutton->class .= ' float-sm-end float-end';
1 efrain 181
        $exportcalendarbutton = new single_button(new moodle_url('/calendar/export.php', calendar_get_export_import_link_params()),
182
                get_string('exportcalendar', 'calendar'), 'get', single_button::BUTTON_PRIMARY);
1441 ariadna 183
        $exportcalendarbutton->class .= ' float-sm-end float-end';
1 efrain 184
        $output = $this->output->heading(get_string('managesubscriptions', 'calendar'));
185
        $output .= html_writer::start_div('header d-flex flex-wrap mt-5');
186
        $headerattr = [
1441 ariadna 187
            'class' => 'me-auto',
1 efrain 188
            'aria-describedby' => 'subscription_details_table',
189
        ];
190
        $output .= html_writer::tag('h3', get_string('yoursubscriptions', 'calendar'), $headerattr);
191
        $output .= $this->output->render($importcalendarbutton);
192
        $output .= $this->output->render($exportcalendarbutton);
193
        $output .= html_writer::end_div();
194
 
195
        return $output;
196
    }
197
 
198
    /**
199
     * Render the subscriptions blank state appearance
200
     *
201
     * @return string
202
     */
203
    public function render_no_calendar_subscriptions(): string {
204
        $output = html_writer::start_div('mt-5');
11 efrain 205
        $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
206
        $output .= get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink);
1 efrain 207
        $output .= html_writer::end_div();
208
 
209
        return $output;
210
    }
211
 
212
    /**
213
     * Renders a table containing information about calendar subscriptions.
214
     *
215
     * @param int $unused
216
     * @param array $subscriptions
217
     * @param string $unused2
218
     * @return string
219
     */
220
    public function subscription_details($unused, $subscriptions, $unused2 = '') {
221
        $table = new html_table();
222
        $table->head  = array(
223
            get_string('colcalendar', 'calendar'),
224
            get_string('collastupdated', 'calendar'),
225
            get_string('eventkind', 'calendar'),
226
            get_string('colpoll', 'calendar'),
227
            get_string('colactions', 'calendar')
228
        );
229
        $table->data  = array();
230
        $table->id = 'subscription_details_table';
231
 
232
        if (empty($subscriptions)) {
11 efrain 233
            $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
234
            $cell = new html_table_cell(get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink));
1 efrain 235
            $cell->colspan = 5;
236
            $table->data[] = new html_table_row(array($cell));
237
        }
238
        $strnever = new lang_string('never', 'calendar');
239
        foreach ($subscriptions as $sub) {
240
            $label = $sub->name;
241
            if (!empty($sub->url)) {
242
                $label = html_writer::link($sub->url, $label);
243
            }
244
            if (empty($sub->lastupdated)) {
245
                $lastupdated = $strnever->out();
246
            } else {
247
                $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
248
            }
249
 
250
            $type = $sub->eventtype . 'events';
251
            $calendarname = new html_table_cell($label);
252
            $calendarname->header = true;
253
 
254
            $tablerow = new html_table_row(array(
255
                $calendarname,
256
                new html_table_cell($lastupdated),
257
                new html_table_cell(get_string($type, 'calendar')),
258
                new html_table_cell($this->render_subscription_update_interval($sub)),
259
                new html_table_cell($this->subscription_action_links())
260
            ));
261
            $tablerow->attributes += [
262
                'data-subid' => $sub->id,
263
                'data-subname' => $sub->name
264
            ];
265
            $table->data[] = $tablerow;
266
        }
267
 
268
        $out  = $this->output->box_start('generalbox calendarsubs');
269
 
270
        $out .= html_writer::table($table);
271
        $out .= $this->output->box_end();
272
 
273
        $this->page->requires->js_call_amd('core_calendar/manage_subscriptions', 'init');
274
        return $out;
275
    }
276
 
277
    /**
278
     * Render subscription update interval form.
279
     *
280
     * @param stdClass $subscription
281
     * @return string
282
     */
283
    protected function render_subscription_update_interval(stdClass $subscription): string {
284
        if (empty($subscription->url)) {
285
            return '';
286
        }
287
 
288
        $tmpl = new \core_calendar\output\refreshintervalcollection($subscription);
289
        return $this->output->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output));
290
    }
291
 
292
    /**
293
     * Creates a form to perform actions on a given subscription.
294
     *
295
     * @return string
296
     */
297
    protected function subscription_action_links(): string {
1441 ariadna 298
        $html = html_writer::start_tag('div', array('class' => 'btn-group float-start'));
1 efrain 299
        $html .= html_writer::span(html_writer::link('#', get_string('delete'),
300
            ['data-action' => 'delete-subscription']), '');
301
        $html .= html_writer::end_tag('div');
302
        return $html;
303
    }
304
 
305
    /**
306
     * Render the event filter region.
307
     *
308
     * @return  string
309
     */
310
    public function event_filter() {
311
        $data = [
312
            'eventtypes' => calendar_get_filter_types(),
313
        ];
314
        return $this->render_from_template('core_calendar/event_filter', $data);
315
    }
316
 
317
    /**
318
     * Render the calendar import result.
319
     *
320
     * @param array $result Import result
321
     * @return string|null
322
     */
323
    public function render_import_result(array $result): ?string {
324
        $data = [
325
            'eventsimported' => $result['eventsimported'],
326
            'eventsskipped' => $result['eventsskipped'],
327
            'eventsupdated' => $result['eventsupdated'],
328
            'eventsdeleted' => $result['eventsdeleted'],
329
            'haserror' => $result['haserror'],
330
            'errors' => $result['errors']
331
        ];
332
 
333
        return $this->render_from_template('core_calendar/subscription_update_result', $data);
334
    }
335
}