MDL-35826 - Calendar - Creating a caption containing the month name for mini-calendars
[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) {
292 $event = calendar_add_event_metadata($event);
293
0f927f1e 294 $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
36dc3b71
SH
295
296 $table = new html_table();
297 $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
298 $table->data = array(
299 0 => new html_table_row(),
300 1 => new html_table_row(),
301 );
302
303 if (!empty($event->icon)) {
0f927f1e 304 $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
36dc3b71 305 } else {
0f927f1e 306 $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
36dc3b71
SH
307 }
308 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
309
310 $table->data[0]->cells[1] = new html_table_cell();
311 $table->data[0]->cells[1]->attributes['class'] .= ' topic';
312 if (!empty($event->referer)) {
313 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
314 } else {
315 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name'));
316 }
317 if (!empty($event->courselink)) {
318 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
319 }
320 if (!empty($event->time)) {
321 $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
322 } else {
323 $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
324 }
325
326 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
327 $table->data[1]->cells[0]->attributes['class'] .= 'side';
328
329 $table->data[1]->cells[1] = new html_table_cell($event->description);
330 $table->data[1]->cells[1]->attributes['class'] .= ' description';
331 if (isset($event->cssclass)) {
332 $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
333 }
334
335 if (calendar_edit_event_allowed($event) && $showactions) {
336 if (empty($event->cmid)) {
337 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
338 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
339 if (!empty($event->calendarcourseid)) {
340 $editlink->param('course', $event->calendarcourseid);
341 $deletelink->param('course', $event->calendarcourseid);
342 }
343 } else {
344 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
345 $deletelink = null;
346 }
347
348 $commands = html_writer::start_tag('div', array('class'=>'commands'));
349 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
350 $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')));
351 $commands .= html_writer::end_tag('a');
352 if ($deletelink != null) {
353 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
354 $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')));
355 $commands .= html_writer::end_tag('a');
356 }
357 $commands .= html_writer::end_tag('div');
358 $table->data[1]->cells[1]->text .= $commands;
359 }
360 return html_writer::table($table);
361 }
362
363 /**
364 * Displays a month in detail
365 *
36dc3b71
SH
366 * @param calendar_information $calendar
367 * @return string
368 */
797cedc7
SH
369 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
370 global $CFG;
371
372 if (empty($returnurl)) {
373 $returnurl = $this->page->url;
374 }
6be8c6b7 375
36dc3b71 376 $date = usergetdate(time());
36dc3b71
SH
377
378 $display = new stdClass;
379 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
380 $display->maxwday = $display->minwday + 6;
ac5e0418
SH
381 $display->thismonth = ($date['mon'] == $calendar->month);
382 $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year);
36dc3b71
SH
383
384 $startwday = 0;
385 if (get_user_timezone_offset() < 99) {
386 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
ac5e0418
SH
387 $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT
388 $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT
36dc3b71
SH
389 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
390 } else {
391 // no timezone info specified
ac5e0418
SH
392 $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year);
393 $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year);
36dc3b71
SH
394 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
395 }
396
397 // Align the starting weekday to fall in our display range
398 if ($startwday < $display->minwday) {
399 $startwday += 7;
400 }
401
402 // Get events from database
403 $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses);
404 if (!empty($events)) {
405 foreach($events as $eventid => $event) {
406 $event = new calendar_event($event);
407 if (!empty($event->modulename)) {
408 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
409 if (!groups_course_module_visible($cm)) {
410 unset($events[$eventid]);
411 }
412 }
413 }
414 }
415
416 // Extract information: events vs. time
ac5e0418 417 calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
36dc3b71
SH
418
419 $output = html_writer::start_tag('div', array('class'=>'header'));
797cedc7
SH
420 if (calendar_user_can_add_event($calendar->course)) {
421 $output .= $this->add_event_button($calendar->course->id, null, $calendar->month, $calendar->year);
36dc3b71 422 }
797cedc7 423 $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl);
36dc3b71
SH
424 $output .= html_writer::end_tag('div', array('class'=>'header'));
425 // Controls
2bf448ca 426 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls'));
36dc3b71 427
797cedc7
SH
428 $days = calendar_get_days();
429
36dc3b71
SH
430 $table = new html_table();
431 $table->attributes = array('class'=>'calendarmonth calendartable');
89c7c7a9
RW
432 $time = make_timestamp($calendar->year, $calendar->month);
433 $table->summary = get_string('calendarheading', 'calendar', userdate($time, get_string('strftimemonthyear')));
36dc3b71
SH
434 $table->data = array();
435
436 $header = new html_table_row();
437 $header->attributes = array('class'=>'weekdays');
438 $header->cells = array();
439 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
440 // This uses the % operator to get the correct weekday no matter what shift we have
441 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
797cedc7 442 $cell = new html_table_cell(get_string($days[$i % 7], 'calendar'));
36dc3b71
SH
443 $cell->header = true;
444 $header->cells[] = $cell;
445 }
446
447 // For the table display. $week is the row; $dayweek is the column.
448 $week = 1;
449 $dayweek = $startwday;
450
2953ad13
JI
451 // Create an array of all the week days.
452 $wdays = array(0 => '<strong>'. get_string('sunday', 'calendar'). '</strong>',
453 1 => '<strong>'. get_string('monday', 'calendar'). '</strong>',
454 2 => '<strong>'. get_string('tuesday', 'calendar'). '</strong>',
455 3 => '<strong>'. get_string('wednesday', 'calendar'). '</strong>',
456 4 => '<strong>'. get_string('thursday', 'calendar'). '</strong>',
457 5 => '<strong>'. get_string('friday', 'calendar'). '</strong>',
458 6 => '<strong>'. get_string('saturday', 'calendar'). '</strong>');
459
460 // Loop only if the day offset is greater than 0.
461 // This loop involves shifting the days around until the desired start day
462 // is at the start of the array.
463 $daycount = 0;
464 while ($display->minwday > $daycount++) {
465 $wdays_end = array_shift($wdays);
466 array_push($wdays, $wdays_end);
467 }
468
043c7cb8
PS
469 // Now we set the (modified) array to the table header to be displayed.
470 $table->head = $wdays;
2953ad13
JI
471
472 $row = new html_table_row(array());
473
36dc3b71
SH
474 // Paddding (the first week may have blank days in the beginning)
475 for($i = $display->minwday; $i < $startwday; ++$i) {
476 $cell = new html_table_cell('&nbsp;');
9e142d11 477 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
478 $row->cells[] = $cell;
479 }
480
481 // Now display all the calendar
797cedc7
SH
482 $weekend = CALENDAR_DEFAULT_WEEKEND;
483 if (isset($CFG->calendar_weekend)) {
484 $weekend = intval($CFG->calendar_weekend);
485 }
486
36dc3b71
SH
487 for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) {
488 if($dayweek > $display->maxwday) {
489 // We need to change week (table row)
490 $table->data[] = $row;
491 $row = new html_table_row(array());
492 $dayweek = $display->minwday;
493 ++$week;
494 }
495
496 // Reset vars
497 $cell = new html_table_cell();
0f927f1e 498 $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
499
500 $cellclasses = array();
501
797cedc7 502 if ($weekend & (1 << ($dayweek % 7))) {
36dc3b71
SH
503 // Weekend. This is true no matter what the exact range is.
504 $cellclasses[] = 'weekend';
505 }
506
507 // Special visual fx if an event is defined
508 if (isset($eventsbyday[$calendar->day])) {
509 if(count($eventsbyday[$calendar->day]) == 1) {
510 $title = get_string('oneevent', 'calendar');
511 } else {
512 $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day]));
513 }
514 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day'));
515 } else {
516 $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day'));
517 }
518
519 // Special visual fx if an event spans many days
520 $durationclass = false;
521 if (isset($typesbyday[$calendar->day]['durationglobal'])) {
522 $durationclass = 'duration_global';
523 } else if (isset($typesbyday[$calendar->day]['durationcourse'])) {
524 $durationclass = 'duration_course';
525 } else if (isset($typesbyday[$calendar->day]['durationgroup'])) {
526 $durationclass = 'duration_group';
527 } else if (isset($typesbyday[$calendar->day]['durationuser'])) {
528 $durationclass = 'duration_user';
529 }
530 if ($durationclass) {
531 $cellclasses[] = 'duration';
532 $cellclasses[] = $durationclass;
533 }
534
535 // Special visual fx for today
9e142d11
JF
536 if ($display->thismonth && $calendar->day == $date['mday']) {
537 $cellclasses[] = 'day today';
36dc3b71 538 } else {
9e142d11 539 $cellclasses[] = 'day nottoday';
36dc3b71
SH
540 }
541 $cell->attributes = array('class'=>join(' ',$cellclasses));
542
543 if (isset($eventsbyday[$calendar->day])) {
544 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
545 foreach($eventsbyday[$calendar->day] as $eventindex) {
546 // If event has a class set then add it to the event <li> tag
547 $attributes = array();
548 if (!empty($events[$eventindex]->class)) {
549 $attributes['class'] = $events[$eventindex]->class;
550 }
0f927f1e
SH
551 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
552 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
36dc3b71
SH
553 $cell->text .= html_writer::tag('li', $link, $attributes);
554 }
555 $cell->text .= html_writer::end_tag('ul');
556 }
557 if (isset($durationbyday[$calendar->day])) {
558 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
559 foreach($durationbyday[$calendar->day] as $eventindex) {
560 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
561 }
562 $cell->text .= html_writer::end_tag('ul');
563 }
564 $row->cells[] = $cell;
565 }
566
567 // Paddding (the last week may have blank days at the end)
568 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
569 $cell = new html_table_cell('&nbsp;');
9e142d11 570 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
571 $row->cells[] = $cell;
572 }
573 $table->data[] = $row;
574 $output .= html_writer::table($table);
575
36dc3b71
SH
576 return $output;
577 }
578
579 /**
580 * Displays a filter selection table
581 *
582 * @param calendar_information $calendar
583 * @return string
ef21ad21
FM
584 * @deprecated since Moodle 2.4 MDL-32309
585 * @see calendar_filter_controls()
36dc3b71 586 */
797cedc7 587 protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) {
36dc3b71 588 global $SESSION;
ef21ad21
FM
589 debugging('Method core_calendar_renderer::filter_selection_table() is deprecated, please use '.
590 'calendar_filter_controls() instead', DEBUG_DEVELOPER);
6be8c6b7 591
797cedc7
SH
592 if ($returnurl === null) {
593 $returnurl = $this->page->url;
594 }
595
36dc3b71
SH
596 $output = html_writer::start_tag('div', array('class'=>'filters'));
597 $output .= html_writer::start_tag('table');
598 $output .= html_writer::start_tag('tr');
599
600 // Global events
eb59a448 601 $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 602 $strglobalevents = get_string('globalevents', 'calendar');
797cedc7 603 if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
36dc3b71 604 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;'));
675715a7 605 $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
606 } else {
607 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 608 $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
609 }
610
611 // Course events
eb59a448 612 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 613 $strcourseevents = get_string('courseevents', 'calendar');
797cedc7 614 if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
36dc3b71 615 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;'));
675715a7 616 $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
617 } else {
618 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 619 $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
620 }
621 $output .= html_writer::end_tag('tr');
622
623 if(isloggedin() && !isguestuser()) {
624 $output .= html_writer::start_tag('tr');
625 // Group events
eb59a448 626 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 627 $strgroupevents = get_string('groupevents', 'calendar');
797cedc7 628 if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
36dc3b71 629 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;'));
675715a7 630 $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
631 } else {
632 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 633 $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
634 }
635 // User events
eb59a448 636 $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
675715a7 637 $struserevents = get_string('userevents', 'calendar');
797cedc7 638 if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
36dc3b71 639 $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;'));
675715a7 640 $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
641 } else {
642 $output .= html_writer::tag('td', '', array('style'=>'width:8px;'));
675715a7 643 $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
644 }
645 $output .= html_writer::end_tag('tr');
646 }
647 $output .= html_writer::end_tag('table');
648 $output .= html_writer::end_tag('div');
649 return $output;
650 }
651
652 /**
653 * Displays upcoming events
654 *
655 * @param calendar_information $calendar
656 * @param int $futuredays
657 * @param int $maxevents
658 * @return string
659 */
797cedc7
SH
660 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
661
662 if ($returnurl === null) {
663 $returnurl = $this->page->url;
664 }
665
36dc3b71
SH
666 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
667
668 $output = html_writer::start_tag('div', array('class'=>'header'));
797cedc7
SH
669 if (calendar_user_can_add_event($calendar->course)) {
670 $output .= $this->add_event_button($calendar->course->id);
36dc3b71
SH
671 }
672 $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump'));
797cedc7 673 $output .= $this->course_filter_selector($returnurl);
36dc3b71
SH
674 $output .= html_writer::end_tag('div');
675
676 if ($events) {
677 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
678 foreach ($events as $event) {
679 // Convert to calendar_event object so that we transform description
680 // accordingly
681 $event = new calendar_event($event);
484617d2 682 $event->calendarcourseid = $calendar->courseid;
36dc3b71
SH
683 $output .= $this->event($event);
684 }
685 $output .= html_writer::end_tag('div');
686 } else {
687 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
688 }
689
690 return $output;
691 }
692
693 /**
694 * Displays a course filter selector
695 *
696 * @param array $getvars
697 * @return string
698 */
797cedc7 699 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
6be8c6b7 700 global $USER, $SESSION, $CFG;
36dc3b71
SH
701
702 if (!isloggedin() or isguestuser()) {
703 return '';
704 }
705
6ca657a7 706 if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
36dc3b71
SH
707 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
708 } else {
709 $courses = enrol_get_my_courses();
710 }
711
712 unset($courses[SITEID]);
713
714 $courseoptions = array();
715 $courseoptions[SITEID] = get_string('fulllistofcourses');
716 foreach ($courses as $course) {
6ca657a7 717 $coursecontext = context_course::instance($course->id);
8ebbb06a 718 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
36dc3b71
SH
719 }
720
797cedc7
SH
721 if ($this->page->course->id !== SITEID) {
722 $selected = $this->page->course->id;
36dc3b71
SH
723 } else {
724 $selected = '';
725 }
eb59a448 726 $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 727 $select->class = 'cal_courses_flt';
91a774e2 728 if ($label !== null) {
5c576552
RW
729 $select->set_label($label);
730 } else {
731 $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
91a774e2
SH
732 }
733 return $this->output->render($select);
36dc3b71 734 }
e30390a0
SH
735
736 /**
737 * Renders a table containing information about calendar subscriptions.
738 *
739 * @param int $courseid
740 * @param array $subscriptions
741 * @param string $importresults
742 * @return string
743 */
744 public function subscription_details($courseid, $subscriptions, $importresults = '') {
745 $table = new html_table();
746 $table->head = array(
747 get_string('colcalendar', 'calendar'),
748 get_string('collastupdated', 'calendar'),
8c84f374 749 get_string('eventkind', 'calendar'),
e30390a0
SH
750 get_string('colpoll', 'calendar'),
751 get_string('colactions', 'calendar')
752 );
753 $table->align = array('left', 'left', 'left', 'center');
754 $table->width = '100%';
755 $table->data = array();
756
757 if (empty($subscriptions)) {
758 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
759 $cell->colspan = 4;
760 $table->data[] = new html_table_row(array($cell));
761 }
762 $strnever = new lang_string('never', 'calendar');
763 foreach ($subscriptions as $sub) {
764 $label = $sub->name;
765 if (!empty($sub->url)) {
766 $label = html_writer::link($sub->url, $label);
767 }
768 if (empty($sub->lastupdated)) {
769 $lastupdated = $strnever->out();
770 } else {
771 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
772 }
773
774 $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
775 $cell->colspan = 2;
8c84f374 776 $type = $sub->eventtype . 'events';
e30390a0
SH
777
778 $table->data[] = new html_table_row(array(
779 new html_table_cell($label),
780 new html_table_cell($lastupdated),
8c84f374 781 new html_table_cell(get_string($type, 'calendar')),
e30390a0
SH
782 $cell
783 ));
784 }
785
786 $out = $this->output->box_start('generalbox calendarsubs');
787
788 $out .= $importresults;
789 $out .= html_writer::table($table);
790 $out .= $this->output->box_end();
791 return $out;
792 }
793
794 /**
795 * Creates a form to perform actions on a given subscription.
796 *
797 * @param stdClass $subscription
798 * @param int $courseid
799 * @return string
800 */
801 protected function subscription_action_form($subscription, $courseid) {
802 // Assemble form for the subscription row.
803 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
804 if (empty($subscription->url)) {
805 // Don't update an iCal file, which has no URL.
806 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
807 } else {
808 // Assemble pollinterval control.
809 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
810 $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
811 foreach (calendar_get_pollinterval_choices() as $k => $v) {
812 $attributes = array();
813 if ($k == $subscription->pollinterval) {
814 $attributes['selected'] = 'selected';
815 }
4e3ce346 816 $attributes['value'] = $k;
e30390a0
SH
817 $html .= html_writer::tag('option', $v, $attributes);
818 }
819 $html .= html_writer::end_tag('select');
820 $html .= html_writer::end_tag('div');
821 }
822 $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
823 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
824 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
825 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
826 if (!empty($subscription->url)) {
827 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('update')));
828 }
829 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('remove')));
830 $html .= html_writer::end_tag('div');
831 $html .= html_writer::end_tag('form');
832 return $html;
833 }
36dc3b71 834}