MDL-50637 calendar: Fix subscription table layout
[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
e30390a0
SH
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
36dc3b71
SH
30/**
31 * The primary renderer for the calendar.
32 */
33class core_calendar_renderer extends plugin_renderer_base {
34
36dc3b71
SH
35 /**
36 * Starts the standard layout for the page
6be8c6b7 37 *
36dc3b71
SH
38 * @return string
39 */
40 public function start_layout() {
41 return html_writer::start_tag('div', array('class'=>'maincalendar'));
42 }
43
44 /**
45 * Creates the remainder of the layout
46 *
47 * @return string
48 */
49 public function complete_layout() {
50 return html_writer::end_tag('div');
51 }
52
53 /**
54 * Produces the content for the filters block (pretend block)
55 *
56 * @param int $courseid
57 * @param int $day
58 * @param int $month
59 * @param int $year
60 * @param int $view
61 * @param int $courses
62 * @return string
63 */
64 public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
05f9d136
AA
65 $returnurl = $this->page->url;
66 $returnurl->param('course', $courseid);
67 return html_writer::tag('div', calendar_filter_controls($returnurl), array('class'=>'calendar_filters filters'));
36dc3b71
SH
68 }
69
70 /**
71 * Produces the content for the three months block (pretend block)
72 *
73 * This includes the previous month, the current month, and the next month
74 *
75 * @param calendar_information $calendar
76 * @return string
77 */
78 public function fake_block_threemonths(calendar_information $calendar) {
da304137
MN
79 // Get the calendar type we are using.
80 $calendartype = \core_calendar\type_factory::get_calendar_instance();
36dc3b71 81
da304137 82 $date = $calendartype->timestamp_to_date_array($calendar->time);
36dc3b71 83
da304137 84 $prevmonth = calendar_sub_month($date['mon'], $date['year']);
1032966c
MN
85 $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
86 $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
87 $prevmonthtime['hour'], $prevmonthtime['minute']);
da304137
MN
88
89 $nextmonth = calendar_add_month($date['mon'], $date['year']);
1032966c
MN
90 $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
91 $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
92 $nextmonthtime['hour'], $nextmonthtime['minute']);
da304137
MN
93
94 $content = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
95 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $prevmonthtime);
36dc3b71 96 $content .= html_writer::end_tag('div');
da304137
MN
97 $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
98 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $calendar->time);
36dc3b71 99 $content .= html_writer::end_tag('div');
da304137
MN
100 $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
101 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $nextmonthtime);
36dc3b71
SH
102 $content .= html_writer::end_tag('div');
103 return $content;
104 }
105
106 /**
107 * Adds a pretent calendar block
108 *
109 * @param block_contents $bc
110 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
111 */
112 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
d9c26e21 113 $this->page->blocks->add_fake_block($bc, $pos);
36dc3b71
SH
114 }
115
116 /**
117 * Creates a button to add a new event
118 *
119 * @param int $courseid
120 * @param int $day
121 * @param int $month
122 * @param int $year
da304137
MN
123 * @param int $time the unixtime, used for multiple calendar support. The values $day,
124 * $month and $year are kept for backwards compatibility.
36dc3b71
SH
125 * @return string
126 */
da304137
MN
127 protected function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) {
128 // If a day, month and year were passed then convert it to a timestamp. If these were passed
129 // then we can assume the day, month and year are passed as Gregorian, as no where in core
130 // should we be passing these values rather than the time. This is done for BC.
131 if (!empty($day) && !empty($month) && !empty($year)) {
132 if (checkdate($month, $day, $year)) {
133 $time = make_timestamp($year, $month, $day);
134 } else {
135 $time = time();
136 }
137 } else if (empty($time)) {
138 $time = time();
139 }
140
36dc3b71 141 $output = html_writer::start_tag('div', array('class'=>'buttons'));
da304137 142 $output .= html_writer::start_tag('form', array('action' => CALENDAR_URL . 'event.php', 'method' => 'get'));
36dc3b71 143 $output .= html_writer::start_tag('div');
da304137
MN
144 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'action', 'value' => 'new'));
145 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'course', 'value' => $courseid));
146 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'time', 'value' => $time));
147 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value' => get_string('newevent', 'calendar')));
36dc3b71
SH
148 $output .= html_writer::end_tag('div');
149 $output .= html_writer::end_tag('form');
150 $output .= html_writer::end_tag('div');
151 return $output;
152 }
153
154 /**
155 * Displays the calendar for a single day
156 *
157 * @param calendar_information $calendar
158 * @return string
159 */
797cedc7
SH
160 public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
161
162 if ($returnurl === null) {
163 $returnurl = $this->page->url;
164 }
165
36dc3b71
SH
166 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
167
168 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 169 $output .= $this->course_filter_selector($returnurl, get_string('dayviewfor', 'calendar'));
797cedc7 170 if (calendar_user_can_add_event($calendar->course)) {
da304137 171 $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
36dc3b71 172 }
36dc3b71
SH
173 $output .= html_writer::end_tag('div');
174 // Controls
da304137 175 $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class'=>'controls'));
36dc3b71
SH
176
177 if (empty($events)) {
178 // There is nothing to display today.
136f8f09 179 $output .= html_writer::span(get_string('daywithnoevents', 'calendar'), 'calendar-information calendar-no-results');
36dc3b71 180 } else {
11edf09c 181 $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
36dc3b71
SH
182 $underway = array();
183 // First, print details about events that start today
184 foreach ($events as $event) {
185 $event = new calendar_event($event);
186 $event->calendarcourseid = $calendar->courseid;
187 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
0f927f1e 188 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
189 $output .= $this->event($event);
190 } else { // Save this for later
191 $underway[] = $event;
192 }
193 }
194
195 // Then, show a list of all events that just span this day
196 if (!empty($underway)) {
136f8f09
BB
197 $output .= html_writer::span(get_string('spanningevents', 'calendar'),
198 'calendar-information calendar-span-multiple-days');
36dc3b71 199 foreach ($underway as $event) {
0f927f1e 200 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
201 $output .= $this->event($event);
202 }
203 }
204
205 $output .= html_writer::end_tag('div');
206 }
207
208 return $output;
209 }
210
211 /**
212 * Displays an event
213 *
214 * @param calendar_event $event
215 * @param bool $showactions
216 * @return string
217 */
218 public function event(calendar_event $event, $showactions=true) {
e73b527c
AA
219 global $CFG;
220
36dc3b71 221 $event = calendar_add_event_metadata($event);
9984132c 222 $context = $event->context;
11edf09c 223 $output = '';
36dc3b71
SH
224
225 if (!empty($event->icon)) {
11edf09c 226 $output .= $event->icon;
36dc3b71 227 } else {
11edf09c 228 $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
36dc3b71 229 }
36dc3b71 230
36dc3b71 231 if (!empty($event->referer)) {
11edf09c 232 $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
36dc3b71 233 } else {
11edf09c
BB
234 $output .= $this->output->heading(
235 format_string($event->name, false, array('context' => $context)),
236 3,
237 array('class' => 'name')
238 );
36dc3b71
SH
239 }
240 if (!empty($event->courselink)) {
11edf09c 241 $output .= html_writer::tag('div', $event->courselink, array('class' => 'course'));
36dc3b71 242 }
e73b527c
AA
243 // Show subscription source if needed.
244 if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
245 if (!empty($event->subscription->url)) {
246 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
247 } else {
248 // File based ical.
249 $source = get_string('subsource', 'calendar', $event->subscription);
250 }
11edf09c 251 $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
8a7326ed 252 }
36dc3b71 253 if (!empty($event->time)) {
11edf09c 254 $output .= html_writer::tag('span', $event->time, array('class' => 'date'));
36dc3b71 255 } else {
11edf09c 256 $output .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class' => 'date'));
36dc3b71
SH
257 }
258
11edf09c
BB
259 $eventdetailshtml = '';
260 $eventdetailsclasses = '';
36dc3b71 261
11edf09c
BB
262 $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
263 $eventdetailsclasses .= 'description';
36dc3b71 264 if (isset($event->cssclass)) {
11edf09c 265 $eventdetailsclasses .= ' '.$event->cssclass;
36dc3b71
SH
266 }
267
11edf09c
BB
268 $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
269
36dc3b71
SH
270 if (calendar_edit_event_allowed($event) && $showactions) {
271 if (empty($event->cmid)) {
272 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
273 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
274 if (!empty($event->calendarcourseid)) {
275 $editlink->param('course', $event->calendarcourseid);
276 $deletelink->param('course', $event->calendarcourseid);
277 }
278 } else {
279 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
280 $deletelink = null;
281 }
282
283 $commands = html_writer::start_tag('div', array('class'=>'commands'));
284 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
285 $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')));
286 $commands .= html_writer::end_tag('a');
287 if ($deletelink != null) {
288 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
289 $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')));
290 $commands .= html_writer::end_tag('a');
291 }
292 $commands .= html_writer::end_tag('div');
11edf09c 293 $output .= $commands;
36dc3b71 294 }
11edf09c 295 return html_writer::tag('div', $output , array('class' => 'event', 'id' => 'event_' . $event->id));
36dc3b71
SH
296 }
297
298 /**
299 * Displays a month in detail
300 *
36dc3b71 301 * @param calendar_information $calendar
da304137 302 * @param moodle_url $returnurl the url to return to
36dc3b71
SH
303 * @return string
304 */
797cedc7
SH
305 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
306 global $CFG;
307
308 if (empty($returnurl)) {
309 $returnurl = $this->page->url;
310 }
6be8c6b7 311
da304137
MN
312 // Get the calendar type we are using.
313 $calendartype = \core_calendar\type_factory::get_calendar_instance();
36dc3b71 314
da304137 315 // Store the display settings.
36dc3b71 316 $display = new stdClass;
da304137
MN
317 $display->thismonth = false;
318
319 // Get the specified date in the calendar type being used.
320 $date = $calendartype->timestamp_to_date_array($calendar->time);
321 $thisdate = $calendartype->timestamp_to_date_array(time());
322 if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
323 $display->thismonth = true;
324 $date = $thisdate;
325 $calendar->time = time();
36dc3b71
SH
326 }
327
1032966c 328 // Get Gregorian date for the start of the month.
da304137 329 $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
1032966c 330 // Store the gregorian date values to be used later.
da304137
MN
331 list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
332 $gregoriandate['hour'], $gregoriandate['minute']);
333
334 // Get the starting week day for this month.
335 $startwday = dayofweek(1, $date['mon'], $date['year']);
336 // Get the days in a week.
337 $daynames = calendar_get_days();
338 // Store the number of days in a week.
339 $numberofdaysinweek = $calendartype->get_num_weekdays();
340
341 $display->minwday = calendar_get_starting_weekday();
342 $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
343 $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
344
345 // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
346 $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
1032966c 347 $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
da304137 348
36dc3b71 349 // Align the starting weekday to fall in our display range
da304137 350 // This is simple, not foolproof.
36dc3b71 351 if ($startwday < $display->minwday) {
da304137 352 $startwday += $numberofdaysinweek;
36dc3b71
SH
353 }
354
355 // Get events from database
da304137 356 $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses);
36dc3b71
SH
357 if (!empty($events)) {
358 foreach($events as $eventid => $event) {
359 $event = new calendar_event($event);
360 if (!empty($event->modulename)) {
361 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
8270f0d0 362 if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
36dc3b71
SH
363 unset($events[$eventid]);
364 }
365 }
366 }
367 }
368
369 // Extract information: events vs. time
da304137 370 calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
36dc3b71
SH
371
372 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 373 $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar'));
797cedc7 374 if (calendar_user_can_add_event($calendar->course)) {
da304137 375 $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
36dc3b71 376 }
36dc3b71
SH
377 $output .= html_writer::end_tag('div', array('class'=>'header'));
378 // Controls
da304137 379 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls'));
797cedc7 380
36dc3b71
SH
381 $table = new html_table();
382 $table->attributes = array('class'=>'calendarmonth calendartable');
da304137 383 $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
36dc3b71
SH
384 $table->data = array();
385
da304137
MN
386 // Get the day names as the header.
387 $header = array();
36dc3b71 388 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
da304137 389 $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
36dc3b71 390 }
da304137 391 $table->head = $header;
36dc3b71
SH
392
393 // For the table display. $week is the row; $dayweek is the column.
394 $week = 1;
395 $dayweek = $startwday;
396
2953ad13
JI
397 $row = new html_table_row(array());
398
36dc3b71
SH
399 // Paddding (the first week may have blank days in the beginning)
400 for($i = $display->minwday; $i < $startwday; ++$i) {
401 $cell = new html_table_cell('&nbsp;');
9e142d11 402 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
403 $row->cells[] = $cell;
404 }
405
406 // Now display all the calendar
797cedc7
SH
407 $weekend = CALENDAR_DEFAULT_WEEKEND;
408 if (isset($CFG->calendar_weekend)) {
409 $weekend = intval($CFG->calendar_weekend);
410 }
411
a0ef87de 412 $daytime = strtotime('-1 day', $display->tstart);
da304137 413 for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
a0ef87de 414 $daytime = strtotime('+1 day', $daytime);
36dc3b71
SH
415 if($dayweek > $display->maxwday) {
416 // We need to change week (table row)
417 $table->data[] = $row;
418 $row = new html_table_row(array());
419 $dayweek = $display->minwday;
420 ++$week;
421 }
422
423 // Reset vars
424 $cell = new html_table_cell();
da304137 425 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
36dc3b71
SH
426
427 $cellclasses = array();
428
da304137 429 if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
36dc3b71
SH
430 // Weekend. This is true no matter what the exact range is.
431 $cellclasses[] = 'weekend';
432 }
433
434 // Special visual fx if an event is defined
da304137
MN
435 if (isset($eventsbyday[$day])) {
436 if(count($eventsbyday[$day]) == 1) {
36dc3b71
SH
437 $title = get_string('oneevent', 'calendar');
438 } else {
da304137 439 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
36dc3b71 440 }
da304137 441 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title'=>$title)), array('class'=>'day'));
36dc3b71 442 } else {
da304137 443 $cell->text = html_writer::tag('div', $day, array('class'=>'day'));
36dc3b71
SH
444 }
445
446 // Special visual fx if an event spans many days
447 $durationclass = false;
da304137 448 if (isset($typesbyday[$day]['durationglobal'])) {
36dc3b71 449 $durationclass = 'duration_global';
da304137 450 } else if (isset($typesbyday[$day]['durationcourse'])) {
36dc3b71 451 $durationclass = 'duration_course';
da304137 452 } else if (isset($typesbyday[$day]['durationgroup'])) {
36dc3b71 453 $durationclass = 'duration_group';
da304137 454 } else if (isset($typesbyday[$day]['durationuser'])) {
36dc3b71
SH
455 $durationclass = 'duration_user';
456 }
457 if ($durationclass) {
458 $cellclasses[] = 'duration';
459 $cellclasses[] = $durationclass;
460 }
461
462 // Special visual fx for today
da304137 463 if ($display->thismonth && $day == $date['mday']) {
9e142d11 464 $cellclasses[] = 'day today';
36dc3b71 465 } else {
9e142d11 466 $cellclasses[] = 'day nottoday';
36dc3b71
SH
467 }
468 $cell->attributes = array('class'=>join(' ',$cellclasses));
469
da304137 470 if (isset($eventsbyday[$day])) {
36dc3b71 471 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
da304137 472 foreach($eventsbyday[$day] as $eventindex) {
36dc3b71
SH
473 // If event has a class set then add it to the event <li> tag
474 $attributes = array();
475 if (!empty($events[$eventindex]->class)) {
476 $attributes['class'] = $events[$eventindex]->class;
477 }
0f927f1e
SH
478 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
479 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
36dc3b71
SH
480 $cell->text .= html_writer::tag('li', $link, $attributes);
481 }
482 $cell->text .= html_writer::end_tag('ul');
483 }
da304137 484 if (isset($durationbyday[$day])) {
36dc3b71 485 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
da304137 486 foreach($durationbyday[$day] as $eventindex) {
36dc3b71
SH
487 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
488 }
489 $cell->text .= html_writer::end_tag('ul');
490 }
491 $row->cells[] = $cell;
492 }
493
494 // Paddding (the last week may have blank days at the end)
495 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
496 $cell = new html_table_cell('&nbsp;');
9e142d11 497 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
498 $row->cells[] = $cell;
499 }
500 $table->data[] = $row;
501 $output .= html_writer::table($table);
502
36dc3b71
SH
503 return $output;
504 }
505
36dc3b71
SH
506 /**
507 * Displays upcoming events
508 *
509 * @param calendar_information $calendar
510 * @param int $futuredays
511 * @param int $maxevents
512 * @return string
513 */
797cedc7
SH
514 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
515
516 if ($returnurl === null) {
517 $returnurl = $this->page->url;
518 }
519
36dc3b71
SH
520 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
521
522 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 523 $output .= $this->course_filter_selector($returnurl, get_string('upcomingeventsfor', 'calendar'));
797cedc7
SH
524 if (calendar_user_can_add_event($calendar->course)) {
525 $output .= $this->add_event_button($calendar->course->id);
36dc3b71 526 }
36dc3b71
SH
527 $output .= html_writer::end_tag('div');
528
529 if ($events) {
11edf09c 530 $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
36dc3b71
SH
531 foreach ($events as $event) {
532 // Convert to calendar_event object so that we transform description
533 // accordingly
534 $event = new calendar_event($event);
484617d2 535 $event->calendarcourseid = $calendar->courseid;
36dc3b71
SH
536 $output .= $this->event($event);
537 }
538 $output .= html_writer::end_tag('div');
539 } else {
136f8f09 540 $output .= html_writer::span(get_string('noupcomingevents', 'calendar'), 'calendar-information calendar-no-results');
36dc3b71
SH
541 }
542
543 return $output;
544 }
545
546 /**
547 * Displays a course filter selector
548 *
ba1ce6e9
SH
549 * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
550 * @param string $label The label to use for the course select.
36dc3b71
SH
551 * @return string
552 */
797cedc7 553 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
6be8c6b7 554 global $USER, $SESSION, $CFG;
36dc3b71
SH
555
556 if (!isloggedin() or isguestuser()) {
557 return '';
558 }
559
6ca657a7 560 if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
36dc3b71
SH
561 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
562 } else {
563 $courses = enrol_get_my_courses();
564 }
565
566 unset($courses[SITEID]);
567
568 $courseoptions = array();
569 $courseoptions[SITEID] = get_string('fulllistofcourses');
570 foreach ($courses as $course) {
6ca657a7 571 $coursecontext = context_course::instance($course->id);
8ebbb06a 572 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
36dc3b71
SH
573 }
574
797cedc7
SH
575 if ($this->page->course->id !== SITEID) {
576 $selected = $this->page->course->id;
36dc3b71
SH
577 } else {
578 $selected = '';
579 }
ba1ce6e9
SH
580 $courseurl = new moodle_url($returnurl);
581 $courseurl->remove_params('course');
582 $select = new single_select($courseurl, 'course', $courseoptions, $selected, null);
36dc3b71 583 $select->class = 'cal_courses_flt';
91a774e2 584 if ($label !== null) {
5c576552
RW
585 $select->set_label($label);
586 } else {
587 $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
91a774e2
SH
588 }
589 return $this->output->render($select);
36dc3b71 590 }
e30390a0
SH
591
592 /**
593 * Renders a table containing information about calendar subscriptions.
594 *
595 * @param int $courseid
596 * @param array $subscriptions
597 * @param string $importresults
598 * @return string
599 */
600 public function subscription_details($courseid, $subscriptions, $importresults = '') {
601 $table = new html_table();
602 $table->head = array(
603 get_string('colcalendar', 'calendar'),
604 get_string('collastupdated', 'calendar'),
8c84f374 605 get_string('eventkind', 'calendar'),
e30390a0
SH
606 get_string('colpoll', 'calendar'),
607 get_string('colactions', 'calendar')
608 );
609 $table->align = array('left', 'left', 'left', 'center');
610 $table->width = '100%';
611 $table->data = array();
612
613 if (empty($subscriptions)) {
614 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
f936c2a9 615 $cell->colspan = 5;
e30390a0
SH
616 $table->data[] = new html_table_row(array($cell));
617 }
618 $strnever = new lang_string('never', 'calendar');
619 foreach ($subscriptions as $sub) {
620 $label = $sub->name;
621 if (!empty($sub->url)) {
622 $label = html_writer::link($sub->url, $label);
623 }
624 if (empty($sub->lastupdated)) {
625 $lastupdated = $strnever->out();
626 } else {
627 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
628 }
629
630 $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
631 $cell->colspan = 2;
8c84f374 632 $type = $sub->eventtype . 'events';
e30390a0
SH
633
634 $table->data[] = new html_table_row(array(
635 new html_table_cell($label),
636 new html_table_cell($lastupdated),
8c84f374 637 new html_table_cell(get_string($type, 'calendar')),
e30390a0
SH
638 $cell
639 ));
640 }
641
642 $out = $this->output->box_start('generalbox calendarsubs');
643
644 $out .= $importresults;
645 $out .= html_writer::table($table);
646 $out .= $this->output->box_end();
647 return $out;
648 }
649
650 /**
651 * Creates a form to perform actions on a given subscription.
652 *
653 * @param stdClass $subscription
654 * @param int $courseid
655 * @return string
656 */
657 protected function subscription_action_form($subscription, $courseid) {
658 // Assemble form for the subscription row.
659 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
660 if (empty($subscription->url)) {
661 // Don't update an iCal file, which has no URL.
662 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
663 } else {
664 // Assemble pollinterval control.
665 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
666 $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
667 foreach (calendar_get_pollinterval_choices() as $k => $v) {
668 $attributes = array();
669 if ($k == $subscription->pollinterval) {
670 $attributes['selected'] = 'selected';
671 }
4e3ce346 672 $attributes['value'] = $k;
e30390a0
SH
673 $html .= html_writer::tag('option', $v, $attributes);
674 }
675 $html .= html_writer::end_tag('select');
676 $html .= html_writer::end_tag('div');
677 }
678 $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
679 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
680 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
681 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
682 if (!empty($subscription->url)) {
ee74a2a1
AA
683 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
684 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
e30390a0 685 }
ee74a2a1
AA
686 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
687 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
e30390a0
SH
688 $html .= html_writer::end_tag('div');
689 $html .= html_writer::end_tag('form');
690 return $html;
691 }
2ad8f0f6 692}