wwwroot.'/calendar/'); define ('CALENDAR_TF_24', '%H:%M'); define ('CALENDAR_TF_12', '%I:%M %p'); // Initialize the session variables here to be sure calendar_session_vars(); function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) { global $CFG, $USER; $display = &New stdClass; $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY); $display->maxwday = $display->minwday + 6; $content = ''; if(!empty($cal_month) && !empty($cal_year)) { $thisdate = usergetdate(time()); // Date and time the user sees at his location if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) { // Navigated to this month $date = $thisdate; $display->thismonth = true; } else { // Navigated to other month, let's do a nice trick and save us a lot of work... if(!checkdate($cal_month, 1, $cal_year)) { $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']); $display->thismonth = true; } else { $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year); $display->thismonth = false; } } } else { $date = usergetdate(time()); // Date and time the user sees at his location $display->thismonth = true; } // Fill in the variables we 're going to use, nice and tidy list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display $display->maxdays = calendar_days_in_month($m, $y); // We 'll keep these values as GMT here, and offset them when the time comes to query the db $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ // Align the starting weekday to fall in our display range // This is simple, not foolproof. if($startwday < $display->minwday) { $startwday += 7; } // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ! $whereclause = calendar_sql_where(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses); if($whereclause === false) { $events = array(); } else { $events = get_records_select('event', $whereclause); } // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after // possibly removing courseid 1 from $courses, there is only one course left, then clicking on a day in the month // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra // arguments to this function. $morehref = ''; if(!empty($courses)) { $courses = array_diff($courses, array(1)); if(count($courses) == 1) { $morehref = '&course='.reset($courses); } } // We want to have easy access by day, since the display is on a per-day basis. // Arguments passed by reference. //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday); calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday); $content .= ''; // Begin table $content .= ''; // Header row: day names // Print out the names of the weekdays $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'); for($i = $display->minwday; $i <= $display->maxwday; ++$i) { // This uses the % operator to get the correct weekday no matter what shift we have // applied to the $display->minwday : $display->maxwday range from the default 0 : 6 $content .= ''; } $content .= ''; // End of day names; prepare for day numbers // For the table display. $week is the row; $dayweek is the column. $dayweek = $startwday; // Paddding (the first week may have blank days in the beginning) for($i = $display->minwday; $i < $startwday; ++$i) { $content .= ''."\n"; } $strftimetimedayshort = get_string('strftimedayshort'); // Now display all the calendar for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) { if($dayweek > $display->maxwday) { // We need to change week (table row) $content .= ''; $dayweek = $display->minwday; } // Reset vars $cell = ''; if($dayweek % 7 == 0 || $dayweek % 7 == 6) { // Weekend. This is true no matter what the exact range is. $class = 'cal_weekend'; } else { // Normal working day. $class = ''; } // Special visual fx if an event is defined if(isset($eventsbyday[$day])) { $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day'.$morehref.'&', $day, $m, $y); // OverLib popup $popupcontent = ''; foreach($eventsbyday[$day] as $eventid) { if (!isset($events[$eventid])) { continue; } $event = $events[$eventid]; if(!empty($event->modulename)) { $popupicon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif'; $popupalt = $event->modulename; } else if ($event->courseid == 1) { // Site event $popupicon = $CFG->pixpath.'/c/site.gif'; $popupalt = ''; } else if ($event->courseid > 1 and empty($event->groupid)) { // Course event $popupicon = $CFG->pixpath.'/c/course.gif'; $popupalt = ''; } else if ($event->groupid) { // Group event $popupicon = $CFG->pixpath.'/c/group.gif'; $popupalt = ''; } else if ($event->userid) { // User event $popupicon = $CFG->pixpath.'/c/user.gif'; $popupalt = ''; } $popupcontent .= '
'.$popupalt.''.$event->name.'
'; } $popupcaption = get_string('eventsfor', 'calendar', userdate($events[$eventid]->timestart, $strftimetimedayshort)); $popupcontent = str_replace("'", "\'", htmlspecialchars($popupcontent)); $popup = 'onmouseover="return overlib(\''.$popupcontent.'\', CAPTION, \''.$popupcaption.'\');" onmouseout="return nd();"'; // Class and cell content if(isset($typesbyday[$day]['startglobal'])) { $class .= ' cal_event_global'; } else if(isset($typesbyday[$day]['startcourse'])) { $class .= ' cal_event_course'; } else if(isset($typesbyday[$day]['startgroup'])) { $class .= ' cal_event_group'; } else if(isset($typesbyday[$day]['startuser'])) { $class .= ' cal_event_user'; } $cell = ''.$day.''; } else { $cell = $day; } if(isset($typesbyday[$day]['durationglobal'])) { $class .= ' cal_duration_global'; } else if(isset($typesbyday[$day]['durationcourse'])) { $class .= ' cal_duration_course'; } else if(isset($typesbyday[$day]['durationgroup'])) { $class .= ' cal_duration_group'; } else if(isset($typesbyday[$day]['durationuser'])) { $class .= ' cal_duration_user'; } // Special visual fx for today if($display->thismonth && $day == $d) { $class .= ' cal_today'; } // Just display it if(!empty($class)) { $class = ' class="'.$class.'"'; } $content .= ''.$cell."\n"; } // Paddding (the last week may have blank days at the end) for($i = $dayweek; $i <= $display->maxwday; ++$i) { $content .= ''; } $content .= ''; // Last row ends $content .= '
'.get_string($days[$i % 7], 'calendar').'
 
 
'; // Tabular display of days ends return $content; } function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) { global $CFG; $display = &New stdClass; $display->range = $daysinfuture; // How many days in the future we 'll look $display->maxevents = $maxevents; $output = array(); // Prepare "course caching", since it may save us a lot of queries $coursecache = array(); $processed = 0; $now = time(); // We 'll need this later $usermidnighttoday = usergetmidnight($now); if ($fromtime) { $display->tstart = $fromtime; } else { $display->tstart = $usermidnighttoday; } // This effectively adds as many days as needed, and the final SECS_IN_DAY - 1 // serves to cover the duration until the end of the final day. We could // just do another gmmktime() and an addition, but this is "faster" :) $display->tend = $display->tstart + (SECS_IN_DAY * $display->range) - 1; // Get the events matching our criteria $whereclause = calendar_sql_where($display->tstart, $display->tend, $users, $groups, $courses); if ($whereclause === false) { $events = false; } else { $whereclause .= ' ORDER BY timestart'; // We want them this way $events = get_records_select('event', $whereclause); } // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after // possibly removing courseid 1 from $courses, there is only one course left, then clicking on a day in the month // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra // arguments to this function. $morehref = ''; if(!empty($courses)) { $courses = array_diff($courses, array(1)); if(count($courses) == 1) { $morehref = '&course='.reset($courses); } } if($events !== false) { foreach($events as $event) { if($processed >= $display->maxevents) { break; } $startdate = usergetdate($event->timestart); $enddate = usergetdate($event->timestart + $event->timeduration); $usermidnightstart = usergetmidnight($event->timestart); if($event->timeduration) { // To avoid doing the math if one IF is enough :) $usermidnightend = usergetmidnight($event->timestart + $event->timeduration); } else { $usermidnightend = $usermidnightstart; } // OK, now to get a meaningful display... // First of all we have to construct a human-readable date/time representation if($event->timestart + $event->timeduration < $now) { // It has expired, so we don't care about duration $day = calendar_day_representation($event->timestart + $event->timeduration, $now); $time = calendar_time_representation($event->timestart + $event->timeduration); // This var always has the printable time representation $eventtime = ''.$day.' ('.$time.')'; } else if($event->timeduration) { // It has a duration if($usermidnightstart == $usermidnightend) { // But it's all on the same day $day = calendar_day_representation($event->timestart, $now); $timestart = calendar_time_representation($event->timestart); $timeend = calendar_time_representation($event->timestart + $event->timeduration); // Set printable representation $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']). ' ('.$timestart.' -> '.$timeend.')'; } else { // It spans two or more days $daystart = calendar_day_representation($event->timestart, $now); $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now); $timestart = calendar_time_representation($event->timestart); $timeend = calendar_time_representation($event->timestart + $event->timeduration); // Set printable representation $eventtime = calendar_get_link_tag($daystart, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $startdate['mday'], $startdate['mon'], $startdate['year']). ' ('.$timestart.') -> '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']). ' ('.$timeend.')'; } } else { // It's an "instantaneous" event $day = calendar_day_representation($event->timestart, $now); $time = calendar_time_representation($event->timestart); // Set printable representation $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $startdate['mday'], $startdate['mon'], $startdate['year']).' ('.$time.')'; } $outkey = count($output); $output[$outkey] = $event; // Grab the whole raw event by default // Now we know how to display the time, we have to see how to display the event if(!empty($event->modulename)) { // Activity event // The module name is set. I will assume that it has to be displayed, and // also that it is an automatically-generated event. And of course that the // three fields for get_coursemodule_from_instance are set correctly. calendar_get_course_cached($coursecache, $event->courseid); $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance, $event->courseid); if ($module === false) { // This shouldn't have happened. What to do now? // Just ignore it continue; } $modulename = get_string('modulename', $event->modulename); $eventtype = get_string($event->eventtype, $event->modulename); $icon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif'; $output[$outkey]->icon = ''; $output[$outkey]->referer = ''.$event->name.''; $output[$outkey]->time = $eventtime; $output[$outkey]->courselink = ''.$coursecache[$event->courseid]->fullname.''; $output[$outkey]->cmid = $module->id; } else if($event->courseid == 1) { // Site event $output[$outkey]->icon = ''; $output[$outkey]->time = $eventtime; } else if($event->courseid > 1 and !$event->groupid) { // Course event calendar_get_course_cached($coursecache, $event->courseid); $output[$outkey]->icon = ''; $output[$outkey]->time = $eventtime; $output[$outkey]->courselink = ''.$coursecache[$event->courseid]->fullname.''; } else if ($event->groupid) { // Group event $output[$outkey]->icon = ''; $output[$outkey]->time = $eventtime; } else if($event->userid) { // User event $output[$outkey]->icon = ''; $output[$outkey]->time = $eventtime; } ++$processed; } } return $output; } function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) { $whereclause = ''; // Quick test if(is_bool($users) && is_bool($groups) && is_bool($courses)) { return false; } if(is_array($users) && !empty($users)) { // Events from a number of users if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0'; } else if(is_numeric($users)) { // Events from one user if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' userid = '.$users.' AND courseid = 0 AND groupid = 0'; } else if($users === true) { // Events from ALL users if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' userid != 0 AND courseid = 0 AND groupid = 0'; } else if($users === false) { // No user at all if(!empty($whereclause)) { $whereclause .= ' OR'; } $whereclause .= ' 0'; } if(is_array($groups) && !empty($groups)) { // Events from a number of groups if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' groupid IN ('.implode(',', $groups).')'; } else if(is_numeric($groups)) { // Events from one group if(!empty($whereclause)) $whereclause .= ' OR '; $whereclause .= ' groupid = '.$groups; } else if($groups === true) { // Events from ALL groups if(!empty($whereclause)) $whereclause .= ' OR '; $whereclause .= ' groupid != 0'; } if(is_array($courses)) { // A number of courses (maybe none at all!) if(!empty($courses)) { if(!empty($whereclause)) { $whereclause .= ' OR'; } $whereclause .= ' groupid = 0 AND courseid IN ('.implode(',', $courses).')'; } else { // This means NO courses, not that we don't care! if(!empty($whereclause)) { $whereclause .= ' OR'; } $whereclause .= ' 0'; } } else if(is_numeric($courses)) { // One course if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' groupid = 0 AND courseid = '.$courses; } else if($courses === true) { // Events from ALL courses if(!empty($whereclause)) $whereclause .= ' OR'; $whereclause .= ' groupid = 0 AND courseid != 0'; } if ($ignorehidden) { if (!empty($whereclause)) $whereclause .= ' AND'; $whereclause .= ' visible = 1'; } if($withduration) { $timeclause = 'timestart + timeduration >= '.$tstart.' AND timestart <= '.$tend; } else { $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend; } if(!empty($whereclause)) { // We have additional constraints $whereclause = $timeclause.' AND ('.$whereclause.')'; } else { // Just basic time filtering $whereclause = $timeclause; } return $whereclause; } function calendar_top_controls($type, $data) { global $CFG; $content = ''; if(!isset($data['d'])) { $data['d'] = 1; } $time = calendar_mktime_check($data['m'], $data['d'], $data['y']); $date = getdate($time); $data['m'] = $date['mon']; $data['y'] = $date['year']; switch($type) { case 'frontpage': list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); $nextlink = calendar_get_link_tag('>>', 'index.php?', 0, $nextmonth, $nextyear); $prevlink = calendar_get_link_tag('<<', 'index.php?', 0, $prevmonth, $prevyear); $content .= ''; $content .= '\n"; $content .= '\n"; $content .= '\n"; $content .= '
'.$prevlink."'.strftime(get_string('strftimemonthyear'), $time)."'.$nextlink."
'; break; case 'course': list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); $nextlink = calendar_get_link_tag('>>', 'view.php?id='.$data['id'].'&', 0, $nextmonth, $nextyear); $prevlink = calendar_get_link_tag('<<', 'view.php?id='.$data['id'].'&', 0, $prevmonth, $prevyear); $content .= ''; $content .= '\n"; $content .= '\n"; $content .= '\n"; $content .= '
'.$prevlink."'.strftime(get_string('strftimemonthyear'), $time)."'.$nextlink."
'; break; case 'upcoming': $content .= '
'.strftime(get_string('strftimemonthyear'), $time)."
\n"; break; case 'display': $content .= '
'.strftime(get_string('strftimemonthyear'), $time)."
\n"; break; case 'month': list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); $prevdate = calendar_mktime_check($prevmonth, 1, $prevyear); $nextdate = calendar_mktime_check($nextmonth, 1, $nextyear); $content .= "\n"; $content .= '\n"; $content .= '\n"; $content .= '\n"; $content .= "
<< '.strftime(get_string('strftimemonthyear'), $prevdate)."'.strftime(get_string('strftimemonthyear'), $time)."'.strftime(get_string('strftimemonthyear'), $nextdate)." >>
\n"; break; case 'day': $data['d'] = $date['mday']; // Just for convenience $dayname = calendar_wday_name($date['weekday']); $prevdate = getdate($time - SECS_IN_DAY); $nextdate = getdate($time + SECS_IN_DAY); $prevname = calendar_wday_name($prevdate['weekday']); $nextname = calendar_wday_name($nextdate['weekday']); $content .= "\n"; $content .= '\n"; // Get the format string $text = get_string('strftimedaydate'); // Regexp hackery to make a link out of the month/year part $text = ereg_replace('(%B.+%Y|%Y.+%B|%Y.+%m[^ ]+)', '\\1', $text); // Replace with actual values and lose any day leading zero $text = strftime($text, $time); $text = str_replace(' 0', ' ', $text); // Print the actual thing $content .= '\n"; $content .= '\n"; $content .= "
<< '.$prevname."'.$text."'.$nextname." >>
\n"; break; } return $content; } function calendar_filter_controls($type, $vars = NULL, $course = NULL) { global $CFG, $SESSION, $USER; $groupevents = true; $getvars = ''; switch($type) { case 'event': case 'upcoming': case 'day': case 'month': $getvars = '&from='.$type; break; case 'course': $getvars = '&from=course&id='.$_GET['id']; if (isset($course->groupmode) and !$course->groupmode and $course->groupmodeforce) { $groupevents = false; } break; } if (!empty($vars)) { $getvars .= '&'.$vars; } $content = ''; $content .= ''; if($SESSION->cal_show_global) { $content .= ''."\n"; } else { $content .= ''."\n"; } if($SESSION->cal_show_course) { $content .= ''."\n"; } else { $content .= ''."\n"; } if(!empty($USER) && !isguest($USER->id)) { $content .= "\n"; if($groupevents) { // This course MIGHT have group events defined, so show the filter if($SESSION->cal_show_groups) { $content .= ''."\n"; } else { $content .= ''."\n"; } if ($SESSION->cal_show_user) { $content .= ''."\n"; } else { $content .= ''."\n"; } } else { // This course CANNOT have group events, so lose the filter $content .= ''."\n"; if($SESSION->cal_show_user) { $content .= ''."\n"; } else { $content .= ''."\n"; } } } $content .= "\n
'.get_string('globalevents', 'calendar').''.get_string('globalevents', 'calendar').''.get_string('courseevents', 'calendar').''.get_string('courseevents', 'calendar').'
'.get_string('groupevents', 'calendar').''.get_string('groupevents', 'calendar').''.get_string('userevents', 'calendar').''.get_string('userevents', 'calendar').' '.get_string('userevents', 'calendar').''.get_string('userevents', 'calendar').'
\n"; return $content; } function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) { static $shortformat; if(empty($shortformat)) { $shortformat = get_string('strftimedayshort'); } if($now === false) { $now = time(); } // To have it in one place, if a change is needed $formal = userdate($tstamp, $shortformat); $datestamp = usergetdate($tstamp); $datenow = usergetdate($now); if($usecommonwords == false) { // We don't want words, just a date return $formal; } else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) { // Today return get_string('today', 'calendar'); } else if( ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) || ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1) ) { // Yesterday return get_string('yesterday', 'calendar'); } else if( ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) || ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1) ) { // Tomorrow return get_string('tomorrow', 'calendar'); } else { return $formal; } } function calendar_time_representation($time) { static $langtimeformat = NULL; if($langtimeformat === NULL) { $langtimeformat = get_string('strftimetime'); } $timeformat = get_user_preferences('calendar_timeformat'); // The ? is needed because the preference might be present, but empty return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat); } function calendar_get_link_href($linkbase, $d, $m, $y) { if(empty($linkbase)) return ''; $paramstr = ''; if(!empty($d)) $paramstr .= '&cal_d='.$d; if(!empty($m)) $paramstr .= '&cal_m='.$m; if(!empty($y)) $paramstr .= '&cal_y='.$y; if(!empty($paramstr)) $paramstr = substr($paramstr, 5); return $linkbase.$paramstr; } function calendar_get_link_tag($text, $linkbase, $d, $m, $y) { $href = calendar_get_link_href($linkbase, $d, $m, $y); if(empty($href)) return $text; return ''.$text.''; } function calendar_gmmktime_check($m, $d, $y, $default = false) { if($default === false) $default = time(); if(!checkdate($m, $d, $y)) { return $default; } else { return gmmktime(0, 0, 0, $m, $d, $y); } } function calendar_mktime_check($m, $d, $y, $default = false) { if($default === false) $default = time(); if(!checkdate($m, $d, $y)) { return $default; } else { return mktime(0, 0, 0, $m, $d, $y); } } function calendar_wday_name($englishname) { return get_string(strtolower($englishname), 'calendar'); } function calendar_days_in_month($month, $year) { return intval(date('t', mktime(0, 0, 0, $month, 1, $year))); } function calendar_get_sideblock_upcoming($courses, $groups, $users, $daysinfuture, $maxevents) { $events = calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents); $content = ''; $lines = count($events); if (!$lines) { return $content; } for ($i = 0; $i < $lines; ++$i) { $content .= '
'.$events[$i]->icon.' '; if (!empty($events[$i]->referer)) { // That's an activity event, so let's provide the hyperlink $content .= $events[$i]->referer; } else { $content .= $events[$i]->name; } $events[$i]->time = str_replace('->', '
->', $events[$i]->time); $content .= '
'.$events[$i]->time.'
'; if ($i < $lines - 1) $content .= '
'; } return $content; } function calendar_add_month($month, $year) { if($month == 12) { return array(1, $year + 1); } else { return array($month + 1, $year); } } function calendar_sub_month($month, $year) { if($month == 1) { return array(12, $year - 1); } else { return array($month - 1, $year); } } function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday) { $eventsbyday = array(); $typesbyday = array(); $durationbyday = array(); if($events === false) { return; } foreach($events as $event) { $startdate = usergetdate($event->timestart); $enddate = usergetdate($event->timestart + $event->timeduration); // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair if(!($startdate['year'] * 13 + $startdate['mon'] >= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] <= $year * 13 + $month)) { // Out of bounds continue; } $eventdaystart = intval($startdate['mday']); if($startdate['mon'] == $month && $startdate['year'] == $year) { // Give the event to its day $eventsbyday[$eventdaystart][] = $event->id; // Mark the day as having such an event if($event->courseid == 1 && $event->groupid == 0) { $typesbyday[$eventdaystart]['startglobal'] = true; } else if($event->courseid > 1 && $event->groupid == 0) { $typesbyday[$eventdaystart]['startcourse'] = true; } else if($event->groupid) { $typesbyday[$eventdaystart]['startgroup'] = true; } else if($event->userid) { $typesbyday[$eventdaystart]['startuser'] = true; } } if($event->timeduration == 0) { // Proceed with the next continue; } // The event starts on $month $year or before. So... $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 1; // Also, it ends on $month $year or later... $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year); // Mark all days between $lowerbound and $upperbound (inclusive) as duration for($i = $lowerbound + 1; $i <= $upperbound; ++$i) { $durationbyday[$i][] = $event->id; if($event->courseid == 1 && $event->groupid == 0) { $typesbyday[$i]['durationglobal'] = true; } else if($event->courseid > 1 && $event->groupid == 0) { $typesbyday[$i]['durationcourse'] = true; } else if($event->groupid) { $typesbyday[$i]['durationgroup'] = true; } else if($event->userid) { $typesbyday[$i]['durationuser'] = true; } } } return; } function calendar_get_module_cached(&$coursecache, $modulename, $instance, $courseid) { $module = get_coursemodule_from_instance($modulename, $instance, $courseid); if($module === false) return false; if(!calendar_get_course_cached($coursecache, $courseid)) { return false; } return $module; } function calendar_get_course_cached(&$coursecache, $courseid) { if(!isset($coursecache[$courseid])) { $coursecache[$courseid] = get_record('course', 'id', $courseid); } return $coursecache[$courseid]; } function calendar_session_vars() { global $SESSION, $USER; if(!isset($SESSION->cal_course_referer)) { $SESSION->cal_course_referer = 0; } if(!isset($SESSION->cal_show_global)) { $SESSION->cal_show_global = true; } if(!isset($SESSION->cal_show_groups)) { $SESSION->cal_show_groups = true; } if(!isset($SESSION->cal_show_course)) { $SESSION->cal_show_course = true; } if(!isset($SESSION->cal_show_user)) { $SESSION->cal_show_user = true; } if(empty($SESSION->cal_courses_shown)) { $SESSION->cal_courses_shown = calendar_get_default_courses(true); } if(empty($SESSION->cal_users_shown)) { // The empty() instead of !isset() here makes a whole world of difference, // as it will automatically change to the user's id when the user first logs // in. With !isset(), it would never do that. $SESSION->cal_users_shown = isset($USER->id) ? $USER->id : false; } else if(is_numeric($SESSION->cal_users_shown) && !empty($USER->id) && $SESSION->cal_users_shown != $USER->id) { // Follow the white rabbit, for example if a teacher logs in as a student $SESSION->cal_users_shown = $USER->id; } } function calendar_overlib_html() { global $CFG; $html = ''; $html .= ''; $html .= ''; return $html; } function calendar_set_referring_course($courseid) { global $SESSION; $SESSION->cal_course_referer = intval($courseid); } function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) { global $SESSION, $USER; // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause // the code to function incorrectly UNLESS we convert it to an integer. One case where // PHP's loose type system works against us. if(is_string($SESSION->cal_courses_shown)) { $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown); } if($courseeventsfrom === NULL) { $courseeventsfrom = $SESSION->cal_courses_shown; } if($groupeventsfrom === NULL) { $groupeventsfrom = $SESSION->cal_courses_shown; } if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) { if(is_int($courseeventsfrom)) { $courses = array(1, $courseeventsfrom); } else if(is_array($courseeventsfrom)) { $courses = array_keys($courseeventsfrom); $courses[] = 1; } } else if($SESSION->cal_show_course) { if(is_int($courseeventsfrom)) { $courses = array($courseeventsfrom); } else if(is_array($courseeventsfrom)) { $courses = array_keys($courseeventsfrom); } $courses = array_diff($courses, array(1)); } else if($SESSION->cal_show_global) { $courses = array(1); } else { $courses = false; } if($SESSION->cal_show_user || $ignorefilters) { // This doesn't work for arrays yet (maybe someday it will) $user = $SESSION->cal_users_shown; } else { $user = false; } if($SESSION->cal_show_groups || $ignorefilters) { if(is_int($groupeventsfrom)) { $groupcourses = array($groupeventsfrom); } else if(is_array($groupeventsfrom)) { $groupcourses = array_keys($groupeventsfrom); } $grouparray = array(); // We already have the courses to examine in $courses // For each course... foreach($groupcourses as $courseid) { // If the user is an editing teacher in there, if(!empty($USER) && isteacheredit($courseid, $USER->id)) { // Show events from all groups if(($grouprecords = get_groups($courseid)) !== false) { $grouparray = array_merge($grouparray, array_keys($grouprecords)); } } // Otherwise show events from the group he is a member of else if(isset($USER->groupmember[$courseid])) { $grouparray[] = $USER->groupmember[$courseid]; } } if(empty($grouparray)) { $group = false; } else { $group = $grouparray; } } else { $group = false; } } function calendar_edit_event_allowed($event) { global $USER; if(empty($USER) || isguest($USER->id)) { return false; } if (isadmin($USER->id)) return true; // Admins are allowed anything if ($event->courseid > 1) { // Course event, only editing teachers may... edit :P if(isteacheredit($event->courseid)) { return true; } } else if($event->courseid == 0 && $event->groupid != 0) { // Group event $group = get_record('groups', 'id', $event->groupid); if($group === false) return false; if(isteacheredit($group->courseid)) { return true; } } else if($event->courseid == 0 && $event->groupid == 0 && $event->userid == $USER->id) { // User event, owned by this user return true; } return false; } function calendar_get_default_courses($ignoreref = false) { global $USER, $CFG, $SESSION; if(!empty($SESSION->cal_course_referer) && !$ignoreref) { return array($SESSION->cal_course_referer => 1); } if(empty($USER)) { return array(); } $courses = array(); if(isadmin($USER->id)) { $courses = get_records_sql('SELECT id, 1 FROM '.$CFG->prefix.'course'); return $courses; } if(isset($USER->student) && is_array($USER->student)) { $courses = $USER->student + $courses; } if(isset($USER->teacher) && is_array($USER->teacher)) { $courses = $USER->teacher + $courses; } return $courses; } function calendar_get_tz_offset() { global $USER, $CFG; static $tzfix; // Caching if(isset($tzfix)) { return $tzfix; } if(empty($USER)) { // Don't forget that there are users which have NOT logged in, even as guests $timezone = $CFG->timezone; } else { // If, on the other hand, we do have a user... $timezone = $USER->timezone; if(abs($timezone > 13)) { // But if the user has specified 'server default' time, // don't get the server's; get the Moodle $CFG setting // (Martin's help text on site cfg implies this) $timezone = $CFG->timezone; } } if(abs($timezone) <= 13) { $tzfix = $timezone * 3600; } else { $tzfix = date('Z'); } return $tzfix; } function calendar_preferences_array() { return array( 'startwday' => get_string('pref_startwday', 'calendar'), 'maxevents' => get_string('pref_maxevents', 'calendar'), 'lookahead' => get_string('pref_lookahead', 'calendar'), 'timeformat' => get_string('pref_timeformat', 'calendar'), ); } function calendar_preferences_button() { global $CFG, $USER; // Guests have no preferences if (empty($USER->id) || isguest()) { return ''; } return "
framename\" method=\"get\" ". " action=\"$CFG->wwwroot/calendar/preferences.php\">". "
"; } if(!function_exists('array_diff_assoc')) { // PHP < 4.3.0 function array_diff_assoc($source, $diff) { $res = $source; foreach ($diff as $key=>$data) { unset($res[$key]); } return $res; } } ?>