MDL-62834 behat: new step for enabling filters
[moodle.git] / calendar / renderer.php
blobe8dadbe4e375007012a853fec0417eb2b1094acc
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);
86 // Current month.
87 $calendar->set_time($current);
88 list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false);
90 // Next month.
91 $calendar->set_time($next);
92 list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false);
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 * @param calendar_event $event
139 * @param bool $showactions
140 * @return string
142 public function event(calendar_event $event, $showactions=true) {
143 global $CFG;
145 $event = calendar_add_event_metadata($event);
146 $context = $event->context;
147 $output = '';
149 $output .= $this->output->box_start('card-header clearfix');
150 if (calendar_edit_event_allowed($event) && $showactions) {
151 if (calendar_delete_event_allowed($event)) {
152 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
153 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
154 if (!empty($event->calendarcourseid)) {
155 $editlink->param('course', $event->calendarcourseid);
156 $deletelink->param('course', $event->calendarcourseid);
158 } else {
159 $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
160 $editlink = new moodle_url('/course/mod.php', $params);
161 $deletelink = null;
164 $commands = html_writer::start_tag('div', array('class' => 'commands pull-xs-right'));
165 $commands .= html_writer::start_tag('a', array('href' => $editlink));
166 $str = get_string('tt_editevent', 'calendar');
167 $commands .= $this->output->pix_icon('t/edit', $str);
168 $commands .= html_writer::end_tag('a');
169 if ($deletelink != null) {
170 $commands .= html_writer::start_tag('a', array('href' => $deletelink));
171 $str = get_string('tt_deleteevent', 'calendar');
172 $commands .= $this->output->pix_icon('t/delete', $str);
173 $commands .= html_writer::end_tag('a');
175 $commands .= html_writer::end_tag('div');
176 $output .= $commands;
178 if (!empty($event->icon)) {
179 $output .= $event->icon;
180 } else {
181 $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
184 if (!empty($event->referer)) {
185 $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
186 } else {
187 $output .= $this->output->heading(
188 format_string($event->name, false, array('context' => $context)),
190 array('class' => 'name d-inline-block')
193 // Show subscription source if needed.
194 if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
195 if (!empty($event->subscription->url)) {
196 $source = html_writer::link($event->subscription->url,
197 get_string('subscriptionsource', 'calendar', $event->subscription->name));
198 } else {
199 // File based ical.
200 $source = get_string('subscriptionsource', 'calendar', $event->subscription->name);
202 $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
204 if (!empty($event->courselink)) {
205 $output .= html_writer::tag('div', $event->courselink);
207 if (!empty($event->time)) {
208 $output .= html_writer::tag('span', $event->time, array('class' => 'date pull-xs-right m-r-1'));
209 } else {
210 $attrs = array('class' => 'date pull-xs-right m-r-1');
211 $output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
214 if (!empty($event->actionurl)) {
215 $actionlink = html_writer::link(new moodle_url($event->actionurl), $event->actionname);
216 $output .= html_writer::tag('div', $actionlink, ['class' => 'action']);
219 $output .= $this->output->box_end();
220 $eventdetailshtml = '';
221 $eventdetailsclasses = '';
223 $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
224 $eventdetailsclasses .= 'description card-block';
225 if (isset($event->cssclass)) {
226 $eventdetailsclasses .= ' '.$event->cssclass;
229 if (!empty($eventdetailshtml)) {
230 $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
233 $eventhtml = html_writer::tag('div', $output, array('class' => 'card'));
234 return html_writer::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id));
238 * Displays a course filter selector
240 * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
241 * @param string $label The label to use for the course select.
242 * @param int $courseid The id of the course to be selected.
243 * @return string
245 public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null) {
246 global $CFG;
248 if (!isloggedin() or isguestuser()) {
249 return '';
252 $courses = calendar_get_default_courses($courseid, 'id, shortname');
254 unset($courses[SITEID]);
256 $courseoptions = array();
257 $courseoptions[SITEID] = get_string('fulllistofcourses');
258 foreach ($courses as $course) {
259 $coursecontext = context_course::instance($course->id);
260 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
263 if ($courseid) {
264 $selected = $courseid;
265 } else if ($this->page->course->id !== SITEID) {
266 $selected = $this->page->course->id;
267 } else {
268 $selected = '';
270 $courseurl = new moodle_url($returnurl);
271 $courseurl->remove_params('course');
273 if ($label === null) {
274 $label = get_string('listofcourses');
277 $select = html_writer::label($label, 'course', false, ['class' => 'm-r-1']);
278 $select .= html_writer::select($courseoptions, 'course', $selected, false, ['class' => 'cal_courses_flt']);
280 return $select;
284 * Renders a table containing information about calendar subscriptions.
286 * @param int $unused
287 * @param array $subscriptions
288 * @param string $importresults
289 * @return string
291 public function subscription_details($unused = null, $subscriptions, $importresults = '') {
292 $table = new html_table();
293 $table->head = array(
294 get_string('colcalendar', 'calendar'),
295 get_string('collastupdated', 'calendar'),
296 get_string('eventkind', 'calendar'),
297 get_string('colpoll', 'calendar'),
298 get_string('colactions', 'calendar')
300 $table->align = array('left', 'left', 'left', 'center');
301 $table->width = '100%';
302 $table->data = array();
304 if (empty($subscriptions)) {
305 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
306 $cell->colspan = 5;
307 $table->data[] = new html_table_row(array($cell));
309 $strnever = new lang_string('never', 'calendar');
310 foreach ($subscriptions as $sub) {
311 $label = $sub->name;
312 if (!empty($sub->url)) {
313 $label = html_writer::link($sub->url, $label);
315 if (empty($sub->lastupdated)) {
316 $lastupdated = $strnever->out();
317 } else {
318 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
321 $cell = new html_table_cell($this->subscription_action_form($sub));
322 $cell->colspan = 2;
323 $type = $sub->eventtype . 'events';
325 $table->data[] = new html_table_row(array(
326 new html_table_cell($label),
327 new html_table_cell($lastupdated),
328 new html_table_cell(get_string($type, 'calendar')),
329 $cell
333 $out = $this->output->box_start('generalbox calendarsubs');
335 $out .= $importresults;
336 $out .= html_writer::table($table);
337 $out .= $this->output->box_end();
338 return $out;
342 * Creates a form to perform actions on a given subscription.
344 * @param stdClass $subscription
345 * @return string
347 protected function subscription_action_form($subscription) {
348 // Assemble form for the subscription row.
349 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
350 if (empty($subscription->url)) {
351 // Don't update an iCal file, which has no URL.
352 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
353 } else {
354 // Assemble pollinterval control.
355 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
356 $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
357 foreach (calendar_get_pollinterval_choices() as $k => $v) {
358 $attributes = array();
359 if ($k == $subscription->pollinterval) {
360 $attributes['selected'] = 'selected';
362 $attributes['value'] = $k;
363 $html .= html_writer::tag('option', $v, $attributes);
365 $html .= html_writer::end_tag('select');
366 $html .= html_writer::end_tag('div');
368 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
369 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
370 $html .= html_writer::start_tag('div', array('class' => 'btn-group pull-right'));
371 if (!empty($subscription->url)) {
372 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
373 'class' => 'btn btn-secondary',
374 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
376 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
377 'class' => 'btn btn-secondary',
378 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
379 $html .= html_writer::end_tag('div');
380 $html .= html_writer::end_tag('form');
381 return $html;
385 * Render the event filter region.
387 * @return string
389 public function event_filter() {
390 $data = [
391 'eventtypes' => calendar_get_filter_types(),
393 return $this->render_from_template('core_calendar/event_filter', $data);