Automatic installer.php lang files by installer_builder (20080417)
[moodle.git] / calendar / view.php
blobafd3e73990f60df9b1d6c29545be696d3095a490
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 //add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
64 $now = usergetdate(time());
65 $pagetitle = '';
67 $nav = calendar_get_link_tag(get_string('calendar', 'calendar'), CALENDAR_URL.'view.php?view=upcoming&amp;', $now['mday'], $now['mon'], $now['year']);
70 if(!checkdate($mon, $day, $yr)) {
71 $day = intval($now['mday']);
72 $mon = intval($now['mon']);
73 $yr = intval($now['year']);
75 $time = make_timestamp($yr, $mon, $day);
77 switch($view) {
78 case 'day':
79 $nav .= ' -> '.userdate($time, get_string('strftimedate'));
80 $pagetitle = get_string('dayview', 'calendar');
81 break;
82 case 'month':
83 $nav .= ' -> '.userdate($time, get_string('strftimemonthyear'));
84 $pagetitle = get_string('detailedmonthview', 'calendar');
85 break;
86 case 'upcoming':
87 $pagetitle = get_string('upcomingevents', 'calendar');
88 break;
91 // If a course has been supplied in the URL, change the filters to show that one
92 if (!empty($courseid)) {
93 if ($course = get_record('course', 'id', $courseid)) {
94 if ($course->id == SITEID) {
95 // If coming from the home page, show all courses
96 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
97 calendar_set_referring_course(0);
99 } else {
100 // Otherwise show just this one
101 $SESSION->cal_courses_shown = $course->id;
102 calendar_set_referring_course($SESSION->cal_courses_shown);
105 } else {
106 $course = null;
109 if (empty($USER->id) or isguest()) {
110 $defaultcourses = calendar_get_default_courses();
111 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
113 } else {
114 calendar_set_filters($courses, $groups, $users);
117 // Let's see if we are supposed to provide a referring course link
118 // but NOT for the "main page" course
119 if ($SESSION->cal_course_referer != SITEID &&
120 ($shortname = get_field('course', 'shortname', 'id', $SESSION->cal_course_referer)) !== false) {
121 // If we know about the referring course, show a return link and ALSO require login!
122 require_login();
123 $nav = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$SESSION->cal_course_referer.'">'.$shortname.'</a> -> '.$nav;
124 if (empty($course)) {
125 $course = get_record('course', 'id', $SESSION->cal_course_referer); // Useful to have around
129 $strcalendar = get_string('calendar', 'calendar');
130 $prefsbutton = calendar_preferences_button();
132 // Print title and header
133 print_header("$site->shortname: $strcalendar: $pagetitle", $strcalendar, $nav,
134 '', '', true, $prefsbutton, user_login_string($site));
136 echo calendar_overlib_html();
138 // Layout the whole page as three big columns.
139 echo '<table id="calendar">';
140 echo '<tr>';
142 // START: Main column
144 echo '<td class="maincalendar">';
146 switch($view) {
147 case 'day':
148 calendar_show_day($day, $mon, $yr, $courses, $groups, $users);
149 break;
150 case 'month':
151 calendar_show_month_detailed($mon, $yr, $courses, $groups, $users);
152 break;
153 case 'upcoming':
154 calendar_show_upcoming_events($courses, $groups, $users, get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS), get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS));
155 break;
158 echo '</td>';
160 // END: Main column
162 // START: Last column (3-month display)
163 echo '<td class="sidecalendar">';
164 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
166 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
167 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
168 $getvars = 'cal_d='.$day.'&amp;cal_m='.$mon.'&amp;cal_y='.$yr; // For filtering
170 echo '<div class="filters">';
171 echo calendar_filter_controls($view, $getvars);
172 echo '</div>';
174 echo '<div class="minicalendarblock">';
175 echo calendar_top_controls('display', array('m' => $prevmon, 'y' => $prevyr));
176 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
177 echo '</div><div class="minicalendarblock">';
178 echo calendar_top_controls('display', array('m' => $mon, 'y' => $yr));
179 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
180 echo '</div><div class="minicalendarblock">';
181 echo calendar_top_controls('display', array('m' => $nextmon, 'y' => $nextyr));
182 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
183 echo '</div>';
185 echo '</td>';
187 echo '</tr></table>';
189 print_footer();
193 function calendar_show_day($d, $m, $y, $courses, $groups, $users) {
194 global $CFG, $USER;
196 if (!checkdate($m, $d, $y)) {
197 $now = usergetdate(time());
198 list($d, $m, $y) = array(intval($now['mday']), intval($now['mon']), intval($now['year']));
201 $getvars = 'from=day&amp;cal_d='.$d.'&amp;cal_m='.$m.'&amp;cal_y='.$y; // For filtering
203 $starttime = make_timestamp($y, $m, $d);
204 $endtime = make_timestamp($y, $m, $d + 1) - 1;
206 $events = calendar_get_upcoming($courses, $groups, $users, 1, 100, $starttime);
208 $text = '';
209 if (!isguest() && !empty($USER->id)) {
210 $text.= '<div class="buttons">';
211 $text.= '<form action="'.CALENDAR_URL.'event.php" method="get">';
212 $text.= '<input type="hidden" name="action" value="new" />';
213 $text.= '<input type="hidden" name="cal_d" value="'.$d.'" />';
214 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
215 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
216 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
217 $text.= '</form></div>';
220 $text .= get_string('dayview', 'calendar').': '.calendar_course_filter_selector($getvars);
222 echo '<div class="header">'.$text.'</div>';
224 echo '<div class="controls">'.calendar_top_controls('day', array('d' => $d, 'm' => $m, 'y' => $y)).'</div>';
226 if (empty($events)) {
227 // There is nothing to display today.
228 echo '<h3>'.get_string('daywithnoevents', 'calendar').'</h3>';
230 } else {
232 echo '<div class="eventlist">';
234 $underway = array();
236 // First, print details about events that start today
237 foreach ($events as $event) {
238 if ($event->timestart >= $starttime && $event->timestart <= $endtime) { // Print it now
242 $dayend = calendar_day_representation($event->timestart + $event->timeduration);
243 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
244 $enddate = usergetdate($event->timestart + $event->timeduration);
245 // Set printable representation
246 echo calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).' ('.$timeend.')';
248 //unset($event->time);
249 $event->time = calendar_format_event_time($event, time(), '', false);
250 calendar_print_event($event);
252 } else { // Save this for later
253 $underway[] = $event;
257 // Then, show a list of all events that just span this day
258 if (!empty($underway)) {
259 echo '<h3>'.get_string('spanningevents', 'calendar').':</h3>';
260 foreach ($underway as $event) {
261 $event->time = calendar_format_event_time($event, time(), '', false);
262 calendar_print_event($event);
266 echo '</div>';
271 function calendar_show_month_detailed($m, $y, $courses, $groups, $users) {
272 global $CFG, $SESSION, $USER, $CALENDARDAYS;
273 global $day, $mon, $yr;
275 $getvars = 'from=month&amp;cal_d='.$day.'&amp;cal_m='.$mon.'&amp;cal_y='.$yr; // For filtering
277 $display = &New stdClass;
278 $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
279 $display->maxwday = $display->minwday + 6;
281 if(!empty($m) && !empty($y)) {
282 $thisdate = usergetdate(time()); // Time and day at the user's location
283 if($m == $thisdate['mon'] && $y == $thisdate['year']) {
284 // Navigated to this month
285 $date = $thisdate;
286 $display->thismonth = true;
288 else {
289 // Navigated to other month, let's do a nice trick and save us a lot of work...
290 if(!checkdate($m, 1, $y)) {
291 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
292 $display->thismonth = true;
294 else {
295 $date = array('mday' => 1, 'mon' => $m, 'year' => $y);
296 $display->thismonth = false;
300 else {
301 $date = usergetdate(time());
302 $display->thismonth = true;
305 // Fill in the variables we 're going to use, nice and tidy
306 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
307 $display->maxdays = calendar_days_in_month($m, $y);
309 if (get_user_timezone_offset() < 99) {
310 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
311 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
312 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
313 } else {
314 // no timezone info specified
315 $display->tstart = mktime(0, 0, 0, $m, 1, $y);
316 $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
319 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
321 // Align the starting weekday to fall in our display range
322 if($startwday < $display->minwday) {
323 $startwday += 7;
326 // Get events from database
327 $whereclause = calendar_sql_where(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses);
328 if($whereclause === false) {
329 $events = array();
331 else {
332 $events = get_records_select('event', $whereclause, 'timestart');
335 // Extract information: events vs. time
336 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday);
338 $text = '';
339 if(!isguest() && !empty($USER->id)) {
340 $text.= '<div class="buttons"><form action="'.CALENDAR_URL.'event.php" method="get">';
341 $text.= '<input type="hidden" name="action" value="new" />';
342 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
343 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
344 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
345 $text.= '</form></div>';
348 $text .= get_string('detailedmonthview', 'calendar').': '.calendar_course_filter_selector($getvars);
350 echo '<div class="header">'.$text.'</div>';
352 echo '<div class="controls">';
353 echo calendar_top_controls('month', array('m' => $m, 'y' => $y));
354 echo '</div>';
356 // Start calendar display
357 echo '<table class="calendarmonth"><tr class="weekdays">'; // Begin table. First row: day names
359 // Print out the names of the weekdays
360 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
361 // This uses the % operator to get the correct weekday no matter what shift we have
362 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
363 echo '<th>'.get_string($CALENDARDAYS[$i % 7], 'calendar').'</th>';
366 echo '</tr><tr>'; // End of day names; prepare for day numbers
368 // For the table display. $week is the row; $dayweek is the column.
369 $week = 1;
370 $dayweek = $startwday;
372 // Paddding (the first week may have blank days in the beginning)
373 for($i = $display->minwday; $i < $startwday; ++$i) {
374 echo '<td>&nbsp;</td>'."\n";
377 // Now display all the calendar
378 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
379 if($dayweek > $display->maxwday) {
380 // We need to change week (table row)
381 echo "</tr>\n<tr>";
382 $dayweek = $display->minwday;
383 ++$week;
386 // Reset vars
387 $cell = '';
388 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day&amp;', $day, $m, $y);
390 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
391 // Weekend. This is true no matter what the exact range is.
392 $class = 'weekend';
394 else {
395 // Normal working day.
396 $class = '';
399 // Special visual fx if an event is defined
400 if(isset($eventsbyday[$day])) {
401 if(isset($typesbyday[$day]['startglobal'])) {
402 $class .= ' event_global';
404 else if(isset($typesbyday[$day]['startcourse'])) {
405 $class .= ' event_course';
407 else if(isset($typesbyday[$day]['startgroup'])) {
408 $class .= ' event_group';
410 else if(isset($typesbyday[$day]['startuser'])) {
411 $class .= ' event_user';
413 if(count($eventsbyday[$day]) == 1) {
414 $title = get_string('oneevent', 'calendar');
416 else {
417 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
419 $cell = '<div class="day"><a href="'.$dayhref.'" title="'.$title.'">'.$day.'</a></div>';
421 else {
422 $cell = '<div class="day">'.$day.'</div>';
425 // Special visual fx if an event spans many days
426 if(isset($typesbyday[$day]['durationglobal'])) {
427 $class .= ' duration_global';
429 else if(isset($typesbyday[$day]['durationcourse'])) {
430 $class .= ' duration_course';
432 else if(isset($typesbyday[$day]['durationgroup'])) {
433 $class .= ' duration_group';
435 else if(isset($typesbyday[$day]['durationuser'])) {
436 $class .= ' duration_user';
439 // Special visual fx for today
440 if($display->thismonth && $day == $d) {
441 $class .= ' today';
444 // Just display it
445 if(!empty($class)) {
446 $class = ' class="'.trim($class).'"';
448 echo '<td'.$class.'>'.$cell;
450 if(isset($eventsbyday[$day])) {
451 echo '<ul class="events-new">';
452 foreach($eventsbyday[$day] as $eventindex) {
453 echo '<li><a href="'.$dayhref.'#event_'.$events[$eventindex]->id.'">'.format_string($events[$eventindex]->name, true).'</a></li>';
455 echo '</ul>';
457 if(isset($durationbyday[$day])) {
458 echo '<ul class="events-underway">';
459 foreach($durationbyday[$day] as $eventindex) {
460 echo '<li>['.format_string($events[$eventindex]->name,true).']</li>';
462 echo '</ul>';
464 echo "</td>\n";
467 // Paddding (the last week may have blank days at the end)
468 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
469 echo '<td>&nbsp;</td>';
471 echo "</tr>\n"; // Last row ends
473 echo "</table>\n"; // Tabular display of days ends
475 // OK, now for the filtering display
476 echo '<div class="filters"><table><tr>';
478 // Global events
479 if($SESSION->cal_show_global) {
480 echo '<td class="event_global" style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
481 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showglobal&amp;'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
483 else {
484 echo '<td style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
485 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showglobal&amp;'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
488 // Course events
489 if(!empty($SESSION->cal_show_course)) {
490 echo '<td class="event_course" style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
491 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showcourses&amp;'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
493 else {
494 echo '<td style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
495 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showcourses&amp;'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
498 echo "</tr>\n";
500 if(!empty($USER->id) && !isguest()) {
501 echo '<tr>';
502 // Group events
503 if($SESSION->cal_show_groups) {
504 echo '<td class="event_group" style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
505 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showgroups&amp;'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
507 else {
508 echo '<td style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
509 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showgroups&amp;'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
511 // User events
512 if($SESSION->cal_show_user) {
513 echo '<td class="event_user" style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
514 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showuser&amp;'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
516 else {
517 echo '<td style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
518 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL.'set.php?var=showuser&amp;'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
520 echo "</tr>\n";
523 echo '</table></div>';
526 function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $maxevents) {
527 global $USER;
529 $events = calendar_get_upcoming($courses, $groups, $users, $futuredays, $maxevents);
531 $text = '';
533 if(!isguest() && !empty($USER->id)) {
534 $text.= '<div class="buttons">';
535 $text.= '<form action="'.CALENDAR_URL.'event.php" method="get">';
536 $text.= '<input type="hidden" name="action" value="new" />';
538 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
539 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
541 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
542 $text.= '</form></div>';
545 $text .= get_string('upcomingevents', 'calendar').': '.calendar_course_filter_selector('from=upcoming');
547 echo '<div class="header">'.$text.'</div>';
549 if ($events) {
550 echo '<div class="eventlist">';
551 foreach ($events as $event) {
552 calendar_print_event($event);
554 echo '</div>';
555 } else {
556 print_heading(get_string('noupcomingevents', 'calendar'));
560 function calendar_course_filter_selector($getvars = '') {
561 global $USER, $SESSION;
563 if (empty($USER->id) or isguest()) {
564 return '';
567 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID)) && !empty($CFG->calendar_adminseesall)) {
568 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
569 } else {
570 $courses = get_my_courses($USER->id, 'shortname');
573 unset($courses[SITEID]);
575 $courseoptions[SITEID] = get_string('fulllistofcourses');
576 foreach ($courses as $course) {
577 $courseoptions[$course->id] = $course->shortname;
580 if (is_numeric($SESSION->cal_courses_shown)) {
581 $selected = $SESSION->cal_courses_shown;
582 } else {
583 $selected = '';
586 return popup_form(CALENDAR_URL.'set.php?var=setcourse&amp;'.$getvars.'&amp;id=',
587 $courseoptions, 'cal_course_flt', $selected, '', '', '', true);