MDL-41792 block_calendar_upcoming: tidy existing code
[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
35 /**
36 * Creates a basic export form
37 *
38 * @param bool $allowthisweek
39 * @param bool $allownextweek
40 * @param bool $allownextmonth
d52777b4 41 * @param int $userid
36dc3b71
SH
42 * @param string $authtoken
43 * @return string
44 */
d52777b4 45 public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $userid, $authtoken) {
af5d990b 46 global $CFG;
36dc3b71
SH
47
48 $output = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header'));
49 $output .= html_writer::start_tag('fieldset');
50 $output .= html_writer::tag('legend', get_string('commontasks', 'calendar'));
51 $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get'));
52
53 $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
6be8c6b7 54
36dc3b71
SH
55 $output .= html_writer::start_tag('div', array('class'=>'indent'));
56 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked'));
57 $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all'));
58 $output .= html_writer::empty_tag('br');
59 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses'));
60 $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course'));
61 $output .= html_writer::empty_tag('br');
62 $output .= html_writer::end_tag('div');
6be8c6b7 63
36dc3b71
SH
64 $output .= html_writer::tag('div', get_string('for', 'calendar').':');
65
66 $output .= html_writer::start_tag('div', array('class'=>'indent'));
67 if ($allowthisweek) {
68 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked'));
69 $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow'));
70 $output .= html_writer::empty_tag('br');
71 }
72 if ($allownextweek) {
73 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext'));
74 $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext'));
75 $output .= html_writer::empty_tag('br');
76 }
77 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow'));
78 $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow'));
79 $output .= html_writer::empty_tag('br');
80 if ($allownextmonth) {
81 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext'));
82 $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext'));
83 $output .= html_writer::empty_tag('br');
84 }
85 $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming'));
86 $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc'));
87 $output .= html_writer::empty_tag('br');
36dc3b71 88
af5d990b
AA
89 if ($CFG->calendar_customexport) {
90 $a = new stdClass();
91 $now = time();
92 $time = $now - $CFG->calendar_exportlookback * DAYSECS;
93 $a->timestart = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
94 $time = $now + $CFG->calendar_exportlookahead * DAYSECS;
95 $a->timeend = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
96 $output .= html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'preset_time', 'id' => 'pt_custom', 'value' => 'custom'));
97 $output .= html_writer::tag('label', get_string('customexport', 'calendar', $a), array('for' => 'pt_custom'));
98 $output .= html_writer::empty_tag('br');
99 }
100
101 $output .= html_writer::end_tag('div');
36dc3b71
SH
102 $output .= html_writer::start_tag('div', array('class'=>'rightalign'));
103 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>''));
104 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>''));
105 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>''));
d52777b4 106 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'userid', 'value'=>$userid));
36dc3b71
SH
107 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
108
d52777b4 109 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name' => 'generateurl', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar')));
36dc3b71
SH
110 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar')));
111
112 $output .= html_writer::end_tag('div');
113
114 $output .= html_writer::end_tag('form');
115 $output .= html_writer::end_tag('fieldset');
116
117 $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;'));
118 $output .= html_writer::tag('p', get_string('urlforical', 'calendar'));
119 $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;'));
120 $output .= html_writer::end_tag('div');
121
36dc3b71
SH
122 return $output;
123 }
124
125 /**
126 * Starts the standard layout for the page
6be8c6b7 127 *
36dc3b71
SH
128 * @return string
129 */
130 public function start_layout() {
131 return html_writer::start_tag('div', array('class'=>'maincalendar'));
132 }
133
134 /**
135 * Creates the remainder of the layout
136 *
137 * @return string
138 */
139 public function complete_layout() {
140 return html_writer::end_tag('div');
141 }
142
143 /**
144 * Produces the content for the filters block (pretend block)
145 *
146 * @param int $courseid
147 * @param int $day
148 * @param int $month
149 * @param int $year
150 * @param int $view
151 * @param int $courses
152 * @return string
153 */
154 public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
05f9d136
AA
155 $returnurl = $this->page->url;
156 $returnurl->param('course', $courseid);
157 return html_writer::tag('div', calendar_filter_controls($returnurl), array('class'=>'calendar_filters filters'));
36dc3b71
SH
158 }
159
160 /**
161 * Produces the content for the three months block (pretend block)
162 *
163 * This includes the previous month, the current month, and the next month
164 *
165 * @param calendar_information $calendar
166 * @return string
167 */
168 public function fake_block_threemonths(calendar_information $calendar) {
169
170 list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year);
171 list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year);
172
173 $content = html_writer::start_tag('div', array('class'=>'minicalendarblock'));
512edd65 174 $content .= calendar_get_mini( $calendar->courses, $calendar->groups, $calendar->users, $prevmon, $prevyr, 'display', $calendar->courseid);
36dc3b71
SH
175 $content .= html_writer::end_tag('div');
176 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
512edd65 177 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $calendar->month, $calendar->year, 'display', $calendar->courseid);
36dc3b71
SH
178 $content .= html_writer::end_tag('div');
179 $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock'));
512edd65 180 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $nextmon, $nextyr, 'display', $calendar->courseid);
36dc3b71
SH
181 $content .= html_writer::end_tag('div');
182 return $content;
183 }
184
185 /**
186 * Adds a pretent calendar block
187 *
188 * @param block_contents $bc
189 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
190 */
191 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
d9c26e21 192 $this->page->blocks->add_fake_block($bc, $pos);
36dc3b71
SH
193 }
194
195 /**
196 * Creates a button to add a new event
197 *
198 * @param int $courseid
199 * @param int $day
200 * @param int $month
201 * @param int $year
202 * @return string
203 */
204 protected function add_event_button($courseid, $day=null, $month=null, $year=null) {
205 $output = html_writer::start_tag('div', array('class'=>'buttons'));
206 $output .= html_writer::start_tag('form', array('action'=>CALENDAR_URL.'event.php', 'method'=>'get'));
207 $output .= html_writer::start_tag('div');
208 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'new'));
209 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'course', 'value'=>$courseid));
210 if ($day !== null) {
211 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>$day));
212 }
213 if ($month !== null) {
214 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>$month));
215 }
216 if ($year !== null) {
217 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>$year));
218 }
219 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('newevent', 'calendar')));
220 $output .= html_writer::end_tag('div');
221 $output .= html_writer::end_tag('form');
222 $output .= html_writer::end_tag('div');
223 return $output;
224 }
225
226 /**
227 * Displays the calendar for a single day
228 *
229 * @param calendar_information $calendar
230 * @return string
231 */
797cedc7
SH
232 public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
233
234 if ($returnurl === null) {
235 $returnurl = $this->page->url;
236 }
237
36dc3b71
SH
238 $calendar->checkdate();
239 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
240
241 $output = html_writer::start_tag('div', array('class'=>'header'));
797cedc7
SH
242 if (calendar_user_can_add_event($calendar->course)) {
243 $output .= $this->add_event_button($calendar->course->id, $calendar->day, $calendar->month, $calendar->year);
36dc3b71 244 }
91a774e2 245 //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump'));
797cedc7 246 $output .= $this->course_filter_selector($returnurl, get_string('dayview', 'calendar'));
36dc3b71
SH
247 $output .= html_writer::end_tag('div');
248 // Controls
249 $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'));
250
251 if (empty($events)) {
252 // There is nothing to display today.
253 $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
254 } else {
255 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
256 $underway = array();
257 // First, print details about events that start today
258 foreach ($events as $event) {
259 $event = new calendar_event($event);
260 $event->calendarcourseid = $calendar->courseid;
261 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
0f927f1e 262 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
263 $output .= $this->event($event);
264 } else { // Save this for later
265 $underway[] = $event;
266 }
267 }
268
269 // Then, show a list of all events that just span this day
270 if (!empty($underway)) {
271 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
272 foreach ($underway as $event) {
0f927f1e 273 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
274 $output .= $this->event($event);
275 }
276 }
277
278 $output .= html_writer::end_tag('div');
279 }
280
281 return $output;
282 }
283
284 /**
285 * Displays an event
286 *
287 * @param calendar_event $event
288 * @param bool $showactions
289 * @return string
290 */
291 public function event(calendar_event $event, $showactions=true) {
e73b527c
AA
292 global $CFG;
293
36dc3b71
SH
294 $event = calendar_add_event_metadata($event);
295
0f927f1e 296 $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
36dc3b71
SH
297
298 $table = new html_table();
299 $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
300 $table->data = array(
301 0 => new html_table_row(),
302 1 => new html_table_row(),
303 );
304
305 if (!empty($event->icon)) {
0f927f1e 306 $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
36dc3b71 307 } else {
0f927f1e 308 $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
36dc3b71
SH
309 }
310 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
311
312 $table->data[0]->cells[1] = new html_table_cell();
313 $table->data[0]->cells[1]->attributes['class'] .= ' topic';
314 if (!empty($event->referer)) {
315 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
316 } else {
317 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
318 }
319 if (!empty($event->courselink)) {
320 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
321 }
e73b527c
AA
322 // Show subscription source if needed.
323 if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
324 if (!empty($event->subscription->url)) {
325 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
326 } else {
327 // File based ical.
328 $source = get_string('subsource', 'calendar', $event->subscription);
329 }
330 $table->data[0]->cells[1]->text .= html_writer::tag('div', $source, array('class' => 'subscription'));
8a7326ed 331 }
36dc3b71
SH
332 if (!empty($event->time)) {
333 $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
334 } else {
335 $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
336 }
337
338 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
339 $table->data[1]->cells[0]->attributes['class'] .= 'side';
340
341 $table->data[1]->cells[1] = new html_table_cell($event->description);
342 $table->data[1]->cells[1]->attributes['class'] .= ' description';
343 if (isset($event->cssclass)) {
344 $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
345 }
346
347 if (calendar_edit_event_allowed($event) && $showactions) {
348 if (empty($event->cmid)) {
349 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
350 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
351 if (!empty($event->calendarcourseid)) {
352 $editlink->param('course', $event->calendarcourseid);
353 $deletelink->param('course', $event->calendarcourseid);
354 }
355 } else {
356 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
357 $deletelink = null;
358 }
359
360 $commands = html_writer::start_tag('div', array('class'=>'commands'));
361 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
362 $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')));
363 $commands .= html_writer::end_tag('a');
364 if ($deletelink != null) {
365 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
366 $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')));
367 $commands .= html_writer::end_tag('a');
368 }
369 $commands .= html_writer::end_tag('div');
370 $table->data[1]->cells[1]->text .= $commands;
371 }
372 return html_writer::table($table);
373 }
374
375 /**
376 * Displays a month in detail
377 *
36dc3b71
SH
378 * @param calendar_information $calendar
379 * @return string
380 */
797cedc7
SH
381 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
382 global $CFG;
383
384 if (empty($returnurl)) {
385 $returnurl = $this->page->url;
386 }
6be8c6b7 387
36dc3b71 388 $date = usergetdate(time());
36dc3b71
SH
389
390 $display = new stdClass;
391 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
392 $display->maxwday = $display->minwday + 6;
ac5e0418
SH
393 $display->thismonth = ($date['mon'] == $calendar->month);
394 $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year);
36dc3b71
SH
395
396 $startwday = 0;
397 if (get_user_timezone_offset() < 99) {
398 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
ac5e0418
SH
399 $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT
400 $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT
36dc3b71
SH
401 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
402 } else {
403 // no timezone info specified
ac5e0418
SH
404 $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year);
405 $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year);
36dc3b71
SH
406 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
407 }
408
409 // Align the starting weekday to fall in our display range
410 if ($startwday < $display->minwday) {
411 $startwday += 7;
412 }
413
414 // Get events from database
415 $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
416 if (!empty($events)) {
417 foreach($events as $eventid => $event) {
418 $event = new calendar_event($event);
419 if (!empty($event->modulename)) {
420 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
421 if (!groups_course_module_visible($cm)) {
422 unset($events[$eventid]);
423 }
424 }
425 }
426 }
427
428 // Extract information: events vs. time
ac5e0418 429 calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
36dc3b71
SH
430
431 $output = html_writer::start_tag('div', array('class'=>'header'));
797cedc7
SH
432 if (calendar_user_can_add_event($calendar->course)) {
433 $output .= $this->add_event_button($calendar->course->id, null, $calendar->month, $calendar->year);
36dc3b71 434 }
797cedc7 435 $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl);
36dc3b71
SH
436 $output .= html_writer::end_tag('div', array('class'=>'header'));
437 // Controls
2bf448ca 438 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
36dc3b71 439
797cedc7
SH
440 $days = calendar_get_days();
441
36dc3b71
SH
442 $table = new html_table();
443 $table->attributes = array('class'=>'calendarmonth calendartable');
89c7c7a9
RW
444 $time = make_timestamp($calendar->year, $calendar->month);
445 $table->summary = get_string('calendarheading', 'calendar', userdate($time, get_string('strftimemonthyear')));
36dc3b71
SH
446 $table->data = array();
447
448 $header = new html_table_row();
449 $header->attributes = array('class'=>'weekdays');
450 $header->cells = array();
451 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
452 // This uses the % operator to get the correct weekday no matter what shift we have
453 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
797cedc7 454 $cell = new html_table_cell(get_string($days[$i % 7], 'calendar'));
36dc3b71
SH
455 $cell->header = true;
456 $header->cells[] = $cell;
457 }
458
459 // For the table display. $week is the row; $dayweek is the column.
460 $week = 1;
461 $dayweek = $startwday;
462
2953ad13 463 // Create an array of all the week days.
310845b9
ME
464 $wdays = array(0 => '<strong>'. get_string('sun', 'calendar'). '</strong>',
465 1 => '<strong>'. get_string('mon', 'calendar'). '</strong>',
466 2 => '<strong>'. get_string('tue', 'calendar'). '</strong>',
467 3 => '<strong>'. get_string('wed', 'calendar'). '</strong>',
468 4 => '<strong>'. get_string('thu', 'calendar'). '</strong>',
469 5 => '<strong>'. get_string('fri', 'calendar'). '</strong>',
470 6 => '<strong>'. get_string('sat', 'calendar'). '</strong>');
2953ad13
JI
471
472 // Loop only if the day offset is greater than 0.
473 // This loop involves shifting the days around until the desired start day
474 // is at the start of the array.
475 $daycount = 0;
476 while ($display->minwday > $daycount++) {
477 $wdays_end = array_shift($wdays);
478 array_push($wdays, $wdays_end);
479 }
480
043c7cb8
PS
481 // Now we set the (modified) array to the table header to be displayed.
482 $table->head = $wdays;
2953ad13
JI
483
484 $row = new html_table_row(array());
485
36dc3b71
SH
486 // Paddding (the first week may have blank days in the beginning)
487 for($i = $display->minwday; $i < $startwday; ++$i) {
488 $cell = new html_table_cell('&nbsp;');
9e142d11 489 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
490 $row->cells[] = $cell;
491 }
492
493 // Now display all the calendar
797cedc7
SH
494 $weekend = CALENDAR_DEFAULT_WEEKEND;
495 if (isset($CFG->calendar_weekend)) {
496 $weekend = intval($CFG->calendar_weekend);
497 }
498
36dc3b71
SH
499 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
500 if($dayweek > $display->maxwday) {
501 // We need to change week (table row)
502 $table->data[] = $row;
503 $row = new html_table_row(array());
504 $dayweek = $display->minwday;
505 ++$week;
506 }
507
508 // Reset vars
509 $cell = new html_table_cell();
0f927f1e 510 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year);
36dc3b71
SH
511
512 $cellclasses = array();
513
797cedc7 514 if ($weekend & (1 << ($dayweek % 7))) {
36dc3b71
SH
515 // Weekend. This is true no matter what the exact range is.
516 $cellclasses[] = 'weekend';
517 }
518
519 // Special visual fx if an event is defined
520 if (isset($eventsbyday[$calendar->day])) {
521 if(count($eventsbyday[$calendar->day]) == 1) {
522 $title = get_string('oneevent', 'calendar');
523 } else {
524 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
525 }
526 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
527 } else {
528 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
529 }
530
531 // Special visual fx if an event spans many days
532 $durationclass = false;
533 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
534 $durationclass = 'duration_global';
535 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
536 $durationclass = 'duration_course';
537 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
538 $durationclass = 'duration_group';
539 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
540 $durationclass = 'duration_user';
541 }
542 if ($durationclass) {
543 $cellclasses[] = 'duration';
544 $cellclasses[] = $durationclass;
545 }
546
547 // Special visual fx for today
9e142d11
JF
548 if ($display->thismonth && $calendar->day == $date['mday']) {
549 $cellclasses[] = 'day today';
36dc3b71 550 } else {
9e142d11 551 $cellclasses[] = 'day nottoday';
36dc3b71
SH
552 }
553 $cell->attributes = array('class'=>join(' ',$cellclasses));
554
555 if (isset($eventsbyday[$calendar->day])) {
556 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
557 foreach($eventsbyday[$calendar->day] as $eventindex) {
558 // If event has a class set then add it to the event <li> tag
559 $attributes = array();
560 if (!empty($events[$eventindex]->class)) {
561 $attributes['class'] = $events[$eventindex]->class;
562 }
0f927f1e
SH
563 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
564 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
36dc3b71
SH
565 $cell->text .= html_writer::tag('li', $link, $attributes);
566 }
567 $cell->text .= html_writer::end_tag('ul');
568 }
569 if (isset($durationbyday[$calendar->day])) {
570 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
571 foreach($durationbyday[$calendar->day] as $eventindex) {
572 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
573 }
574 $cell->text .= html_writer::end_tag('ul');
575 }
576 $row->cells[] = $cell;
577 }
578
579 // Paddding (the last week may have blank days at the end)
580 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
581 $cell = new html_table_cell('&nbsp;');
9e142d11 582 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
583 $row->cells[] = $cell;
584 }
585 $table->data[] = $row;
586 $output .= html_writer::table($table);
587
36dc3b71
SH
588 return $output;
589 }
590
591 /**
592 * Displays a filter selection table
593 *
594 * @param calendar_information $calendar
595 * @return string
ef21ad21
FM
596 * @deprecated since Moodle 2.4 MDL-32309
597 * @see calendar_filter_controls()
36dc3b71 598 */
797cedc7 599 protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
36dc3b71 600 global $SESSION;
ef21ad21
FM
601 debugging('Method core_calendar_renderer::filter_selection_table() is deprecated, please use '.
602 'calendar_filter_controls() instead', DEBUG_DEVELOPER);
6be8c6b7 603
797cedc7
SH
604 if ($returnurl === null) {
605 $returnurl = $this->page->url;
606 }
607
36dc3b71
SH
608 $output = html_writer::start_tag('div', array('class'=>'filters'));
609 $output .= html_writer::start_tag('table');
610 $output .= html_writer::start_tag('tr');
611
612 // Global events
eb59a448 613 $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 614 $strglobalevents = get_string('globalevents', 'calendar');
797cedc7 615 if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
36dc3b71 616 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
675715a7 617 $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>').')');
36dc3b71
SH
618 } else {
619 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 620 $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>').')');
36dc3b71
SH
621 }
622
623 // Course events
eb59a448 624 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 625 $strcourseevents = get_string('courseevents', 'calendar');
797cedc7 626 if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
36dc3b71 627 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
675715a7 628 $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>').')');
36dc3b71
SH
629 } else {
630 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 631 $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>').')');
36dc3b71
SH
632 }
633 $output .= html_writer::end_tag('tr');
634
635 if(isloggedin() && !isguestuser()) {
636 $output .= html_writer::start_tag('tr');
637 // Group events
eb59a448 638 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 639 $strgroupevents = get_string('groupevents', 'calendar');
797cedc7 640 if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
36dc3b71 641 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
675715a7 642 $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>').')');
36dc3b71
SH
643 } else {
644 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 645 $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>').')');
36dc3b71
SH
646 }
647 // User events
eb59a448 648 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 649 $struserevents = get_string('userevents', 'calendar');
797cedc7 650 if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
36dc3b71 651 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
675715a7 652 $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>').')');
36dc3b71
SH
653 } else {
654 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 655 $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>').')');
36dc3b71
SH
656 }
657 $output .= html_writer::end_tag('tr');
658 }
659 $output .= html_writer::end_tag('table');
660 $output .= html_writer::end_tag('div');
661 return $output;
662 }
663
664 /**
665 * Displays upcoming events
666 *
667 * @param calendar_information $calendar
668 * @param int $futuredays
669 * @param int $maxevents
670 * @return string
671 */
797cedc7
SH
672 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
673
674 if ($returnurl === null) {
675 $returnurl = $this->page->url;
676 }
677
36dc3b71
SH
678 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
679
680 $output = html_writer::start_tag('div', array('class'=>'header'));
797cedc7
SH
681 if (calendar_user_can_add_event($calendar->course)) {
682 $output .= $this->add_event_button($calendar->course->id);
36dc3b71
SH
683 }
684 $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
797cedc7 685 $output .= $this->course_filter_selector($returnurl);
36dc3b71
SH
686 $output .= html_writer::end_tag('div');
687
688 if ($events) {
689 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
690 foreach ($events as $event) {
691 // Convert to calendar_event object so that we transform description
692 // accordingly
693 $event = new calendar_event($event);
484617d2 694 $event->calendarcourseid = $calendar->courseid;
36dc3b71
SH
695 $output .= $this->event($event);
696 }
697 $output .= html_writer::end_tag('div');
698 } else {
699 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
700 }
701
702 return $output;
703 }
704
705 /**
706 * Displays a course filter selector
707 *
708 * @param array $getvars
709 * @return string
710 */
797cedc7 711 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
6be8c6b7 712 global $USER, $SESSION, $CFG;
36dc3b71
SH
713
714 if (!isloggedin() or isguestuser()) {
715 return '';
716 }
717
6ca657a7 718 if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
36dc3b71
SH
719 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
720 } else {
721 $courses = enrol_get_my_courses();
722 }
723
724 unset($courses[SITEID]);
725
726 $courseoptions = array();
727 $courseoptions[SITEID] = get_string('fulllistofcourses');
728 foreach ($courses as $course) {
6ca657a7 729 $coursecontext = context_course::instance($course->id);
8ebbb06a 730 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
36dc3b71
SH
731 }
732
797cedc7
SH
733 if ($this->page->course->id !== SITEID) {
734 $selected = $this->page->course->id;
36dc3b71
SH
735 } else {
736 $selected = '';
737 }
eb59a448 738 $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);
36dc3b71 739 $select->class = 'cal_courses_flt';
91a774e2 740 if ($label !== null) {
5c576552
RW
741 $select->set_label($label);
742 } else {
743 $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
91a774e2
SH
744 }
745 return $this->output->render($select);
36dc3b71 746 }
e30390a0
SH
747
748 /**
749 * Renders a table containing information about calendar subscriptions.
750 *
751 * @param int $courseid
752 * @param array $subscriptions
753 * @param string $importresults
754 * @return string
755 */
756 public function subscription_details($courseid, $subscriptions, $importresults = '') {
757 $table = new html_table();
758 $table->head = array(
759 get_string('colcalendar', 'calendar'),
760 get_string('collastupdated', 'calendar'),
8c84f374 761 get_string('eventkind', 'calendar'),
e30390a0
SH
762 get_string('colpoll', 'calendar'),
763 get_string('colactions', 'calendar')
764 );
765 $table->align = array('left', 'left', 'left', 'center');
766 $table->width = '100%';
767 $table->data = array();
768
769 if (empty($subscriptions)) {
770 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
771 $cell->colspan = 4;
772 $table->data[] = new html_table_row(array($cell));
773 }
774 $strnever = new lang_string('never', 'calendar');
775 foreach ($subscriptions as $sub) {
776 $label = $sub->name;
777 if (!empty($sub->url)) {
778 $label = html_writer::link($sub->url, $label);
779 }
780 if (empty($sub->lastupdated)) {
781 $lastupdated = $strnever->out();
782 } else {
783 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
784 }
785
786 $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
787 $cell->colspan = 2;
8c84f374 788 $type = $sub->eventtype . 'events';
e30390a0
SH
789
790 $table->data[] = new html_table_row(array(
791 new html_table_cell($label),
792 new html_table_cell($lastupdated),
8c84f374 793 new html_table_cell(get_string($type, 'calendar')),
e30390a0
SH
794 $cell
795 ));
796 }
797
798 $out = $this->output->box_start('generalbox calendarsubs');
799
800 $out .= $importresults;
801 $out .= html_writer::table($table);
802 $out .= $this->output->box_end();
803 return $out;
804 }
805
806 /**
807 * Creates a form to perform actions on a given subscription.
808 *
809 * @param stdClass $subscription
810 * @param int $courseid
811 * @return string
812 */
813 protected function subscription_action_form($subscription, $courseid) {
814 // Assemble form for the subscription row.
815 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
816 if (empty($subscription->url)) {
817 // Don't update an iCal file, which has no URL.
818 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
819 } else {
820 // Assemble pollinterval control.
821 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
822 $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
823 foreach (calendar_get_pollinterval_choices() as $k => $v) {
824 $attributes = array();
825 if ($k == $subscription->pollinterval) {
826 $attributes['selected'] = 'selected';
827 }
4e3ce346 828 $attributes['value'] = $k;
e30390a0
SH
829 $html .= html_writer::tag('option', $v, $attributes);
830 }
831 $html .= html_writer::end_tag('select');
832 $html .= html_writer::end_tag('div');
833 }
834 $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
835 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
836 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
837 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
838 if (!empty($subscription->url)) {
ee74a2a1
AA
839 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
840 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
e30390a0 841 }
ee74a2a1
AA
842 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
843 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
e30390a0
SH
844 $html .= html_writer::end_tag('div');
845 $html .= html_writer::end_tag('form');
846 return $html;
847 }
36dc3b71 848}