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-end'));
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-end me-1'));
165 $attrs = array('class' => 'date float-sm-end me-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, fullname');
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
]);
238 // Limit the displayed course name to prevent the dropdown from getting too wide.
239 $coursename = format_string($course->fullname
, true, [
240 'context' => \core\context\course
::instance($course->id
),
242 $courseoptions[$course->id
] = shorten_text($coursename, 50, true);
246 $selected = $courseid;
247 } else if ($this->page
->course
->id
!== SITEID
) {
248 $selected = $this->page
->course
->id
;
252 $courseurl = new moodle_url($returnurl);
253 $courseurl->remove_params('course');
255 $labelattributes = [];
257 $label = get_string('listofcourses');
258 $labelattributes['class'] = 'visually-hidden';
261 $filterid = 'calendar-course-filter';
262 if ($calendarinstanceid) {
263 $filterid .= "-$calendarinstanceid";
265 $select = html_writer
::label($label, $filterid, false, $labelattributes);
266 $select .= html_writer
::select($courseoptions, 'course', $selected, false,
267 ['class' => 'cal_courses_flt ms-1 me-auto me-2 mb-2', 'id' => $filterid]);
273 * Render the subscriptions header
277 public function render_subscriptions_header(): string {
278 $importcalendarbutton = new single_button(new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()),
279 get_string('importcalendar', 'calendar'), 'get', single_button
::BUTTON_PRIMARY
);
280 $importcalendarbutton->class .= ' float-sm-end float-end';
281 $exportcalendarbutton = new single_button(new moodle_url('/calendar/export.php', calendar_get_export_import_link_params()),
282 get_string('exportcalendar', 'calendar'), 'get', single_button
::BUTTON_PRIMARY
);
283 $exportcalendarbutton->class .= ' float-sm-end float-end';
284 $output = $this->output
->heading(get_string('managesubscriptions', 'calendar'));
285 $output .= html_writer
::start_div('header d-flex flex-wrap mt-5');
287 'class' => 'me-auto',
288 'aria-describedby' => 'subscription_details_table',
290 $output .= html_writer
::tag('h3', get_string('yoursubscriptions', 'calendar'), $headerattr);
291 $output .= $this->output
->render($importcalendarbutton);
292 $output .= $this->output
->render($exportcalendarbutton);
293 $output .= html_writer
::end_div();
299 * Render the subscriptions blank state appearance
303 public function render_no_calendar_subscriptions(): string {
304 $output = html_writer
::start_div('mt-5');
305 $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
306 $output .= get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink);
307 $output .= html_writer
::end_div();
313 * Renders a table containing information about calendar subscriptions.
316 * @param array $subscriptions
317 * @param string $unused2
320 public function subscription_details($unused, $subscriptions, $unused2 = '') {
321 $table = new html_table();
322 $table->head
= array(
323 get_string('colcalendar', 'calendar'),
324 get_string('collastupdated', 'calendar'),
325 get_string('eventkind', 'calendar'),
326 get_string('colpoll', 'calendar'),
327 get_string('colactions', 'calendar')
329 $table->data
= array();
330 $table->id
= 'subscription_details_table';
332 if (empty($subscriptions)) {
333 $importlink = (new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out();
334 $cell = new html_table_cell(get_string('nocalendarsubscriptionsimportexternal', 'core_calendar', $importlink));
336 $table->data
[] = new html_table_row(array($cell));
338 $strnever = new lang_string('never', 'calendar');
339 foreach ($subscriptions as $sub) {
341 if (!empty($sub->url
)) {
342 $label = html_writer
::link($sub->url
, $label);
344 if (empty($sub->lastupdated
)) {
345 $lastupdated = $strnever->out();
347 $lastupdated = userdate($sub->lastupdated
, get_string('strftimedatetimeshort', 'langconfig'));
350 $type = $sub->eventtype
. 'events';
351 $calendarname = new html_table_cell($label);
352 $calendarname->header
= true;
354 $tablerow = new html_table_row(array(
356 new html_table_cell($lastupdated),
357 new html_table_cell(get_string($type, 'calendar')),
358 new html_table_cell($this->render_subscription_update_interval($sub)),
359 new html_table_cell($this->subscription_action_links())
361 $tablerow->attributes +
= [
362 'data-subid' => $sub->id
,
363 'data-subname' => $sub->name
365 $table->data
[] = $tablerow;
368 $out = $this->output
->box_start('generalbox calendarsubs');
370 $out .= html_writer
::table($table);
371 $out .= $this->output
->box_end();
373 $this->page
->requires
->js_call_amd('core_calendar/manage_subscriptions', 'init');
378 * Render subscription update interval form.
380 * @param stdClass $subscription
383 protected function render_subscription_update_interval(stdClass
$subscription): string {
384 if (empty($subscription->url
)) {
388 $tmpl = new \core_calendar\output\refreshintervalcollection
($subscription);
389 return $this->output
->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output
));
393 * Creates a form to perform actions on a given subscription.
397 protected function subscription_action_links(): string {
398 $html = html_writer
::start_tag('div', array('class' => 'btn-group float-start'));
399 $html .= html_writer
::span(html_writer
::link('#', get_string('delete'),
400 ['data-action' => 'delete-subscription']), '');
401 $html .= html_writer
::end_tag('div');
406 * Render the event filter region.
410 public function event_filter() {
412 'eventtypes' => calendar_get_filter_types(),
414 return $this->render_from_template('core_calendar/event_filter', $data);
418 * Render the calendar import result.
420 * @param array $result Import result
421 * @return string|null
423 public function render_import_result(array $result): ?
string {
425 'eventsimported' => $result['eventsimported'],
426 'eventsskipped' => $result['eventsskipped'],
427 'eventsupdated' => $result['eventsupdated'],
428 'eventsdeleted' => $result['eventsdeleted'],
429 'haserror' => $result['haserror'],
430 'errors' => $result['errors']
433 return $this->render_from_template('core_calendar/subscription_update_result', $data);