MDL-32569 drop migrated module tests
[moodle.git] / calendar / renderer.php
blob4fe8301b8bf41f293476e59b6825cfe229aa01ef
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 $time = make_timestamp($calendar->year, $calendar->month);
417 $table->summary = get_string('calendarheading', 'calendar', userdate($time, get_string('strftimemonthyear')));
418 $table->data = array();
420 $header = new html_table_row();
421 $header->attributes = array('class'=>'weekdays');
422 $header->cells = array();
423 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
424 // This uses the % operator to get the correct weekday no matter what shift we have
425 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
426 $cell = new html_table_cell(get_string($days[$i % 7], 'calendar'));
427 $cell->header = true;
428 $header->cells[] = $cell;
431 // For the table display. $week is the row; $dayweek is the column.
432 $week = 1;
433 $dayweek = $startwday;
435 // Create an array of all the week days.
436 $wdays = array(0 => '<strong>'. get_string('sunday', 'calendar'). '</strong>',
437 1 => '<strong>'. get_string('monday', 'calendar'). '</strong>',
438 2 => '<strong>'. get_string('tuesday', 'calendar'). '</strong>',
439 3 => '<strong>'. get_string('wednesday', 'calendar'). '</strong>',
440 4 => '<strong>'. get_string('thursday', 'calendar'). '</strong>',
441 5 => '<strong>'. get_string('friday', 'calendar'). '</strong>',
442 6 => '<strong>'. get_string('saturday', 'calendar'). '</strong>');
444 // Loop only if the day offset is greater than 0.
445 // This loop involves shifting the days around until the desired start day
446 // is at the start of the array.
447 $daycount = 0;
448 while ($display->minwday > $daycount++) {
449 $wdays_end = array_shift($wdays);
450 array_push($wdays, $wdays_end);
453 // Now we set the (modified) array to the table header to be displayed.
454 $table->head = $wdays;
456 $row = new html_table_row(array());
458 // Paddding (the first week may have blank days in the beginning)
459 for($i = $display->minwday; $i < $startwday; ++$i) {
460 $cell = new html_table_cell('&nbsp;');
461 $cell->attributes = array('class'=>'nottoday');
462 $row->cells[] = $cell;
465 // Now display all the calendar
466 $weekend = CALENDAR_DEFAULT_WEEKEND;
467 if (isset($CFG->calendar_weekend)) {
468 $weekend = intval($CFG->calendar_weekend);
471 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
472 if($dayweek > $display->maxwday) {
473 // We need to change week (table row)
474 $table->data[] = $row;
475 $row = new html_table_row(array());
476 $dayweek = $display->minwday;
477 ++$week;
480 // Reset vars
481 $cell = new html_table_cell();
482 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year);
484 $cellclasses = array();
486 if ($weekend & (1 << ($dayweek % 7))) {
487 // Weekend. This is true no matter what the exact range is.
488 $cellclasses[] = 'weekend';
491 // Special visual fx if an event is defined
492 if (isset($eventsbyday[$calendar->day])) {
493 if(count($eventsbyday[$calendar->day]) == 1) {
494 $title = get_string('oneevent', 'calendar');
495 } else {
496 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
498 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
499 } else {
500 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
503 // Special visual fx if an event spans many days
504 $durationclass = false;
505 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
506 $durationclass = 'duration_global';
507 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
508 $durationclass = 'duration_course';
509 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
510 $durationclass = 'duration_group';
511 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
512 $durationclass = 'duration_user';
514 if ($durationclass) {
515 $cellclasses[] = 'duration';
516 $cellclasses[] = $durationclass;
519 // Special visual fx for today
520 if($display->thismonth && $calendar->day == $calendar->day) {
521 $cellclasses[] = 'today';
522 } else {
523 $cellclasses[] = 'nottoday';
525 $cell->attributes = array('class'=>join(' ',$cellclasses));
527 if (isset($eventsbyday[$calendar->day])) {
528 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
529 foreach($eventsbyday[$calendar->day] as $eventindex) {
530 // If event has a class set then add it to the event <li> tag
531 $attributes = array();
532 if (!empty($events[$eventindex]->class)) {
533 $attributes['class'] = $events[$eventindex]->class;
535 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
536 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
537 $cell->text .= html_writer::tag('li', $link, $attributes);
539 $cell->text .= html_writer::end_tag('ul');
541 if (isset($durationbyday[$calendar->day])) {
542 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
543 foreach($durationbyday[$calendar->day] as $eventindex) {
544 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
546 $cell->text .= html_writer::end_tag('ul');
548 $row->cells[] = $cell;
551 // Paddding (the last week may have blank days at the end)
552 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
553 $cell = new html_table_cell('&nbsp;');
554 $cell->attributes = array('class'=>'nottoday');
555 $row->cells[] = $cell;
557 $table->data[] = $row;
558 $output .= html_writer::table($table);
560 // OK, now for the filtering display
561 $output .= $this->filter_selection_table($calendar);
562 return $output;
566 * Displays a filter selection table
568 * @param calendar_information $calendar
569 * @return string
571 protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
572 global $SESSION;
574 if ($returnurl === null) {
575 $returnurl = $this->page->url;
578 $output = html_writer::start_tag('div', array('class'=>'filters'));
579 $output .= html_writer::start_tag('table');
580 $output .= html_writer::start_tag('tr');
582 // Global events
583 $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
584 $strglobalevents = get_string('globalevents', 'calendar');
585 if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
586 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
587 $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>').')');
588 } else {
589 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
590 $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>').')');
593 // Course events
594 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
595 $strcourseevents = get_string('courseevents', 'calendar');
596 if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
597 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
598 $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>').')');
599 } else {
600 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
601 $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>').')');
603 $output .= html_writer::end_tag('tr');
605 if(isloggedin() && !isguestuser()) {
606 $output .= html_writer::start_tag('tr');
607 // Group events
608 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
609 $strgroupevents = get_string('groupevents', 'calendar');
610 if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
611 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
612 $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>').')');
613 } else {
614 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
615 $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>').')');
617 // User events
618 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
619 $struserevents = get_string('userevents', 'calendar');
620 if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
621 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
622 $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>').')');
623 } else {
624 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
625 $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>').')');
627 $output .= html_writer::end_tag('tr');
629 $output .= html_writer::end_tag('table');
630 $output .= html_writer::end_tag('div');
631 return $output;
635 * Displays upcoming events
637 * @param calendar_information $calendar
638 * @param int $futuredays
639 * @param int $maxevents
640 * @return string
642 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
644 if ($returnurl === null) {
645 $returnurl = $this->page->url;
648 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
650 $output = html_writer::start_tag('div', array('class'=>'header'));
651 if (calendar_user_can_add_event($calendar->course)) {
652 $output .= $this->add_event_button($calendar->course->id);
654 $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
655 $output .= $this->course_filter_selector($returnurl);
656 $output .= html_writer::end_tag('div');
658 if ($events) {
659 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
660 foreach ($events as $event) {
661 // Convert to calendar_event object so that we transform description
662 // accordingly
663 $event = new calendar_event($event);
664 $event->calendarcourseid = $calendar->courseid;
665 $output .= $this->event($event);
667 $output .= html_writer::end_tag('div');
668 } else {
669 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
672 return $output;
676 * Displays a course filter selector
678 * @param array $getvars
679 * @return string
681 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
682 global $USER, $SESSION, $CFG;
684 if (!isloggedin() or isguestuser()) {
685 return '';
688 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) {
689 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
690 } else {
691 $courses = enrol_get_my_courses();
694 unset($courses[SITEID]);
696 $courseoptions = array();
697 $courseoptions[SITEID] = get_string('fulllistofcourses');
698 foreach ($courses as $course) {
699 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
700 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
703 if ($this->page->course->id !== SITEID) {
704 $selected = $this->page->course->id;
705 } else {
706 $selected = '';
708 $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);
709 $select->class = 'cal_courses_flt';
710 if ($label !== null) {
711 $select->label = $label;
713 return $this->output->render($select);