Merge branch 'MDL-70796_39' of https://github.com/timhunt/moodle into MOODLE_39_STABLE
[moodle.git] / calendar / renderer.php
blobdcb3afcebbcc89379d6b800fb43fd3c7507d8072
1 <?php
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.
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/>.
18 /**
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
23 * @package calendar
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 /**
31 * The primary renderer for the calendar.
33 class core_calendar_renderer extends plugin_renderer_base {
35 /**
36 * Starts the standard layout for the page
38 * @return string
40 public function start_layout() {
41 return html_writer::start_tag('div', ['data-region' => 'calendar', 'class' => 'maincalendar']);
44 /**
45 * Creates the remainder of the layout
47 * @return string
49 public function complete_layout() {
50 return html_writer::end_tag('div');
53 /**
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
59 * @return string
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(
69 $prevmonthyear[1],
70 $prevmonthyear[0],
73 $nextmonthyear = $calendartype->get_next_month($time['year'], $time['mon']);
74 $next = $calendartype->convert_to_timestamp(
75 $nextmonthyear[1],
76 $nextmonthyear[0],
80 $content = '';
82 // Previous.
83 $calendar->set_time($prev);
84 list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
86 // Current month.
87 $calendar->set_time($current);
88 list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
90 // Next month.
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);
97 $data = (object) [
98 'previousmonth' => $previousmonth,
99 'currentmonth' => $currentmonth,
100 'nextmonth' => $nextmonth,
103 $template = 'core_calendar/calendar_threemonth';
104 $content .= $this->render_from_template($template, $data);
105 return $content;
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
126 * @return string
128 public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
129 $data = [
130 'contextid' => (\context_course::instance($courseid))->id,
132 return $this->render_from_template('core_calendar/add_event_button', $data);
136 * Displays an event
138 * @deprecated since 3.9
140 * @param calendar_event $event
141 * @param bool $showactions
142 * @return string
144 public function event(calendar_event $event, $showactions=true) {
145 global $CFG;
146 debugging('This function is no longer used', DEBUG_DEVELOPER);
148 $event = calendar_add_event_metadata($event);
149 $context = $event->context;
150 $output = '';
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);
161 } else {
162 $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
163 $editlink = new moodle_url('/course/mod.php', $params);
164 $deletelink = null;
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;
183 } else {
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'));
189 } else {
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));
201 } else {
202 // File based ical.
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'));
212 } else {
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 * @return string
248 public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null) {
249 global $CFG, $DB;
251 if (!isloggedin() or isguestuser()) {
252 return '';
255 $contextrecords = [];
256 $courses = calendar_get_default_courses($courseid, 'id, shortname');
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);
276 unset($courses[SITEID]);
278 $courseoptions = array();
279 $courseoptions[SITEID] = get_string('fulllistofcourses');
280 foreach ($courses as $course) {
281 if (isset($contextrecords[$course->id])) {
282 context_helper::preload_from_record($contextrecords[$course->id]);
284 $coursecontext = context_course::instance($course->id);
285 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
288 if ($courseid) {
289 $selected = $courseid;
290 } else if ($this->page->course->id !== SITEID) {
291 $selected = $this->page->course->id;
292 } else {
293 $selected = '';
295 $courseurl = new moodle_url($returnurl);
296 $courseurl->remove_params('course');
298 $labelattributes = [];
299 if (empty($label)) {
300 $label = get_string('listofcourses');
301 $labelattributes['class'] = 'sr-only';
304 $select = html_writer::label($label, 'course', false, $labelattributes);
305 $select .= html_writer::select($courseoptions, 'course', $selected, false,
306 ['class' => 'cal_courses_flt ml-1 mr-auto', 'id' => 'course']);
308 return $select;
312 * Renders a table containing information about calendar subscriptions.
314 * @param int $unused
315 * @param array $subscriptions
316 * @param string $importresults
317 * @return string
319 public function subscription_details($unused = null, $subscriptions, $importresults = '') {
320 $table = new html_table();
321 $table->head = array(
322 get_string('colcalendar', 'calendar'),
323 get_string('collastupdated', 'calendar'),
324 get_string('eventkind', 'calendar'),
325 get_string('colpoll', 'calendar'),
326 get_string('colactions', 'calendar')
328 $table->align = array('left', 'left', 'left', 'center');
329 $table->width = '100%';
330 $table->data = array();
332 if (empty($subscriptions)) {
333 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
334 $cell->colspan = 5;
335 $table->data[] = new html_table_row(array($cell));
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);
343 if (empty($sub->lastupdated)) {
344 $lastupdated = $strnever->out();
345 } else {
346 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
349 $cell = new html_table_cell($this->subscription_action_form($sub));
350 $cell->colspan = 2;
351 $type = $sub->eventtype . 'events';
353 $table->data[] = new html_table_row(array(
354 new html_table_cell($label),
355 new html_table_cell($lastupdated),
356 new html_table_cell(get_string($type, 'calendar')),
357 $cell
361 $out = $this->output->box_start('generalbox calendarsubs');
363 $out .= $importresults;
364 $out .= html_writer::table($table);
365 $out .= $this->output->box_end();
366 return $out;
370 * Creates a form to perform actions on a given subscription.
372 * @param stdClass $subscription
373 * @return string
375 protected function subscription_action_form($subscription) {
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;'));
384 $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
385 foreach (calendar_get_pollinterval_choices() as $k => $v) {
386 $attributes = array();
387 if ($k == $subscription->pollinterval) {
388 $attributes['selected'] = 'selected';
390 $attributes['value'] = $k;
391 $html .= html_writer::tag('option', $v, $attributes);
393 $html .= html_writer::end_tag('select');
394 $html .= html_writer::end_tag('div');
396 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
397 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
398 $html .= html_writer::start_tag('div', array('class' => 'btn-group float-right'));
399 if (!empty($subscription->url)) {
400 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
401 'class' => 'btn btn-secondary',
402 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
404 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
405 'class' => 'btn btn-secondary',
406 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
407 $html .= html_writer::end_tag('div');
408 $html .= html_writer::end_tag('form');
409 return $html;
413 * Render the event filter region.
415 * @return string
417 public function event_filter() {
418 $data = [
419 'eventtypes' => calendar_get_filter_types(),
421 return $this->render_from_template('core_calendar/event_filter', $data);