commit 174e8b6b9c177bbe442bd3da24ae5462fc3f3a28
Author: root <root@srv-xen-moodle08.tauntons.ac.uk>
Date:   Mon Oct 5 10:41:22 2009 +0100

    Fixed CONTRIB-1152 where bookings after a DST change aren't displayed when viewing them before the change.

diff --git a/blocks/mrbs/web/day.php b/blocks/mrbs/web/day.php
index 9e57182..63a2e3a 100644
--- a/blocks/mrbs/web/day.php
+++ b/blocks/mrbs/web/day.php
@@ -10,7 +10,7 @@ include "mincals.php";
 
 $day = optional_param('day', 0, PARAM_INT);
 $month = optional_param('month', 0, PARAM_INT);
-$year = optional_param('year', 0, PARAM_INT); 
+$year = optional_param('year', 0, PARAM_INT);
 $id = optional_param('id', 0, PARAM_INT);
 $area = optional_param('area', get_default_area(),  PARAM_INT);
 $room = optional_param('room', 0, PARAM_INT);
@@ -34,7 +34,7 @@ add_to_log($course->id, "mrbs", "view all", "day.php?id=$course->id", "");
 
 if (empty($debug_flag)) $debug_flag = 0;
 
-#If we dont know the right date then make it up 
+#If we dont know the right date then make it up
 if (($day==0) or ($month==0) or ($year==0))
 {
 	$day   = date("d");
@@ -72,8 +72,11 @@ if( $enable_periods ) {
 #  0 => entering DST
 #  1 => leaving DST
 $dst_change = is_dst($month,$day,$year);
+
 $am7=mktime($morningstarts,$morningstarts_minutes,0,$month,$day,$year,is_dst($month,$day,$year,$morningstarts));
 $pm7=mktime($eveningends,$eveningends_minutes,0,$month,$day,$year,is_dst($month,$day,$year,$eveningends));
+$am7 = mrbs_correct_dst($am7);
+$pm7 = mrbs_correct_dst($pm7);
 
 if ( $pview != 1 ) {
    echo "<table width=\"100%\"><tr><td width=\"40%\">";
@@ -89,7 +92,7 @@ if ( $pview != 1 ) {
 	# show the standard html list
 	$sql = "select id, area_name from $tbl_area order by area_name";
    	$res = sql_query($sql);
-    
+
    	if ($res) for ($i = 0; ($row = sql_row($res, $i)); $i++)
    	{
 		echo "<a href=\"day.php?year=$year&month=$month&day=$day&area=$row[0]\">";
@@ -108,7 +111,7 @@ if ( $pview != 1 ) {
             <input type='hidden' name='month' value='$month'>
             <input type='hidden' name='year' value='$year'>
             <input type='submit' value='Go'>
-        </form></td>"; 
+        </form></td>";
 
    #Draw the three month calendars
    minicals($year, $month, $day, $area, '', 'day');
@@ -129,7 +132,7 @@ $tm = date("m",$i);
 $td = date("d",$i);
 
 #We want to build an array containing all the data we want to show
-#and then spit it out. 
+#and then spit it out.
 
 #Get all appointments for today in the area that we care about
 #Note: The predicate clause 'start_time <= ...' is an equivalent but simpler
@@ -158,6 +161,7 @@ if (!empty($area)) {
     $res = sql_query($sql);
     if (!$res) fatal_error(0, sql_error());
     for ($i = 0; ($row = sql_row($res, $i)); $i++) { //Each row we have is an appointment.
+
         #Row[0] = Room ID
 	    #row[1] = start time
 	    #row[2] = end time
@@ -182,13 +186,14 @@ if (!empty($area)) {
 	   # Note: int casts on database rows for max may be needed for PHP3.
 	   # Adjust the starting and ending times so that bookings which don't
 	   # start or end at a recognized time still appear.
+
 	   $start_t = max(round_t_down($row[1], $resolution, $am7), $am7);
 	   $end_t = min(round_t_up($row[2], $resolution, $am7) - $resolution, $pm7);
 	   for ($t = $start_t; $t <= $end_t; $t += $resolution)
 	   {
           //checks for double bookings
           if(empty($today[$row[0]][date($format,$t)])){
-          
+
 		  $today[$row[0]][date($format,$t)]["id"]    = $row[4];
 		  $today[$row[0]][date($format,$t)]["color"] = $row[5];
               $today[$row[0]][date($format,$t)]["data"]  .= "";
@@ -217,7 +222,7 @@ if (!empty($area)) {
     }
 
 
-    if ($debug_flag) 
+    if ($debug_flag)
     {
 	   echo "<p>DEBUG:<pre>\n";
 	   echo "\$dst_change = $dst_change\n";
@@ -296,7 +301,7 @@ if (!empty($area)) {
             ."</th>";
         }
         echo "</tr>\n";
-  
+
 	   # URL for highlighting a time. Don't use REQUEST_URI or you will get
 	   # the timetohighlight parameter duplicated each time you click.
 	   $hilite_url="day.php?year=$year&month=$month&day=$day&area=$area&timetohighlight";
@@ -308,7 +313,7 @@ if (!empty($area)) {
 	   # the day after to generate timesteps through the day as this
 	   # will ensure a constant time step
 	   ( $dst_change != -1 ) ? $j = 1 : $j = 0;
-	
+
 	   $row_class = "even_row";
 	   for (
 		  $t = mktime($morningstarts, $morningstarts_minutes, 0, $month, $day+$j, $year);
@@ -429,7 +434,7 @@ if (!empty($area)) {
 		        echo "<a href=\"$hilite_url=$time_t\" title=\""
                 . get_string('highlight_line','block_mrbs') . "\">"
                 . userdate($t,hour_min_format()) . "</a></td>\n";
-                
+
             }
         }
 
diff --git a/blocks/mrbs/web/edit_entry_handler.php b/blocks/mrbs/web/edit_entry_handler.php
index 8a071ca..bae6217 100644
--- a/blocks/mrbs/web/edit_entry_handler.php
+++ b/blocks/mrbs/web/edit_entry_handler.php
@@ -123,6 +123,8 @@ if(isset($all_day) && ($all_day == "yes"))
         ($eveningends_minutes > 59) ? $end_minutes += 60 : '';
         $endtime   = mktime($eveningends, $end_minutes, 0, $month, $day, $year, is_dst($month, $day, $year));
     }
+    $starttime = mrbs_correct_dst($starttime);
+    $endtime = mrbs_correct_dst($endtime);
 }
 else
 {
@@ -147,7 +149,12 @@ else
     if (($tmp = $diff % $resolution) != 0 || $diff == 0)
         $endtime += $resolution - $tmp;
 
-    $endtime += cross_dst( $starttime, $endtime );
+    if(cross_dst($starttime, $endtime) > 0){
+        $endtime += cross_dst( $starttime, $endtime );
+    } else {
+        $starttime = mrbs_correct_dst($starttime);
+        $endtime = mrbs_correct_dst($endtime);
+    }
 }
 
 $room_nos = mysql_fetch_Array(mysql_query("select room_name from mdl_mrbs_room where id = $rooms[0]"));
diff --git a/blocks/mrbs/web/functions.php b/blocks/mrbs/web/functions.php
index 82018ba..194e877 100644
--- a/blocks/mrbs/web/functions.php
+++ b/blocks/mrbs/web/functions.php
@@ -1388,4 +1388,22 @@ function to_hr_time($time){
     }
 }
 
+/**
+ * Check if a the time is in DST when the current time is not, or vice versa,
+ * and make appropriate corrections to ensure it shows up when being viewed booked or
+ * viewed in the other timezone.
+ *
+ * @param int $time     Unix timestamp
+ *
+ */
+function mrbs_correct_dst($time) {
+	if(date('I', time()) == 1 && date('I', $time) == 0) {
+        // if we're making a booking rather than viewing one, reverse the change.
+            $time = $time-3600;
+	} else if (date('I', time()) == 0 && date('I', $time) == 1) {
+            $time = $time+3600;
+	}
+    return $time;
+}
+
 ?>

 
diff --git a/blocks/mrbs/web/month.php b/blocks/mrbs/web/month.php
index febef60..4bce3ce 100644
--- a/blocks/mrbs/web/month.php
+++ b/blocks/mrbs/web/month.php
@@ -15,7 +15,7 @@ if ($CFG->forcelogin) {
     }
 $day = optional_param('day', 1, PARAM_INT);
 $month = optional_param('month', 0, PARAM_INT);
-$year = optional_param('year', 0, PARAM_INT); 
+$year = optional_param('year', 0, PARAM_INT);
 $id = optional_param('id', 0, PARAM_INT);
 $area = optional_param('area', 0,  PARAM_INT);
 $room = optional_param('room', 0, PARAM_INT);
@@ -85,6 +85,9 @@ for ($j = 1; $j<=$days_in_month; $j++) {
 		$midnight[$j]=mktime(12,0,0,$month,$j,$year, is_dst($month,$j,$year, 0));
 		$midnight_tonight[$j]=mktime(12,count($periods),59,$month,$j,$year, is_dst($month,$j,$year, 23));
         }
+
+    $midnight[$j] = mrbs_correct_dst($midnight[$j]);
+    $midnight_tonight[$j] = mrbs_correct_dst($midnight_tonight[$j]);
 }
 
 if ( $pview != 1 ) {
@@ -121,7 +124,7 @@ if ( $pview != 1 ) {
 
 if ( $pview != 1 ) {
     echo "</td>\n";
-    
+
     # Show all rooms in the current area:
     echo "<td width=\"30%\"><u>".get_string('rooms','block_mrbs')."</u><br>";
 }
@@ -148,7 +151,7 @@ if ( $pview != 1 ) {
 
 if ( $pview != 1 ) {
     echo "</td>\n";
-    
+
     #Draw the three month calendars
     minicals($year, $month, $day, $area, $room, 'month');
     echo "</tr></table>\n";
@@ -310,7 +313,7 @@ if ($debug_flag)
     echo "</pre>\n";
 }
 
-// Include the active cell content management routines. 
+// Include the active cell content management routines.
 // Must be included before the beginnning of the main table.
 if ($javascript_cursor) // If authorized in config.inc.php, include the javascript cursor management.
     {
diff --git a/blocks/mrbs/web/userweek.php b/blocks/mrbs/web/userweek.php
index 96bfc91..3b84108 100644
--- a/blocks/mrbs/web/userweek.php
+++ b/blocks/mrbs/web/userweek.php
@@ -82,6 +82,8 @@ for ($j = 0; $j<=($num_of_days-1); $j++) {
     $dst_change[$j] = is_dst($month,$day+$j,$year);
     $am7[$j]=mktime($morningstarts,$morningstarts_minutes,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$morningstarts));
     $pm7[$j]=mktime($eveningends,$eveningends_minutes,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$eveningends));
+    $am7[$j] = mrbs_correct_dst($am7[$j]);
+    $pm7[$j] = mrbs_correct_dst($pm7[$j]);
 }
 
 if ( $pview != 1 ) {
diff --git a/blocks/mrbs/web/week.php b/blocks/mrbs/web/week.php
index 5de8ca4..d24646f 100644
--- a/blocks/mrbs/web/week.php
+++ b/blocks/mrbs/web/week.php
@@ -85,6 +85,8 @@ for ($j = 0; $j<=($num_of_days-1); $j++) {
     $dst_change[$j] = is_dst($month,$day+$j,$year);
     $am7[$j]=mktime($morningstarts,$morningstarts_minutes,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$morningstarts));
     $pm7[$j]=mktime($eveningends,$eveningends_minutes,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$eveningends));
+    $am7[$j] = mrbs_correct_dst($am7[$j]);
+    $pm7[$j] = mrbs_correct_dst($pm7[$j]);
 }
 

