be082ab90501c254cb77b3b3d340d8fc5c989b28
[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 /**
27  * The primary renderer for the calendar.
28  */
29 class core_calendar_renderer extends plugin_renderer_base {
31     /**
32      * Creates a basic export form
33      *
34      * @param bool $allowthisweek
35      * @param bool $allownextweek
36      * @param bool $allownextmonth
37      * @param string $username
38      * @param string $authtoken
39      * @return string
40      */
41     public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $username, $authtoken) {
43         $output  = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header'));
44         $output .= html_writer::start_tag('fieldset');
45         $output .= html_writer::tag('legend', get_string('commontasks', 'calendar'));
46         $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get'));
48         $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
50         $output .= html_writer::start_tag('div', array('class'=>'indent'));
51         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked'));
52         $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all'));
53         $output .= html_writer::empty_tag('br');
54         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses'));
55         $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course'));
56         $output .= html_writer::empty_tag('br');
57         $output .= html_writer::end_tag('div');
59         $output .= html_writer::tag('div', get_string('for', 'calendar').':');
61         $output .= html_writer::start_tag('div', array('class'=>'indent'));
62         if ($allowthisweek) {
63             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked'));
64             $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow'));
65             $output .= html_writer::empty_tag('br');
66         }
67         if ($allownextweek) {
68             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext'));
69             $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext'));
70             $output .= html_writer::empty_tag('br');
71         }
72         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow'));
73         $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow'));
74         $output .= html_writer::empty_tag('br');
75         if ($allownextmonth) {
76             $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext'));
77             $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext'));
78             $output .= html_writer::empty_tag('br');
79         }
80         $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming'));
81         $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc'));
82         $output .= html_writer::empty_tag('br');
83         $output .= html_writer::end_tag('div');
85         $output .= html_writer::start_tag('div', array('class'=>'rightalign'));
86         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>''));
87         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>''));
88         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>''));
89         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'username', 'value'=>$username));
90         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
92         $output .= html_writer::empty_tag('input', array('type'=>'button', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar')));
93         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar')));
95         $output .= html_writer::end_tag('div');
97         $output .= html_writer::end_tag('form');
98         $output .= html_writer::end_tag('fieldset');
100         $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;'));
101         $output .= html_writer::tag('p', get_string('urlforical', 'calendar'));
102         $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;'));
103         $output .= html_writer::end_tag('div');
105         $this->page->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.init_basic_export', array($allowthisweek, $allownextweek, $allownextmonth, $username, $authtoken));
107         return $output;
108     }
110     /**
111      * Starts the standard layout for the page
112      *
113      * @return string
114      */
115     public function start_layout() {
116         return html_writer::start_tag('div', array('class'=>'maincalendar'));
117     }
119     /**
120      * Creates the remainder of the layout
121      *
122      * @return string
123      */
124     public function complete_layout() {
125         return html_writer::end_tag('div');
126     }
128     /**
129      * Produces the content for the filters block (pretend block)
130      *
131      * @param int $courseid
132      * @param int $day
133      * @param int $month
134      * @param int $year
135      * @param int $view
136      * @param int $courses
137      * @return string
138      */
139     public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
140         $getvars = 'id='.$courseid.'&amp;cal_d='.$day.'&amp;cal_m='.$month.'&amp;cal_y='.$year;
141         return html_writer::tag('div', calendar_filter_controls($view, $getvars, NULL, $courses), array('class'=>'calendar_filters filters'));
142     }
144     /**
145      * Produces the content for the three months block (pretend block)
146      *
147      * This includes the previous month, the current month, and the next month
148      *
149      * @param calendar_information $calendar
150      * @return string
151      */
152     public function fake_block_threemonths(calendar_information $calendar) {
154         list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year);
155         list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year);
157         $content  = html_writer::start_tag('div', array('class'=>'minicalendarblock'));
158         $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $prevmon, 'y' => $prevyr));
159         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $prevmon, $prevyr);
160         $content .= html_writer::end_tag('div');
161         $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
162         $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year));
163         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $calendar->month, $calendar->year);
164         $content .= html_writer::end_tag('div');
165         $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
166         $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $nextmon, 'y' => $nextyr));
167         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $nextmon, $nextyr);
168         $content .= html_writer::end_tag('div');
169         return $content;
170     }
172     /**
173      * Adds a pretent calendar block
174      *
175      * @param block_contents $bc
176      * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
177      */
178     public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
179         $this->page->blocks->add_pretend_block($bc, $pos);
180     }
182     /**
183      * Creates a button to add a new event
184      *
185      * @param int $courseid
186      * @param int $day
187      * @param int $month
188      * @param int $year
189      * @return string
190      */
191     protected function add_event_button($courseid, $day=null, $month=null, $year=null) {
192         $output = html_writer::start_tag('div', array('class'=>'buttons'));
193         $output .= html_writer::start_tag('form', array('action'=>CALENDAR_URL.'event.php', 'method'=>'get'));
194         $output .= html_writer::start_tag('div');
195         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'new'));
196         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'course', 'value'=>$courseid));
197         if ($day !== null) {
198             $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>$day));
199         }
200         if ($month !== null) {
201             $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>$month));
202         }
203         if ($year !== null) {
204             $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>$year));
205         }
206         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('newevent', 'calendar')));
207         $output .= html_writer::end_tag('div');
208         $output .= html_writer::end_tag('form');
209         $output .= html_writer::end_tag('div');
210         return $output;
211     }
213     /**
214      * Displays the calendar for a single day
215      *
216      * @param calendar_information $calendar
217      * @return string
218      */
219     public function show_day(calendar_information $calendar) {
220         $calendar->checkdate();
221         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
223         $output  = html_writer::start_tag('div', array('class'=>'header'));
224         if (!isguestuser() && isloggedin() && calendar_user_can_add_event()) {
225             $output .= $this->add_event_button($calendar->courseid, $calendar->day, $calendar->month, $calendar->year);
226         }
227         //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump'));
228         $output .= $this->course_filter_selector(array('from'=>'day', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year), get_string('dayview', 'calendar'));
229         $output .= html_writer::end_tag('div');
230         // Controls
231         $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'd' => $calendar->day, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
233         if (empty($events)) {
234             // There is nothing to display today.
235             $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
236         } else {
237             $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
238             $underway = array();
239             // First, print details about events that start today
240             foreach ($events as $event) {
241                 $event = new calendar_event($event);
242                 $event->calendarcourseid = $calendar->courseid;
243                 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) {  // Print it now
244                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
245                     $output .= $this->event($event);
246                 } else {                                                                 // Save this for later
247                     $underway[] = $event;
248                 }
249             }
251             // Then, show a list of all events that just span this day
252             if (!empty($underway)) {
253                 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
254                 foreach ($underway as $event) {
255                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
256                     $output .= $this->event($event);
257                 }
258             }
260             $output .= html_writer::end_tag('div');
261         }
263         return $output;
264     }
266     /**
267      * Displays an event
268      *
269      * @param calendar_event $event
270      * @param bool $showactions
271      * @return string
272      */
273     public function event(calendar_event $event, $showactions=true) {
274         $event = calendar_add_event_metadata($event);
276         $anchor  = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
278         $table = new html_table();
279         $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
280         $table->data = array(
281             0 => new html_table_row(),
282             1 => new html_table_row(),
283         );
285         if (!empty($event->icon)) {
286             $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
287         } else {
288             $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
289         }
290         $table->data[0]->cells[0]->attributes['class'] .= ' picture';
292         $table->data[0]->cells[1] = new html_table_cell();
293         $table->data[0]->cells[1]->attributes['class'] .= ' topic';
294         if (!empty($event->referer)) {
295             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
296         } else {
297             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
298         }
299         if (!empty($event->courselink)) {
300             $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
301         }
302         if (!empty($event->time)) {
303             $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
304         } else {
305             $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
306         }
308         $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
309         $table->data[1]->cells[0]->attributes['class'] .= 'side';
311         $table->data[1]->cells[1] = new html_table_cell($event->description);
312         $table->data[1]->cells[1]->attributes['class'] .= ' description';
313         if (isset($event->cssclass)) {
314             $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
315         }
317         if (calendar_edit_event_allowed($event) && $showactions) {
318             if (empty($event->cmid)) {
319                 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
320                 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
321                 if (!empty($event->calendarcourseid)) {
322                     $editlink->param('course', $event->calendarcourseid);
323                     $deletelink->param('course', $event->calendarcourseid);
324                 }
325             } else {
326                 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
327                 $deletelink = null;
328             }
330             $commands  = html_writer::start_tag('div', array('class'=>'commands'));
331             $commands .= html_writer::start_tag('a', array('href'=>$editlink));
332             $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')));
333             $commands .= html_writer::end_tag('a');
334             if ($deletelink != null) {
335                 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
336                 $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')));
337                 $commands .= html_writer::end_tag('a');
338             }
339             $commands .= html_writer::end_tag('div');
340             $table->data[1]->cells[1]->text .= $commands;
341         }
342         return html_writer::table($table);
343     }
345     /**
346      * Displays a month in detail
347      *
348      * @global array $CALENDARDAYS
349      * @param calendar_information $calendar
350      * @return string
351      */
352     public function show_month_detailed(calendar_information $calendar) {
353         global $CALENDARDAYS;
355         $date = usergetdate(time());
357         $display = new stdClass;
358         $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
359         $display->maxwday = $display->minwday + 6;
360         $display->thismonth = ($date['mon'] == $calendar->month);
361         $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year);
363         $startwday = 0;
364         if (get_user_timezone_offset() < 99) {
365             // We 'll keep these values as GMT here, and offset them when the time comes to query the db
366             $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT
367             $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT
368             $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
369         } else {
370             // no timezone info specified
371             $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year);
372             $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year);
373             $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
374         }
376         // Align the starting weekday to fall in our display range
377         if ($startwday < $display->minwday) {
378             $startwday += 7;
379         }
381         // Get events from database
382         $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
383         if (!empty($events)) {
384             foreach($events as $eventid => $event) {
385                 $event = new calendar_event($event);
386                 if (!empty($event->modulename)) {
387                     $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
388                     if (!groups_course_module_visible($cm)) {
389                         unset($events[$eventid]);
390                     }
391                 }
392             }
393         }
395         // Extract information: events vs. time
396         calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
398         $output  = html_writer::start_tag('div', array('class'=>'header'));
399         if(!isguestuser() && isloggedin() && calendar_user_can_add_event()) {
400             $output .= $this->add_event_button($calendar->courseid, null, $calendar->month, $calendar->year);
401         }
402         $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector(array('from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
403         $output .= html_writer::end_tag('div', array('class'=>'header'));
404         // Controls
405         $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
407         $table = new html_table();
408         $table->attributes = array('class'=>'calendarmonth calendartable');
409         $table->data = array();
411         $header = new html_table_row();
412         $header->attributes = array('class'=>'weekdays');
413         $header->cells = array();
414         for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
415             // This uses the % operator to get the correct weekday no matter what shift we have
416             // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
417             $cell = new html_table_cell(get_string($CALENDARDAYS[$i % 7], 'calendar'));
418             $cell->header = true;
419             $header->cells[] = $cell;
420         }
422         // For the table display. $week is the row; $dayweek is the column.
423         $week = 1;
424         $dayweek = $startwday;
426         $row = new html_table_row(array());
427         // Paddding (the first week may have blank days in the beginning)
428         for($i = $display->minwday; $i < $startwday; ++$i) {
429             $cell = new html_table_cell('&nbsp;');
430             $cell->attributes = array('class'=>'nottoday');
431             $row->cells[] = $cell;
432         }
434         // Now display all the calendar
435         for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
436             if($dayweek > $display->maxwday) {
437                 // We need to change week (table row)
438                 $table->data[] = $row;
439                 $row = new html_table_row(array());
440                 $dayweek = $display->minwday;
441                 ++$week;
442             }
444             // Reset vars
445             $cell = new html_table_cell();
446             $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year);
448             $cellclasses = array();
450             if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
451                 // Weekend. This is true no matter what the exact range is.
452                 $cellclasses[] = 'weekend';
453             }
455             // Special visual fx if an event is defined
456             if (isset($eventsbyday[$calendar->day])) {
457                 if(count($eventsbyday[$calendar->day]) == 1) {
458                     $title = get_string('oneevent', 'calendar');
459                 } else {
460                     $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
461                 }
462                 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
463             } else {
464                 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
465             }
467             // Special visual fx if an event spans many days
468             $durationclass = false;
469             if (isset($typesbyday[$calendar->day]['durationglobal'])) {
470                 $durationclass = 'duration_global';
471             } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
472                 $durationclass = 'duration_course';
473             } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
474                 $durationclass = 'duration_group';
475             } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
476                 $durationclass = 'duration_user';
477             }
478             if ($durationclass) {
479                 $cellclasses[] = 'duration';
480                 $cellclasses[] = $durationclass;
481             }
483             // Special visual fx for today
484             if($display->thismonth && $calendar->day == $calendar->day) {
485                 $cellclasses[] = 'today';
486             } else {
487                 $cellclasses[] = 'nottoday';
488             }
489             $cell->attributes = array('class'=>join(' ',$cellclasses));
491             if (isset($eventsbyday[$calendar->day])) {
492                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
493                 foreach($eventsbyday[$calendar->day] as $eventindex) {
494                     // If event has a class set then add it to the event <li> tag
495                     $attributes = array();
496                     if (!empty($events[$eventindex]->class)) {
497                         $attributes['class'] = $events[$eventindex]->class;
498                     }
499                     $dayhref->set_anchor('event_'.$events[$eventindex]->id);
500                     $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
501                     $cell->text .= html_writer::tag('li', $link, $attributes);
502                 }
503                 $cell->text .= html_writer::end_tag('ul');
504             }
505             if (isset($durationbyday[$calendar->day])) {
506                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
507                 foreach($durationbyday[$calendar->day] as $eventindex) {
508                     $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
509                 }
510                 $cell->text .= html_writer::end_tag('ul');
511             }
512             $row->cells[] = $cell;
513         }
515         // Paddding (the last week may have blank days at the end)
516         for($i = $dayweek; $i <= $display->maxwday; ++$i) {
517             $cell = new html_table_cell('&nbsp;');
518             $cell->attributes = array('class'=>'nottoday');
519             $row->cells[] = $cell;
520         }
521         $table->data[] = $row;
522         $output .= html_writer::table($table);
524         // OK, now for the filtering display
525         $output .= $this->filter_selection_table($calendar);
526         return $output;
527     }
529     /**
530      * Displays a filter selection table
531      *
532      * @param calendar_information $calendar
533      * @return string
534      */
535     protected function filter_selection_table(calendar_information $calendar) {
536         global $SESSION;
538         $output  = html_writer::start_tag('div', array('class'=>'filters'));
539         $output .= html_writer::start_tag('table');
540         $output .= html_writer::start_tag('tr');
542         // Global events
543         $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showglobal', 'from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
544         if($SESSION->cal_show_global) {
545             $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
546             $output .= html_writer::tag('td', html_writer::tag('strong', get_string('globalevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')');
547         } else {
548             $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
549             $output .= html_writer::tag('td', html_writer::tag('strong', get_string('globalevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')');
550         }
552         // Course events
553         $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
554         if(!empty($SESSION->cal_show_course)) {
555             $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
556             $output .= html_writer::tag('td', html_writer::tag('strong', get_string('courseevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')');
557         } else {
558             $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
559             $output .= html_writer::tag('td', html_writer::tag('strong', get_string('courseevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')');
560         }
561         $output .= html_writer::end_tag('tr');
563         if(isloggedin() && !isguestuser()) {
564             $output .= html_writer::start_tag('tr');
565             // Group events
566             $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
567             if($SESSION->cal_show_groups) {
568                 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
569                 $output .= html_writer::tag('td', html_writer::tag('strong', get_string('groupevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')');
570             } else {
571                 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
572                 $output .= html_writer::tag('td', html_writer::tag('strong', get_string('groupevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')');
573             }
574             // User events
575             $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
576             if($SESSION->cal_show_user) {
577                 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
578                 $output .= html_writer::tag('td', html_writer::tag('strong', get_string('userevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')');
579             } else {
580                 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
581                 $output .= html_writer::tag('td', html_writer::tag('strong', get_string('userevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')');
582             }
583             $output .= html_writer::end_tag('tr');
584         }
585         $output .= html_writer::end_tag('table');
586         $output .= html_writer::end_tag('div');
587         return $output;
588     }
590     /**
591      * Displays upcoming events
592      *
593      * @param calendar_information $calendar
594      * @param int $futuredays
595      * @param int $maxevents
596      * @return string
597      */
598     public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents) {
599         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
601         $output  = html_writer::start_tag('div', array('class'=>'header'));
602         if (!isguestuser() && isloggedin() && calendar_user_can_add_event()) {
603             $output .= $this->add_event_button($calendar->courseid);
604         }
605         $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
606         $output .= $this->course_filter_selector(array('from'=>'upcoming'));
607         $output .= html_writer::end_tag('div');
609         if ($events) {
610             $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
611             foreach ($events as $event) {
612                 // Convert to calendar_event object so that we transform description
613                 // accordingly
614                 $event = new calendar_event($event);
615                 $event->calendarcourseid = $calendar->courseid;
616                 $output .= $this->event($event);
617             }
618             $output .= html_writer::end_tag('div');
619         } else {
620             $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
621         }
623         return $output;
624     }
626     /**
627      * Displays a course filter selector
628      *
629      * @param array $getvars
630      * @return string
631      */
632     protected function course_filter_selector(array $getvars = array(), $label=null) {
633         global $USER, $SESSION, $CFG;
635         if (!isloggedin() or isguestuser()) {
636             return '';
637         }
639         if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) {
640             $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
641         } else {
642             $courses = enrol_get_my_courses();
643         }
645         unset($courses[SITEID]);
647         $courseoptions = array();
648         $courseoptions[SITEID] = get_string('fulllistofcourses');
649         foreach ($courses as $course) {
650             $courseoptions[$course->id] = format_string($course->shortname);
651         }
653         if (is_numeric($SESSION->cal_courses_shown)) {
654             $selected = $SESSION->cal_courses_shown;
655         } else {
656             $selected = '';
657         }
658         $getvars['var'] = 'setcourse';
659         $select = new single_select(new moodle_url(CALENDAR_URL.'set.php', $getvars), 'id', $courseoptions, $selected, null);
660         $select->class = 'cal_courses_flt';
661         if ($label !== null) {
662             $select->label = $label;
663         }
664         return $this->output->render($select);
665     }