From b4892fa2a0c82def298a682f050420082305aaf5 Mon Sep 17 00:00:00 2001 From: dwoolhead Date: Tue, 12 Jun 2007 16:26:38 +0000 Subject: [PATCH] MDL-10048 General calendar view enhancements as per spec with regard to event highlighting --- calendar/lib.php | 239 ++++++++++++++++++++++++++++++-------- calendar/view.php | 113 ++++++++---------- lang/en_utf8/calendar.php | 1 + 3 files changed, 236 insertions(+), 117 deletions(-) diff --git a/calendar/lib.php b/calendar/lib.php index bd166dd354..f94f3618a5 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -148,6 +148,15 @@ function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_y $events = get_records_select('event', $whereclause, 'timestart'); } + // Set event course class for course events + if (!empty($events)) { + foreach ($events as $event) { + if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { + $event->class = 'event_course'.array_search($event->courseid, $courses) % 3; + } + } + } + // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after // possibly removing SITEID 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 @@ -283,6 +292,21 @@ function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_y $class .= ' duration_user'; } + // If event has a class set then add it to the table day tag + // Note: only one colour for minicalendar + if(isset($eventsbyday[$day])) { + foreach($eventsbyday[$day] as $eventid) { + if (!isset($events[$eventid])) { + continue; + } + $event = $events[$eventid]; + if (!empty($event->class)) { + $class .= ' '.$event->class; + } + break; + } + } + // Special visual fx for today //Accessibility: hidden text for today, and popup. if($display->thismonth && $day == $d) { @@ -481,7 +505,14 @@ function calendar_print_event($event) { echo ''; echo ' '; - echo ''; + + // If event has a class set then add it to the event tag + $eventclass=''; + if (!empty($event->class)) { + $eventclass = ' '.$event->class; + } + + echo ''; echo format_text($event->description, FORMAT_HTML); if (calendar_edit_event_allowed($event)) { echo '
'; @@ -580,7 +611,7 @@ function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withdura } if($withduration) { - $timeclause = 'timestart + timeduration >= '.$tstart.' AND timestart <= '.$tend; + $timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend; } else { $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend; @@ -689,7 +720,7 @@ function calendar_top_controls($type, $data) { return $content; } -function calendar_filter_controls($type, $vars = NULL, $course = NULL) { +function calendar_filter_controls($type, $vars = NULL, $course = NULL, $courses = NULL) { global $CFG, $SESSION, $USER; $groupevents = true; @@ -725,51 +756,113 @@ function calendar_filter_controls($type, $vars = NULL, $course = NULL) { $content .= ''; if($SESSION->cal_show_global) { $content .= ''.get_string('hide').''; - $content .= ''.get_string('globalevents', 'calendar').''."\n"; + $content .= ''.get_string('global', 'calendar').''."\n"; } else { $content .= ''.get_string('show').''; - $content .= ''.get_string('globalevents', 'calendar').''."\n"; - } - if($SESSION->cal_show_course) { - $content .= ''.get_string('hide').''; - $content .= ''.get_string('courseevents', 'calendar').''."\n"; - } - else { - $content .= ''.get_string('hide').''; - $content .= ''.get_string('courseevents', 'calendar').''."\n"; + $content .= ''.get_string('global', 'calendar').''."\n"; } + $tr = ''; if(!empty($USER->id) && !isguest()) { - $content .= "\n"; - if($groupevents) { + + $content .= $tr; + $tr = $tr ? '' : "\n"; + // This course MIGHT have group events defined, so show the filter if($SESSION->cal_show_groups) { $content .= ''.get_string('hide').''; - $content .= ''.get_string('groupevents', 'calendar').''."\n"; + $content .= ''.get_string('group', 'calendar').''."\n"; } else { $content .= ''.get_string('show').''; - $content .= ''.get_string('groupevents', 'calendar').''."\n"; + $content .= ''.get_string('group', 'calendar').''."\n"; } + + $content .= $tr; + $tr = $tr ? '' : "\n"; + if ($SESSION->cal_show_user) { $content .= ''.get_string('hide').''; - $content .= ''.get_string('userevents', 'calendar').''."\n"; + $content .= ''.get_string('user', 'calendar').''."\n"; } else { $content .= ''.get_string('show').''; - $content .= ''.get_string('userevents', 'calendar').''."\n"; + $content .= ''.get_string('user', 'calendar').''."\n"; } } else { + + $content .= $tr; + $tr = $tr ? '' : "\n"; + // This course CANNOT have group events, so lose the filter $content .= ' '."\n"; + $content .= $tr; + $tr = $tr ? '' : "\n"; + if($SESSION->cal_show_user) { $content .= ''.get_string('hide').''; - $content .= ''.get_string('userevents', 'calendar').''."\n"; + $content .= ''.get_string('user', 'calendar').''."\n"; } else { $content .= ''.get_string('show').''; - $content .= ''.get_string('userevents', 'calendar').''."\n"; + $content .= ''.get_string('user', 'calendar').''."\n"; + } + } + } + + // Remove global SITE ID from courses array as do not want to display this + if (!empty($courses)) { + $key = array_search(SITEID, $courses); + if ($key !== false) { + unset($courses[$key]); + } + } + + if (empty($courses) || count($courses) == 1) { + + // If not multiple courses then just display default single course colour highlighting + $content .= $tr; + $tr = $tr ? '' : "\n"; + + if($SESSION->cal_show_course) { + $content .= ''.get_string('hide').''; + $content .= ''.get_string('course', 'calendar').''."\n"; + } + else { + $content .= ''.get_string('hide').''; + $content .= ''.get_string('course', 'calendar').''."\n"; + } + } else { + + // Otherwise display list of course shortnames and relevant colours + // Get list of course shortnames (Limit to 12 for now - who would have more than that?) + $select = 'id in ('.implode(',', $courses).')'; + $sort = 'id'; + $fields = 'id, shortname'; + $courseshortnames = get_records_select('course', $select, $sort, $fields, 0, 12); + + for ($i = 0; $i < 3; $i++) { + + // Concatenate shortnames if there are more than 3 courses + $strshortnames = ''; + for ($j = $i; $j < count($courses); $j += 3) { + $strshortnames .= ', '.(!empty($courseshortnames[$courses[$j]]->shortname) ? $courseshortnames[$courses[$j]]->shortname : $courses[$j]); + } + + if (!empty($strshortnames)) { + + $content .= $tr; + $tr = $tr ? '' : "\n"; + + if($SESSION->cal_show_course) { + $content .= ''.get_string('hide').''; + $content .= ''.substr($strshortnames, 2).''."\n"; + } + else { + $content .= ''.get_string('hide').''; + $content .= ''.substr($strshortnames, 2).''."\n"; + } } } } @@ -948,10 +1041,18 @@ function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$duratio return; } + // Define array of course id's + $courseids = array(); + foreach($events as $event) { $startdate = usergetdate($event->timestart); - $enddate = usergetdate($event->timestart + $event->timeduration); + // Set end date = start date if no duration + if ($event->timeduration) { + $enddate = usergetdate($event->timestart + $event->timeduration - 1); + } else { + $enddate = $startdate; + } // 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)) { @@ -968,15 +1069,26 @@ function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$duratio // Mark the day as having such an event if($event->courseid == SITEID && $event->groupid == 0) { $typesbyday[$eventdaystart]['startglobal'] = true; + // Set event class for global event + $events[$event->id]->class = 'event_global'; } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { $typesbyday[$eventdaystart]['startcourse'] = true; + // Set event class for course event + if (!in_array($event->courseid, $courseids)) { + $courseids[] = $event->courseid; + } + $events[$event->id]->class = 'event_course'.array_search($event->courseid, $courseids) % 3; } else if($event->groupid) { $typesbyday[$eventdaystart]['startgroup'] = true; + // Set event class for group event + $events[$event->id]->class = 'event_group'; } else if($event->userid) { $typesbyday[$eventdaystart]['startuser'] = true; + // Set event class for user event + $events[$event->id]->class = 'event_user'; } } @@ -1066,9 +1178,11 @@ function calendar_session_vars() { if(!isset($SESSION->cal_show_user)) { $SESSION->cal_show_user = true; } - // if(empty($SESSION->cal_courses_shown)) { + // Version 1.76 to version 1.77 change, which commented out following if + // statement prevented individual course from being selected! Investigate + 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 @@ -1294,7 +1408,7 @@ function calendar_preferences_button() { "
"; } -function calendar_format_event_time($event, $now, $morehref, $usecommonwords = true) { +function calendar_format_event_time($event, $now, $morehref, $usecommonwords = true, $showtime=0) { $startdate = usergetdate($event->timestart); $enddate = usergetdate($event->timestart + $event->timeduration); $usermidnightstart = usergetmidnight($event->timestart); @@ -1310,47 +1424,70 @@ function calendar_format_event_time($event, $now, $morehref, $usecommonwords = t // 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, $usecommonwords); - $time = calendar_time_representation($event->timestart + $event->timeduration); - - // This var always has the printable time representation - $eventtime = ''.$day.' ('.$time.')'; - - } - else if($event->timeduration) { + if($event->timeduration) { // It has a duration - if($usermidnightstart == $usermidnightend) { + if($usermidnightstart == $usermidnightend || + ($event->timestart == $usermidnightstart) && ($event->timeduration == 86400 || $event->timeduration == 86399) || + ($event->timestart + $event->timeduration <= $usermidnightstart + 86400)) { // But it's all on the same day - $day = calendar_day_representation($event->timestart, $now, $usecommonwords); $timestart = calendar_time_representation($event->timestart); $timeend = calendar_time_representation($event->timestart + $event->timeduration); + $time = $timestart.' » '.$timeend; + + if ($event->timestart == $usermidnightstart && ($event->timeduration == 86400 || $event->timeduration == 86399)) { + $time = get_string('allday', 'calendar'); + } // 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 { + if (!$showtime) { + $day = calendar_day_representation($event->timestart, $now, $usecommonwords); + $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']).', '.$time; + } else { + $eventtime = $time; + } + } else { // It spans two or more days - $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords); - $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords); + $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords).', '; + if ($showtime == $usermidnightstart) { + $daystart = ''; + } $timestart = calendar_time_representation($event->timestart); + $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords).', '; + if ($showtime == $usermidnightend) { + $dayend = ''; + } $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.')'; + if ($now >= $usermidnightstart && $now < ($usermidnightstart + 86400)) { + $eventtime = $timestart.' » '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']). + $timeend; + } else { + $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 { + } else { // It's an "instantaneous" event - $day = calendar_day_representation($event->timestart, $now, $usecommonwords); - $time = calendar_time_representation($event->timestart); + if ($event->timestart != usergetmidnight($event->timestart)) { + $time = ', '.calendar_time_representation($event->timestart); + } else { + $time = ' '; + } // Set printable representation - $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $startdate['mday'], $startdate['mon'], $startdate['year']).' ('.$time.')'; + if (!$showtime) { + $day = calendar_day_representation($event->timestart, $now, $usecommonwords); + $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $startdate['mday'], $startdate['mon'], $startdate['year']).trim($time); + } else { + $eventtime = $time; + } + } + + if($event->timestart + $event->timeduration < $now) { + // It has expired + $eventtime = ''.str_replace(' href=', ' class="dimmed" href=', $eventtime).''; } return $eventtime; diff --git a/calendar/view.php b/calendar/view.php index 6b6cc440ab..01e465a53f 100644 --- a/calendar/view.php +++ b/calendar/view.php @@ -117,6 +117,18 @@ calendar_set_filters($courses, $groups, $users); } + // Sort courses for consistent colour highlighting + // Effectively ignoring SITEID as setting as last course id + // Consider inside calendar_set_filters() and SITEID always last + $key = array_search(SITEID, $courses); + if ($key !== false) { + unset($courses[$key]); + sort($courses); + $courses[] = SITEID; + } else { + sort($courses); + } + // Let's see if we are supposed to provide a referring course link // but NOT for the "main page" course if ($SESSION->cal_course_referer != SITEID && @@ -273,7 +285,13 @@ function calendar_show_day($d, $m, $y, $courses, $groups, $users) { echo calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&', $enddate['mday'], $enddate['mon'], $enddate['year']).' ('.$timeend.')'; */ //unset($event->time); - $event->time = calendar_format_event_time($event, time(), '', false); + + // Set event course class if a course event + if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { + $event->class = 'event_course'.array_search($event->courseid, $courses) % 3; + } + + $event->time = calendar_format_event_time($event, time(), '', false, $starttime); calendar_print_event($event); } else { // Save this for later @@ -285,7 +303,7 @@ function calendar_show_day($d, $m, $y, $courses, $groups, $users) { if (!empty($underway)) { echo '

'.get_string('spanningevents', 'calendar').':

'; foreach ($underway as $event) { - $event->time = calendar_format_event_time($event, time(), '', false); + $event->time = calendar_format_event_time($event, time(), '', false, $starttime); calendar_print_event($event); } } @@ -400,7 +418,7 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users) { // Paddding (the first week may have blank days in the beginning) for($i = $display->minwday; $i < $startwday; ++$i) { - echo ' '."\n"; + echo ' '."\n"; } // Now display all the calendar @@ -427,18 +445,6 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users) { // Special visual fx if an event is defined if(isset($eventsbyday[$day])) { - if(isset($typesbyday[$day]['startglobal'])) { - $class .= ' event_global'; - } - else if(isset($typesbyday[$day]['startcourse'])) { - $class .= ' event_course'; - } - else if(isset($typesbyday[$day]['startgroup'])) { - $class .= ' event_group'; - } - else if(isset($typesbyday[$day]['startuser'])) { - $class .= ' event_user'; - } if(count($eventsbyday[$day]) == 1) { $title = get_string('oneevent', 'calendar'); } @@ -468,6 +474,8 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users) { // Special visual fx for today if($display->thismonth && $day == $d) { $class .= ' today'; + } else { + $class .= ' nottoday'; } // Just display it @@ -479,7 +487,14 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users) { if(isset($eventsbyday[$day])) { echo ''; } @@ -495,61 +510,12 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users) { // Paddding (the last week may have blank days at the end) for($i = $dayweek; $i <= $display->maxwday; ++$i) { - echo ' '; + echo ' '; } echo "\n"; // Last row ends echo "\n"; // Tabular display of days ends - // OK, now for the filtering display - echo '
'; - - // Global events - if($SESSION->cal_show_global) { - echo ''."\n"; - } - else { - echo ''."\n"; - } - - // Course events - if(!empty($SESSION->cal_show_course)) { - echo ''."\n"; - } - else { - echo ''."\n"; - } - - echo "\n"; - - if(!empty($USER->id) && !isguest()) { - echo ''; - // Group events - if($SESSION->cal_show_groups) { - echo ''."\n"; - } - else { - echo ''."\n"; - } - // User events - if($SESSION->cal_show_user) { - echo ''."\n"; - } - else { - echo ''."\n"; - } - echo "\n"; - } - - echo '
'.get_string('globalevents', 'calendar').': '; - echo get_string('shown', 'calendar').' ('.get_string('clickhide', 'calendar').')'.get_string('globalevents', 'calendar').': '; - echo get_string('hidden', 'calendar').' ('.get_string('clickshow', 'calendar').')'.get_string('courseevents', 'calendar').': '; - echo get_string('shown', 'calendar').' ('.get_string('clickhide', 'calendar').')'.get_string('courseevents', 'calendar').': '; - echo get_string('hidden', 'calendar').' ('.get_string('clickshow', 'calendar').')
'.get_string('groupevents', 'calendar').': '; - echo get_string('shown', 'calendar').' ('.get_string('clickhide', 'calendar').')'.get_string('groupevents', 'calendar').': '; - echo get_string('hidden', 'calendar').' ('.get_string('clickshow', 'calendar').')'.get_string('userevents', 'calendar').': '; - echo get_string('shown', 'calendar').' ('.get_string('clickhide', 'calendar').')'.get_string('userevents', 'calendar').': '; - echo get_string('hidden', 'calendar').' ('.get_string('clickshow', 'calendar').')
'; } function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $maxevents) { @@ -577,8 +543,23 @@ function calendar_show_upcoming_events($courses, $groups, $users, $futuredays, $ echo '
'.$text.'
'; if ($events) { + + // Sort courses for consistent colour highlighting (ignoring SITEID) + $courseids = $courses; + $key = array_search(SITEID, $courseids); + if ($key !== false) { + unset($courseids[$key]); + } + sort($courseids); + echo '
'; foreach ($events as $event) { + + // Set event course class if a course event + if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { + $event->class = 'event_course'.array_search($event->courseid, $courseids) % 3; + } + calendar_print_event($event); } echo '
'; diff --git a/lang/en_utf8/calendar.php b/lang/en_utf8/calendar.php index eb9679c399..2518b0f58b 100644 --- a/lang/en_utf8/calendar.php +++ b/lang/en_utf8/calendar.php @@ -1,6 +1,7 @@