Merge branch 'MDL-31829' of git://github.com/timhunt/moodle
[moodle.git] / calendar / renderer.php
blob312c572ec02a6912fe632c5bd4a6f6a089f37226
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 /**
27 * The primary renderer for the calendar.
29 class core_calendar_renderer extends plugin_renderer_base {
31 /**
32 * Creates a basic export form
34 * @param bool $allowthisweek
35 * @param bool $allownextweek
36 * @param bool $allownextmonth
37 * @param int $userid
38 * @param string $authtoken
39 * @return string
41 public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $userid, $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');
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');
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');
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'=>'userid', 'value'=>$userid));
90 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
92 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name' => 'generateurl', '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 return $output;
109 * Starts the standard layout for the page
111 * @return string
113 public function start_layout() {
114 return html_writer::start_tag('div', array('class'=>'maincalendar'));
118 * Creates the remainder of the layout
120 * @return string
122 public function complete_layout() {
123 return html_writer::end_tag('div');
127 * Produces the content for the filters block (pretend block)
129 * @param int $courseid
130 * @param int $day
131 * @param int $month
132 * @param int $year
133 * @param int $view
134 * @param int $courses
135 * @return string
137 public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
138 return html_writer::tag('div', calendar_filter_controls($this->page->url), array('class'=>'calendar_filters filters'));
142 * Produces the content for the three months block (pretend block)
144 * This includes the previous month, the current month, and the next month
146 * @param calendar_information $calendar
147 * @return string
149 public function fake_block_threemonths(calendar_information $calendar) {
151 list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year);
152 list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year);
154 $content = html_writer::start_tag('div', array('class'=>'minicalendarblock'));
155 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $prevmon, 'y' => $prevyr));
156 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $prevmon, $prevyr);
157 $content .= html_writer::end_tag('div');
158 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
159 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year));
160 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $calendar->month, $calendar->year);
161 $content .= html_writer::end_tag('div');
162 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
163 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $nextmon, 'y' => $nextyr));
164 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $nextmon, $nextyr);
165 $content .= html_writer::end_tag('div');
166 return $content;
170 * Adds a pretent calendar block
172 * @param block_contents $bc
173 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
175 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
176 $this->page->blocks->add_fake_block($bc, $pos);
180 * Creates a button to add a new event
182 * @param int $courseid
183 * @param int $day
184 * @param int $month
185 * @param int $year
186 * @return string
188 protected function add_event_button($courseid, $day=null, $month=null, $year=null) {
189 $output = html_writer::start_tag('div', array('class'=>'buttons'));
190 $output .= html_writer::start_tag('form', array('action'=>CALENDAR_URL.'event.php', 'method'=>'get'));
191 $output .= html_writer::start_tag('div');
192 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'new'));
193 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'course', 'value'=>$courseid));
194 if ($day !== null) {
195 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>$day));
197 if ($month !== null) {
198 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>$month));
200 if ($year !== null) {
201 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>$year));
203 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('newevent', 'calendar')));
204 $output .= html_writer::end_tag('div');
205 $output .= html_writer::end_tag('form');
206 $output .= html_writer::end_tag('div');
207 return $output;
211 * Displays the calendar for a single day
213 * @param calendar_information $calendar
214 * @return string
216 public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
218 if ($returnurl === null) {
219 $returnurl = $this->page->url;
222 $calendar->checkdate();
223 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
225 $output = html_writer::start_tag('div', array('class'=>'header'));
226 if (calendar_user_can_add_event($calendar->course)) {
227 $output .= $this->add_event_button($calendar->course->id, $calendar->day, $calendar->month, $calendar->year);
229 //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump'));
230 $output .= $this->course_filter_selector($returnurl, get_string('dayview', 'calendar'));
231 $output .= html_writer::end_tag('div');
232 // Controls
233 $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'));
235 if (empty($events)) {
236 // There is nothing to display today.
237 $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
238 } else {
239 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
240 $underway = array();
241 // First, print details about events that start today
242 foreach ($events as $event) {
243 $event = new calendar_event($event);
244 $event->calendarcourseid = $calendar->courseid;
245 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
246 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
247 $output .= $this->event($event);
248 } else { // Save this for later
249 $underway[] = $event;
253 // Then, show a list of all events that just span this day
254 if (!empty($underway)) {
255 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
256 foreach ($underway as $event) {
257 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
258 $output .= $this->event($event);
262 $output .= html_writer::end_tag('div');
265 return $output;
269 * Displays an event
271 * @param calendar_event $event
272 * @param bool $showactions
273 * @return string
275 public function event(calendar_event $event, $showactions=true) {
276 $event = calendar_add_event_metadata($event);
278 $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
280 $table = new html_table();
281 $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
282 $table->data = array(
283 0 => new html_table_row(),
284 1 => new html_table_row(),
287 if (!empty($event->icon)) {
288 $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
289 } else {
290 $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
292 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
294 $table->data[0]->cells[1] = new html_table_cell();
295 $table->data[0]->cells[1]->attributes['class'] .= ' topic';
296 if (!empty($event->referer)) {
297 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
298 } else {
299 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
301 if (!empty($event->courselink)) {
302 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
304 if (!empty($event->time)) {
305 $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
306 } else {
307 $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
310 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
311 $table->data[1]->cells[0]->attributes['class'] .= 'side';
313 $table->data[1]->cells[1] = new html_table_cell($event->description);
314 $table->data[1]->cells[1]->attributes['class'] .= ' description';
315 if (isset($event->cssclass)) {
316 $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
319 if (calendar_edit_event_allowed($event) && $showactions) {
320 if (empty($event->cmid)) {
321 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
322 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
323 if (!empty($event->calendarcourseid)) {
324 $editlink->param('course', $event->calendarcourseid);
325 $deletelink->param('course', $event->calendarcourseid);
327 } else {
328 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
329 $deletelink = null;
332 $commands = html_writer::start_tag('div', array('class'=>'commands'));
333 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
334 $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')));
335 $commands .= html_writer::end_tag('a');
336 if ($deletelink != null) {
337 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
338 $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')));
339 $commands .= html_writer::end_tag('a');
341 $commands .= html_writer::end_tag('div');
342 $table->data[1]->cells[1]->text .= $commands;
344 return html_writer::table($table);
348 * Displays a month in detail
350 * @param calendar_information $calendar
351 * @return string
353 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
354 global $CFG;
356 if (empty($returnurl)) {
357 $returnurl = $this->page->url;
360 $date = usergetdate(time());
362 $display = new stdClass;
363 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
364 $display->maxwday = $display->minwday + 6;
365 $display->thismonth = ($date['mon'] == $calendar->month);
366 $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year);
368 $startwday = 0;
369 if (get_user_timezone_offset() < 99) {
370 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
371 $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT
372 $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT
373 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
374 } else {
375 // no timezone info specified
376 $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year);
377 $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year);
378 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
381 // Align the starting weekday to fall in our display range
382 if ($startwday < $display->minwday) {
383 $startwday += 7;
386 // Get events from database
387 $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
388 if (!empty($events)) {
389 foreach($events as $eventid => $event) {
390 $event = new calendar_event($event);
391 if (!empty($event->modulename)) {
392 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
393 if (!groups_course_module_visible($cm)) {
394 unset($events[$eventid]);
400 // Extract information: events vs. time
401 calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
403 $output = html_writer::start_tag('div', array('class'=>'header'));
404 if (calendar_user_can_add_event($calendar->course)) {
405 $output .= $this->add_event_button($calendar->course->id, null, $calendar->month, $calendar->year);
407 $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl);
408 $output .= html_writer::end_tag('div', array('class'=>'header'));
409 // Controls
410 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
412 $days = calendar_get_days();
414 $table = new html_table();
415 $table->attributes = array('class'=>'calendarmonth calendartable');
416 $table->data = array();
418 $header = new html_table_row();
419 $header->attributes = array('class'=>'weekdays');
420 $header->cells = array();
421 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
422 // This uses the % operator to get the correct weekday no matter what shift we have
423 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
424 $cell = new html_table_cell(get_string($days[$i % 7], 'calendar'));
425 $cell->header = true;
426 $header->cells[] = $cell;
429 // For the table display. $week is the row; $dayweek is the column.
430 $week = 1;
431 $dayweek = $startwday;
433 // Create an array of all the week days.
434 $wdays = array(0 => '<strong>'. get_string('sunday', 'calendar'). '</strong>',
435 1 => '<strong>'. get_string('monday', 'calendar'). '</strong>',
436 2 => '<strong>'. get_string('tuesday', 'calendar'). '</strong>',
437 3 => '<strong>'. get_string('wednesday', 'calendar'). '</strong>',
438 4 => '<strong>'. get_string('thursday', 'calendar'). '</strong>',
439 5 => '<strong>'. get_string('friday', 'calendar'). '</strong>',
440 6 => '<strong>'. get_string('saturday', 'calendar'). '</strong>');
442 // Loop only if the day offset is greater than 0.
443 // This loop involves shifting the days around until the desired start day
444 // is at the start of the array.
445 $daycount = 0;
446 while ($display->minwday > $daycount++) {
447 $wdays_end = array_shift($wdays);
448 array_push($wdays, $wdays_end);
451 // Now we set the (modified) array to the table header to be displayed.
452 $table->head = $wdays;
454 $row = new html_table_row(array());
456 // Paddding (the first week may have blank days in the beginning)
457 for($i = $display->minwday; $i < $startwday; ++$i) {
458 $cell = new html_table_cell('&nbsp;');
459 $cell->attributes = array('class'=>'nottoday');
460 $row->cells[] = $cell;
463 // Now display all the calendar
464 $weekend = CALENDAR_DEFAULT_WEEKEND;
465 if (isset($CFG->calendar_weekend)) {
466 $weekend = intval($CFG->calendar_weekend);
469 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
470 if($dayweek > $display->maxwday) {
471 // We need to change week (table row)
472 $table->data[] = $row;
473 $row = new html_table_row(array());
474 $dayweek = $display->minwday;
475 ++$week;
478 // Reset vars
479 $cell = new html_table_cell();
480 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year);
482 $cellclasses = array();
484 if ($weekend & (1 << ($dayweek % 7))) {
485 // Weekend. This is true no matter what the exact range is.
486 $cellclasses[] = 'weekend';
489 // Special visual fx if an event is defined
490 if (isset($eventsbyday[$calendar->day])) {
491 if(count($eventsbyday[$calendar->day]) == 1) {
492 $title = get_string('oneevent', 'calendar');
493 } else {
494 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
496 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
497 } else {
498 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
501 // Special visual fx if an event spans many days
502 $durationclass = false;
503 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
504 $durationclass = 'duration_global';
505 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
506 $durationclass = 'duration_course';
507 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
508 $durationclass = 'duration_group';
509 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
510 $durationclass = 'duration_user';
512 if ($durationclass) {
513 $cellclasses[] = 'duration';
514 $cellclasses[] = $durationclass;
517 // Special visual fx for today
518 if($display->thismonth && $calendar->day == $calendar->day) {
519 $cellclasses[] = 'today';
520 } else {
521 $cellclasses[] = 'nottoday';
523 $cell->attributes = array('class'=>join(' ',$cellclasses));
525 if (isset($eventsbyday[$calendar->day])) {
526 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
527 foreach($eventsbyday[$calendar->day] as $eventindex) {
528 // If event has a class set then add it to the event <li> tag
529 $attributes = array();
530 if (!empty($events[$eventindex]->class)) {
531 $attributes['class'] = $events[$eventindex]->class;
533 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
534 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
535 $cell->text .= html_writer::tag('li', $link, $attributes);
537 $cell->text .= html_writer::end_tag('ul');
539 if (isset($durationbyday[$calendar->day])) {
540 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
541 foreach($durationbyday[$calendar->day] as $eventindex) {
542 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
544 $cell->text .= html_writer::end_tag('ul');
546 $row->cells[] = $cell;
549 // Paddding (the last week may have blank days at the end)
550 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
551 $cell = new html_table_cell('&nbsp;');
552 $cell->attributes = array('class'=>'nottoday');
553 $row->cells[] = $cell;
555 $table->data[] = $row;
556 $output .= html_writer::table($table);
558 // OK, now for the filtering display
559 $output .= $this->filter_selection_table($calendar);
560 return $output;
564 * Displays a filter selection table
566 * @param calendar_information $calendar
567 * @return string
569 protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
570 global $SESSION;
572 if ($returnurl === null) {
573 $returnurl = $this->page->url;
576 $output = html_writer::start_tag('div', array('class'=>'filters'));
577 $output .= html_writer::start_tag('table');
578 $output .= html_writer::start_tag('tr');
580 // Global events
581 $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
582 if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
583 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
584 $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')).')');
585 } else {
586 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
587 $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')).')');
590 // Course events
591 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
592 if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
593 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
594 $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')).')');
595 } else {
596 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
597 $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')).')');
599 $output .= html_writer::end_tag('tr');
601 if(isloggedin() && !isguestuser()) {
602 $output .= html_writer::start_tag('tr');
603 // Group events
604 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
605 if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
606 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
607 $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')).')');
608 } else {
609 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
610 $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')).')');
612 // User events
613 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
614 if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
615 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
616 $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')).')');
617 } else {
618 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
619 $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')).')');
621 $output .= html_writer::end_tag('tr');
623 $output .= html_writer::end_tag('table');
624 $output .= html_writer::end_tag('div');
625 return $output;
629 * Displays upcoming events
631 * @param calendar_information $calendar
632 * @param int $futuredays
633 * @param int $maxevents
634 * @return string
636 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
638 if ($returnurl === null) {
639 $returnurl = $this->page->url;
642 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
644 $output = html_writer::start_tag('div', array('class'=>'header'));
645 if (calendar_user_can_add_event($calendar->course)) {
646 $output .= $this->add_event_button($calendar->course->id);
648 $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
649 $output .= $this->course_filter_selector($returnurl);
650 $output .= html_writer::end_tag('div');
652 if ($events) {
653 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
654 foreach ($events as $event) {
655 // Convert to calendar_event object so that we transform description
656 // accordingly
657 $event = new calendar_event($event);
658 $event->calendarcourseid = $calendar->courseid;
659 $output .= $this->event($event);
661 $output .= html_writer::end_tag('div');
662 } else {
663 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
666 return $output;
670 * Displays a course filter selector
672 * @param array $getvars
673 * @return string
675 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
676 global $USER, $SESSION, $CFG;
678 if (!isloggedin() or isguestuser()) {
679 return '';
682 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) {
683 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
684 } else {
685 $courses = enrol_get_my_courses();
688 unset($courses[SITEID]);
690 $courseoptions = array();
691 $courseoptions[SITEID] = get_string('fulllistofcourses');
692 foreach ($courses as $course) {
693 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
694 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
697 if ($this->page->course->id !== SITEID) {
698 $selected = $this->page->course->id;
699 } else {
700 $selected = '';
702 $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);
703 $select->class = 'cal_courses_flt';
704 if ($label !== null) {
705 $select->label = $label;
707 return $this->output->render($select);