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 * Produces the content for the three months block (pretend block)
56 * This includes the previous month, the current month, and the next month
58 * @param calendar_information $calendar
61 public function fake_block_threemonths(calendar_information
$calendar) {
62 // Get the calendar type we are using.
63 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
64 $time = $calendartype->timestamp_to_date_array($calendar->time
);
66 $current = $calendar->time
;
67 $prevmonthyear = $calendartype->get_prev_month($time['year'], $time['mon']);
68 $prev = $calendartype->convert_to_timestamp(
73 $nextmonthyear = $calendartype->get_next_month($time['year'], $time['mon']);
74 $next = $calendartype->convert_to_timestamp(
83 $calendar->set_time($prev);
84 list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
87 $calendar->set_time($current);
88 list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
91 $calendar->set_time($next);
92 list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
94 // Reset the time back.
95 $calendar->set_time($current);
98 'previousmonth' => $previousmonth,
99 'currentmonth' => $currentmonth,
100 'nextmonth' => $nextmonth,
103 $template = 'core_calendar/calendar_threemonth';
104 $content .= $this->render_from_template($template, $data);
109 * Adds a pretent calendar block
111 * @param block_contents $bc
112 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
114 public function add_pretend_calendar_block(block_contents
$bc, $pos=BLOCK_POS_RIGHT
) {
115 $this->page
->blocks
->add_fake_block($bc, $pos);
119 * Creates a button to add a new event.
121 * @param int $courseid
122 * @param int $unused1
123 * @param int $unused2
124 * @param int $unused3
125 * @param int $unused4
128 public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
130 'contextid' => (\context_course
::instance($courseid))->id
,
132 return $this->render_from_template('core_calendar/add_event_button', $data);
138 * @deprecated since 3.9
140 * @param calendar_event $event
141 * @param bool $showactions
144 public function event(calendar_event
$event, $showactions=true) {
146 debugging('This function is no longer used', DEBUG_DEVELOPER
);
148 $event = calendar_add_event_metadata($event);
149 $context = $event->context
;
152 $output .= $this->output
->box_start('card-header clearfix');
153 if (calendar_edit_event_allowed($event) && $showactions) {
154 if (calendar_delete_event_allowed($event)) {
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
));
157 if (!empty($event->calendarcourseid
)) {
158 $editlink->param('course', $event->calendarcourseid
);
159 $deletelink->param('course', $event->calendarcourseid
);
162 $params = array('update' => $event->cmid
, 'return' => true, 'sesskey' => sesskey());
163 $editlink = new moodle_url('/course/mod.php', $params);
167 $commands = html_writer
::start_tag('div', array('class' => 'commands float-sm-right'));
168 $commands .= html_writer
::start_tag('a', array('href' => $editlink));
169 $str = get_string('tt_editevent', 'calendar');
170 $commands .= $this->output
->pix_icon('t/edit', $str);
171 $commands .= html_writer
::end_tag('a');
172 if ($deletelink != null) {
173 $commands .= html_writer
::start_tag('a', array('href' => $deletelink));
174 $str = get_string('tt_deleteevent', 'calendar');
175 $commands .= $this->output
->pix_icon('t/delete', $str);
176 $commands .= html_writer
::end_tag('a');
178 $commands .= html_writer
::end_tag('div');
179 $output .= $commands;
181 if (!empty($event->icon
)) {
182 $output .= $event->icon
;
184 $output .= $this->output
->spacer(array('height' => 16, 'width' => 16));
187 if (!empty($event->referer
)) {
188 $output .= $this->output
->heading($event->referer
, 3, array('class' => 'referer'));
190 $output .= $this->output
->heading(
191 format_string($event->name
, false, array('context' => $context)),
193 array('class' => 'name d-inline-block')
196 // Show subscription source if needed.
197 if (!empty($event->subscription
) && $CFG->calendar_showicalsource
) {
198 if (!empty($event->subscription
->url
)) {
199 $source = html_writer
::link($event->subscription
->url
,
200 get_string('subscriptionsource', 'calendar', $event->subscription
->name
));
203 $source = get_string('subscriptionsource', 'calendar', $event->subscription
->name
);
205 $output .= html_writer
::tag('div', $source, array('class' => 'subscription'));
207 if (!empty($event->courselink
)) {
208 $output .= html_writer
::tag('div', $event->courselink
);
210 if (!empty($event->time
)) {
211 $output .= html_writer
::tag('span', $event->time
, array('class' => 'date float-sm-right mr-1'));
213 $attrs = array('class' => 'date float-sm-right mr-1');
214 $output .= html_writer
::tag('span', calendar_time_representation($event->timestart
), $attrs);
217 if (!empty($event->actionurl
)) {
218 $actionlink = html_writer
::link(new moodle_url($event->actionurl
), $event->actionname
);
219 $output .= html_writer
::tag('div', $actionlink, ['class' => 'action']);
222 $output .= $this->output
->box_end();
223 $eventdetailshtml = '';
224 $eventdetailsclasses = '';
226 $eventdetailshtml .= format_text($event->description
, $event->format
, array('context' => $context));
227 $eventdetailsclasses .= 'description card-block';
228 if (isset($event->cssclass
)) {
229 $eventdetailsclasses .= ' '.$event->cssclass
;
232 if (!empty($eventdetailshtml)) {
233 $output .= html_writer
::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
236 $eventhtml = html_writer
::tag('div', $output, array('class' => 'card'));
237 return html_writer
::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id
));
241 * Displays a course filter selector
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.
245 * @param int $courseid The id of the course to be selected.
246 * @param int|null $calendarinstanceid The instance ID of the calendar we're generating this course filter for.
249 public function course_filter_selector(moodle_url
$returnurl, $label = null, $courseid = null, int $calendarinstanceid = null) {
252 if (!isloggedin() or isguestuser()) {
256 $contextrecords = [];
257 $courses = calendar_get_default_courses($courseid, 'id, shortname');
259 if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE
) {
260 // We need to pull the context records from the DB to preload them
261 // below. The calendar_get_default_courses code will actually preload
262 // the contexts itself however the context cache is capped to a certain
263 // amount before it starts recycling. Unfortunately that starts to happen
264 // quite a bit if a user has access to a large number of courses (e.g. admin).
265 // So in order to avoid hitting the DB for each context as we loop below we
266 // can load all of the context records and add them to the cache just in time.
267 $courseids = array_map(function($c) {
270 list($insql, $params) = $DB->get_in_or_equal($courseids);
271 $contextsql = "SELECT ctx.instanceid, " . context_helper
::get_preload_record_columns_sql('ctx') .
272 " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
273 array_unshift($params, CONTEXT_COURSE
);
274 $contextrecords = $DB->get_records_sql($contextsql, $params);
277 unset($courses[SITEID
]);
279 $courseoptions = array();
280 $courseoptions[SITEID
] = get_string('fulllistofcourses');
281 foreach ($courses as $course) {
282 if (isset($contextrecords[$course->id
])) {
283 context_helper
::preload_from_record($contextrecords[$course->id
]);
285 $coursecontext = context_course
::instance($course->id
);
286 $courseoptions[$course->id
] = format_string($course->shortname
, true, array('context' => $coursecontext));
290 $selected = $courseid;
291 } else if ($this->page
->course
->id
!== SITEID
) {
292 $selected = $this->page
->course
->id
;
296 $courseurl = new moodle_url($returnurl);
297 $courseurl->remove_params('course');
299 $labelattributes = [];
301 $label = get_string('listofcourses');
302 $labelattributes['class'] = 'sr-only';
305 $filterid = 'calendar-course-filter';
306 if ($calendarinstanceid) {
307 $filterid .= "-$calendarinstanceid";
309 $select = html_writer
::label($label, $filterid, false, $labelattributes);
310 $select .= html_writer
::select($courseoptions, 'course', $selected, false,
311 ['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => $filterid]);
317 * Render the subscriptions header
321 public function render_subscriptions_header(): string {
322 $importcalendarbutton = new single_button(new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()),
323 get_string('importcalendar', 'calendar'), 'get', true);
324 $importcalendarbutton->class .= ' float-sm-right float-right';
325 $exportcalendarbutton = new single_button(new moodle_url('/calendar/export.php', calendar_get_export_import_link_params()),
326 get_string('exportcalendar', 'calendar'), 'get', true);
327 $exportcalendarbutton->class .= ' float-sm-right float-right';
328 $output = $this->output
->heading(get_string('managesubscriptions', 'calendar'));
329 $output .= html_writer
::start_div('header d-flex flex-wrap mt-5');
331 'class' => 'mr-auto',
332 'aria-describedby' => 'subscription_details_table',
334 $output .= html_writer
::tag('h3', get_string('yoursubscriptions', 'calendar'), $headerattr);
335 $output .= $this->output
->render($importcalendarbutton);
336 $output .= $this->output
->render($exportcalendarbutton);
337 $output .= html_writer
::end_div();
343 * Render the subscriptions blank state appearance
347 public function render_no_calendar_subscriptions(): string {
348 $output = html_writer
::start_div('mt-5');
349 $importlink = html_writer
::link((new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out(),
350 get_string('importcalendarexternal', 'calendar'));
351 $output .= get_string('nocalendarsubscriptions', 'calendar', $importlink);
352 $output .= html_writer
::end_div();
358 * Renders a table containing information about calendar subscriptions.
361 * @param array $subscriptions
362 * @param string $unused2
365 public function subscription_details($unused, $subscriptions, $unused2 = '') {
366 $table = new html_table();
367 $table->head
= array(
368 get_string('colcalendar', 'calendar'),
369 get_string('collastupdated', 'calendar'),
370 get_string('eventkind', 'calendar'),
371 get_string('colpoll', 'calendar'),
372 get_string('colactions', 'calendar')
374 $table->data
= array();
375 $table->id
= 'subscription_details_table';
377 if (empty($subscriptions)) {
378 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
380 $table->data
[] = new html_table_row(array($cell));
382 $strnever = new lang_string('never', 'calendar');
383 foreach ($subscriptions as $sub) {
385 if (!empty($sub->url
)) {
386 $label = html_writer
::link($sub->url
, $label);
388 if (empty($sub->lastupdated
)) {
389 $lastupdated = $strnever->out();
391 $lastupdated = userdate($sub->lastupdated
, get_string('strftimedatetimeshort', 'langconfig'));
394 $type = $sub->eventtype
. 'events';
395 $calendarname = new html_table_cell($label);
396 $calendarname->header
= true;
398 $tablerow = new html_table_row(array(
400 new html_table_cell($lastupdated),
401 new html_table_cell(get_string($type, 'calendar')),
402 new html_table_cell($this->render_subscription_update_interval($sub)),
403 new html_table_cell($this->subscription_action_links())
405 $tablerow->attributes +
= [
406 'data-subid' => $sub->id
,
407 'data-subname' => $sub->name
409 $table->data
[] = $tablerow;
412 $out = $this->output
->box_start('generalbox calendarsubs');
414 $out .= html_writer
::table($table);
415 $out .= $this->output
->box_end();
417 $this->page
->requires
->js_call_amd('core_calendar/manage_subscriptions', 'init');
422 * Render subscription update interval form.
424 * @param stdClass $subscription
427 protected function render_subscription_update_interval(stdClass
$subscription): string {
428 if (empty($subscription->url
)) {
432 $tmpl = new \core_calendar\output\refreshintervalcollection
($subscription);
433 return $this->output
->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output
));
437 * Creates a form to perform actions on a given subscription.
441 protected function subscription_action_links(): string {
442 $html = html_writer
::start_tag('div', array('class' => 'btn-group float-left'));
443 $html .= html_writer
::span(html_writer
::link('#', get_string('delete'),
444 ['data-action' => 'delete-subscription']), '');
445 $html .= html_writer
::end_tag('div');
450 * Render the event filter region.
454 public function event_filter() {
456 'eventtypes' => calendar_get_filter_types(),
458 return $this->render_from_template('core_calendar/event_filter', $data);
462 * Render the calendar import result.
464 * @param array $result Import result
465 * @return string|null
467 public function render_import_result(array $result): ?
string {
469 'eventsimported' => $result['eventsimported'],
470 'eventsskipped' => $result['eventsskipped'],
471 'eventsupdated' => $result['eventsupdated'],
472 'eventsdeleted' => $result['eventsdeleted'],
473 'haserror' => $result['haserror'],
474 'errors' => $result['errors']
477 return $this->render_from_template('core_calendar/subscription_update_result', $data);