Merge branch 'MDL-41792_master' of https://github.com/markn86/moodle
[moodle.git] / calendar / renderer.php
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.
14 //
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
20  *
21  * @copyright 2010 Sam Hemelryk
22  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  * @package calendar
24  */
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
30 /**
31  * The primary renderer for the calendar.
32  */
33 class core_calendar_renderer extends plugin_renderer_base {
35     /**
36      * Creates a basic export form
37      *
38      * @param bool $allowthisweek
39      * @param bool $allownextweek
40      * @param bool $allownextmonth
41      * @param int $userid
42      * @param string $authtoken
43      * @return string
44      */
45     public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $userid, $authtoken) {
46         global $CFG;
48         $output  = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header'));
49         $output .= html_writer::start_tag('fieldset');
50         $output .= html_writer::tag('legend', get_string('commontasks', 'calendar'));
51         $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get'));
53         $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
55         $output .= html_writer::start_tag('div', array('class'=>'indent'));
56         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked'));
57         $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all'));
58         $output .= html_writer::empty_tag('br');
59         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses'));
60         $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course'));
61         $output .= html_writer::empty_tag('br');
62         $output .= html_writer::end_tag('div');
64         $output .= html_writer::tag('div', get_string('for', 'calendar').':');
66         $output .= html_writer::start_tag('div', array('class'=>'indent'));
67         if ($allowthisweek) {
68             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked'));
69             $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow'));
70             $output .= html_writer::empty_tag('br');
71         }
72         if ($allownextweek) {
73             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext'));
74             $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext'));
75             $output .= html_writer::empty_tag('br');
76         }
77         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow'));
78         $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow'));
79         $output .= html_writer::empty_tag('br');
80         if ($allownextmonth) {
81             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext'));
82             $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext'));
83             $output .= html_writer::empty_tag('br');
84         }
85         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming'));
86         $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc'));
87         $output .= html_writer::empty_tag('br');
89         if ($CFG->calendar_customexport) {
90             $a = new stdClass();
91             $now = time();
92             $time = $now - $CFG->calendar_exportlookback * DAYSECS;
93             $a->timestart = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
94             $time = $now + $CFG->calendar_exportlookahead * DAYSECS;
95             $a->timeend = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
96             $output .= html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'preset_time', 'id' => 'pt_custom', 'value' => 'custom'));
97             $output .= html_writer::tag('label', get_string('customexport', 'calendar', $a), array('for' => 'pt_custom'));
98             $output .= html_writer::empty_tag('br');
99         }
101         $output .= html_writer::end_tag('div');
102         $output .= html_writer::start_tag('div', array('class'=>'rightalign'));
103         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>''));
104         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>''));
105         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>''));
106         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'userid', 'value'=>$userid));
107         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
109         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name' => 'generateurl', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar')));
110         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar')));
112         $output .= html_writer::end_tag('div');
114         $output .= html_writer::end_tag('form');
115         $output .= html_writer::end_tag('fieldset');
117         $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;'));
118         $output .= html_writer::tag('p', get_string('urlforical', 'calendar'));
119         $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;'));
120         $output .= html_writer::end_tag('div');
122         return $output;
123     }
125     /**
126      * Starts the standard layout for the page
127      *
128      * @return string
129      */
130     public function start_layout() {
131         return html_writer::start_tag('div', array('class'=>'maincalendar'));
132     }
134     /**
135      * Creates the remainder of the layout
136      *
137      * @return string
138      */
139     public function complete_layout() {
140         return html_writer::end_tag('div');
141     }
143     /**
144      * Produces the content for the filters block (pretend block)
145      *
146      * @param int $courseid
147      * @param int $day
148      * @param int $month
149      * @param int $year
150      * @param int $view
151      * @param int $courses
152      * @return string
153      */
154     public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
155         $returnurl = $this->page->url;
156         $returnurl->param('course', $courseid);
157         return html_writer::tag('div', calendar_filter_controls($returnurl), array('class'=>'calendar_filters filters'));
158     }
160     /**
161      * Produces the content for the three months block (pretend block)
162      *
163      * This includes the previous month, the current month, and the next month
164      *
165      * @param calendar_information $calendar
166      * @return string
167      */
168     public function fake_block_threemonths(calendar_information $calendar) {
169         // Get the calendar type we are using.
170         $calendartype = \core_calendar\type_factory::get_calendar_instance();
172         $date = $calendartype->timestamp_to_date_array($calendar->time);
174         $prevmonth = calendar_sub_month($date['mon'], $date['year']);
175         $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], $date['mday']);
176         $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day']);
178         $nextmonth = calendar_add_month($date['mon'], $date['year']);
179         $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], $date['mday']);
180         $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day']);
182         $content  = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
183         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $prevmonthtime);
184         $content .= html_writer::end_tag('div');
185         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
186         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $calendar->time);
187         $content .= html_writer::end_tag('div');
188         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
189         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $nextmonthtime);
190         $content .= html_writer::end_tag('div');
191         return $content;
192     }
194     /**
195      * Adds a pretent calendar block
196      *
197      * @param block_contents $bc
198      * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
199      */
200     public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
201         $this->page->blocks->add_fake_block($bc, $pos);
202     }
204     /**
205      * Creates a button to add a new event
206      *
207      * @param int $courseid
208      * @param int $day
209      * @param int $month
210      * @param int $year
211      * @param int $time the unixtime, used for multiple calendar support. The values $day,
212      *     $month and $year are kept for backwards compatibility.
213      * @return string
214      */
215     protected function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) {
216         // If a day, month and year were passed then convert it to a timestamp. If these were passed
217         // then we can assume the day, month and year are passed as Gregorian, as no where in core
218         // should we be passing these values rather than the time. This is done for BC.
219         if (!empty($day) && !empty($month) && !empty($year)) {
220             if (checkdate($month, $day, $year)) {
221                 $time = make_timestamp($year, $month, $day);
222             } else {
223                 $time = time();
224             }
225         } else if (empty($time)) {
226             $time = time();
227         }
229         $output = html_writer::start_tag('div', array('class'=>'buttons'));
230         $output .= html_writer::start_tag('form', array('action' => CALENDAR_URL . 'event.php', 'method' => 'get'));
231         $output .= html_writer::start_tag('div');
232         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'action', 'value' => 'new'));
233         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'course', 'value' => $courseid));
234         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'time', 'value' => $time));
235         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value' => get_string('newevent', 'calendar')));
236         $output .= html_writer::end_tag('div');
237         $output .= html_writer::end_tag('form');
238         $output .= html_writer::end_tag('div');
239         return $output;
240     }
242     /**
243      * Displays the calendar for a single day
244      *
245      * @param calendar_information $calendar
246      * @return string
247      */
248     public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
250         if ($returnurl === null) {
251             $returnurl = $this->page->url;
252         }
254         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
256         $output  = html_writer::start_tag('div', array('class'=>'header'));
257         if (calendar_user_can_add_event($calendar->course)) {
258             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
259         }
260         //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump'));
261         $output .= $this->course_filter_selector($returnurl, get_string('dayview', 'calendar'));
262         $output .= html_writer::end_tag('div');
263         // Controls
264         $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class'=>'controls'));
266         if (empty($events)) {
267             // There is nothing to display today.
268             $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
269         } else {
270             $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
271             $underway = array();
272             // First, print details about events that start today
273             foreach ($events as $event) {
274                 $event = new calendar_event($event);
275                 $event->calendarcourseid = $calendar->courseid;
276                 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) {  // Print it now
277                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
278                     $output .= $this->event($event);
279                 } else {                                                                 // Save this for later
280                     $underway[] = $event;
281                 }
282             }
284             // Then, show a list of all events that just span this day
285             if (!empty($underway)) {
286                 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
287                 foreach ($underway as $event) {
288                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
289                     $output .= $this->event($event);
290                 }
291             }
293             $output .= html_writer::end_tag('div');
294         }
296         return $output;
297     }
299     /**
300      * Displays an event
301      *
302      * @param calendar_event $event
303      * @param bool $showactions
304      * @return string
305      */
306     public function event(calendar_event $event, $showactions=true) {
307         global $CFG;
309         $event = calendar_add_event_metadata($event);
311         $anchor  = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
313         $table = new html_table();
314         $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
315         $table->data = array(
316             0 => new html_table_row(),
317             1 => new html_table_row(),
318         );
320         if (!empty($event->icon)) {
321             $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
322         } else {
323             $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
324         }
325         $table->data[0]->cells[0]->attributes['class'] .= ' picture';
327         $table->data[0]->cells[1] = new html_table_cell();
328         $table->data[0]->cells[1]->attributes['class'] .= ' topic';
329         if (!empty($event->referer)) {
330             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
331         } else {
332             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
333         }
334         if (!empty($event->courselink)) {
335             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
336         }
337         // Show subscription source if needed.
338         if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
339             if (!empty($event->subscription->url)) {
340                 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
341             } else {
342                 // File based ical.
343                 $source = get_string('subsource', 'calendar', $event->subscription);
344             }
345             $table->data[0]->cells[1]->text .= html_writer::tag('div', $source, array('class' => 'subscription'));
346         }
347         if (!empty($event->time)) {
348             $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
349         } else {
350             $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
351         }
353         $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
354         $table->data[1]->cells[0]->attributes['class'] .= 'side';
356         $table->data[1]->cells[1] = new html_table_cell($event->description);
357         $table->data[1]->cells[1]->attributes['class'] .= ' description';
358         if (isset($event->cssclass)) {
359             $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
360         }
362         if (calendar_edit_event_allowed($event) && $showactions) {
363             if (empty($event->cmid)) {
364                 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
365                 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
366                 if (!empty($event->calendarcourseid)) {
367                     $editlink->param('course', $event->calendarcourseid);
368                     $deletelink->param('course', $event->calendarcourseid);
369                 }
370             } else {
371                 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
372                 $deletelink = null;
373             }
375             $commands  = html_writer::start_tag('div', array('class'=>'commands'));
376             $commands .= html_writer::start_tag('a', array('href'=>$editlink));
377             $commands .= html_writer::empty_tag('img', array('src'=>$this->output->pix_url('t/edit'), 'alt'=>get_string('tt_editevent', 'calendar'), 'title'=>get_string('tt_editevent', 'calendar')));
378             $commands .= html_writer::end_tag('a');
379             if ($deletelink != null) {
380                 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
381                 $commands .= html_writer::empty_tag('img', array('src'=>$this->output->pix_url('t/delete'), 'alt'=>get_string('tt_deleteevent', 'calendar'), 'title'=>get_string('tt_deleteevent', 'calendar')));
382                 $commands .= html_writer::end_tag('a');
383             }
384             $commands .= html_writer::end_tag('div');
385             $table->data[1]->cells[1]->text .= $commands;
386         }
387         return html_writer::table($table);
388     }
390     /**
391      * Displays a month in detail
392      *
393      * @param calendar_information $calendar
394      * @param moodle_url $returnurl the url to return to
395      * @return string
396      */
397     public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl  = null) {
398         global $CFG;
400         if (empty($returnurl)) {
401             $returnurl = $this->page->url;
402         }
404         // Get the calendar type we are using.
405         $calendartype = \core_calendar\type_factory::get_calendar_instance();
407         // Store the display settings.
408         $display = new stdClass;
409         $display->thismonth = false;
411         // Get the specified date in the calendar type being used.
412         $date = $calendartype->timestamp_to_date_array($calendar->time);
413         $thisdate = $calendartype->timestamp_to_date_array(time());
414         if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
415             $display->thismonth = true;
416             $date = $thisdate;
417             $calendar->time = time();
418         }
420         // Get Gregorian date.
421         $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
423         // Store the gregorian year and month to be used later.
424         list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
425             $gregoriandate['hour'], $gregoriandate['minute']);
427         // Get the starting week day for this month.
428         $startwday = dayofweek(1, $date['mon'], $date['year']);
429         // Get the days in a week.
430         $daynames = calendar_get_days();
431         // Store the number of days in a week.
432         $numberofdaysinweek = $calendartype->get_num_weekdays();
434         $display->minwday = calendar_get_starting_weekday();
435         $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
436         $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
438         // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
439         $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
440         $display->tend = make_timestamp($gy, $gm, $display->maxdays, 23, 59, 59);
442         // Align the starting weekday to fall in our display range
443         // This is simple, not foolproof.
444         if ($startwday < $display->minwday) {
445             $startwday += $numberofdaysinweek;
446         }
448         // Get events from database
449         $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses);
450         if (!empty($events)) {
451             foreach($events as $eventid => $event) {
452                 $event = new calendar_event($event);
453                 if (!empty($event->modulename)) {
454                     $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
455                     if (!groups_course_module_visible($cm)) {
456                         unset($events[$eventid]);
457                     }
458                 }
459             }
460         }
462         // Extract information: events vs. time
463         calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
465         $output  = html_writer::start_tag('div', array('class'=>'header'));
466         if (calendar_user_can_add_event($calendar->course)) {
467             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
468         }
469         $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl);
470         $output .= html_writer::end_tag('div', array('class'=>'header'));
471         // Controls
472         $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls'));
474         $table = new html_table();
475         $table->attributes = array('class'=>'calendarmonth calendartable');
476         $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
477         $table->data = array();
479         // Get the day names as the header.
480         $header = array();
481         for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
482             $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
483         }
484         $table->head = $header;
486         // For the table display. $week is the row; $dayweek is the column.
487         $week = 1;
488         $dayweek = $startwday;
490         $row = new html_table_row(array());
492         // Paddding (the first week may have blank days in the beginning)
493         for($i = $display->minwday; $i < $startwday; ++$i) {
494             $cell = new html_table_cell('&nbsp;');
495             $cell->attributes = array('class'=>'nottoday dayblank');
496             $row->cells[] = $cell;
497         }
499         // Now display all the calendar
500         $weekend = CALENDAR_DEFAULT_WEEKEND;
501         if (isset($CFG->calendar_weekend)) {
502             $weekend = intval($CFG->calendar_weekend);
503         }
505         $daytime = $display->tstart - DAYSECS;
506         for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
507             $daytime = $daytime + DAYSECS;
508             if($dayweek > $display->maxwday) {
509                 // We need to change week (table row)
510                 $table->data[] = $row;
511                 $row = new html_table_row(array());
512                 $dayweek = $display->minwday;
513                 ++$week;
514             }
516             // Reset vars
517             $cell = new html_table_cell();
518             $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
520             $cellclasses = array();
522             if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
523                 // Weekend. This is true no matter what the exact range is.
524                 $cellclasses[] = 'weekend';
525             }
527             // Special visual fx if an event is defined
528             if (isset($eventsbyday[$day])) {
529                 if(count($eventsbyday[$day]) == 1) {
530                     $title = get_string('oneevent', 'calendar');
531                 } else {
532                     $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
533                 }
534                 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title'=>$title)), array('class'=>'day'));
535             } else {
536                 $cell->text = html_writer::tag('div', $day, array('class'=>'day'));
537             }
539             // Special visual fx if an event spans many days
540             $durationclass = false;
541             if (isset($typesbyday[$day]['durationglobal'])) {
542                 $durationclass = 'duration_global';
543             } else if (isset($typesbyday[$day]['durationcourse'])) {
544                 $durationclass = 'duration_course';
545             } else if (isset($typesbyday[$day]['durationgroup'])) {
546                 $durationclass = 'duration_group';
547             } else if (isset($typesbyday[$day]['durationuser'])) {
548                 $durationclass = 'duration_user';
549             }
550             if ($durationclass) {
551                 $cellclasses[] = 'duration';
552                 $cellclasses[] = $durationclass;
553             }
555             // Special visual fx for today
556             if ($display->thismonth && $day == $date['mday']) {
557                 $cellclasses[] = 'day today';
558             } else {
559                 $cellclasses[] = 'day nottoday';
560             }
561             $cell->attributes = array('class'=>join(' ',$cellclasses));
563             if (isset($eventsbyday[$day])) {
564                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
565                 foreach($eventsbyday[$day] as $eventindex) {
566                     // If event has a class set then add it to the event <li> tag
567                     $attributes = array();
568                     if (!empty($events[$eventindex]->class)) {
569                         $attributes['class'] = $events[$eventindex]->class;
570                     }
571                     $dayhref->set_anchor('event_'.$events[$eventindex]->id);
572                     $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
573                     $cell->text .= html_writer::tag('li', $link, $attributes);
574                 }
575                 $cell->text .= html_writer::end_tag('ul');
576             }
577             if (isset($durationbyday[$day])) {
578                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
579                 foreach($durationbyday[$day] as $eventindex) {
580                     $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
581                 }
582                 $cell->text .= html_writer::end_tag('ul');
583             }
584             $row->cells[] = $cell;
585         }
587         // Paddding (the last week may have blank days at the end)
588         for($i = $dayweek; $i <= $display->maxwday; ++$i) {
589             $cell = new html_table_cell('&nbsp;');
590             $cell->attributes = array('class'=>'nottoday dayblank');
591             $row->cells[] = $cell;
592         }
593         $table->data[] = $row;
594         $output .= html_writer::table($table);
596         return $output;
597     }
599     /**
600      * Displays a filter selection table
601      *
602      * @param calendar_information $calendar
603      * @return string
604      * @deprecated since Moodle 2.4 MDL-32309
605      * @see calendar_filter_controls()
606      */
607     protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
608         global $SESSION;
609         debugging('Method core_calendar_renderer::filter_selection_table() is deprecated, please use '.
610                 'calendar_filter_controls() instead', DEBUG_DEVELOPER);
612         if ($returnurl === null) {
613             $returnurl = $this->page->url;
614         }
616         $output  = html_writer::start_tag('div', array('class'=>'filters'));
617         $output .= html_writer::start_tag('table');
618         $output .= html_writer::start_tag('tr');
620         // Global events
621         $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
622         $strglobalevents = get_string('globalevents', 'calendar');
623         if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
624             $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
625             $output .= html_writer::tag('td', html_writer::tag('strong', $strglobalevents).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar').'<span class="accesshide">'.$strglobalevents.'</span>').')');
626         } else {
627             $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
628             $output .= html_writer::tag('td', html_writer::tag('strong', $strglobalevents).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar').'<span class="accesshide">'.$strglobalevents.'</span>').')');
629         }
631         // Course events
632         $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
633         $strcourseevents = get_string('courseevents', 'calendar');
634         if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
635             $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
636             $output .= html_writer::tag('td', html_writer::tag('strong', $strcourseevents).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar').'<span class="accesshide">'.$strcourseevents.'</span>').')');
637         } else {
638             $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
639             $output .= html_writer::tag('td', html_writer::tag('strong', $strcourseevents).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar').'<span class="accesshide">'.$strcourseevents.'</span>').')');
640         }
641         $output .= html_writer::end_tag('tr');
643         if(isloggedin() && !isguestuser()) {
644             $output .= html_writer::start_tag('tr');
645             // Group events
646             $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
647             $strgroupevents = get_string('groupevents', 'calendar');
648             if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
649                 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
650                 $output .= html_writer::tag('td', html_writer::tag('strong', $strgroupevents).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar').'<span class="accesshide">'.$strgroupevents.'</span>').')');
651             } else {
652                 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
653                 $output .= html_writer::tag('td', html_writer::tag('strong', $strgroupevents).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar').'<span class="accesshide">'.$strgroupevents.'</span>').')');
654             }
655             // User events
656             $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
657             $struserevents = get_string('userevents', 'calendar');
658             if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
659                 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
660                 $output .= html_writer::tag('td', html_writer::tag('strong', $struserevents).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar').'<span class="accesshide">'.$struserevents.'</span>').')');
661             } else {
662                 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
663                 $output .= html_writer::tag('td', html_writer::tag('strong', $struserevents).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar').'<span class="accesshide">'.$struserevents.'</span>').')');
664             }
665             $output .= html_writer::end_tag('tr');
666         }
667         $output .= html_writer::end_tag('table');
668         $output .= html_writer::end_tag('div');
669         return $output;
670     }
672     /**
673      * Displays upcoming events
674      *
675      * @param calendar_information $calendar
676      * @param int $futuredays
677      * @param int $maxevents
678      * @return string
679      */
680     public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
682         if ($returnurl === null) {
683             $returnurl = $this->page->url;
684         }
686         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
688         $output  = html_writer::start_tag('div', array('class'=>'header'));
689         if (calendar_user_can_add_event($calendar->course)) {
690             $output .= $this->add_event_button($calendar->course->id);
691         }
692         $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
693         $output .= $this->course_filter_selector($returnurl);
694         $output .= html_writer::end_tag('div');
696         if ($events) {
697             $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
698             foreach ($events as $event) {
699                 // Convert to calendar_event object so that we transform description
700                 // accordingly
701                 $event = new calendar_event($event);
702                 $event->calendarcourseid = $calendar->courseid;
703                 $output .= $this->event($event);
704             }
705             $output .= html_writer::end_tag('div');
706         } else {
707             $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
708         }
710         return $output;
711     }
713     /**
714      * Displays a course filter selector
715      *
716      * @param array $getvars
717      * @return string
718      */
719     protected function course_filter_selector(moodle_url $returnurl, $label=null) {
720         global $USER, $SESSION, $CFG;
722         if (!isloggedin() or isguestuser()) {
723             return '';
724         }
726         if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
727             $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
728         } else {
729             $courses = enrol_get_my_courses();
730         }
732         unset($courses[SITEID]);
734         $courseoptions = array();
735         $courseoptions[SITEID] = get_string('fulllistofcourses');
736         foreach ($courses as $course) {
737             $coursecontext = context_course::instance($course->id);
738             $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
739         }
741         if ($this->page->course->id !== SITEID) {
742             $selected = $this->page->course->id;
743         } else {
744             $selected = '';
745         }
746         $select = new single_select(new moodle_url(CALENDAR_URL.'set.php', array('return' => base64_encode($returnurl->out(false)), 'var' => 'setcourse', 'sesskey'=>sesskey())), 'id', $courseoptions, $selected, null);
747         $select->class = 'cal_courses_flt';
748         if ($label !== null) {
749             $select->set_label($label);
750         } else {
751             $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
752         }
753         return $this->output->render($select);
754     }
756     /**
757      * Renders a table containing information about calendar subscriptions.
758      *
759      * @param int $courseid
760      * @param array $subscriptions
761      * @param string $importresults
762      * @return string
763      */
764     public function subscription_details($courseid, $subscriptions, $importresults = '') {
765         $table = new html_table();
766         $table->head  = array(
767             get_string('colcalendar', 'calendar'),
768             get_string('collastupdated', 'calendar'),
769             get_string('eventkind', 'calendar'),
770             get_string('colpoll', 'calendar'),
771             get_string('colactions', 'calendar')
772         );
773         $table->align = array('left', 'left', 'left', 'center');
774         $table->width = '100%';
775         $table->data  = array();
777         if (empty($subscriptions)) {
778             $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
779             $cell->colspan = 4;
780             $table->data[] = new html_table_row(array($cell));
781         }
782         $strnever = new lang_string('never', 'calendar');
783         foreach ($subscriptions as $sub) {
784             $label = $sub->name;
785             if (!empty($sub->url)) {
786                 $label = html_writer::link($sub->url, $label);
787             }
788             if (empty($sub->lastupdated)) {
789                 $lastupdated = $strnever->out();
790             } else {
791                 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
792             }
794             $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
795             $cell->colspan = 2;
796             $type = $sub->eventtype . 'events';
798             $table->data[] = new html_table_row(array(
799                 new html_table_cell($label),
800                 new html_table_cell($lastupdated),
801                 new html_table_cell(get_string($type, 'calendar')),
802                 $cell
803             ));
804         }
806         $out  = $this->output->box_start('generalbox calendarsubs');
808         $out .= $importresults;
809         $out .= html_writer::table($table);
810         $out .= $this->output->box_end();
811         return $out;
812     }
814     /**
815      * Creates a form to perform actions on a given subscription.
816      *
817      * @param stdClass $subscription
818      * @param int $courseid
819      * @return string
820      */
821     protected function subscription_action_form($subscription, $courseid) {
822         // Assemble form for the subscription row.
823         $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
824         if (empty($subscription->url)) {
825             // Don't update an iCal file, which has no URL.
826             $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
827         } else {
828             // Assemble pollinterval control.
829             $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
830             $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
831             foreach (calendar_get_pollinterval_choices() as $k => $v) {
832                 $attributes = array();
833                 if ($k == $subscription->pollinterval) {
834                     $attributes['selected'] = 'selected';
835                 }
836                 $attributes['value'] = $k;
837                 $html .= html_writer::tag('option', $v, $attributes);
838             }
839             $html .= html_writer::end_tag('select');
840             $html .= html_writer::end_tag('div');
841         }
842         $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
843         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
844         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
845         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
846         if (!empty($subscription->url)) {
847             $html .= html_writer::tag('button', get_string('update'), array('type'  => 'submit', 'name' => 'action',
848                                                                             'value' => CALENDAR_SUBSCRIPTION_UPDATE));
849         }
850         $html .= html_writer::tag('button', get_string('remove'), array('type'  => 'submit', 'name' => 'action',
851                                                                         'value' => CALENDAR_SUBSCRIPTION_REMOVE));
852         $html .= html_writer::end_tag('div');
853         $html .= html_writer::end_tag('form');
854         return $html;
855     }