commit 6b9310532b2b36aac24b2a2227bd49085d95f4da
Author: root <root@srv-xen-moodle08.tauntons.ac.uk>
Date:   Wed Jun 17 09:58:40 2009 +0100

    Changed import script to prevent multiple email being sent when a clash is detected. Also edited lang string for e-mail body to inform user it's their only warning.

diff --git a/blocks/mrbs/import.php b/blocks/mrbs/import.php
index 13a054a..4aa4577 100644
--- a/blocks/mrbs/import.php
+++ b/blocks/mrbs/import.php
@@ -21,8 +21,8 @@ if(file_exists($cfg_mrbs->cronfile)){
 
 
 
-        //wipe old mrbs bookings
-        delete_records_select('mrbs_entry', 'type=\'K\' and start_time > unix_timestamp()');
+        // Change old imported (type K) records to temporary type M
+        set_field_select('mrbs_entry', 'type', 'M', 'type=\'K\' and start_time > unix_timestamp()');
 
         //import timetable into mrbs
         $now = time();
@@ -50,6 +50,7 @@ if(file_exists($cfg_mrbs->cronfile)){
                     //only timetable class if it isn't already timetabled elsewhere (class been moved)
                     if(!is_timetabled($csvrow->name,$start_time)){
 
+
                             $entry->start_time=$start_time;
                             $entry->end_time=$end_time;
                             $entry->room_id=$room;
@@ -60,17 +61,17 @@ if(file_exists($cfg_mrbs->cronfile)){
                             $entry->description=$csvrow->description;
 
                             $newentryid=insert_record('mrbs_entry',$entry);
-                            $newclass=get_record('mrbs_entry','id',$newentryid);
+
 
                         //If there is another non-imported booking there, send emails. It is assumed that simultanious imported classes are intentional
 
                         $sql = "SELECT *
                                 FROM {$CFG->prefix}mrbs_entry
                                 WHERE
-                                    (({$CFG->prefix}mrbs_entry.start_time<$newclass->start_time AND {$CFG->prefix}mrbs_entry.end_time>$newclass->start_time)
-                                  OR ({$CFG->prefix}mrbs_entry.start_time<$newclass->end_time AND {$CFG->prefix}mrbs_entry.end_time>$newclass->end_time)
-                                  OR ({$CFG->prefix}mrbs_entry.start_time>=$newclass->start_time AND {$CFG->prefix}mrbs_entry.end_time<=$newclass->end_time ))
-                                AND mdl_mrbs_entry.room_id = $room AND {$CFG->prefix}mrbs_entry.id<>$newentryid AND type<>'K'";
+                                    (({$CFG->prefix}mrbs_entry.start_time<$start_time AND {$CFG->prefix}mrbs_entry.end_time>$start_time)
+                                  OR ({$CFG->prefix}mrbs_entry.start_time<$end_time AND {$CFG->prefix}mrbs_entry.end_time>$end_time)
+                                  OR ({$CFG->prefix}mrbs_entry.start_time>=$start_time AND {$CFG->prefix}mrbs_entry.end_time<=$end_time ))
+                                AND mdl_mrbs_entry.room_id = $room AND type<>'K'";
 
 
                         //limit to 1 to keep this simpler- if there is a 3-way clash it will be noticed by one of the 2 teachers notified
@@ -78,19 +79,19 @@ if(file_exists($cfg_mrbs->cronfile)){
                             $hr_start_time=date("j F, Y",$start_time) . ", " . to_hr_time($start_time);
                             $a = new object;
                             $a->oldbooking=$existingclass->description.'('.$existingclass->id.')';
-                            $a->newbooking=$newclass->description.'('.$newclass->id.')';
+                            $a->newbooking=$csvrow->description.'('.$newentryid.')';
                             $a->time=$hr_start_time;
                             $a->room=$csvrow->room_name;
                             $a->admin=$cfg_mrbs->admin.' ('.$cfg_mrbs->admin_email.')';
                             $output.= get_string('clash','block_mrbs',$a);
 
                             $existingteacher=get_record('user','username',$existingclass->create_by);
-                            $newteacher=get_record('user','username',$newclass->create_by);
+                            $newteacher=get_record('user','username',$csvrow->username);
 
                             $body = get_string('clashemailbody','block_mrbs',$a);
 
                             if(email_to_user($existingteacher,$newteacher,get_string('clashemailsub','block_mrbs',$a),$body)){$output.=', '.get_string('clashemailsent','block_mrbs').' '.$existingteacher->firstname.' '.$existingteacher->lastname.'<'.$existingteacher->email.'>';}else{$output.=get_string('clashemailnotsent','block_mrbs').$existingclass->description.'('.$existingclass->id.')';}
-                            if(email_to_user($newteacher,$existingteacher,get_string('clashemailsub','block_mrbs',$a),$body)){$output.=', '.get_string('clashemailsent','block_mrbs').' '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>';} else{$output.=get_string('clashemailnotsent','block_mrbs').$newclass->description.'('.$newclass->id.')';}
+                            if(email_to_user($newteacher,$existingteacher,get_string('clashemailsub','block_mrbs',$a),$body)){$output.=', '.get_string('clashemailsent','block_mrbs').' '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>';} else{$output.=get_string('clashemailnotsent','block_mrbs').$csvrow->description.'('.$newentryid.')';}
                             $output.="\n";
                         }
                     }
@@ -104,6 +105,9 @@ if(file_exists($cfg_mrbs->cronfile)){
             }
         }
 
+        // any remaining type M records are no longer in the import file, so delete
+        delete_records_select('mrbs_entry', 'type=\'M\'');
+
         //move the processed file to prevent wasted time re-processing TODO: option for how long to keep these- I've found them useful for debugging but obviously can't keep them for ever
         $date=date('Ymd');
         if(rename($cfg_mrbs->cronfile,$cfg_mrbs->cronfile.'.'.$date)){
@@ -135,12 +139,29 @@ function room_id_lookup($name){
     }
 }
 
-//checks if the class is timetabled into a lesson at the time specified TODO: not sure the best way to work out if a booking that has been edited is the same one or not.
-function is_timetabled($class,$time){
-    if(!get_record('mrbs_entry','name',$class,'start_time',$time)){
-        return FALSE;
-    }else{
-        return TRUE;
+/**
+  * Checks if a class already has a timetable entry. If a previous imported entry exists,
+  * and was edited, leave it. If it wasn't edited (flagged by type M), change it's type back to
+  * K (to show it's an imported record), and return true. If there's no record for the class, or
+  * updating the type back to K fails, return false.
+  *
+  * @param $name string name of the booking
+  * @param $time int start time of the booking in unix timestamp format
+  * @return bool does a previous booking exist?
+  */
+function is_timetabled($name,$time){
+    global $CFG;
+    if(get_record_sql('select * from '.$CFG->prefix.'mrbs_entry where name=\''.$name.'\' and start_time = '.$time.' and type=\'L\'', true)){
+        return true;
+    }else if($record = get_record_sql('select * from '.$CFG->prefix.'mrbs_entry where name=\''.$name.'\' and start_time = '.$time.' and type=\'M\'', true)){
+        $record->type = 'K';
+        if(update_record('mrbs_entry', $record)) {
+            return true;
+        } else {
+           return false;
+        }
+    } else {
+        return false;
     }
 }
 
diff --git a/blocks/mrbs/lang/en_utf8/block_mrbs.php b/blocks/mrbs/lang/en_utf8/block_mrbs.php
index 37b05d4..1bfab24 100644
--- a/blocks/mrbs/lang/en_utf8/block_mrbs.php
+++ b/blocks/mrbs/lang/en_utf8/block_mrbs.php
@@ -28,7 +28,7 @@ $string['browserlang']        = 'Your browser is set to use';
 $string['capacity']           = 'Capacity';
 $string['charset']            = 'UTF-8';
 $string['clash']            = 'Clash: $a->oldbooking $a->newbooking $a->time in Room: $a->room';
-$string['clashemailbody']            = 'Due to a recent timetable import, there is a clash involving one of your bookings:\n On $a->time, $a->oldbooking and $a->newbooking are both booked into room $a->room\nPlease resolve this between yourselves in advance to avoid unnecessary disruption.\n\n\nThis message has been sent automatically by the online room booking system, if you think you have recieved this in error please contact $a->admin';
+$string['clashemailbody']            = 'Due to a recent timetable import, there is a clash involving one of your bookings:\n On $a->time, $a->oldbooking and $a->newbooking are both booked into room $a->room\nPlease resolve this between yourselves in advance to avoid unnecessary disruption.\n\nTHIS WILL BE YOUR ONLY WARNING OF THIS CLASH. IF YOU IGNORE THIS E-MAIL YOU WILL HAVE PROBLEMS.\n\nThis message has been sent automatically by the online room booking system, if you think you have recieved this in error please contact $a->admin';
 $string['clashemailnotsent']            = 'COULDN\'T SEND EMAIL TO TEACHER OF ';
 $string['clashemailsent']            = 'email sent to';
 $string['clashemailsub']            = 'Room Clash Alert';
diff --git a/blocks/mrbs/web/config.inc.php b/blocks/mrbs/web/config.inc.php
index 68b6c3b..c53c2c4 100644
--- a/blocks/mrbs/web/config.inc.php
+++ b/blocks/mrbs/web/config.inc.php
@@ -196,12 +196,12 @@ $default_room = $cfg_mrbs->default_room;
 ###############################################
 # Authentication settings - read AUTHENTICATION
 ###############################################
-$auth["session"] = "php"; 
+$auth["session"] = "php";
 $auth["type"] = "moodle";
 if ($CFG->sessioncookiepath=='/') { //if one is not set in Moodle then use default
     $cookie_path_override = ''; //is this even needed with the moodle auth type?
 } else { //if one is set in Moodle then use that path for MRBS block cookies
-    $cookie_path_override = $CFG->$cookie_path_override = ''; 
+    $cookie_path_override = $CFG->$cookie_path_override = '';
 }
 
 
@@ -244,7 +244,7 @@ define ("MAIL_BOOKER", $cfg_mrbs->mail_booker);
 # scheme other than 'auth_db', you need to provide the mail domain that will
 # be appended to the username to produce a valid email address (ie.
 # "@domain.com").
-define ("MAIL_DOMAIN", ''); //I am not including this option as we should be using Moodle email options - see CONTRIB-630 
+define ("MAIL_DOMAIN", ''); //I am not including this option as we should be using Moodle email options - see CONTRIB-630
 
 # If you use MAIL_DOMAIN above and username returned by mrbs contains extra
 # strings appended like domain name ('username.domain'), you need to provide
@@ -267,7 +267,7 @@ define ("SENDMAIL_PATH", "/usr/bin/sendmail");
 define ("SENDMAIL_ARGS", '');
 
 #*******************
-# SMTP settings 
+# SMTP settings
 
 # Set smtp server to connect. Default is 'localhost' (only used with "smtp"
 # backend).
@@ -300,7 +300,7 @@ if (isset($USER->lang)) {
     define ("MAIL_ADMIN_LANG", 'en');
 }
 # Set the email address of the From field. Default is $mrbs_admin_email
-define ("MAIL_FROM", $cfg_mrbs->mail_from); 
+define ("MAIL_FROM", $cfg_mrbs->mail_from);
 
 # Set the recipient email. Default is $mrbs_admin_email. You can define
 # more than one recipient like this "john@doe.com,scott@tiger.com"
@@ -311,7 +311,7 @@ define ("MAIL_RECIPIENTS", $cfg_mrbs->mail_recipients);
 define ("MAIL_CC", $cfg_mrbs->mail_cc);
 
 // Users wanting to use a different strings can change set them as a custom language string
-# Set the content of the Subject field for added/changed entries. 
+# Set the content of the Subject field for added/changed entries.
 $mail["subject"] = get_string('mail_subject', 'block_mrbs', $mrbs_company);
 
 # Set the content of the Subject field for deleted fields.
@@ -319,7 +319,7 @@ $mail["subject_delete"] = get_string('mail_subject_delete', 'block_mrbs', $mrbs_
 
 # Set the content of the message when a new entry is booked. What you type
 # here will be added at the top of the message body.
-$mail["new_entry"] = get_string('mail_new_entry','block_mrbs'); 
+$mail["new_entry"] = get_string('mail_new_entry','block_mrbs');
 
 # Set the content of the message when an entry is modified. What you type
 # here will be added at the top of the message body.
@@ -404,7 +404,7 @@ if(!empty($cfg_mrbs->cronfile)){
     $typel["K"] = get_string('importedbooking','block_mrbs');
     $typel["L"] = get_string('importedbookingmoved','block_mrbs');
 }
-
+//WARNING: DO NOT USE TYPE M, type M is used by import script and will delete other type M bookings
 
 error_reporting (E_ALL ^ E_NOTICE);
 
