3 /////////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Calendar extension //
9 // Copyright (C) 2003-2004 Greek School Network www.sch.gr //
12 // Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13 // Jon Papaioannou (pj@moodle.org) //
15 // Programming and development: //
16 // Jon Papaioannou (pj@moodle.org) //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@moodle.org) //
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. //
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. //
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: //
37 // http://www.gnu.org/copyleft/gpl.html //
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');
58 require_login($courseid);
59 } else if ($CFG->forcelogin
) {
63 //add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
64 $now = usergetdate(time());
67 $nav = calendar_get_link_tag(get_string('calendar', 'calendar'), CALENDAR_URL
.'view.php?view=upcoming&', $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);
79 $nav .= ' -> '.userdate($time, get_string('strftimedate'));
80 $pagetitle = get_string('dayview', 'calendar');
83 $nav .= ' -> '.userdate($time, get_string('strftimemonthyear'));
84 $pagetitle = get_string('detailedmonthview', 'calendar');
87 $pagetitle = get_string('upcomingevents', 'calendar');
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);
100 // Otherwise show just this one
101 $SESSION->cal_courses_shown
= $course->id
;
102 calendar_set_referring_course($SESSION->cal_courses_shown
);
109 if (empty($USER->id
) or isguest()) {
110 $defaultcourses = calendar_get_default_courses();
111 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
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!
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" style="height:100%;">';
142 // START: Main column
144 echo '<td class="maincalendar">';
145 echo '<div class="heightcontainer">';
149 calendar_show_day($day, $mon, $yr, $courses, $groups, $users);
152 calendar_show_month_detailed($mon, $yr, $courses, $groups, $users);
155 calendar_show_upcoming_events($courses, $groups, $users, get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS
), get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS
));
159 //Link to calendar export page
160 echo '<div class="bottom">';
161 print_single_button('export.php', false, get_string('exportcalendar', 'calendar'));
163 if (!empty($USER->id
)) {
164 $authtoken = sha1($USER->username
. $USER->password
);
165 $usernameencoded = urlencode($USER->username
);
167 echo "<a href=\"export_execute.php?preset_what=all&preset_time=recentupcoming&username=$usernameencoded&authtoken=$authtoken\">"
168 .'<img src="'.$CFG->pixpath
.'/i/ical.gif" height="14" width="36" '
169 .'alt="'.get_string('ical', 'calendar').'" '
170 .'title="'.get_string('quickdownloadcalendar', 'calendar').'" />'
180 // START: Last column (3-month display)
181 echo '<td class="sidecalendar">';
182 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
184 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
185 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
186 $getvars = 'cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
188 echo '<div class="filters">';
189 echo calendar_filter_controls($view, $getvars);
192 echo '<div class="minicalendarblock">';
193 echo calendar_top_controls('display', array('m' => $prevmon, 'y' => $prevyr));
194 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
195 echo '</div><div class="minicalendarblock">';
196 echo calendar_top_controls('display', array('m' => $mon, 'y' => $yr));
197 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
198 echo '</div><div class="minicalendarblock">';
199 echo calendar_top_controls('display', array('m' => $nextmon, 'y' => $nextyr));
200 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
205 echo '</tr></table>';
211 function calendar_show_day($d, $m, $y, $courses, $groups, $users) {
214 if (!checkdate($m, $d, $y)) {
215 $now = usergetdate(time());
216 list($d, $m, $y) = array(intval($now['mday']), intval($now['mon']), intval($now['year']));
219 $getvars = 'from=day&cal_d='.$d.'&cal_m='.$m.'&cal_y='.$y; // For filtering
221 $starttime = make_timestamp($y, $m, $d);
222 $endtime = make_timestamp($y, $m, $d +
1) - 1;
224 $events = calendar_get_upcoming($courses, $groups, $users, 1, 100, $starttime);
227 if (!isguest() && !empty($USER->id
)) {
228 $text.= '<div class="buttons">';
229 $text.= '<form action="'.CALENDAR_URL
.'event.php" method="get">';
231 $text.= '<input type="hidden" name="action" value="new" />';
232 $text.= '<input type="hidden" name="cal_d" value="'.$d.'" />';
233 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
234 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
235 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
236 $text.= '</div></form></div>';
239 $text .= get_string('dayview', 'calendar').': '.calendar_course_filter_selector($getvars);
241 echo '<div class="header">'.$text.'</div>';
243 echo '<div class="controls">'.calendar_top_controls('day', array('d' => $d, 'm' => $m, 'y' => $y)).'</div>';
245 if (empty($events)) {
246 // There is nothing to display today.
247 echo '<h3>'.get_string('daywithnoevents', 'calendar').'</h3>';
251 echo '<div class="eventlist">';
255 // First, print details about events that start today
256 foreach ($events as $event) {
257 if ($event->timestart
>= $starttime && $event->timestart
<= $endtime) { // Print it now
261 $dayend = calendar_day_representation($event->timestart + $event->timeduration);
262 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
263 $enddate = usergetdate($event->timestart + $event->timeduration);
264 // Set printable representation
265 echo calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']).' ('.$timeend.')';
267 //unset($event->time);
268 $event->time
= calendar_format_event_time($event, time(), '', false);
269 calendar_print_event($event);
271 } else { // Save this for later
272 $underway[] = $event;
276 // Then, show a list of all events that just span this day
277 if (!empty($underway)) {
278 echo '<h3>'.get_string('spanningevents', 'calendar').':</h3>';
279 foreach ($underway as $event) {
280 $event->time
= calendar_format_event_time($event, time(), '', false);
281 calendar_print_event($event);
290 function calendar_show_month_detailed($m, $y, $courses, $groups, $users) {
291 global $CFG, $SESSION, $USER, $CALENDARDAYS;
292 global $day, $mon, $yr;
294 $getvars = 'from=month&cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
296 $display = &New stdClass
;
297 $display->minwday
= get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY
);
298 $display->maxwday
= $display->minwday +
6;
300 if(!empty($m) && !empty($y)) {
301 $thisdate = usergetdate(time()); // Time and day at the user's location
302 if($m == $thisdate['mon'] && $y == $thisdate['year']) {
303 // Navigated to this month
305 $display->thismonth
= true;
308 // Navigated to other month, let's do a nice trick and save us a lot of work...
309 if(!checkdate($m, 1, $y)) {
310 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
311 $display->thismonth
= true;
314 $date = array('mday' => 1, 'mon' => $m, 'year' => $y);
315 $display->thismonth
= false;
320 $date = usergetdate(time());
321 $display->thismonth
= true;
324 // Fill in the variables we 're going to use, nice and tidy
325 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
326 $display->maxdays
= calendar_days_in_month($m, $y);
328 if (get_user_timezone_offset() < 99) {
329 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
330 $display->tstart
= gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
331 $display->tend
= gmmktime(23, 59, 59, $m, $display->maxdays
, $y); // GMT
333 // no timezone info specified
334 $display->tstart
= mktime(0, 0, 0, $m, 1, $y);
335 $display->tend
= mktime(23, 59, 59, $m, $display->maxdays
, $y);
338 $startwday = gmdate('w', $display->tstart
); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
340 // Align the starting weekday to fall in our display range
341 if($startwday < $display->minwday
) {
345 // Get events from database
346 $whereclause = calendar_sql_where(usertime($display->tstart
), usertime($display->tend
), $users, $groups, $courses);
347 if($whereclause === false) {
351 $events = get_records_select('event', $whereclause, 'timestart');
354 // Extract information: events vs. time
355 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday);
358 if(!isguest() && !empty($USER->id
)) {
359 $text.= '<div class="buttons"><form action="'.CALENDAR_URL
.'event.php" method="get">';
361 $text.= '<input type="hidden" name="action" value="new" />';
362 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
363 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
364 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
365 $text.= '</div></form></div>';
368 $text .= get_string('detailedmonthview', 'calendar').': '.calendar_course_filter_selector($getvars);
370 echo '<div class="header">'.$text.'</div>';
372 echo '<div class="controls">';
373 echo calendar_top_controls('month', array('m' => $m, 'y' => $y));
376 // Start calendar display
377 echo '<table class="calendarmonth"><tr class="weekdays">'; // Begin table. First row: day names
379 // Print out the names of the weekdays
380 for($i = $display->minwday
; $i <= $display->maxwday
; ++
$i) {
381 // This uses the % operator to get the correct weekday no matter what shift we have
382 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
383 echo '<th scope="col">'.get_string($CALENDARDAYS[$i %
7], 'calendar').'</th>';
386 echo '</tr><tr>'; // End of day names; prepare for day numbers
388 // For the table display. $week is the row; $dayweek is the column.
390 $dayweek = $startwday;
392 // Paddding (the first week may have blank days in the beginning)
393 for($i = $display->minwday
; $i < $startwday; ++
$i) {
394 echo '<td> </td>'."\n";
397 // Now display all the calendar
398 for($day = 1; $day <= $display->maxdays
; ++
$day, ++
$dayweek) {
399 if($dayweek > $display->maxwday
) {
400 // We need to change week (table row)
402 $dayweek = $display->minwday
;
408 $dayhref = calendar_get_link_href(CALENDAR_URL
.'view.php?view=day&', $day, $m, $y);
410 if(CALENDAR_WEEKEND
& (1 << ($dayweek %
7))) {
411 // Weekend. This is true no matter what the exact range is.
415 // Normal working day.
419 // Special visual fx if an event is defined
420 if(isset($eventsbyday[$day])) {
421 if(isset($typesbyday[$day]['startglobal'])) {
422 $class .= ' event_global';
424 else if(isset($typesbyday[$day]['startcourse'])) {
425 $class .= ' event_course';
427 else if(isset($typesbyday[$day]['startgroup'])) {
428 $class .= ' event_group';
430 else if(isset($typesbyday[$day]['startuser'])) {
431 $class .= ' event_user';
433 if(count($eventsbyday[$day]) == 1) {
434 $title = get_string('oneevent', 'calendar');
437 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
439 $cell = '<div class="day"><a href="'.$dayhref.'" title="'.$title.'">'.$day.'</a></div>';
442 $cell = '<div class="day">'.$day.'</div>';
445 // Special visual fx if an event spans many days
446 if(isset($typesbyday[$day]['durationglobal'])) {
447 $class .= ' duration_global';
449 else if(isset($typesbyday[$day]['durationcourse'])) {
450 $class .= ' duration_course';
452 else if(isset($typesbyday[$day]['durationgroup'])) {
453 $class .= ' duration_group';
455 else if(isset($typesbyday[$day]['durationuser'])) {
456 $class .= ' duration_user';
459 // Special visual fx for today
460 if($display->thismonth
&& $day == $d) {
466 $class = ' class="'.trim($class).'"';
468 echo '<td'.$class.'>'.$cell;
470 if(isset($eventsbyday[$day])) {
471 echo '<ul class="events-new">';
472 foreach($eventsbyday[$day] as $eventindex) {
473 echo '<li><a href="'.$dayhref.'#event_'.$events[$eventindex]->id
.'">'.format_string($events[$eventindex]->name
, true).'</a></li>';
477 if(isset($durationbyday[$day])) {
478 echo '<ul class="events-underway">';
479 foreach($durationbyday[$day] as $eventindex) {
480 echo '<li>['.format_string($events[$eventindex]->name
,true).']</li>';
487 // Paddding (the last week may have blank days at the end)
488 for($i = $dayweek; $i <= $display->maxwday
; ++
$i) {
489 echo '<td> </td>';
491 echo "</tr>\n"; // Last row ends
493 echo "</table>\n"; // Tabular display of days ends
495 // OK, now for the filtering display
496 echo '<div class="filters"><table><tr>';
499 if($SESSION->cal_show_global
) {
500 echo '<td class="event_global" style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
501 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showglobal&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
504 echo '<td style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
505 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showglobal&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
509 if(!empty($SESSION->cal_show_course
)) {
510 echo '<td class="event_course" style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
511 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showcourses&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
514 echo '<td style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
515 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showcourses&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
520 if(!empty($USER->id
) && !isguest()) {
523 if($SESSION->cal_show_groups
) {
524 echo '<td class="event_group" style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
525 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showgroups&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
528 echo '<td style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
529 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showgroups&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
532 if($SESSION->cal_show_user
) {
533 echo '<td class="event_user" style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
534 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showuser&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
537 echo '<td style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
538 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showuser&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
543 echo '</table></div>';
546 function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $maxevents) {
549 $events = calendar_get_upcoming($courses, $groups, $users, $futuredays, $maxevents);
553 if(!isguest() && !empty($USER->id
)) {
554 $text.= '<div class="buttons">';
555 $text.= '<form action="'.CALENDAR_URL
.'event.php" method="get">';
557 $text.= '<input type="hidden" name="action" value="new" />';
559 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
560 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
562 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
563 $text.= '</div></form></div>';
566 $text .= get_string('upcomingevents', 'calendar').': '.calendar_course_filter_selector('from=upcoming');
568 echo '<div class="header">'.$text.'</div>';
571 echo '<div class="eventlist">';
572 foreach ($events as $event) {
573 calendar_print_event($event);
577 print_heading(get_string('noupcomingevents', 'calendar'));
581 function calendar_course_filter_selector($getvars = '') {
582 global $USER, $SESSION;
584 if (empty($USER->id
) or isguest()) {
588 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) && !empty($CFG->calendar_adminseesall
)) {
589 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
591 $courses = get_my_courses($USER->id
, 'shortname');
594 unset($courses[SITEID
]);
596 $courseoptions[SITEID
] = get_string('fulllistofcourses');
597 foreach ($courses as $course) {
598 $courseoptions[$course->id
] = format_string($course->shortname
);
601 if (is_numeric($SESSION->cal_courses_shown
)) {
602 $selected = $SESSION->cal_courses_shown
;
607 return popup_form(CALENDAR_URL
.'set.php?var=setcourse&'.$getvars.'&id=',
608 $courseoptions, 'cal_course_flt', $selected, '', '', '', true);