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');
57 if ($courseid && $courseid != SITEID
) {
58 require_login($courseid);
59 } else if ($CFG->forcelogin
) {
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());
70 $strcalendar = get_string('calendar', 'calendar');
72 $navlinks[] = array('name' => $strcalendar,
73 'link' =>calendar_get_link_href(CALENDAR_URL
.'view.php?view=upcoming&course='.$courseid.'&',
74 $now['mday'], $now['mon'], $now['year']),
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);
87 $navlinks[] = array('name' => userdate($time, get_string('strftimedate')), 'link' => null, 'type' => 'misc');
88 $pagetitle = get_string('dayview', 'calendar');
91 $navlinks[] = array('name' => userdate($time, get_string('strftimemonthyear')), 'link' => null, 'type' => 'misc');
92 $pagetitle = get_string('detailedmonthview', 'calendar');
95 $pagetitle = get_string('upcomingevents', 'calendar');
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);
108 // Otherwise show just this one
109 $SESSION->cal_courses_shown
= $course->id
;
110 calendar_set_referring_course($SESSION->cal_courses_shown
);
117 if (empty($USER->id
) or isguest()) {
118 $defaultcourses = calendar_get_default_courses();
119 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
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) {
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%;">';
149 // START: Main column
151 echo '<td class="maincalendar">';
152 echo '<div class="heightcontainer">';
156 calendar_show_day($day, $mon, $yr, $courses, $groups, $users, $courseid);
159 calendar_show_month_detailed($mon, $yr, $courses, $groups, $users, $courseid);
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);
166 //Link to calendar export page
167 echo '<div class="bottom">';
168 if (!empty($CFG->enablecalendarexport
)) {
169 print_single_button('export.php', array('course'=>$courseid), get_string('exportcalendar', 'calendar'));
171 if (!empty($USER->id
)) {
172 $authtoken = sha1($USER->username
. $USER->password
. $CFG->calendar_exportsalt
);
173 $usernameencoded = urlencode($USER->username
);
175 echo "<a href=\"export_execute.php?preset_what=all&preset_time=recentupcoming&username=$usernameencoded&authtoken=$authtoken\">"
176 .'<img src="'.$CFG->pixpath
.'/i/ical.gif" height="14" width="36" '
177 .'alt="'.get_string('ical', 'calendar').'" '
178 .'title="'.get_string('quickdownloadcalendar', 'calendar').'" />'
189 // START: Last column (3-month display)
190 echo '<td class="sidecalendar">';
191 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
192 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
193 $getvars = 'id='.$courseid.'&cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
195 echo '<div class="sideblock">';
196 echo '<div class="header"><h2>'.get_string('eventskey', 'calendar').'</h2></div>';
197 echo '<div class="filters">';
198 echo calendar_filter_controls($view, $getvars, NULL, $courses);
202 echo '<div class="sideblock">';
203 echo '<div class="header"><h2>'.get_string('monthlyview', 'calendar').'</h2></div>';
205 echo '<div class="minicalendarblock minicalendartop">';
206 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $prevmon, 'y' => $prevyr));
207 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
208 echo '</div><div class="minicalendarblock">';
209 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $mon, 'y' => $yr));
210 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
211 echo '</div><div class="minicalendarblock">';
212 echo calendar_top_controls('display', array('id' => $courseid, 'm' => $nextmon, 'y' => $nextyr));
213 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
219 echo '</tr></table>';
225 function calendar_show_day($d, $m, $y, $courses, $groups, $users, $courseid) {
228 if (!checkdate($m, $d, $y)) {
229 $now = usergetdate(time());
230 list($d, $m, $y) = array(intval($now['mday']), intval($now['mon']), intval($now['year']));
233 $getvars = 'from=day&cal_d='.$d.'&cal_m='.$m.'&cal_y='.$y; // For filtering
235 $starttime = make_timestamp($y, $m, $d);
236 $endtime = make_timestamp($y, $m, $d +
1) - 1;
238 $events = calendar_get_upcoming($courses, $groups, $users, 1, 100, $starttime);
241 if (!isguest() && !empty($USER->id
) && calendar_user_can_add_event()) {
242 $text.= '<div class="buttons">';
243 $text.= '<form action="'.CALENDAR_URL
.'event.php" method="get">';
245 $text.= '<input type="hidden" name="action" value="new" />';
246 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
247 $text.= '<input type="hidden" name="cal_d" value="'.$d.'" />';
248 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
249 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
250 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
251 $text.= '</div></form></div>';
254 $text .= '<label for="cal_course_flt_jump">'.
255 get_string('dayview', 'calendar').
257 calendar_course_filter_selector($getvars);
259 echo '<div class="header">'.$text.'</div>';
261 echo '<div class="controls">'.calendar_top_controls('day', array('id' => $courseid, 'd' => $d, 'm' => $m, 'y' => $y)).'</div>';
263 if (empty($events)) {
264 // There is nothing to display today.
265 echo '<h3>'.get_string('daywithnoevents', 'calendar').'</h3>';
269 echo '<div class="eventlist">';
273 // First, print details about events that start today
274 foreach ($events as $event) {
276 $event->calendarcourseid
= $courseid;
278 if ($event->timestart
>= $starttime && $event->timestart
<= $endtime) { // Print it now
282 $dayend = calendar_day_representation($event->timestart + $event->timeduration);
283 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
284 $enddate = usergetdate($event->timestart + $event->timeduration);
285 // Set printable representation
286 echo calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']).' ('.$timeend.')';
288 //unset($event->time);
290 $event->time
= calendar_format_event_time($event, time(), '', false, $starttime);
291 calendar_print_event($event);
293 } else { // Save this for later
294 $underway[] = $event;
298 // Then, show a list of all events that just span this day
299 if (!empty($underway)) {
300 echo '<h3>'.get_string('spanningevents', 'calendar').':</h3>';
301 foreach ($underway as $event) {
302 $event->time
= calendar_format_event_time($event, time(), '', false, $starttime);
303 calendar_print_event($event);
312 function calendar_show_month_detailed($m, $y, $courses, $groups, $users, $courseid) {
313 global $CFG, $SESSION, $USER, $CALENDARDAYS;
314 global $day, $mon, $yr;
316 $getvars = 'from=month&cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
318 $display = &New stdClass
;
319 $display->minwday
= get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY
);
320 $display->maxwday
= $display->minwday +
6;
322 if(!empty($m) && !empty($y)) {
323 $thisdate = usergetdate(time()); // Time and day at the user's location
324 if($m == $thisdate['mon'] && $y == $thisdate['year']) {
325 // Navigated to this month
327 $display->thismonth
= true;
330 // Navigated to other month, let's do a nice trick and save us a lot of work...
331 if(!checkdate($m, 1, $y)) {
332 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
333 $display->thismonth
= true;
336 $date = array('mday' => 1, 'mon' => $m, 'year' => $y);
337 $display->thismonth
= false;
342 $date = usergetdate(time());
343 $display->thismonth
= true;
346 // Fill in the variables we 're going to use, nice and tidy
347 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
348 $display->maxdays
= calendar_days_in_month($m, $y);
351 if (get_user_timezone_offset() < 99) {
352 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
353 $display->tstart
= gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
354 $display->tend
= gmmktime(23, 59, 59, $m, $display->maxdays
, $y); // GMT
355 $startwday = gmdate('w', $display->tstart
); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
357 // no timezone info specified
358 $display->tstart
= mktime(0, 0, 0, $m, 1, $y);
359 $display->tend
= mktime(23, 59, 59, $m, $display->maxdays
, $y);
360 $startwday = date('w', $display->tstart
); // $display->tstart not necessarily GMT, so use date()
363 // Align the starting weekday to fall in our display range
364 if($startwday < $display->minwday
) {
368 // Get events from database
369 $events = calendar_get_events(usertime($display->tstart
), usertime($display->tend
), $users, $groups, $courses);
370 if (!empty($events)) {
371 foreach($events as $eventid => $event) {
372 if (!empty($event->modulename
)) {
373 $cm = get_coursemodule_from_instance($event->modulename
, $event->instance
);
374 if (!groups_course_module_visible($cm)) {
375 unset($events[$eventid]);
381 // Extract information: events vs. time
382 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
385 if(!isguest() && !empty($USER->id
) && calendar_user_can_add_event()) {
386 $text.= '<div class="buttons"><form action="'.CALENDAR_URL
.'event.php" method="get">';
388 $text.= '<input type="hidden" name="action" value="new" />';
389 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
390 $text.= '<input type="hidden" name="cal_m" value="'.$m.'" />';
391 $text.= '<input type="hidden" name="cal_y" value="'.$y.'" />';
392 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
393 $text.= '</div></form></div>';
396 $text .= '<label for="cal_course_flt_jump">'.
397 get_string('detailedmonthview', 'calendar').
399 calendar_course_filter_selector($getvars);
401 echo '<div class="header">'.$text.'</div>';
403 echo '<div class="controls">';
404 echo calendar_top_controls('month', array('id' => $courseid, 'm' => $m, 'y' => $y));
407 // Start calendar display
408 echo '<table class="calendarmonth"><tr class="weekdays">'; // Begin table. First row: day names
410 // Print out the names of the weekdays
411 for($i = $display->minwday
; $i <= $display->maxwday
; ++
$i) {
412 // This uses the % operator to get the correct weekday no matter what shift we have
413 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
414 echo '<th scope="col">'.get_string($CALENDARDAYS[$i %
7], 'calendar').'</th>';
417 echo '</tr><tr>'; // End of day names; prepare for day numbers
419 // For the table display. $week is the row; $dayweek is the column.
421 $dayweek = $startwday;
423 // Paddding (the first week may have blank days in the beginning)
424 for($i = $display->minwday
; $i < $startwday; ++
$i) {
425 echo '<td class="nottoday"> </td>'."\n";
428 // Now display all the calendar
429 for($day = 1; $day <= $display->maxdays
; ++
$day, ++
$dayweek) {
430 if($dayweek > $display->maxwday
) {
431 // We need to change week (table row)
433 $dayweek = $display->minwday
;
439 $dayhref = calendar_get_link_href(CALENDAR_URL
.'view.php?view=day&course='.$courseid.'&', $day, $m, $y);
441 if(CALENDAR_WEEKEND
& (1 << ($dayweek %
7))) {
442 // Weekend. This is true no matter what the exact range is.
446 // Normal working day.
450 // Special visual fx if an event is defined
451 if(isset($eventsbyday[$day])) {
452 if(count($eventsbyday[$day]) == 1) {
453 $title = get_string('oneevent', 'calendar');
456 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
458 $cell = '<div class="day"><a href="'.$dayhref.'" title="'.$title.'">'.$day.'</a></div>';
461 $cell = '<div class="day">'.$day.'</div>';
464 // Special visual fx if an event spans many days
465 if(isset($typesbyday[$day]['durationglobal'])) {
466 $class .= ' duration_global';
468 else if(isset($typesbyday[$day]['durationcourse'])) {
469 $class .= ' duration_course';
471 else if(isset($typesbyday[$day]['durationgroup'])) {
472 $class .= ' duration_group';
474 else if(isset($typesbyday[$day]['durationuser'])) {
475 $class .= ' duration_user';
478 // Special visual fx for today
479 if($display->thismonth
&& $day == $d) {
482 $class .= ' nottoday';
487 $class = ' class="'.trim($class).'"';
489 echo '<td'.$class.'>'.$cell;
491 if(isset($eventsbyday[$day])) {
492 echo '<ul class="events-new">';
493 foreach($eventsbyday[$day] as $eventindex) {
495 // If event has a class set then add it to the event <li> tag
497 if (!empty($events[$eventindex]->class)) {
498 $eventclass = ' class="'.$events[$eventindex]->class.'"';
501 echo '<li'.$eventclass.'><a href="'.$dayhref.'#event_'.$events[$eventindex]->id
.'">'.format_string($events[$eventindex]->name
, true).'</a></li>';
505 if(isset($durationbyday[$day])) {
506 echo '<ul class="events-underway">';
507 foreach($durationbyday[$day] as $eventindex) {
508 echo '<li>['.format_string($events[$eventindex]->name
,true).']</li>';
515 // Paddding (the last week may have blank days at the end)
516 for($i = $dayweek; $i <= $display->maxwday
; ++
$i) {
517 echo '<td class="nottoday"> </td>';
519 echo "</tr>\n"; // Last row ends
521 echo "</table>\n"; // Tabular display of days ends
523 // OK, now for the filtering display
524 echo '<div class="filters"><table><tr>';
527 if($SESSION->cal_show_global
) {
528 echo '<td class="event_global" style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
529 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showglobal&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
531 echo '<td style="width: 8px;"></td><td><strong>'.get_string('globalevents', 'calendar').':</strong> ';
532 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showglobal&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
536 if(!empty($SESSION->cal_show_course
)) {
537 echo '<td class="event_course" style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
538 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showcourses&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
540 echo '<td style="width: 8px;"></td><td><strong>'.get_string('courseevents', 'calendar').':</strong> ';
541 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showcourses&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
546 if(!empty($USER->id
) && !isguest()) {
549 if($SESSION->cal_show_groups
) {
550 echo '<td class="event_group" style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
551 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showgroups&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
553 echo '<td style="width: 8px;"></td><td><strong>'.get_string('groupevents', 'calendar').':</strong> ';
554 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showgroups&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
557 if($SESSION->cal_show_user
) {
558 echo '<td class="event_user" style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
559 echo get_string('shown', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showuser&'.$getvars.'">'.get_string('clickhide', 'calendar').'</a>)</td>'."\n";
561 echo '<td style="width: 8px;"></td><td><strong>'.get_string('userevents', 'calendar').':</strong> ';
562 echo get_string('hidden', 'calendar').' (<a href="'.CALENDAR_URL
.'set.php?var=showuser&'.$getvars.'">'.get_string('clickshow', 'calendar').'</a>)</td>'."\n";
567 echo '</table></div>';
570 function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $maxevents, $courseid) {
573 $events = calendar_get_upcoming($courses, $groups, $users, $futuredays, $maxevents);
577 if(!isguest() && !empty($USER->id
) && calendar_user_can_add_event()) {
578 $text.= '<div class="buttons">';
579 $text.= '<form action="'.CALENDAR_URL
.'event.php" method="get">';
581 $text.= '<input type="hidden" name="action" value="new" />';
582 $text.= '<input type="hidden" name="course" value="'.$courseid.'" />';
583 $text.= '<input type="submit" value="'.get_string('newevent', 'calendar').'" />';
584 $text.= '</div></form></div>';
587 $text .= '<label for="cal_course_flt_jump">'.
588 get_string('upcomingevents', 'calendar').
590 calendar_course_filter_selector('from=upcoming');
592 echo '<div class="header">'.$text.'</div>';
596 echo '<div class="eventlist">';
597 foreach ($events as $event) {
598 $event->calendarcourseid
= $courseid;
599 calendar_print_event($event);
603 print_heading(get_string('noupcomingevents', 'calendar'));
607 function calendar_course_filter_selector($getvars = '') {
608 global $USER, $SESSION;
610 if (empty($USER->id
) or isguest()) {
614 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM
)) && !empty($CFG->calendar_adminseesall
)) {
615 $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
617 $courses = get_my_courses($USER->id
, 'shortname');
620 unset($courses[SITEID
]);
622 $courseoptions[SITEID
] = get_string('fulllistofcourses');
623 foreach ($courses as $course) {
624 $courseoptions[$course->id
] = format_string($course->shortname
);
627 if (is_numeric($SESSION->cal_courses_shown
)) {
628 $selected = $SESSION->cal_courses_shown
;
633 return popup_form(CALENDAR_URL
.'set.php?var=setcourse&'.$getvars.'&id=',
634 $courseoptions, 'cal_course_flt', $selected, '', '', '', true);