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/userweek.php b/blocks/mrbs/web/userweek.php
index 35ebeee..96bfc91 100644
--- a/blocks/mrbs/web/userweek.php
+++ b/blocks/mrbs/web/userweek.php
@@ -181,7 +181,7 @@ for ($j = 0; $j<=($num_of_days-1) ; $j++) {
         # 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.
-
+        mrbs_correct_dst($row[1], $row[2]);
         $start_t = max(round_t_down($row[0], $resolution, $am7[$j]), $am7[$j]);
         $end_t = min(round_t_up($row[1], $resolution, $am7[$j]) - $resolution, $pm7[$j]);
 
diff --git a/blocks/mrbs/web/week.php b/blocks/mrbs/web/week.php
index 2617e9d..5de8ca4 100644
--- a/blocks/mrbs/web/week.php
+++ b/blocks/mrbs/web/week.php
@@ -235,7 +235,7 @@ for ($j = 0; $j<=($num_of_days-1) ; $j++) {
         # 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.
-
+        mrbs_correct_dst($row[1], $row[2]);
         $start_t = max(round_t_down($row[0], $resolution, $am7[$j]), $am7[$j]);
         $end_t = min(round_t_up($row[1], $resolution, $am7[$j]) - $resolution, $pm7[$j]);
 
