### Eclipse Workspace Patch 1.0
#P 19stable
Index: lib/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v
retrieving revision 1.154.2.46
diff -u -r1.154.2.46 upgrade.php
--- lib/db/upgrade.php	31 Mar 2009 10:23:54 -0000	1.154.2.46
+++ lib/db/upgrade.php	20 Apr 2009 03:53:10 -0000
@@ -3112,9 +3112,17 @@
     /// Main savepoint reached
         upgrade_main_savepoint($result, 2007101545.01);
     }
+    
+    if ($result && $oldversion < 2007101547) {
+        $deletedusers = get_records_select('user', "deleted = 1", 'id'); // get all deleted users
+        foreach ($deletedusers as $deleteduser) {
+            move_deleted_user_messages($deleteduser->id); // move messages
+        }
+    /// Main savepoint reached
+        upgrade_main_savepoint($result, 2007101547);
+    }
 
     return $result;
 }
 
-
 ?>
Index: admin/uploaduser.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/uploaduser.php,v
retrieving revision 1.68.2.14
diff -u -r1.68.2.14 uploaduser.php
--- admin/uploaduser.php	18 Oct 2008 00:10:00 -0000	1.68.2.14
+++ admin/uploaduser.php	20 Apr 2009 03:53:08 -0000
@@ -327,10 +327,28 @@
                 $upt->track('status', $strusernotdeletedmissing, 'error');
                 $deleteerrors++;
             }
+            unset($user->deleted);
             continue;
+        } else {
+            unset($user->deleted);
+            if (!empty($existinguser->deleted)) { //if user was previously deleted but now being restored
+                $upt->track('status', 'trying to move mail', 'warning');
+                if ($messages = get_records_select('message_read', "useridfrom = $existinguser->id AND timeread = 0", 'timecreated')) {
+                    foreach ($messages as $message) {
+                        $message = addslashes_object($message);
+                        $messageid = $message->id;
+                        unset($message->id); // allow message id to use autoincrement id field
+                        unset($message->timeread); //timeread field not in message table
+                        unset($message->mailed); //mailed field not in message table
+                        if (insert_record('message', $message)) {
+                            delete_records('message_read', 'id', $messageid);
+                            $user->deleted=0;
+                            
+                        }
+                    }
+                }
+            }
         }
-        // we do not need the deleted flag anymore
-        unset($user->deleted);
 
         // renaming requested?
         if (!empty($user->oldusername) ) {
@@ -445,6 +463,7 @@
                             }
                         }
                     }
+                        
                     if ((array_key_exists($column, $existinguser) and array_key_exists($column, $user)) or in_array($column, $PRF_FIELDS)) {
                         if ($updatetype == 3 and $existinguser->$column !== '') {
                             //missing == non-empty only
Index: lib/moodlelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v
retrieving revision 1.960.2.126
diff -u -r1.960.2.126 moodlelib.php
--- lib/moodlelib.php	3 Apr 2009 13:11:43 -0000	1.960.2.126
+++ lib/moodlelib.php	20 Apr 2009 03:53:09 -0000
@@ -3010,6 +3010,34 @@
 }
 
 /**
+ * Moves unread messages from message table to message_read to prevent popups
+ * @param object $userid       User id
+ * @return boolean success
+ */
+function move_deleted_user_messages($userid) {
+
+    // move all unread messages from message table to messasge_read
+    if ($messages = get_records_select('message', "useridfrom = $userid", 'timecreated')) {
+        $success = TRUE;
+        foreach ($messages as $message) {
+            $message->timeread = 0; //the message was never read
+            $message = addslashes_object($message);
+            $messageid = $message->id;
+            unset($message->id);
+            if (insert_record('message_read', $message)) {
+                delete_records('message', 'id', $messageid);
+            } else {
+                $success= FALSE;
+            }
+        }
+        return $success;
+    } else {
+        return FALSE;
+    }
+}
+
+
+/**
  * Marks user deleted in internal user database and notifies the auth plugin.
  * Also unenrols user from all roles and does other cleanup.
  * @param object $user       Userobject before delete    (without system magic quotes)
@@ -3028,7 +3056,9 @@
             $grade->delete('userdelete');
         }
     }
-
+    
+    move_deleted_user_messages($user->id);
+    
     // remove from all groups
     delete_records('groups_members', 'userid', $user->id);
 
Index: message/history.php
===================================================================
RCS file: /cvsroot/moodle/moodle/message/history.php,v
retrieving revision 1.14.4.2
diff -u -r1.14.4.2 history.php
--- message/history.php	2 May 2008 04:07:32 -0000	1.14.4.2
+++ message/history.php	20 Apr 2009 03:53:10 -0000
@@ -19,12 +19,24 @@
     if (! $user1 = get_record("user", "id", $userid1)) {  // Check it's correct
         error("User ID 1 was incorrect");
     }
+    if ($user1->deleted) {
+        print_header();
+        print_heading(get_string('userdeleted').': '.$userid1);
+        print_footer();
+        die;
+    }
 
     if (has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) {             // Able to see any discussion
         $userid2 = optional_param('user2', $USER->id, PARAM_INT);
         if (! $user2 = get_record("user", "id", $userid2)) {  // Check
             error("User ID 2 was incorrect");
         }
+        if ($user2->deleted) {
+        print_header();
+        print_heading(get_string('userdeleted').': '.$userid2);
+        print_footer();
+        die;
+    }
     } else {
         $userid2 = $USER->id;    // Can only see messages involving yourself
         $user2 = $USER; 
