3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file contains the renderers for the calendar within Moodle
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
31 * The primary renderer for the calendar.
33 class core_calendar_renderer
extends plugin_renderer_base
{
36 * Starts the standard layout for the page
40 public function start_layout() {
41 return html_writer
::start_tag('div', ['data-region' => 'calendar', 'class' => 'maincalendar']);
45 * Creates the remainder of the layout
49 public function complete_layout() {
50 return html_writer
::end_tag('div');
54 * @deprecated since 4.0 MDL-72810.
56 public function fake_block_threemonths() {
57 throw new coding_exception(__FUNCTION__
. '() has been removed.');
61 * Adds a pretent calendar block
63 * @param block_contents $bc
64 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
66 public function add_pretend_calendar_block(block_contents
$bc, $pos=BLOCK_POS_RIGHT
) {
67 $this->page
->blocks
->add_fake_block($bc, $pos);
71 * Creates a button to add a new event.
73 * @param int $courseid
80 public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
82 'contextid' => (\context_course
::instance($courseid))->id
,
84 return $this->render_from_template('core_calendar/add_event_button', $data);
90 * @deprecated since 3.9
92 * @param calendar_event $event
93 * @param bool $showactions
96 public function event(calendar_event
$event, $showactions=true) {
98 debugging('This function is no longer used', DEBUG_DEVELOPER
);
100 $event = calendar_add_event_metadata($event);
101 $context = $event->context
;
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
);
114 $params = array('update' => $event->cmid
, 'return' => true, 'sesskey' => sesskey());
115 $editlink = new moodle_url('/course/mod.php', $params);
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');
130 $commands .= html_writer
::end_tag('div');
131 $output .= $commands;
133 if (!empty($event->icon
)) {
134 $output .= $event->icon
;
136 $output .= $this->output
->spacer(array('height' => 16, 'width' => 16));
139 if (!empty($event->referer
)) {
140 $output .= $this->output
->heading($event->referer
, 3, array('class' => 'referer'));
142 $output .= $this->output
->heading(
143 format_string($event->name
, false, array('context' => $context)),
145 array('class' => 'name d-inline-block')
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
));
155 $source = get_string('subscriptionsource', 'calendar', $event->subscription
->name
);
157 $output .= html_writer
::tag('div', $source, array('class' => 'subscription'));
159 if (!empty($event->courselink
)) {
160 $output .= html_writer
::tag('div', $event->courselink
);
162 if (!empty($event->time
)) {
163 $output .= html_writer
::tag('span', $event->time
, array('class' => 'date float-sm-right mr-1'));
165 $attrs = array('class' => 'date float-sm-right mr-1');
166 $output .= html_writer
::tag('span', calendar_time_representation($event->timestart
), $attrs);
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']);
174 $output .= $this->output
->box_end();
175 $eventdetailshtml = '';
176 $eventdetailsclasses = '';
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
;
184 if (!empty($eventdetailshtml)) {
185 $output .= html_writer
::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
188 $eventhtml = html_writer
::tag('div', $output, array('class' => 'card'));
189 return html_writer
::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id
));
193 * Displays a course filter selector
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.
201 public function course_filter_selector(moodle_url
$returnurl, $label = null, $courseid = null, int $calendarinstanceid = null) {
204 if (!isloggedin() or isguestuser()) {
208 $contextrecords = [];
209 $courses = calendar_get_default_courses($courseid, 'id, shortname');
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) {
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);
229 unset($courses[SITEID
]);
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
]);
237 $coursecontext = context_course
::instance($course->id
);
238 $courseoptions[$course->id
] = format_string($course->shortname
, true, array('context' => $coursecontext));
242 $selected = $courseid;
243 } else if ($this->page
->course
->id
!== SITEID
) {
244 $selected = $this->page
->course
->id
;
248 $courseurl = new moodle_url($returnurl);
249 $courseurl->remove_params('course');
251 $labelattributes = [];
253 $label = get_string('listofcourses');
254 $labelattributes['class'] = 'sr-only';
257 $filterid = 'calendar-course-filter';
258 if ($calendarinstanceid) {
259 $filterid .= "-$calendarinstanceid";
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]);
269 * Render the subscriptions header
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');
283 'class' => 'mr-auto',
284 'aria-describedby' => 'subscription_details_table',
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();
295 * Render the subscriptions blank state appearance
299 public function render_no_calendar_subscriptions(): string {
300 $output = html_writer
::start_div('mt-5');
301 $importlink = html_writer
::link((new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out(),
302 get_string('importcalendarexternal', 'calendar'));
303 $output .= get_string('nocalendarsubscriptions', 'calendar', $importlink);
304 $output .= html_writer
::end_div();
310 * Renders a table containing information about calendar subscriptions.
313 * @param array $subscriptions
314 * @param string $unused2
317 public function subscription_details($unused, $subscriptions, $unused2 = '') {
318 $table = new html_table();
319 $table->head
= array(
320 get_string('colcalendar', 'calendar'),
321 get_string('collastupdated', 'calendar'),
322 get_string('eventkind', 'calendar'),
323 get_string('colpoll', 'calendar'),
324 get_string('colactions', 'calendar')
326 $table->data
= array();
327 $table->id
= 'subscription_details_table';
329 if (empty($subscriptions)) {
330 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
332 $table->data
[] = new html_table_row(array($cell));
334 $strnever = new lang_string('never', 'calendar');
335 foreach ($subscriptions as $sub) {
337 if (!empty($sub->url
)) {
338 $label = html_writer
::link($sub->url
, $label);
340 if (empty($sub->lastupdated
)) {
341 $lastupdated = $strnever->out();
343 $lastupdated = userdate($sub->lastupdated
, get_string('strftimedatetimeshort', 'langconfig'));
346 $type = $sub->eventtype
. 'events';
347 $calendarname = new html_table_cell($label);
348 $calendarname->header
= true;
350 $tablerow = new html_table_row(array(
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())
357 $tablerow->attributes +
= [
358 'data-subid' => $sub->id
,
359 'data-subname' => $sub->name
361 $table->data
[] = $tablerow;
364 $out = $this->output
->box_start('generalbox calendarsubs');
366 $out .= html_writer
::table($table);
367 $out .= $this->output
->box_end();
369 $this->page
->requires
->js_call_amd('core_calendar/manage_subscriptions', 'init');
374 * Render subscription update interval form.
376 * @param stdClass $subscription
379 protected function render_subscription_update_interval(stdClass
$subscription): string {
380 if (empty($subscription->url
)) {
384 $tmpl = new \core_calendar\output\refreshintervalcollection
($subscription);
385 return $this->output
->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output
));
389 * Creates a form to perform actions on a given subscription.
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');
402 * Render the event filter region.
406 public function event_filter() {
408 'eventtypes' => calendar_get_filter_types(),
410 return $this->render_from_template('core_calendar/event_filter', $data);
414 * Render the calendar import result.
416 * @param array $result Import result
417 * @return string|null
419 public function render_import_result(array $result): ?
string {
421 'eventsimported' => $result['eventsimported'],
422 'eventsskipped' => $result['eventsskipped'],
423 'eventsupdated' => $result['eventsupdated'],
424 'eventsdeleted' => $result['eventsdeleted'],
425 'haserror' => $result['haserror'],
426 'errors' => $result['errors']
429 return $this->render_from_template('core_calendar/subscription_update_result', $data);