MDL-70237 payment: Allow html tags in gateway description
[moodle.git] / calendar / renderer.php
CommitLineData
36dc3b71
SH
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
e30390a0
SH
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
36dc3b71
SH
30/**
31 * The primary renderer for the calendar.
32 */
33class core_calendar_renderer extends plugin_renderer_base {
34
36dc3b71
SH
35 /**
36 * Starts the standard layout for the page
6be8c6b7 37 *
36dc3b71
SH
38 * @return string
39 */
40 public function start_layout() {
0d38888e 41 return html_writer::start_tag('div', ['data-region' => 'calendar', 'class' => 'maincalendar']);
36dc3b71
SH
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
36dc3b71
SH
53 /**
54 * Produces the content for the three months block (pretend block)
55 *
56 * This includes the previous month, the current month, and the next month
57 *
58 * @param calendar_information $calendar
59 * @return string
60 */
61 public function fake_block_threemonths(calendar_information $calendar) {
da304137
MN
62 // Get the calendar type we are using.
63 $calendartype = \core_calendar\type_factory::get_calendar_instance();
6038d626 64 $time = $calendartype->timestamp_to_date_array($calendar->time);
36dc3b71 65
6038d626 66 $current = $calendar->time;
a37faf9c 67 $prevmonthyear = $calendartype->get_prev_month($time['year'], $time['mon']);
6038d626 68 $prev = $calendartype->convert_to_timestamp(
a37faf9c
AN
69 $prevmonthyear[1],
70 $prevmonthyear[0],
71 1
6038d626 72 );
a37faf9c 73 $nextmonthyear = $calendartype->get_next_month($time['year'], $time['mon']);
6038d626 74 $next = $calendartype->convert_to_timestamp(
a37faf9c
AN
75 $nextmonthyear[1],
76 $nextmonthyear[0],
77 1
6038d626
AN
78 );
79
80 $content = '';
81
82 // Previous.
83 $calendar->set_time($prev);
0ef41ee0 84 list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
6038d626
AN
85
86 // Current month.
87 $calendar->set_time($current);
0ef41ee0 88 list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
6038d626
AN
89
90 // Next month.
91 $calendar->set_time($next);
0ef41ee0 92 list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
6038d626
AN
93
94 // Reset the time back.
95 $calendar->set_time($current);
96
97 $data = (object) [
98 'previousmonth' => $previousmonth,
99 'currentmonth' => $currentmonth,
100 'nextmonth' => $nextmonth,
101 ];
102
103 $template = 'core_calendar/calendar_threemonth';
104 $content .= $this->render_from_template($template, $data);
36dc3b71
SH
105 return $content;
106 }
107
108 /**
109 * Adds a pretent calendar block
110 *
111 * @param block_contents $bc
112 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
113 */
114 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
d9c26e21 115 $this->page->blocks->add_fake_block($bc, $pos);
36dc3b71
SH
116 }
117
118 /**
3fb45f57 119 * Creates a button to add a new event.
36dc3b71
SH
120 *
121 * @param int $courseid
3fb45f57
AN
122 * @param int $unused1
123 * @param int $unused2
124 * @param int $unused3
125 * @param int $unused4
36dc3b71
SH
126 * @return string
127 */
3fb45f57
AN
128 public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
129 $data = [
130 'contextid' => (\context_course::instance($courseid))->id,
aa091225 131 ];
3fb45f57 132 return $this->render_from_template('core_calendar/add_event_button', $data);
36dc3b71
SH
133 }
134
36dc3b71
SH
135 /**
136 * Displays an event
137 *
1a972b06
MG
138 * @deprecated since 3.9
139 *
e1cd93ce 140 * @param calendar_event $event
36dc3b71
SH
141 * @param bool $showactions
142 * @return string
143 */
e1cd93ce 144 public function event(calendar_event $event, $showactions=true) {
e73b527c 145 global $CFG;
1a972b06 146 debugging('This function is no longer used', DEBUG_DEVELOPER);
e73b527c 147
23a29de7 148 $event = calendar_add_event_metadata($event);
9984132c 149 $context = $event->context;
11edf09c 150 $output = '';
36dc3b71 151
7ee4a287 152 $output .= $this->output->box_start('card-header clearfix');
23a29de7 153 if (calendar_edit_event_allowed($event) && $showactions) {
436d39ba 154 if (calendar_delete_event_allowed($event)) {
48489fb9
DW
155 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
156 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
7ee4a287
DW
157 if (!empty($event->calendarcourseid)) {
158 $editlink->param('course', $event->calendarcourseid);
159 $deletelink->param('course', $event->calendarcourseid);
160 }
161 } else {
48489fb9
DW
162 $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
163 $editlink = new moodle_url('/course/mod.php', $params);
7ee4a287
DW
164 $deletelink = null;
165 }
166
d470c68b 167 $commands = html_writer::start_tag('div', array('class' => 'commands float-sm-right'));
48489fb9 168 $commands .= html_writer::start_tag('a', array('href' => $editlink));
7ee4a287 169 $str = get_string('tt_editevent', 'calendar');
663640f5 170 $commands .= $this->output->pix_icon('t/edit', $str);
7ee4a287
DW
171 $commands .= html_writer::end_tag('a');
172 if ($deletelink != null) {
48489fb9 173 $commands .= html_writer::start_tag('a', array('href' => $deletelink));
7ee4a287 174 $str = get_string('tt_deleteevent', 'calendar');
663640f5 175 $commands .= $this->output->pix_icon('t/delete', $str);
7ee4a287
DW
176 $commands .= html_writer::end_tag('a');
177 }
178 $commands .= html_writer::end_tag('div');
179 $output .= $commands;
180 }
36dc3b71 181 if (!empty($event->icon)) {
11edf09c 182 $output .= $event->icon;
36dc3b71 183 } else {
11edf09c 184 $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
36dc3b71 185 }
36dc3b71 186
36dc3b71 187 if (!empty($event->referer)) {
11edf09c 188 $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
36dc3b71 189 } else {
11edf09c
BB
190 $output .= $this->output->heading(
191 format_string($event->name, false, array('context' => $context)),
192 3,
7ee4a287 193 array('class' => 'name d-inline-block')
11edf09c 194 );
36dc3b71 195 }
e73b527c
AA
196 // Show subscription source if needed.
197 if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
198 if (!empty($event->subscription->url)) {
8afe9f8a
SL
199 $source = html_writer::link($event->subscription->url,
200 get_string('subscriptionsource', 'calendar', $event->subscription->name));
e73b527c
AA
201 } else {
202 // File based ical.
8afe9f8a 203 $source = get_string('subscriptionsource', 'calendar', $event->subscription->name);
e73b527c 204 }
11edf09c 205 $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
8a7326ed 206 }
a05e4f25 207 if (!empty($event->courselink)) {
0b8e7216 208 $output .= html_writer::tag('div', $event->courselink);
a05e4f25 209 }
36dc3b71 210 if (!empty($event->time)) {
d470c68b 211 $output .= html_writer::tag('span', $event->time, array('class' => 'date float-sm-right mr-1'));
36dc3b71 212 } else {
d470c68b 213 $attrs = array('class' => 'date float-sm-right mr-1');
23a29de7 214 $output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
7ee4a287 215 }
a05e4f25
CB
216
217 if (!empty($event->actionurl)) {
0b8e7216
SL
218 $actionlink = html_writer::link(new moodle_url($event->actionurl), $event->actionname);
219 $output .= html_writer::tag('div', $actionlink, ['class' => 'action']);
36dc3b71
SH
220 }
221
7ee4a287 222 $output .= $this->output->box_end();
11edf09c
BB
223 $eventdetailshtml = '';
224 $eventdetailsclasses = '';
36dc3b71 225
11edf09c 226 $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
7ee4a287 227 $eventdetailsclasses .= 'description card-block';
36dc3b71 228 if (isset($event->cssclass)) {
11edf09c 229 $eventdetailsclasses .= ' '.$event->cssclass;
36dc3b71
SH
230 }
231
7ee4a287
DW
232 if (!empty($eventdetailshtml)) {
233 $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
36dc3b71 234 }
7ee4a287
DW
235
236 $eventhtml = html_writer::tag('div', $output, array('class' => 'card'));
237 return html_writer::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id));
36dc3b71
SH
238 }
239
36dc3b71
SH
240 /**
241 * Displays a course filter selector
242 *
ba1ce6e9
SH
243 * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
244 * @param string $label The label to use for the course select.
72c4cacf 245 * @param int $courseid The id of the course to be selected.
36dc3b71
SH
246 * @return string
247 */
72c4cacf 248 public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null) {
a606f2e2 249 global $CFG, $DB;
36dc3b71
SH
250
251 if (!isloggedin() or isguestuser()) {
252 return '';
253 }
254
a606f2e2 255 $contextrecords = [];
01ed53df 256 $courses = calendar_get_default_courses($courseid, 'id, shortname');
36dc3b71 257
a606f2e2
RW
258 if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE) {
259 // We need to pull the context records from the DB to preload them
260 // below. The calendar_get_default_courses code will actually preload
261 // the contexts itself however the context cache is capped to a certain
262 // amount before it starts recycling. Unfortunately that starts to happen
263 // quite a bit if a user has access to a large number of courses (e.g. admin).
264 // So in order to avoid hitting the DB for each context as we loop below we
265 // can load all of the context records and add them to the cache just in time.
266 $courseids = array_map(function($c) {
267 return $c->id;
268 }, $courses);
269 list($insql, $params) = $DB->get_in_or_equal($courseids);
270 $contextsql = "SELECT ctx.instanceid, " . context_helper::get_preload_record_columns_sql('ctx') .
271 " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
272 array_unshift($params, CONTEXT_COURSE);
273 $contextrecords = $DB->get_records_sql($contextsql, $params);
274 }
275
36dc3b71
SH
276 unset($courses[SITEID]);
277
278 $courseoptions = array();
279 $courseoptions[SITEID] = get_string('fulllistofcourses');
280 foreach ($courses as $course) {
a606f2e2
RW
281 if (isset($contextrecords[$course->id])) {
282 context_helper::preload_from_record($contextrecords[$course->id]);
283 }
6ca657a7 284 $coursecontext = context_course::instance($course->id);
8ebbb06a 285 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
36dc3b71
SH
286 }
287
72c4cacf
MN
288 if ($courseid) {
289 $selected = $courseid;
290 } else if ($this->page->course->id !== SITEID) {
797cedc7 291 $selected = $this->page->course->id;
36dc3b71
SH
292 } else {
293 $selected = '';
294 }
ba1ce6e9
SH
295 $courseurl = new moodle_url($returnurl);
296 $courseurl->remove_params('course');
ce679f6c 297
d24f97c4
SR
298 $labelattributes = [];
299 if (empty($label)) {
ce679f6c 300 $label = get_string('listofcourses');
d24f97c4 301 $labelattributes['class'] = 'sr-only';
91a774e2 302 }
ce679f6c 303
d24f97c4 304 $select = html_writer::label($label, 'course', false, $labelattributes);
fed1cc73 305 $select .= html_writer::select($courseoptions, 'course', $selected, false,
d24f97c4 306 ['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => 'course']);
ce679f6c
SL
307
308 return $select;
36dc3b71 309 }
e30390a0
SH
310
311 /**
312 * Renders a table containing information about calendar subscriptions.
313 *
b9fd5164 314 * @param int $unused
e30390a0
SH
315 * @param array $subscriptions
316 * @param string $importresults
317 * @return string
318 */
b9fd5164 319 public function subscription_details($unused = null, $subscriptions, $importresults = '') {
e30390a0
SH
320 $table = new html_table();
321 $table->head = array(
322 get_string('colcalendar', 'calendar'),
323 get_string('collastupdated', 'calendar'),
8c84f374 324 get_string('eventkind', 'calendar'),
e30390a0
SH
325 get_string('colpoll', 'calendar'),
326 get_string('colactions', 'calendar')
327 );
328 $table->align = array('left', 'left', 'left', 'center');
329 $table->width = '100%';
330 $table->data = array();
331
332 if (empty($subscriptions)) {
333 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
f936c2a9 334 $cell->colspan = 5;
e30390a0
SH
335 $table->data[] = new html_table_row(array($cell));
336 }
337 $strnever = new lang_string('never', 'calendar');
338 foreach ($subscriptions as $sub) {
339 $label = $sub->name;
340 if (!empty($sub->url)) {
341 $label = html_writer::link($sub->url, $label);
342 }
343 if (empty($sub->lastupdated)) {
344 $lastupdated = $strnever->out();
345 } else {
346 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
347 }
348
b9fd5164 349 $cell = new html_table_cell($this->subscription_action_form($sub));
e30390a0 350 $cell->colspan = 2;
8c84f374 351 $type = $sub->eventtype . 'events';
e30390a0
SH
352
353 $table->data[] = new html_table_row(array(
354 new html_table_cell($label),
355 new html_table_cell($lastupdated),
8c84f374 356 new html_table_cell(get_string($type, 'calendar')),
e30390a0
SH
357 $cell
358 ));
359 }
360
361 $out = $this->output->box_start('generalbox calendarsubs');
362
363 $out .= $importresults;
364 $out .= html_writer::table($table);
365 $out .= $this->output->box_end();
366 return $out;
367 }
368
369 /**
370 * Creates a form to perform actions on a given subscription.
371 *
372 * @param stdClass $subscription
e30390a0
SH
373 * @return string
374 */
b9fd5164 375 protected function subscription_action_form($subscription) {
e30390a0
SH
376 // Assemble form for the subscription row.
377 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
378 if (empty($subscription->url)) {
379 // Don't update an iCal file, which has no URL.
380 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
381 } else {
382 // Assemble pollinterval control.
383 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
d1ee1e5f 384 $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
23a29de7 385 foreach (calendar_get_pollinterval_choices() as $k => $v) {
e30390a0
SH
386 $attributes = array();
387 if ($k == $subscription->pollinterval) {
388 $attributes['selected'] = 'selected';
389 }
4e3ce346 390 $attributes['value'] = $k;
e30390a0
SH
391 $html .= html_writer::tag('option', $v, $attributes);
392 }
393 $html .= html_writer::end_tag('select');
394 $html .= html_writer::end_tag('div');
395 }
e30390a0 396 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
e30390a0 397 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
5db1ce53 398 $html .= html_writer::start_tag('div', array('class' => 'btn-group float-right'));
e30390a0 399 if (!empty($subscription->url)) {
ee74a2a1 400 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
d1ee1e5f 401 'class' => 'btn btn-secondary',
ee74a2a1 402 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
e30390a0 403 }
ee74a2a1 404 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
d1ee1e5f 405 'class' => 'btn btn-secondary',
ee74a2a1 406 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
e30390a0
SH
407 $html .= html_writer::end_tag('div');
408 $html .= html_writer::end_tag('form');
409 return $html;
410 }
c8b6e9ab
AN
411
412 /**
413 * Render the event filter region.
414 *
415 * @return string
416 */
417 public function event_filter() {
418 $data = [
419 'eventtypes' => calendar_get_filter_types(),
420 ];
421 return $this->render_from_template('core_calendar/event_filter', $data);
422 }
2ad8f0f6 423}