MDL-35238 Print help on the script usage in CLI mode
[moodle.git] / calendar / renderer.php
blob842ecc40c9a124eade31418042b03363c571c06a
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 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 /**
31 * The primary renderer for the calendar.
33 class core_calendar_renderer extends plugin_renderer_base {
35 /**
36 * Creates a basic export form
38 * @param bool $allowthisweek
39 * @param bool $allownextweek
40 * @param bool $allownextmonth
41 * @param int $userid
42 * @param string $authtoken
43 * @return string
45 public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $userid, $authtoken) {
47 $output = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header'));
48 $output .= html_writer::start_tag('fieldset');
49 $output .= html_writer::tag('legend', get_string('commontasks', 'calendar'));
50 $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get'));
52 $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
54 $output .= html_writer::start_tag('div', array('class'=>'indent'));
55 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked'));
56 $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all'));
57 $output .= html_writer::empty_tag('br');
58 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses'));
59 $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course'));
60 $output .= html_writer::empty_tag('br');
61 $output .= html_writer::end_tag('div');
63 $output .= html_writer::tag('div', get_string('for', 'calendar').':');
65 $output .= html_writer::start_tag('div', array('class'=>'indent'));
66 if ($allowthisweek) {
67 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked'));
68 $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow'));
69 $output .= html_writer::empty_tag('br');
71 if ($allownextweek) {
72 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext'));
73 $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext'));
74 $output .= html_writer::empty_tag('br');
76 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow'));
77 $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow'));
78 $output .= html_writer::empty_tag('br');
79 if ($allownextmonth) {
80 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext'));
81 $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext'));
82 $output .= html_writer::empty_tag('br');
84 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming'));
85 $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc'));
86 $output .= html_writer::empty_tag('br');
87 $output .= html_writer::end_tag('div');
89 $output .= html_writer::start_tag('div', array('class'=>'rightalign'));
90 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>''));
91 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>''));
92 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>''));
93 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'userid', 'value'=>$userid));
94 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
96 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name' => 'generateurl', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar')));
97 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar')));
99 $output .= html_writer::end_tag('div');
101 $output .= html_writer::end_tag('form');
102 $output .= html_writer::end_tag('fieldset');
104 $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;'));
105 $output .= html_writer::tag('p', get_string('urlforical', 'calendar'));
106 $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;'));
107 $output .= html_writer::end_tag('div');
109 return $output;
113 * Starts the standard layout for the page
115 * @return string
117 public function start_layout() {
118 return html_writer::start_tag('div', array('class'=>'maincalendar'));
122 * Creates the remainder of the layout
124 * @return string
126 public function complete_layout() {
127 return html_writer::end_tag('div');
131 * Produces the content for the filters block (pretend block)
133 * @param int $courseid
134 * @param int $day
135 * @param int $month
136 * @param int $year
137 * @param int $view
138 * @param int $courses
139 * @return string
141 public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
142 $returnurl = $this->page->url;
143 $returnurl->param('course', $courseid);
144 return html_writer::tag('div', calendar_filter_controls($returnurl), array('class'=>'calendar_filters filters'));
148 * Produces the content for the three months block (pretend block)
150 * This includes the previous month, the current month, and the next month
152 * @param calendar_information $calendar
153 * @return string
155 public function fake_block_threemonths(calendar_information $calendar) {
157 list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year);
158 list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year);
160 $content = html_writer::start_tag('div', array('class'=>'minicalendarblock'));
161 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $prevmon, 'y' => $prevyr));
162 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $prevmon, $prevyr);
163 $content .= html_writer::end_tag('div');
164 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
165 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year));
166 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $calendar->month, $calendar->year);
167 $content .= html_writer::end_tag('div');
168 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
169 $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $nextmon, 'y' => $nextyr));
170 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $nextmon, $nextyr);
171 $content .= html_writer::end_tag('div');
172 return $content;
176 * Adds a pretent calendar block
178 * @param block_contents $bc
179 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
181 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
182 $this->page->blocks->add_fake_block($bc, $pos);
186 * Creates a button to add a new event
188 * @param int $courseid
189 * @param int $day
190 * @param int $month
191 * @param int $year
192 * @return string
194 protected function add_event_button($courseid, $day=null, $month=null, $year=null) {
195 $output = html_writer::start_tag('div', array('class'=>'buttons'));
196 $output .= html_writer::start_tag('form', array('action'=>CALENDAR_URL.'event.php', 'method'=>'get'));
197 $output .= html_writer::start_tag('div');
198 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'new'));
199 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'course', 'value'=>$courseid));
200 if ($day !== null) {
201 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>$day));
203 if ($month !== null) {
204 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>$month));
206 if ($year !== null) {
207 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>$year));
209 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('newevent', 'calendar')));
210 $output .= html_writer::end_tag('div');
211 $output .= html_writer::end_tag('form');
212 $output .= html_writer::end_tag('div');
213 return $output;
217 * Displays the calendar for a single day
219 * @param calendar_information $calendar
220 * @return string
222 public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
224 if ($returnurl === null) {
225 $returnurl = $this->page->url;
228 $calendar->checkdate();
229 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
231 $output = html_writer::start_tag('div', array('class'=>'header'));
232 if (calendar_user_can_add_event($calendar->course)) {
233 $output .= $this->add_event_button($calendar->course->id, $calendar->day, $calendar->month, $calendar->year);
235 //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump'));
236 $output .= $this->course_filter_selector($returnurl, get_string('dayview', 'calendar'));
237 $output .= html_writer::end_tag('div');
238 // Controls
239 $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'));
241 if (empty($events)) {
242 // There is nothing to display today.
243 $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
244 } else {
245 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
246 $underway = array();
247 // First, print details about events that start today
248 foreach ($events as $event) {
249 $event = new calendar_event($event);
250 $event->calendarcourseid = $calendar->courseid;
251 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
252 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
253 $output .= $this->event($event);
254 } else { // Save this for later
255 $underway[] = $event;
259 // Then, show a list of all events that just span this day
260 if (!empty($underway)) {
261 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
262 foreach ($underway as $event) {
263 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
264 $output .= $this->event($event);
268 $output .= html_writer::end_tag('div');
271 return $output;
275 * Displays an event
277 * @param calendar_event $event
278 * @param bool $showactions
279 * @return string
281 public function event(calendar_event $event, $showactions=true) {
282 $event = calendar_add_event_metadata($event);
284 $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
286 $table = new html_table();
287 $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
288 $table->data = array(
289 0 => new html_table_row(),
290 1 => new html_table_row(),
293 if (!empty($event->icon)) {
294 $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
295 } else {
296 $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
298 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
300 $table->data[0]->cells[1] = new html_table_cell();
301 $table->data[0]->cells[1]->attributes['class'] .= ' topic';
302 if (!empty($event->referer)) {
303 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
304 } else {
305 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
307 if (!empty($event->courselink)) {
308 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
310 if (!empty($event->time)) {
311 $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
312 } else {
313 $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
316 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
317 $table->data[1]->cells[0]->attributes['class'] .= 'side';
319 $table->data[1]->cells[1] = new html_table_cell($event->description);
320 $table->data[1]->cells[1]->attributes['class'] .= ' description';
321 if (isset($event->cssclass)) {
322 $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
325 if (calendar_edit_event_allowed($event) && $showactions) {
326 if (empty($event->cmid)) {
327 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
328 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
329 if (!empty($event->calendarcourseid)) {
330 $editlink->param('course', $event->calendarcourseid);
331 $deletelink->param('course', $event->calendarcourseid);
333 } else {
334 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
335 $deletelink = null;
338 $commands = html_writer::start_tag('div', array('class'=>'commands'));
339 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
340 $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')));
341 $commands .= html_writer::end_tag('a');
342 if ($deletelink != null) {
343 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
344 $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')));
345 $commands .= html_writer::end_tag('a');
347 $commands .= html_writer::end_tag('div');
348 $table->data[1]->cells[1]->text .= $commands;
350 return html_writer::table($table);
354 * Displays a month in detail
356 * @param calendar_information $calendar
357 * @return string
359 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
360 global $CFG;
362 if (empty($returnurl)) {
363 $returnurl = $this->page->url;
366 $date = usergetdate(time());
368 $display = new stdClass;
369 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
370 $display->maxwday = $display->minwday + 6;
371 $display->thismonth = ($date['mon'] == $calendar->month);
372 $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year);
374 $startwday = 0;
375 if (get_user_timezone_offset() < 99) {
376 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
377 $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT
378 $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT
379 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
380 } else {
381 // no timezone info specified
382 $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year);
383 $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year);
384 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
387 // Align the starting weekday to fall in our display range
388 if ($startwday < $display->minwday) {
389 $startwday += 7;
392 // Get events from database
393 $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
394 if (!empty($events)) {
395 foreach($events as $eventid => $event) {
396 $event = new calendar_event($event);
397 if (!empty($event->modulename)) {
398 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
399 if (!groups_course_module_visible($cm)) {
400 unset($events[$eventid]);
406 // Extract information: events vs. time
407 calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
409 $output = html_writer::start_tag('div', array('class'=>'header'));
410 if (calendar_user_can_add_event($calendar->course)) {
411 $output .= $this->add_event_button($calendar->course->id, null, $calendar->month, $calendar->year);
413 $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl);
414 $output .= html_writer::end_tag('div', array('class'=>'header'));
415 // Controls
416 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
418 $days = calendar_get_days();
420 $table = new html_table();
421 $table->attributes = array('class'=>'calendarmonth calendartable');
422 $time = make_timestamp($calendar->year, $calendar->month);
423 $table->summary = get_string('calendarheading', 'calendar', userdate($time, get_string('strftimemonthyear')));
424 $table->data = array();
426 $header = new html_table_row();
427 $header->attributes = array('class'=>'weekdays');
428 $header->cells = array();
429 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
430 // This uses the % operator to get the correct weekday no matter what shift we have
431 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
432 $cell = new html_table_cell(get_string($days[$i % 7], 'calendar'));
433 $cell->header = true;
434 $header->cells[] = $cell;
437 // For the table display. $week is the row; $dayweek is the column.
438 $week = 1;
439 $dayweek = $startwday;
441 // Create an array of all the week days.
442 $wdays = array(0 => '<strong>'. get_string('sunday', 'calendar'). '</strong>',
443 1 => '<strong>'. get_string('monday', 'calendar'). '</strong>',
444 2 => '<strong>'. get_string('tuesday', 'calendar'). '</strong>',
445 3 => '<strong>'. get_string('wednesday', 'calendar'). '</strong>',
446 4 => '<strong>'. get_string('thursday', 'calendar'). '</strong>',
447 5 => '<strong>'. get_string('friday', 'calendar'). '</strong>',
448 6 => '<strong>'. get_string('saturday', 'calendar'). '</strong>');
450 // Loop only if the day offset is greater than 0.
451 // This loop involves shifting the days around until the desired start day
452 // is at the start of the array.
453 $daycount = 0;
454 while ($display->minwday > $daycount++) {
455 $wdays_end = array_shift($wdays);
456 array_push($wdays, $wdays_end);
459 // Now we set the (modified) array to the table header to be displayed.
460 $table->head = $wdays;
462 $row = new html_table_row(array());
464 // Paddding (the first week may have blank days in the beginning)
465 for($i = $display->minwday; $i < $startwday; ++$i) {
466 $cell = new html_table_cell('&nbsp;');
467 $cell->attributes = array('class'=>'nottoday dayblank');
468 $row->cells[] = $cell;
471 // Now display all the calendar
472 $weekend = CALENDAR_DEFAULT_WEEKEND;
473 if (isset($CFG->calendar_weekend)) {
474 $weekend = intval($CFG->calendar_weekend);
477 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
478 if($dayweek > $display->maxwday) {
479 // We need to change week (table row)
480 $table->data[] = $row;
481 $row = new html_table_row(array());
482 $dayweek = $display->minwday;
483 ++$week;
486 // Reset vars
487 $cell = new html_table_cell();
488 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year);
490 $cellclasses = array();
492 if ($weekend & (1 << ($dayweek % 7))) {
493 // Weekend. This is true no matter what the exact range is.
494 $cellclasses[] = 'weekend';
497 // Special visual fx if an event is defined
498 if (isset($eventsbyday[$calendar->day])) {
499 if(count($eventsbyday[$calendar->day]) == 1) {
500 $title = get_string('oneevent', 'calendar');
501 } else {
502 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
504 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
505 } else {
506 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
509 // Special visual fx if an event spans many days
510 $durationclass = false;
511 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
512 $durationclass = 'duration_global';
513 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
514 $durationclass = 'duration_course';
515 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
516 $durationclass = 'duration_group';
517 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
518 $durationclass = 'duration_user';
520 if ($durationclass) {
521 $cellclasses[] = 'duration';
522 $cellclasses[] = $durationclass;
525 // Special visual fx for today
526 if ($display->thismonth && $calendar->day == $date['mday']) {
527 $cellclasses[] = 'day today';
528 } else {
529 $cellclasses[] = 'day nottoday';
531 $cell->attributes = array('class'=>join(' ',$cellclasses));
533 if (isset($eventsbyday[$calendar->day])) {
534 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
535 foreach($eventsbyday[$calendar->day] as $eventindex) {
536 // If event has a class set then add it to the event <li> tag
537 $attributes = array();
538 if (!empty($events[$eventindex]->class)) {
539 $attributes['class'] = $events[$eventindex]->class;
541 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
542 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
543 $cell->text .= html_writer::tag('li', $link, $attributes);
545 $cell->text .= html_writer::end_tag('ul');
547 if (isset($durationbyday[$calendar->day])) {
548 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
549 foreach($durationbyday[$calendar->day] as $eventindex) {
550 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
552 $cell->text .= html_writer::end_tag('ul');
554 $row->cells[] = $cell;
557 // Paddding (the last week may have blank days at the end)
558 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
559 $cell = new html_table_cell('&nbsp;');
560 $cell->attributes = array('class'=>'nottoday dayblank');
561 $row->cells[] = $cell;
563 $table->data[] = $row;
564 $output .= html_writer::table($table);
566 return $output;
570 * Displays a filter selection table
572 * @param calendar_information $calendar
573 * @return string
574 * @deprecated since Moodle 2.4 MDL-32309
575 * @see calendar_filter_controls()
577 protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
578 global $SESSION;
579 debugging('Method core_calendar_renderer::filter_selection_table() is deprecated, please use '.
580 'calendar_filter_controls() instead', DEBUG_DEVELOPER);
582 if ($returnurl === null) {
583 $returnurl = $this->page->url;
586 $output = html_writer::start_tag('div', array('class'=>'filters'));
587 $output .= html_writer::start_tag('table');
588 $output .= html_writer::start_tag('tr');
590 // Global events
591 $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
592 $strglobalevents = get_string('globalevents', 'calendar');
593 if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
594 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
595 $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>').')');
596 } else {
597 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
598 $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>').')');
601 // Course events
602 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
603 $strcourseevents = get_string('courseevents', 'calendar');
604 if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
605 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
606 $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>').')');
607 } else {
608 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
609 $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>').')');
611 $output .= html_writer::end_tag('tr');
613 if(isloggedin() && !isguestuser()) {
614 $output .= html_writer::start_tag('tr');
615 // Group events
616 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
617 $strgroupevents = get_string('groupevents', 'calendar');
618 if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
619 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
620 $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>').')');
621 } else {
622 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
623 $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>').')');
625 // User events
626 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
627 $struserevents = get_string('userevents', 'calendar');
628 if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
629 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
630 $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>').')');
631 } else {
632 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
633 $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>').')');
635 $output .= html_writer::end_tag('tr');
637 $output .= html_writer::end_tag('table');
638 $output .= html_writer::end_tag('div');
639 return $output;
643 * Displays upcoming events
645 * @param calendar_information $calendar
646 * @param int $futuredays
647 * @param int $maxevents
648 * @return string
650 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
652 if ($returnurl === null) {
653 $returnurl = $this->page->url;
656 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
658 $output = html_writer::start_tag('div', array('class'=>'header'));
659 if (calendar_user_can_add_event($calendar->course)) {
660 $output .= $this->add_event_button($calendar->course->id);
662 $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
663 $output .= $this->course_filter_selector($returnurl);
664 $output .= html_writer::end_tag('div');
666 if ($events) {
667 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
668 foreach ($events as $event) {
669 // Convert to calendar_event object so that we transform description
670 // accordingly
671 $event = new calendar_event($event);
672 $event->calendarcourseid = $calendar->courseid;
673 $output .= $this->event($event);
675 $output .= html_writer::end_tag('div');
676 } else {
677 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
680 return $output;
684 * Displays a course filter selector
686 * @param array $getvars
687 * @return string
689 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
690 global $USER, $SESSION, $CFG;
692 if (!isloggedin() or isguestuser()) {
693 return '';
696 if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
697 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
698 } else {
699 $courses = enrol_get_my_courses();
702 unset($courses[SITEID]);
704 $courseoptions = array();
705 $courseoptions[SITEID] = get_string('fulllistofcourses');
706 foreach ($courses as $course) {
707 $coursecontext = context_course::instance($course->id);
708 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
711 if ($this->page->course->id !== SITEID) {
712 $selected = $this->page->course->id;
713 } else {
714 $selected = '';
716 $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);
717 $select->class = 'cal_courses_flt';
718 if ($label !== null) {
719 $select->set_label($label);
720 } else {
721 $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
723 return $this->output->render($select);
727 * Renders a table containing information about calendar subscriptions.
729 * @param int $courseid
730 * @param array $subscriptions
731 * @param string $importresults
732 * @return string
734 public function subscription_details($courseid, $subscriptions, $importresults = '') {
735 $table = new html_table();
736 $table->head = array(
737 get_string('colcalendar', 'calendar'),
738 get_string('collastupdated', 'calendar'),
739 get_string('colpoll', 'calendar'),
740 get_string('colactions', 'calendar')
742 $table->align = array('left', 'left', 'left', 'center');
743 $table->width = '100%';
744 $table->data = array();
746 if (empty($subscriptions)) {
747 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
748 $cell->colspan = 4;
749 $table->data[] = new html_table_row(array($cell));
751 $strnever = new lang_string('never', 'calendar');
752 foreach ($subscriptions as $sub) {
753 $label = $sub->name;
754 if (!empty($sub->url)) {
755 $label = html_writer::link($sub->url, $label);
757 if (empty($sub->lastupdated)) {
758 $lastupdated = $strnever->out();
759 } else {
760 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
763 $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
764 $cell->colspan = 2;
766 $table->data[] = new html_table_row(array(
767 new html_table_cell($label),
768 new html_table_cell($lastupdated),
769 $cell
773 $out = $this->output->box_start('generalbox calendarsubs');
775 $out .= $importresults;
776 $out .= html_writer::table($table);
777 $out .= $this->output->box_end();
778 return $out;
782 * Creates a form to perform actions on a given subscription.
784 * @param stdClass $subscription
785 * @param int $courseid
786 * @return string
788 protected function subscription_action_form($subscription, $courseid) {
789 // Assemble form for the subscription row.
790 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
791 if (empty($subscription->url)) {
792 // Don't update an iCal file, which has no URL.
793 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
794 } else {
795 // Assemble pollinterval control.
796 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
797 $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
798 foreach (calendar_get_pollinterval_choices() as $k => $v) {
799 $attributes = array();
800 if ($k == $subscription->pollinterval) {
801 $attributes['selected'] = 'selected';
803 $html .= html_writer::tag('option', $v, $attributes);
805 $html .= html_writer::end_tag('select');
806 $html .= html_writer::end_tag('div');
808 $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
809 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
810 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
811 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
812 if (!empty($subscription->url)) {
813 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('update')));
815 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('remove')));
816 $html .= html_writer::end_tag('div');
817 $html .= html_writer::end_tag('form');
818 return $html;