calendar MDL-22887 Output is now through a renderer, all pages have navigation,...
[moodle.git] / calendar / renderer.php
CommitLineData
36dc3b71
SH
1<?php
2
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/>.
17
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 */
25
26/**
27 * The primary renderer for the calendar.
28 */
29class core_calendar_renderer extends plugin_renderer_base {
30
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) {
42
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'));
47
48 $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
49
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');
58
59 $output .= html_writer::tag('div', get_string('for', 'calendar').':');
60
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');
84
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));
91
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')));
94
95 $output .= html_writer::end_tag('div');
96
97 $output .= html_writer::end_tag('form');
98 $output .= html_writer::end_tag('fieldset');
99
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');
104
105 $this->page->requires->js_init_call('M.core_calendar.init_basic_export', array($allowthisweek, $allownextweek, $allownextmonth, $username, $authtoken));
106
107 return $output;
108 }
109
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 }
118
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 }
127
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 }
143
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) {
153
154 list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year);
155 list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year);
156
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 }
171
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 }
181
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 }
212
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());
222
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));
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'));
232
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(), '', false, $calendar->timestamp_today());
245 $output .= $this->event($event);
246 } else { // Save this for later
247 $underway[] = $event;
248 }
249 }
250
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(), '', false, $calendar->timestamp_today());
256 $output .= $this->event($event);
257 }
258 }
259
260 $output .= html_writer::end_tag('div');
261 }
262
263 return $output;
264 }
265
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);
275
276 $output = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
277
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 );
284
285 if (!empty($event->icon)) {
286 $table->data[0]->cells[0] = new html_table_cell($event->icon);
287 } else {
288 $table->data[0]->cells[0] = new html_table_cell($this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
289 }
290 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
291
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 }
307
308 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
309 $table->data[1]->cells[0]->attributes['class'] .= 'side';
310
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 }
316
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 }
329
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 }
344
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;
354
355 $date = usergetdate(time());
356 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
357
358 $display = new stdClass;
359 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
360 $display->maxwday = $display->minwday + 6;
361 $display->thismonth = true;
362 $display->maxdays = calendar_days_in_month($m, $y);
363
364 $startwday = 0;
365 if (get_user_timezone_offset() < 99) {
366 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
367 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
368 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
369 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
370 } else {
371 // no timezone info specified
372 $display->tstart = mktime(0, 0, 0, $m, 1, $y);
373 $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
374 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
375 }
376
377 // Align the starting weekday to fall in our display range
378 if ($startwday < $display->minwday) {
379 $startwday += 7;
380 }
381
382 // Get events from database
383 $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
384 if (!empty($events)) {
385 foreach($events as $eventid => $event) {
386 $event = new calendar_event($event);
387 if (!empty($event->modulename)) {
388 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
389 if (!groups_course_module_visible($cm)) {
390 unset($events[$eventid]);
391 }
392 }
393 }
394 }
395
396 // Extract information: events vs. time
397 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
398
399 $output = html_writer::start_tag('div', array('class'=>'header'));
400 if(!isguestuser() && isloggedin() && calendar_user_can_add_event()) {
401 $output .= $this->add_event_button($calendar->courseid, null, $calendar->month, $calendar->year);
402 }
403 $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector(array('from'=>'month', 'cal_d'=>$calendar->day, 'cal_m'=>$calendar->month, 'cal_y'=>$calendar->year));
404 $output .= html_writer::end_tag('div', array('class'=>'header'));
405 // Controls
406 $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
407
408 $table = new html_table();
409 $table->attributes = array('class'=>'calendarmonth calendartable');
410 $table->data = array();
411
412 $header = new html_table_row();
413 $header->attributes = array('class'=>'weekdays');
414 $header->cells = array();
415 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
416 // This uses the % operator to get the correct weekday no matter what shift we have
417 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
418 $cell = new html_table_cell(get_string($CALENDARDAYS[$i % 7], 'calendar'));
419 $cell->header = true;
420 $header->cells[] = $cell;
421 }
422
423 // For the table display. $week is the row; $dayweek is the column.
424 $week = 1;
425 $dayweek = $startwday;
426
427 $row = new html_table_row(array());
428 // Paddding (the first week may have blank days in the beginning)
429 for($i = $display->minwday; $i < $startwday; ++$i) {
430 $cell = new html_table_cell('&nbsp;');
431 $cell->attributes = array('class'=>'nottoday');
432 $row->cells[] = $cell;
433 }
434
435 // Now display all the calendar
436 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
437 if($dayweek > $display->maxwday) {
438 // We need to change week (table row)
439 $table->data[] = $row;
440 $row = new html_table_row(array());
441 $dayweek = $display->minwday;
442 ++$week;
443 }
444
445 // Reset vars
446 $cell = new html_table_cell();
447 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day&amp;course='.$calendar->courseid.'&amp;', $calendar->day, $m, $y);
448
449 $cellclasses = array();
450
451 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
452 // Weekend. This is true no matter what the exact range is.
453 $cellclasses[] = 'weekend';
454 }
455
456 // Special visual fx if an event is defined
457 if (isset($eventsbyday[$calendar->day])) {
458 if(count($eventsbyday[$calendar->day]) == 1) {
459 $title = get_string('oneevent', 'calendar');
460 } else {
461 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
462 }
463 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
464 } else {
465 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
466 }
467
468 // Special visual fx if an event spans many days
469 $durationclass = false;
470 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
471 $durationclass = 'duration_global';
472 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
473 $durationclass = 'duration_course';
474 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
475 $durationclass = 'duration_group';
476 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
477 $durationclass = 'duration_user';
478 }
479 if ($durationclass) {
480 $cellclasses[] = 'duration';
481 $cellclasses[] = $durationclass;
482 }
483
484 // Special visual fx for today
485 if($display->thismonth && $calendar->day == $d) {
486 $cellclasses[] = 'today';
487 } else {
488 $cellclasses[] = 'nottoday';
489 }
490 $cell->attributes = array('class'=>join(' ',$cellclasses));
491
492 if (isset($eventsbyday[$calendar->day])) {
493 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
494 foreach($eventsbyday[$calendar->day] as $eventindex) {
495 // If event has a class set then add it to the event <li> tag
496 $attributes = array();
497 if (!empty($events[$eventindex]->class)) {
498 $attributes['class'] = $events[$eventindex]->class;
499 }
500 $link = html_writer::link($dayhref.'#event_'.$events[$eventindex]->id, 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 }
514
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);
523
524 // OK, now for the filtering display
525 $output .= $this->filter_selection_table($calendar);
526 return $output;
527 }
528
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;
537
538 $output = html_writer::start_tag('div', array('class'=>'filters'));
539 $output .= html_writer::start_tag('table');
540 $output .= html_writer::start_tag('tr');
541
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 }
551
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');
562
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 }
589
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);
600
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');
608
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 = $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 }
622
623 return $output;
624 }
625
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()) {
633 global $USER, $SESSION;
634
635 if (!isloggedin() or isguestuser()) {
636 return '';
637 }
638
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 }
644
645 unset($courses[SITEID]);
646
647 $courseoptions = array();
648 $courseoptions[SITEID] = get_string('fulllistofcourses');
649 foreach ($courses as $course) {
650 $courseoptions[$course->id] = format_string($course->shortname);
651 }
652
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 echo $this->output->render($select);
662 }
663}