MDL-11328 Added the missing variable
[moodle-pu.git] / calendar / view.php
blobd45f055e20fb159a269730b7fec785aad39086f2
1 <?php // $Id$
3 /////////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Calendar extension //
8 // //
9 // Copyright (C) 2003-2004 Greek School Network www.sch.gr //
10 // //
11 // Designed by: //
12 // Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13 // Jon Papaioannou (pj@moodle.org) //
14 // //
15 // Programming and development: //
16 // Jon Papaioannou (pj@moodle.org) //
17 // //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@moodle.org) //
20 // //
21 // The current module was developed at the University of Macedonia //
22 // (www.uom.gr) under the funding of the Greek School Network (www.sch.gr) //
23 // The aim of this project is to provide additional and improved //
24 // functionality to the Asynchronous Distance Education service that the //
25 // Greek School Network deploys. //
26 // //
27 // This program is free software; you can redistribute it and/or modify //
28 // it under the terms of the GNU General Public License as published by //
29 // the Free Software Foundation; either version 2 of the License, or //
30 // (at your option) any later version. //
31 // //
32 // This program is distributed in the hope that it will be useful, //
33 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
34 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
35 // GNU General Public License for more details: //
36 // //
37 // http://www.gnu.org/copyleft/gpl.html //
38 // //
39 /////////////////////////////////////////////////////////////////////////////
41 // Display the calendar page.
43 require_once('../config.php');
44 require_once($CFG->dirroot.'/course/lib.php');
45 require_once($CFG->dirroot.'/calendar/lib.php');
47 $courseid = optional_param('course', 0, PARAM_INT);
48 $view = optional_param('view', 'upcoming', PARAM_ALPHA);
49 $day = optional_param('cal_d', 0, PARAM_INT);
50 $mon = optional_param('cal_m', 0, PARAM_INT);
51 $yr = optional_param('cal_y', 0, PARAM_INT);
53 if(!$site = get_site()) {
54 redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
57 if ($courseid) {
58 require_login($courseid);
59 } else if ($CFG->forcelogin) {
60 require_login();
63 // Initialize the session variables
64 calendar_session_vars();
66 //add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
67 $now = usergetdate(time());
68 $pagetitle = '';
70 $strcalendar = get_string('calendar', 'calendar');
71 $navlinks = array();
72 $navlinks[] = array('name' => $strcalendar,
73 'link' =>calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&amp;course='.$courseid.'&amp;',
74 $now['mday'], $now['mon'], $now['year']),
75 'type' => 'misc');
78 if(!checkdate($mon, $day, $yr)) {
79 $day = intval($now['mday']);
80 $mon = intval($now['mon']);
81 $yr = intval($now['year']);
83 $time = make_timestamp($yr, $mon, $day);
85 switch($view) {
86 case 'day':
87 $navlinks[] = array('name' => userdate($time, get_string('strftimedate')), 'link' => null, 'type' => 'misc');
88 $pagetitle = get_string('dayview', 'calendar');
89 break;
90 case 'month':
91 $navlinks[] = array('name' => userdate($time, get_string('strftimemonthyear')), 'link' => null, 'type' => 'misc');
92 $pagetitle = get_string('detailedmonthview', 'calendar');
93 break;
94 case 'upcoming':
95 $pagetitle = get_string('upcomingevents', 'calendar');
96 break;
99 // If a course has been supplied in the URL, change the filters to show that one
100 if (!empty($courseid)) {
101 if ($course = get_record('course', 'id', $courseid)) {
102 if ($course->id == SITEID) {
103 // If coming from the home page, show all courses
104 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
105 calendar_set_referring_course(0);
107 } else {
108 // Otherwise show just this one
109 $SESSION->cal_courses_shown = $course->id;
110 calendar_set_referring_course($SESSION->cal_courses_shown);
113 } else {
114 $course = null;
117 if (empty($USER->id) or isguest()) {
118 $defaultcourses = calendar_get_default_courses();
119 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
121 } else {
122 calendar_set_filters($courses, $groups, $users);
125 // Let's see if we are supposed to provide a referring course link
126 // but NOT for the "main page" course
127 if ($SESSION->cal_course_referer != SITEID &&
128 ($shortname = get_field('course', 'shortname', 'id', $SESSION->cal_course_referer)) !== false) {
129 require_login();
130 if (empty($course)) {
131 $course = get_record('course', 'id', $SESSION->cal_course_referer); // Useful to have around
135 $strcalendar = get_string('calendar', 'calendar');
136 $prefsbutton = calendar_preferences_button();
138 // Print title and header
139 $navigation = build_navigation($navlinks);
140 print_header("$site->shortname: $strcalendar: $pagetitle", $strcalendar, $navigation,
141 '', '', true, $prefsbutton, user_login_string($site));
143 echo calendar_overlib_html();
145 // Layout the whole page as three big columns.
146 echo '<table id="calendar" style="height:100%;">';
147 echo '<tr>';
149 // START: Main column
151 echo '<td class="maincalendar">';
152 echo '<div class="heightcontainer">';
154 switch($view) {
155 case 'day':
156 calendar_show_day($day, $mon, $yr, $courses, $groups, $users, $courseid);
157 break;
158 case 'month':
159 calendar_show_month_detailed($mon, $yr, $courses, $groups, $users, $courseid);
160 break;
161 case 'upcoming':
162 calendar_show_upcoming_events($courses, $groups, $users, get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS), get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS), $courseid);
163 break;
166 //Link to calendar export page
167 echo '<div class="bottom">';
168 print_single_button('export.php', false, get_string('exportcalendar', 'calendar'));
170 if (!empty($USER->id)) {
171 $authtoken = sha1($USER->username . $USER->password);
172 $usernameencoded = urlencode($USER->username);
174 echo "<a href=\"export_execute.php?preset_what=all&amp;preset_time=recentupcoming&amp;username=$usernameencoded&amp;authtoken=$authtoken\">"
175 .'<img src="'.$CFG->pixpath.'/i/ical.gif" height="14" width="36" '
176 .'alt="'.get_string('ical', 'calendar').'" '
177 .'title="'.get_string('quickdownloadcalendar', 'calendar').'" />'
178 .'</a>';
181 echo '</div>';
182 echo '</div>';
183 echo '</td>';
185 // END: Main column
187 // START: Last column (3-month display)
188 echo '<td class="sidecalendar">';
189 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
190 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
191 $getvars = 'id='.$courseid.'&amp;cal_d='.$day.'&amp;cal_m='.$mon.'&amp;cal_y='.$yr; // For filtering
193 echo '<div class="sideblock">';
194 echo '<div class="header">'.get_string('eventskey', 'calendar').'</div>';
195 echo '<div class="filters">';
196 echo calendar_filter_controls($view, $getvars, NULL, $courses);
197 echo '</div>';
198 echo '</div>';
200 echo '<div class="sideblock">';
201 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
203 echo '<div class="minicalendarblock minicalendartop">';
204 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $prevmon, 'y' => $prevyr));
205 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
206 echo '</div><div class="minicalendarblock">';
207 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $mon, 'y' => $yr));
208 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
209 echo '</div><div class="minicalendarblock">';
210 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $nextmon, 'y' => $nextyr));
211 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
212 echo '</div>';
213 echo '</div>';
215 echo '</td>';
217 echo '</tr></table>';
219 print_footer();
223 function calendar_show_day($d, $m, $y, $courses, $groups, $users, $courseid) {
224 global $CFG, $USER;
226 if (!checkdate($m, $d, $y)) {
227 $now = usergetdate(time());
228 list($d, $m, $y) = array(intval($now['mday']), intval($now['mon']), intval($now['year']));
231 $getvars = 'from=day&amp;cal_d='.$d.'&amp;cal_m='.$m.'&amp;cal_y='.$y; // For filtering
233 $starttime = make_timestamp($y, $m, $d);
234 $endtime = make_timestamp($y, $m, $d + 1) - 1;
236 $events = calendar_get_upcoming($courses, $groups, $users, 1, 100, $starttime);
238 $text = '';
239 if (!isguest() && !empty($USER->id) && calendar_user_can_add_event()) {
240 $text.= '<div class="buttons">';
241 $text.= '<form action="'.CALENDAR_URL.'event.php" method="get">';
242 $text.= '<div>';
243 $text.= '<input type="hidden" name="action" value="new" />';
244 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
245 $text.= '<input type="hidden" name="cal_d" value="'.$d.'" />';
246 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
247 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
248 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
249 $text.= '</div></form></div>';
252 $text .= get_string('dayview', 'calendar').': '.calendar_course_filter_selector($getvars);
254 echo '<div class="header">'.$text.'</div>';
256 echo '<div class="controls">'.calendar_top_controls('day', array('id' => $courseid, 'd' => $d, 'm' => $m, 'y' => $y)).'</div>';
258 if (empty($events)) {
259 // There is nothing to display today.
260 echo '<h3>'.get_string('daywithnoevents', 'calendar').'</h3>';
262 } else {
264 echo '<div class="eventlist">';
266 $underway = array();
268 // First, print details about events that start today
269 foreach ($events as $event) {
271 // Set event course class if a course event
272 if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
273 $event->class = 'event_course'.array_search($event->courseid, $courses) % CALENDAR_MAXCOURSES;
275 $event->calendarcourseid = $courseid;
277 if ($event->timestart >= $starttime && $event->timestart <= $endtime) { // Print it now
281 $dayend = calendar_day_representation($event->timestart + $event->timeduration);
282 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
283 $enddate = usergetdate($event->timestart + $event->timeduration);
284 // Set printable representation
285 echo calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).' ('.$timeend.')';
287 //unset($event->time);
289 $event->time = calendar_format_event_time($event, time(), '', false, $starttime);
290 calendar_print_event($event);
292 } else { // Save this for later
293 $underway[] = $event;
297 // Then, show a list of all events that just span this day
298 if (!empty($underway)) {
299 echo '<h3>'.get_string('spanningevents', 'calendar').':</h3>';
300 foreach ($underway as $event) {
301 $event->time = calendar_format_event_time($event, time(), '', false, $starttime);
302 calendar_print_event($event);
306 echo '</div>';
311 function calendar_show_month_detailed($m, $y, $courses, $groups, $users, $courseid) {
312 global $CFG, $SESSION, $USER, $CALENDARDAYS;
313 global $day, $mon, $yr;
315 $getvars = 'from=month&amp;cal_d='.$day.'&amp;cal_m='.$mon.'&amp;cal_y='.$yr; // For filtering
317 $display = &New stdClass;
318 $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
319 $display->maxwday = $display->minwday + 6;
321 if(!empty($m) && !empty($y)) {
322 $thisdate = usergetdate(time()); // Time and day at the user's location
323 if($m == $thisdate['mon'] && $y == $thisdate['year']) {
324 // Navigated to this month
325 $date = $thisdate;
326 $display->thismonth = true;
328 else {
329 // Navigated to other month, let's do a nice trick and save us a lot of work...
330 if(!checkdate($m, 1, $y)) {
331 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
332 $display->thismonth = true;
334 else {
335 $date = array('mday' => 1, 'mon' => $m, 'year' => $y);
336 $display->thismonth = false;
340 else {
341 $date = usergetdate(time());
342 $display->thismonth = true;
345 // Fill in the variables we 're going to use, nice and tidy
346 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
347 $display->maxdays = calendar_days_in_month($m, $y);
349 $startwday = 0;
350 if (get_user_timezone_offset() < 99) {
351 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
352 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
353 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
354 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
355 } else {
356 // no timezone info specified
357 $display->tstart = mktime(0, 0, 0, $m, 1, $y);
358 $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
359 $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date()
362 // Align the starting weekday to fall in our display range
363 if($startwday < $display->minwday) {
364 $startwday += 7;
367 // Get events from database
368 $whereclause = calendar_sql_where(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses);
369 if($whereclause === false) {
370 $events = array();
372 else {
373 $events = get_records_select('event', $whereclause, 'timestart');
375 if (!empty($events)) {
376 foreach($events as $eventid => $event) {
377 if (!empty($event->modulename)) {
378 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
379 if (!groups_course_module_visible($cm)) {
380 unset($events[$eventid]);
386 // Extract information: events vs. time
387 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
389 $text = '';
390 if(!isguest() && !empty($USER->id) && calendar_user_can_add_event()) {
391 $text.= '<div class="buttons"><form action="'.CALENDAR_URL.'event.php" method="get">';
392 $text.= '<div>';
393 $text.= '<input type="hidden" name="action" value="new" />';
394 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
395 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
396 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
397 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
398 $text.= '</div></form></div>';
401 $text .= get_string('detailedmonthview', 'calendar').': '.calendar_course_filter_selector($getvars);
403 echo '<div class="header">'.$text.'</div>';
405 echo '<div class="controls">';
406 echo calendar_top_controls('month', array('id' => $courseid, 'm' => $m, 'y' => $y));
407 echo '</div>';
409 // Start calendar display
410 echo '<table class="calendarmonth"><tr class="weekdays">'; // Begin table. First row: day names
412 // Print out the names of the weekdays
413 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
414 // This uses the % operator to get the correct weekday no matter what shift we have
415 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
416 echo '<th scope="col">'.get_string($CALENDARDAYS[$i % 7], 'calendar').'</th>';
419 echo '</tr><tr>'; // End of day names; prepare for day numbers
421 // For the table display. $week is the row; $dayweek is the column.
422 $week = 1;
423 $dayweek = $startwday;
425 // Paddding (the first week may have blank days in the beginning)
426 for($i = $display->minwday; $i < $startwday; ++$i) {
427 echo '<td class="nottoday">&nbsp;</td>'."\n";
430 // Now display all the calendar
431 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
432 if($dayweek > $display->maxwday) {
433 // We need to change week (table row)
434 echo "</tr>\n<tr>";
435 $dayweek = $display->minwday;
436 ++$week;
439 // Reset vars
440 $cell = '';
441 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day&amp;course='.$courseid.'&amp;', $day, $m, $y);
443 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
444 // Weekend. This is true no matter what the exact range is.
445 $class = 'weekend';
447 else {
448 // Normal working day.
449 $class = '';
452 // Special visual fx if an event is defined
453 if(isset($eventsbyday[$day])) {
454 if(count($eventsbyday[$day]) == 1) {
455 $title = get_string('oneevent', 'calendar');
457 else {
458 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
460 $cell = '<div class="day"><a href="'.$dayhref.'" title="'.$title.'">'.$day.'</a></div>';
462 else {
463 $cell = '<div class="day">'.$day.'</div>';
466 // Special visual fx if an event spans many days
467 if(isset($typesbyday[$day]['durationglobal'])) {
468 $class .= ' duration_global';
470 else if(isset($typesbyday[$day]['durationcourse'])) {
471 $class .= ' duration_course';
473 else if(isset($typesbyday[$day]['durationgroup'])) {
474 $class .= ' duration_group';
476 else if(isset($typesbyday[$day]['durationuser'])) {
477 $class .= ' duration_user';
480 // Special visual fx for today
481 if($display->thismonth && $day == $d) {
482 $class .= ' today';
483 } else {
484 $class .= ' nottoday';
487 // Just display it
488 if(!empty($class)) {
489 $class = ' class="'.trim($class).'"';
491 echo '<td'.$class.'>'.$cell;
493 if(isset($eventsbyday[$day])) {
494 echo '<ul class="events-new">';
495 foreach($eventsbyday[$day] as $eventindex) {
497 // If event has a class set then add it to the event <li> tag
498 $eventclass = '';
499 if (!empty($events[$eventindex]->class)) {
500 $eventclass = ' class="'.$events[$eventindex]->class.'"';
503 echo '<li'.$eventclass.'><a href="'.$dayhref.'#event_'.$events[$eventindex]->id.'">'.format_string($events[$eventindex]->name, true).'</a></li>';
505 echo '</ul>';
507 if(isset($durationbyday[$day])) {
508 echo '<ul class="events-underway">';
509 foreach($durationbyday[$day] as $eventindex) {
510 echo '<li>['.format_string($events[$eventindex]->name,true).']</li>';
512 echo '</ul>';
514 echo "</td>\n";
517 // Paddding (the last week may have blank days at the end)
518 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
519 echo '<td class="nottoday">&nbsp;</td>';
521 echo "</tr>\n"; // Last row ends
523 echo "</table>\n"; // Tabular display of days ends
527 function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $maxevents, $courseid) {
528 global $USER;
530 $events = calendar_get_upcoming($courses, $groups, $users, $futuredays, $maxevents);
532 $text = '';
534 if(!isguest() && !empty($USER->id) && calendar_user_can_add_event()) {
535 $text.= '<div class="buttons">';
536 $text.= '<form action="'.CALENDAR_URL.'event.php" method="get">';
537 $text.= '<div>';
538 $text.= '<input type="hidden" name="action" value="new" />';
539 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
541 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
542 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
544 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
545 $text.= '</div></form></div>';
548 $text .= get_string('upcomingevents', 'calendar').': '.calendar_course_filter_selector('from=upcoming');
550 echo '<div class="header">'.$text.'</div>';
552 if ($events) {
554 echo '<div class="eventlist">';
555 foreach ($events as $event) {
557 // Set event course class if a course event
558 if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
559 $event->class = 'event_course'.array_search($event->courseid, $courses) % CALENDAR_MAXCOURSES;
562 calendar_print_event($event);
564 echo '</div>';
565 } else {
566 print_heading(get_string('noupcomingevents', 'calendar'));
570 function calendar_course_filter_selector($getvars = '') {
571 global $USER, $SESSION;
573 if (empty($USER->id) or isguest()) {
574 return '';
577 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID)) && !empty($CFG->calendar_adminseesall)) {
578 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
579 } else {
580 $courses = get_my_courses($USER->id, 'shortname');
583 unset($courses[SITEID]);
585 $courseoptions[SITEID] = get_string('fulllistofcourses');
586 foreach ($courses as $course) {
587 $courseoptions[$course->id] = format_string($course->shortname);
590 if (is_numeric($SESSION->cal_courses_shown)) {
591 $selected = $SESSION->cal_courses_shown;
592 } else {
593 $selected = '';
596 return popup_form(CALENDAR_URL.'set.php?var=setcourse&amp;'.$getvars.'&amp;id=',
597 $courseoptions, 'cal_course_flt', $selected, '', '', '', true);