MDL-51177 core: Ignore built files in stylelint
[moodle.git] / calendar / renderer.php
blobe451b2f2f6cbbbe6859a6b1d073ae564a88678ae
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 * @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, $DB;
248 if (!isloggedin() or isguestuser()) {
249 return '';
252 $contextrecords = [];
253 $courses = calendar_get_default_courses($courseid, 'id, shortname');
255 if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE) {
256 // We need to pull the context records from the DB to preload them
257 // below. The calendar_get_default_courses code will actually preload
258 // the contexts itself however the context cache is capped to a certain
259 // amount before it starts recycling. Unfortunately that starts to happen
260 // quite a bit if a user has access to a large number of courses (e.g. admin).
261 // So in order to avoid hitting the DB for each context as we loop below we
262 // can load all of the context records and add them to the cache just in time.
263 $courseids = array_map(function($c) {
264 return $c->id;
265 }, $courses);
266 list($insql, $params) = $DB->get_in_or_equal($courseids);
267 $contextsql = "SELECT ctx.instanceid, " . context_helper::get_preload_record_columns_sql('ctx') .
268 " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
269 array_unshift($params, CONTEXT_COURSE);
270 $contextrecords = $DB->get_records_sql($contextsql, $params);
273 unset($courses[SITEID]);
275 $courseoptions = array();
276 $courseoptions[SITEID] = get_string('fulllistofcourses');
277 foreach ($courses as $course) {
278 if (isset($contextrecords[$course->id])) {
279 context_helper::preload_from_record($contextrecords[$course->id]);
281 $coursecontext = context_course::instance($course->id);
282 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
285 if ($courseid) {
286 $selected = $courseid;
287 } else if ($this->page->course->id !== SITEID) {
288 $selected = $this->page->course->id;
289 } else {
290 $selected = '';
292 $courseurl = new moodle_url($returnurl);
293 $courseurl->remove_params('course');
295 if ($label === null) {
296 $label = get_string('listofcourses');
299 $select = html_writer::label($label, 'course', false, ['class' => 'm-r-1']);
300 $select .= html_writer::select($courseoptions, 'course', $selected, false, ['class' => 'cal_courses_flt']);
302 return $select;
306 * Renders a table containing information about calendar subscriptions.
308 * @param int $unused
309 * @param array $subscriptions
310 * @param string $importresults
311 * @return string
313 public function subscription_details($unused = null, $subscriptions, $importresults = '') {
314 $table = new html_table();
315 $table->head = array(
316 get_string('colcalendar', 'calendar'),
317 get_string('collastupdated', 'calendar'),
318 get_string('eventkind', 'calendar'),
319 get_string('colpoll', 'calendar'),
320 get_string('colactions', 'calendar')
322 $table->align = array('left', 'left', 'left', 'center');
323 $table->width = '100%';
324 $table->data = array();
326 if (empty($subscriptions)) {
327 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
328 $cell->colspan = 5;
329 $table->data[] = new html_table_row(array($cell));
331 $strnever = new lang_string('never', 'calendar');
332 foreach ($subscriptions as $sub) {
333 $label = $sub->name;
334 if (!empty($sub->url)) {
335 $label = html_writer::link($sub->url, $label);
337 if (empty($sub->lastupdated)) {
338 $lastupdated = $strnever->out();
339 } else {
340 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
343 $cell = new html_table_cell($this->subscription_action_form($sub));
344 $cell->colspan = 2;
345 $type = $sub->eventtype . 'events';
347 $table->data[] = new html_table_row(array(
348 new html_table_cell($label),
349 new html_table_cell($lastupdated),
350 new html_table_cell(get_string($type, 'calendar')),
351 $cell
355 $out = $this->output->box_start('generalbox calendarsubs');
357 $out .= $importresults;
358 $out .= html_writer::table($table);
359 $out .= $this->output->box_end();
360 return $out;
364 * Creates a form to perform actions on a given subscription.
366 * @param stdClass $subscription
367 * @return string
369 protected function subscription_action_form($subscription) {
370 // Assemble form for the subscription row.
371 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
372 if (empty($subscription->url)) {
373 // Don't update an iCal file, which has no URL.
374 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
375 } else {
376 // Assemble pollinterval control.
377 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
378 $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
379 foreach (calendar_get_pollinterval_choices() as $k => $v) {
380 $attributes = array();
381 if ($k == $subscription->pollinterval) {
382 $attributes['selected'] = 'selected';
384 $attributes['value'] = $k;
385 $html .= html_writer::tag('option', $v, $attributes);
387 $html .= html_writer::end_tag('select');
388 $html .= html_writer::end_tag('div');
390 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
391 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
392 $html .= html_writer::start_tag('div', array('class' => 'btn-group pull-right'));
393 if (!empty($subscription->url)) {
394 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
395 'class' => 'btn btn-secondary',
396 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
398 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
399 'class' => 'btn btn-secondary',
400 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
401 $html .= html_writer::end_tag('div');
402 $html .= html_writer::end_tag('form');
403 return $html;
407 * Render the event filter region.
409 * @return string
411 public function event_filter() {
412 $data = [
413 'eventtypes' => calendar_get_filter_types(),
415 return $this->render_from_template('core_calendar/event_filter', $data);