weekly release 2.8dev
[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) {
da304137
MN
169 // Get the calendar type we are using.
170 $calendartype = \core_calendar\type_factory::get_calendar_instance();
36dc3b71 171
da304137 172 $date = $calendartype->timestamp_to_date_array($calendar->time);
36dc3b71 173
da304137 174 $prevmonth = calendar_sub_month($date['mon'], $date['year']);
1032966c
MN
175 $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
176 $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
177 $prevmonthtime['hour'], $prevmonthtime['minute']);
da304137
MN
178
179 $nextmonth = calendar_add_month($date['mon'], $date['year']);
1032966c
MN
180 $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
181 $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
182 $nextmonthtime['hour'], $nextmonthtime['minute']);
da304137
MN
183
184 $content = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
185 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $prevmonthtime);
36dc3b71 186 $content .= html_writer::end_tag('div');
da304137
MN
187 $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
188 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $calendar->time);
36dc3b71 189 $content .= html_writer::end_tag('div');
da304137
MN
190 $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
191 $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $nextmonthtime);
36dc3b71
SH
192 $content .= html_writer::end_tag('div');
193 return $content;
194 }
195
196 /**
197 * Adds a pretent calendar block
198 *
199 * @param block_contents $bc
200 * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
201 */
202 public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
d9c26e21 203 $this->page->blocks->add_fake_block($bc, $pos);
36dc3b71
SH
204 }
205
206 /**
207 * Creates a button to add a new event
208 *
209 * @param int $courseid
210 * @param int $day
211 * @param int $month
212 * @param int $year
da304137
MN
213 * @param int $time the unixtime, used for multiple calendar support. The values $day,
214 * $month and $year are kept for backwards compatibility.
36dc3b71
SH
215 * @return string
216 */
da304137
MN
217 protected function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) {
218 // If a day, month and year were passed then convert it to a timestamp. If these were passed
219 // then we can assume the day, month and year are passed as Gregorian, as no where in core
220 // should we be passing these values rather than the time. This is done for BC.
221 if (!empty($day) && !empty($month) && !empty($year)) {
222 if (checkdate($month, $day, $year)) {
223 $time = make_timestamp($year, $month, $day);
224 } else {
225 $time = time();
226 }
227 } else if (empty($time)) {
228 $time = time();
229 }
230
36dc3b71 231 $output = html_writer::start_tag('div', array('class'=>'buttons'));
da304137 232 $output .= html_writer::start_tag('form', array('action' => CALENDAR_URL . 'event.php', 'method' => 'get'));
36dc3b71 233 $output .= html_writer::start_tag('div');
da304137
MN
234 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'action', 'value' => 'new'));
235 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'course', 'value' => $courseid));
236 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'time', 'value' => $time));
237 $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value' => get_string('newevent', 'calendar')));
36dc3b71
SH
238 $output .= html_writer::end_tag('div');
239 $output .= html_writer::end_tag('form');
240 $output .= html_writer::end_tag('div');
241 return $output;
242 }
243
244 /**
245 * Displays the calendar for a single day
246 *
247 * @param calendar_information $calendar
248 * @return string
249 */
797cedc7
SH
250 public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
251
252 if ($returnurl === null) {
253 $returnurl = $this->page->url;
254 }
255
36dc3b71
SH
256 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
257
258 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 259 $output .= $this->course_filter_selector($returnurl, get_string('dayviewfor', 'calendar'));
797cedc7 260 if (calendar_user_can_add_event($calendar->course)) {
da304137 261 $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
36dc3b71 262 }
36dc3b71
SH
263 $output .= html_writer::end_tag('div');
264 // Controls
da304137 265 $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class'=>'controls'));
36dc3b71
SH
266
267 if (empty($events)) {
268 // There is nothing to display today.
269 $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3);
270 } else {
271 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
272 $underway = array();
273 // First, print details about events that start today
274 foreach ($events as $event) {
275 $event = new calendar_event($event);
276 $event->calendarcourseid = $calendar->courseid;
277 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
0f927f1e 278 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
279 $output .= $this->event($event);
280 } else { // Save this for later
281 $underway[] = $event;
282 }
283 }
284
285 // Then, show a list of all events that just span this day
286 if (!empty($underway)) {
287 $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3);
288 foreach ($underway as $event) {
0f927f1e 289 $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
36dc3b71
SH
290 $output .= $this->event($event);
291 }
292 }
293
294 $output .= html_writer::end_tag('div');
295 }
296
297 return $output;
298 }
299
300 /**
301 * Displays an event
302 *
303 * @param calendar_event $event
304 * @param bool $showactions
305 * @return string
306 */
307 public function event(calendar_event $event, $showactions=true) {
e73b527c
AA
308 global $CFG;
309
36dc3b71 310 $event = calendar_add_event_metadata($event);
9984132c 311 $context = $event->context;
36dc3b71 312
0f927f1e 313 $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
36dc3b71
SH
314
315 $table = new html_table();
316 $table->attributes = array('class'=>'event', 'cellspacing'=>'0');
317 $table->data = array(
318 0 => new html_table_row(),
319 1 => new html_table_row(),
320 );
321
322 if (!empty($event->icon)) {
0f927f1e 323 $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon);
36dc3b71 324 } else {
0f927f1e 325 $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true)));
36dc3b71
SH
326 }
327 $table->data[0]->cells[0]->attributes['class'] .= ' picture';
328
329 $table->data[0]->cells[1] = new html_table_cell();
330 $table->data[0]->cells[1]->attributes['class'] .= ' topic';
331 if (!empty($event->referer)) {
332 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer'));
333 } else {
9984132c 334 $table->data[0]->cells[1]->text .= html_writer::tag('div', format_string($event->name, false, array('context' => $context)), array('class'=>'name'));
36dc3b71
SH
335 }
336 if (!empty($event->courselink)) {
337 $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
338 }
e73b527c
AA
339 // Show subscription source if needed.
340 if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
341 if (!empty($event->subscription->url)) {
342 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
343 } else {
344 // File based ical.
345 $source = get_string('subsource', 'calendar', $event->subscription);
346 }
347 $table->data[0]->cells[1]->text .= html_writer::tag('div', $source, array('class' => 'subscription'));
8a7326ed 348 }
36dc3b71
SH
349 if (!empty($event->time)) {
350 $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
351 } else {
352 $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date'));
353 }
354
355 $table->data[1]->cells[0] = new html_table_cell('&nbsp;');
356 $table->data[1]->cells[0]->attributes['class'] .= 'side';
357
9984132c 358 $table->data[1]->cells[1] = new html_table_cell(format_text($event->description, $event->format, array('context' => $context)));
36dc3b71
SH
359 $table->data[1]->cells[1]->attributes['class'] .= ' description';
360 if (isset($event->cssclass)) {
361 $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass;
362 }
363
364 if (calendar_edit_event_allowed($event) && $showactions) {
365 if (empty($event->cmid)) {
366 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
367 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
368 if (!empty($event->calendarcourseid)) {
369 $editlink->param('course', $event->calendarcourseid);
370 $deletelink->param('course', $event->calendarcourseid);
371 }
372 } else {
373 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
374 $deletelink = null;
375 }
376
377 $commands = html_writer::start_tag('div', array('class'=>'commands'));
378 $commands .= html_writer::start_tag('a', array('href'=>$editlink));
379 $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')));
380 $commands .= html_writer::end_tag('a');
381 if ($deletelink != null) {
382 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
383 $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')));
384 $commands .= html_writer::end_tag('a');
385 }
386 $commands .= html_writer::end_tag('div');
387 $table->data[1]->cells[1]->text .= $commands;
388 }
389 return html_writer::table($table);
390 }
391
392 /**
393 * Displays a month in detail
394 *
36dc3b71 395 * @param calendar_information $calendar
da304137 396 * @param moodle_url $returnurl the url to return to
36dc3b71
SH
397 * @return string
398 */
797cedc7
SH
399 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) {
400 global $CFG;
401
402 if (empty($returnurl)) {
403 $returnurl = $this->page->url;
404 }
6be8c6b7 405
da304137
MN
406 // Get the calendar type we are using.
407 $calendartype = \core_calendar\type_factory::get_calendar_instance();
36dc3b71 408
da304137 409 // Store the display settings.
36dc3b71 410 $display = new stdClass;
da304137
MN
411 $display->thismonth = false;
412
413 // Get the specified date in the calendar type being used.
414 $date = $calendartype->timestamp_to_date_array($calendar->time);
415 $thisdate = $calendartype->timestamp_to_date_array(time());
416 if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
417 $display->thismonth = true;
418 $date = $thisdate;
419 $calendar->time = time();
36dc3b71
SH
420 }
421
1032966c 422 // Get Gregorian date for the start of the month.
da304137 423 $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
1032966c 424 // Store the gregorian date values to be used later.
da304137
MN
425 list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
426 $gregoriandate['hour'], $gregoriandate['minute']);
427
428 // Get the starting week day for this month.
429 $startwday = dayofweek(1, $date['mon'], $date['year']);
430 // Get the days in a week.
431 $daynames = calendar_get_days();
432 // Store the number of days in a week.
433 $numberofdaysinweek = $calendartype->get_num_weekdays();
434
435 $display->minwday = calendar_get_starting_weekday();
436 $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
437 $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
438
439 // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
440 $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
1032966c 441 $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
da304137 442
36dc3b71 443 // Align the starting weekday to fall in our display range
da304137 444 // This is simple, not foolproof.
36dc3b71 445 if ($startwday < $display->minwday) {
da304137 446 $startwday += $numberofdaysinweek;
36dc3b71
SH
447 }
448
449 // Get events from database
da304137 450 $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses);
36dc3b71
SH
451 if (!empty($events)) {
452 foreach($events as $eventid => $event) {
453 $event = new calendar_event($event);
454 if (!empty($event->modulename)) {
455 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
8270f0d0 456 if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
36dc3b71
SH
457 unset($events[$eventid]);
458 }
459 }
460 }
461 }
462
463 // Extract information: events vs. time
da304137 464 calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
36dc3b71
SH
465
466 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 467 $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar'));
797cedc7 468 if (calendar_user_can_add_event($calendar->course)) {
da304137 469 $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
36dc3b71 470 }
36dc3b71
SH
471 $output .= html_writer::end_tag('div', array('class'=>'header'));
472 // Controls
da304137 473 $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls'));
797cedc7 474
36dc3b71
SH
475 $table = new html_table();
476 $table->attributes = array('class'=>'calendarmonth calendartable');
da304137 477 $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
36dc3b71
SH
478 $table->data = array();
479
da304137
MN
480 // Get the day names as the header.
481 $header = array();
36dc3b71 482 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
da304137 483 $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
36dc3b71 484 }
da304137 485 $table->head = $header;
36dc3b71
SH
486
487 // For the table display. $week is the row; $dayweek is the column.
488 $week = 1;
489 $dayweek = $startwday;
490
2953ad13
JI
491 $row = new html_table_row(array());
492
36dc3b71
SH
493 // Paddding (the first week may have blank days in the beginning)
494 for($i = $display->minwday; $i < $startwday; ++$i) {
495 $cell = new html_table_cell('&nbsp;');
9e142d11 496 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
497 $row->cells[] = $cell;
498 }
499
500 // Now display all the calendar
797cedc7
SH
501 $weekend = CALENDAR_DEFAULT_WEEKEND;
502 if (isset($CFG->calendar_weekend)) {
503 $weekend = intval($CFG->calendar_weekend);
504 }
505
da304137
MN
506 $daytime = $display->tstart - DAYSECS;
507 for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
508 $daytime = $daytime + DAYSECS;
36dc3b71
SH
509 if($dayweek > $display->maxwday) {
510 // We need to change week (table row)
511 $table->data[] = $row;
512 $row = new html_table_row(array());
513 $dayweek = $display->minwday;
514 ++$week;
515 }
516
517 // Reset vars
518 $cell = new html_table_cell();
da304137 519 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
36dc3b71
SH
520
521 $cellclasses = array();
522
da304137 523 if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
36dc3b71
SH
524 // Weekend. This is true no matter what the exact range is.
525 $cellclasses[] = 'weekend';
526 }
527
528 // Special visual fx if an event is defined
da304137
MN
529 if (isset($eventsbyday[$day])) {
530 if(count($eventsbyday[$day]) == 1) {
36dc3b71
SH
531 $title = get_string('oneevent', 'calendar');
532 } else {
da304137 533 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
36dc3b71 534 }
da304137 535 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title'=>$title)), array('class'=>'day'));
36dc3b71 536 } else {
da304137 537 $cell->text = html_writer::tag('div', $day, array('class'=>'day'));
36dc3b71
SH
538 }
539
540 // Special visual fx if an event spans many days
541 $durationclass = false;
da304137 542 if (isset($typesbyday[$day]['durationglobal'])) {
36dc3b71 543 $durationclass = 'duration_global';
da304137 544 } else if (isset($typesbyday[$day]['durationcourse'])) {
36dc3b71 545 $durationclass = 'duration_course';
da304137 546 } else if (isset($typesbyday[$day]['durationgroup'])) {
36dc3b71 547 $durationclass = 'duration_group';
da304137 548 } else if (isset($typesbyday[$day]['durationuser'])) {
36dc3b71
SH
549 $durationclass = 'duration_user';
550 }
551 if ($durationclass) {
552 $cellclasses[] = 'duration';
553 $cellclasses[] = $durationclass;
554 }
555
556 // Special visual fx for today
da304137 557 if ($display->thismonth && $day == $date['mday']) {
9e142d11 558 $cellclasses[] = 'day today';
36dc3b71 559 } else {
9e142d11 560 $cellclasses[] = 'day nottoday';
36dc3b71
SH
561 }
562 $cell->attributes = array('class'=>join(' ',$cellclasses));
563
da304137 564 if (isset($eventsbyday[$day])) {
36dc3b71 565 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
da304137 566 foreach($eventsbyday[$day] as $eventindex) {
36dc3b71
SH
567 // If event has a class set then add it to the event <li> tag
568 $attributes = array();
569 if (!empty($events[$eventindex]->class)) {
570 $attributes['class'] = $events[$eventindex]->class;
571 }
0f927f1e
SH
572 $dayhref->set_anchor('event_'.$events[$eventindex]->id);
573 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
36dc3b71
SH
574 $cell->text .= html_writer::tag('li', $link, $attributes);
575 }
576 $cell->text .= html_writer::end_tag('ul');
577 }
da304137 578 if (isset($durationbyday[$day])) {
36dc3b71 579 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
da304137 580 foreach($durationbyday[$day] as $eventindex) {
36dc3b71
SH
581 $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
582 }
583 $cell->text .= html_writer::end_tag('ul');
584 }
585 $row->cells[] = $cell;
586 }
587
588 // Paddding (the last week may have blank days at the end)
589 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
590 $cell = new html_table_cell('&nbsp;');
9e142d11 591 $cell->attributes = array('class'=>'nottoday dayblank');
36dc3b71
SH
592 $row->cells[] = $cell;
593 }
594 $table->data[] = $row;
595 $output .= html_writer::table($table);
596
36dc3b71
SH
597 return $output;
598 }
599
36dc3b71
SH
600 /**
601 * Displays upcoming events
602 *
603 * @param calendar_information $calendar
604 * @param int $futuredays
605 * @param int $maxevents
606 * @return string
607 */
797cedc7
SH
608 public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
609
610 if ($returnurl === null) {
611 $returnurl = $this->page->url;
612 }
613
36dc3b71
SH
614 $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
615
616 $output = html_writer::start_tag('div', array('class'=>'header'));
712e5f22 617 $output .= $this->course_filter_selector($returnurl, get_string('upcomingeventsfor', 'calendar'));
797cedc7
SH
618 if (calendar_user_can_add_event($calendar->course)) {
619 $output .= $this->add_event_button($calendar->course->id);
36dc3b71 620 }
36dc3b71
SH
621 $output .= html_writer::end_tag('div');
622
623 if ($events) {
624 $output .= html_writer::start_tag('div', array('class'=>'eventlist'));
625 foreach ($events as $event) {
626 // Convert to calendar_event object so that we transform description
627 // accordingly
628 $event = new calendar_event($event);
484617d2 629 $event->calendarcourseid = $calendar->courseid;
36dc3b71
SH
630 $output .= $this->event($event);
631 }
632 $output .= html_writer::end_tag('div');
633 } else {
634 $output .= $this->output->heading(get_string('noupcomingevents', 'calendar'));
635 }
636
637 return $output;
638 }
639
640 /**
641 * Displays a course filter selector
642 *
ba1ce6e9
SH
643 * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
644 * @param string $label The label to use for the course select.
36dc3b71
SH
645 * @return string
646 */
797cedc7 647 protected function course_filter_selector(moodle_url $returnurl, $label=null) {
6be8c6b7 648 global $USER, $SESSION, $CFG;
36dc3b71
SH
649
650 if (!isloggedin() or isguestuser()) {
651 return '';
652 }
653
6ca657a7 654 if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
36dc3b71
SH
655 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
656 } else {
657 $courses = enrol_get_my_courses();
658 }
659
660 unset($courses[SITEID]);
661
662 $courseoptions = array();
663 $courseoptions[SITEID] = get_string('fulllistofcourses');
664 foreach ($courses as $course) {
6ca657a7 665 $coursecontext = context_course::instance($course->id);
8ebbb06a 666 $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
36dc3b71
SH
667 }
668
797cedc7
SH
669 if ($this->page->course->id !== SITEID) {
670 $selected = $this->page->course->id;
36dc3b71
SH
671 } else {
672 $selected = '';
673 }
ba1ce6e9
SH
674 $courseurl = new moodle_url($returnurl);
675 $courseurl->remove_params('course');
676 $select = new single_select($courseurl, 'course', $courseoptions, $selected, null);
36dc3b71 677 $select->class = 'cal_courses_flt';
91a774e2 678 if ($label !== null) {
5c576552
RW
679 $select->set_label($label);
680 } else {
681 $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
91a774e2
SH
682 }
683 return $this->output->render($select);
36dc3b71 684 }
e30390a0
SH
685
686 /**
687 * Renders a table containing information about calendar subscriptions.
688 *
689 * @param int $courseid
690 * @param array $subscriptions
691 * @param string $importresults
692 * @return string
693 */
694 public function subscription_details($courseid, $subscriptions, $importresults = '') {
695 $table = new html_table();
696 $table->head = array(
697 get_string('colcalendar', 'calendar'),
698 get_string('collastupdated', 'calendar'),
8c84f374 699 get_string('eventkind', 'calendar'),
e30390a0
SH
700 get_string('colpoll', 'calendar'),
701 get_string('colactions', 'calendar')
702 );
703 $table->align = array('left', 'left', 'left', 'center');
704 $table->width = '100%';
705 $table->data = array();
706
707 if (empty($subscriptions)) {
708 $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
709 $cell->colspan = 4;
710 $table->data[] = new html_table_row(array($cell));
711 }
712 $strnever = new lang_string('never', 'calendar');
713 foreach ($subscriptions as $sub) {
714 $label = $sub->name;
715 if (!empty($sub->url)) {
716 $label = html_writer::link($sub->url, $label);
717 }
718 if (empty($sub->lastupdated)) {
719 $lastupdated = $strnever->out();
720 } else {
721 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
722 }
723
724 $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
725 $cell->colspan = 2;
8c84f374 726 $type = $sub->eventtype . 'events';
e30390a0
SH
727
728 $table->data[] = new html_table_row(array(
729 new html_table_cell($label),
730 new html_table_cell($lastupdated),
8c84f374 731 new html_table_cell(get_string($type, 'calendar')),
e30390a0
SH
732 $cell
733 ));
734 }
735
736 $out = $this->output->box_start('generalbox calendarsubs');
737
738 $out .= $importresults;
739 $out .= html_writer::table($table);
740 $out .= $this->output->box_end();
741 return $out;
742 }
743
744 /**
745 * Creates a form to perform actions on a given subscription.
746 *
747 * @param stdClass $subscription
748 * @param int $courseid
749 * @return string
750 */
751 protected function subscription_action_form($subscription, $courseid) {
752 // Assemble form for the subscription row.
753 $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
754 if (empty($subscription->url)) {
755 // Don't update an iCal file, which has no URL.
756 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
757 } else {
758 // Assemble pollinterval control.
759 $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
760 $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
761 foreach (calendar_get_pollinterval_choices() as $k => $v) {
762 $attributes = array();
763 if ($k == $subscription->pollinterval) {
764 $attributes['selected'] = 'selected';
765 }
4e3ce346 766 $attributes['value'] = $k;
e30390a0
SH
767 $html .= html_writer::tag('option', $v, $attributes);
768 }
769 $html .= html_writer::end_tag('select');
770 $html .= html_writer::end_tag('div');
771 }
772 $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
773 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
774 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
775 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
776 if (!empty($subscription->url)) {
ee74a2a1
AA
777 $html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
778 'value' => CALENDAR_SUBSCRIPTION_UPDATE));
e30390a0 779 }
ee74a2a1
AA
780 $html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
781 'value' => CALENDAR_SUBSCRIPTION_REMOVE));
e30390a0
SH
782 $html .= html_writer::end_tag('div');
783 $html .= html_writer::end_tag('form');
784 return $html;
785 }
2ad8f0f6 786}