### Eclipse Workspace Patch 1.0
#P moodle19
Index: lib/authlib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/authlib.php,v
retrieving revision 1.8.2.4
diff -u -r1.8.2.4 authlib.php
--- lib/authlib.php	26 Apr 2008 13:47:30 -0000	1.8.2.4
+++ lib/authlib.php	27 Apr 2008 22:09:22 -0000
@@ -61,8 +61,8 @@
      * The fields we can lock and update from/to external authentication backends
      *
      */
-    var $userfields = array("firstname", "lastname", "email", "phone1", "phone2", 
-            "institution", "department", "address", "city", "country", 
+    var $userfields = array("firstname", "lastname", "email", "phone1", "phone2",
+            "institution", "department", "address", "city", "country",
             "description", "idnumber", "lang");
 
     /**
@@ -352,7 +352,7 @@
         }
         return $authdescription;
     }
-    
+
     /**
      * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
      * @abstract Implement in child classes
@@ -362,7 +362,455 @@
         return false;
     }
 
+}
+
+
+/////////////////////////////////
+/// Various utility functions ///
+/////////////////////////////////
+
+/**
+ * Return JOIN and WHERE parts to be used when searching
+ * for suspended users.
+ * @param string $t_alias - aleas of the temp table
+ * @param string $auth auth plugin name
+ * @return array ($join, $where) strings
+ */
+function suspended_users_sql($t_alias, $auth) {
+    global $CFG;
+
+    $join  = "JOIN {$CFG->prefix}user u ON u.username = $t_alias.username
+              JOIN {$CFG->prefix}user_preferences up ON up.userid = u.id";
+
+    $where = "u.auth='nologin' AND u.mnethostid = {$CFG->mnet_localhost_id}
+              AND up.name = 'nologin_auto_prevauth' AND up.value = '$auth'";
+
+    return array($join, $where);
+}
+
+/**
+ * Suspend user with given id
+ *  - user can not login anymore
+ *  - username is "reserved"
+ *  - "Login as" is possible
+ * @param mixed $userorid object or number
+ * @param bool manual sync sctripts use false, manaul user changes from UI true
+ * @return bool success
+ */
+function suspend_user($userorid, $manual=false) {
+    if (is_numeric($userorid)) {
+        if (!$user = get_record('user', 'id', $userorid)) {
+            return false;
+        }
+    } else {
+        $user = $userorid;
+    }
+
+    if ($user->deleted) {
+        // can not suspend deleted users!
+        return false;
+    }
+
+    if ($user->auth == 'nologin') {
+        // already suspended
+        return true;
+    }
+
+    if ($manual) {
+        set_user_preference('nologin_man_prevauth', $user->auth, $user->id);
+        unset_user_preference('nologin_auto_prevauth', $user->id);
+    } else {
+        set_user_preference('nologin_auto_prevauth', $user->auth, $user->id);
+        unset_user_preference('nologin_man_prevauth', $user->id);
+    }
 
+    $updateuser = new object();
+    $updateuser->id   = $user->id;
+    $updateuser->auth = 'nologin';
+
+    return update_record('user', $updateuser);
 }
 
+/**
+ * Revive user account previously suspended
+ * @param mixed $userorid object or number
+ * @param bool manual must match value used when suspending!
+ * @return success
+ */
+function unsuspend_user($userorid, $manual=false) {
+    if (is_numeric($userorid)) {
+        if (!$user = get_record('user', 'id', $userorid)) {
+            return false;
+        }
+    } else {
+        $user = $userorid;
+    }
+
+    if ($user->deleted) {
+        // can not unsuspend deleted users!
+        return false;
+    }
+
+    if ($user->auth != 'nologin') {
+        // not suspended
+        return true;
+    }
+
+    if ($manual) {
+        if (!$oldauth = get_user_preferences('nologin_man_prevauth', null, $user->id)) {
+            return false;
+        }
+    } else {
+        if (!$oldauth = get_user_preferences('nologin_auto_prevauth', null, $user->id)) {
+            return false;
+        }
+    }
+
+    $updateuser = new object();
+    $updateuser->id   = $user->id;
+    $updateuser->auth = $oldauth;
+
+    if (!update_record('user', $updateuser)) {
+        return false;
+    }
+
+    unset_user_preference('nologin_auto_prevauth', $user->id);
+    unset_user_preference('nologin_man_prevauth', $user->id);
+
+    return true;
+}
+
+/**
+ * Was user suspended manually?
+ * @param mixed $userorid object or number
+ * @return bool true is was suspended and can be unsuspended
+ */
+function is_man_suspended($userorid) {
+    if (is_numeric($userorid)) {
+        if (!$user = get_record('user', 'id', $userorid)) {
+            return false;
+        }
+    } else {
+        $user = $userorid;
+    }
+
+    $prevauth = get_user_preferences('nologin_man_prevauth', null, $user->id);
+    return !empty($prevauth);
+}
+
+/**
+ * Purge all information related to this user - may be called repeatedly.
+ * As much as possible should be removed
+ * @param int user id empty if all deleted users
+ * @param mixed purging hints (similar to course reset implementation)
+ * @return array status array
+ */
+function purge_user($userid=0, $hints=null) {
+    global $CFG;
+
+    //TODO:
+    // * implements hints - anonymize instead of delete, etc
+
+    // result array: component, item, error
+    $status = array();
+
+    $componentstr = get_string('general');
+
+    if ($userid) {
+        if (!$user = get_record('user', 'id', $userid)) {
+            $status[] = array('component'=>$componentstr, 'item'=>get_string('general'), 'error'=>'User does not exist!');
+            return $status;
+        }
+        $useridcond = "=$userid";
+
+    } else {
+        $useridcond = "IN (SELECT id FROM {$CFG->prefix}user WHERE deleted=1)";
+    }
+
+
+/// rerun delete cleanup too
+    $status2 = deleted_user_cleanup($userid);
+    $ok = true;
+    foreach ($status2 as $s2) {
+        if ($s2['error'] !== false) {
+            $ok = false;
+            break;
+        }
+    }
+    unset($status2);
+    $status[] = array('component'=>$componentstr, 'item'=>get_string('userdeletecleanup', 'admin'), 'error'=>($ok ? false : get_string('error')));
+
+/// anonymise user record
+    $purgefileds = array();
+
+    if (!isset($hints->purge_name) or $hints->purge_name) {
+        $purgefileds += array('firstname','lastname');
+    }
+
+    if (!isset($hints->purge_profile) or $hints->purge_profile) {
+        $purgefileds += array('idnumber', 'email', 'phone1', 'phone2',
+                              'institution', 'department', 'address', 'city', 'country',
+                              'description', 'idnumber', 'lang', 'password', 'icq', 'skype',
+                              'yahoo', 'msn', 'description', 'lastip', 'url', 'imagealt');
+    }
+
+    $now = time();
+    if ($userid) {
+        $updateuser = new object();
+        $updateuser->id           = $user->id;
+        $updateuser->timemodified = $now;
+        $updateuser->picture      = 0;  // done during delete now, but better do that again
+        $updateuser->password     = ''; // better remove password hash too
+
+        foreach ($purgefileds as $property) {
+            $updateuser->$property = '';
+        }
+
+        $ok = update_record('user', $updateuser);
+    } else {
+        $sql = "UPDATE {$CFG->prefix}user
+                   SET picture=0, password='', timemodified=$now";
+        foreach ($purgefileds as $property) {
+            $sql .= ", $property = ''";
+        }
+        $sql .= " WHERE deleted=1";
+        $ok = execute_sql($sql, false);
+    }
+    $status[] = array('component'=>$componentstr, 'item'=>'User record fields', 'error'=>($ok ? false : get_string('error'))); // TODO: localise
+
+/// give all modules a chance - including the disabled
+    if ($modules = get_records('modules')) {
+        foreach($modules as $module) {
+            @include_once("$CFG->dirroot/mod/$module->name/lib");
+            $functionname = $module->name.'_purge_user';
+            if (function_exists($functionname)) {
+                $status = array_merge($status, $functionname($userid, $hints));
+            }
+        }
+    }
+
+/// give all blocks a chance - including the disabled
+    if ($blocks = get_records('block')) {
+        foreach($blocks as $block) {
+            $blockinstance = block_instance($block->name);
+            if (method_exists($blockinstance, 'purge_user')) {
+                $status = array_merge($status, $blockinstance->deleted_user_cleanup($userid, $hints));
+            }
+        }
+    }
+
+/// delete user settings and info from other tables
+    $tables = array();
+
+    if (!isset($hints->purge_messages) or $hints->purge_messages) {
+        $tables += array('message'      => 'useridfrom',
+                         'message'      => 'useridto',
+                         'message_read' => array('useridfrom', 'useridto'));
+    }
+
+    if (!isset($hints->purge_logs) or $hints->purge_logs) {
+        $tables += array('log' => 'userid');
+    }
+
+    if (!isset($hints->purge_stats) or $hints->purge_stats) {
+        $tables += array('stats_user_daily'  => 'userid',
+                         'stats_user_monthly'=> 'userid',
+                         'stats_user_weekly' => 'userid');
+    }
+
+    foreach ($tables as $tablename=>$fieldnames) {
+        if (!is_array($fieldnames)) {
+            $fieldnames = array($fieldnames);
+        }
+        $ok = true;
+        foreach ($fieldnames as $fieldname) {
+            $ok = delete_records_select($tablename, "$fieldname $useridcond") && $ok;
+        }
+        $status[] = array('component'=>$componentstr, 'item'=>'User data in table: '.$tablename, 'error'=>($ok ? false : get_string('error'))); // TODO: localise
+    }
+
+/// delete notes from and about user
+    if (!isset($hints->purge_notes) or $hints->purge_notes) {
+        $ok = delete_records_select('post', "module='notes' AND (userid $useridcond OR usermodified $useridcond)");
+        $status[] = array('component'=>$componentstr, 'item'=>get_string('notes', 'notes'), 'error'=>($ok ? false : get_string('error')));
+    }
+
+/// delete blog entries and attachments
+    if (!isset($hints->purge_blog) or $hints->purge_blog) {
+        if ($rs = get_recordset_select('post', "module='blog' AND attachment IS NOT NULL AND userid $useridcond", '', 'id')) {
+            while($a = rs_fetch_next_record($rs)) {
+                remove_dir("$CFG->dataroot/blog/attachments/$a->id/");
+            }
+            rs_close($rs);
+        }
+        $ok = delete_records_select('post', "module='blog' AND userid $useridcond");
+        $status[] = array('component'=>$componentstr, 'item'=>get_string('blog', 'blog'), 'error'=>($ok ? false : get_string('error')));
+    }
+
+/// delete tags
+    if (!isset($hints->purge_tags) or $hints->purge_tags) {
+        $ok = delete_records_select('tag_correlation', "tagid IN (SELECT id FROM {$CFG->prefix}tag WHERE userid $useridcond)");
+        $ok = delete_records_select('tag_instance',    "tagid IN (SELECT id FROM {$CFG->prefix}tag WHERE userid $useridcond)") && $ok;
+        $ok = delete_records_select('tag', "userid $useridcond") && $ok;
+        $status[] = array('component'=>$componentstr, 'item'=>get_string('tags'), 'error'=>($ok ? false : get_string('error')));
+    }
+
+    return $status;
+}
+
+/**
+ * User delete cleanup - may be called repeatedly.
+ * Most of the activity data is kept.
+ * @param int user id, empty means all deleted users
+ * @return status array
+ */
+function deleted_user_cleanup($userid=0) {
+    global $CFG;
+
+    require_once($CFG->libdir.'/grouplib.php');
+    require_once($CFG->libdir.'/gradelib.php');
+    require_once($CFG->libdir.'/blocklib.php');
+
+    // result array: component, item, error
+    $status = array();
+
+    $componentstr = get_string('general');
+
+    if ($userid) {
+        if (!$user = get_record('user', 'id', $userid)) {
+            $status[] = array('component'=>$componentstr, 'item'=>get_string('general'), 'error'=>'User does not exist!');
+            return $status;
+        }
+        $useridcond = "=$userid";
+
+    } else {
+        $useridcond = "IN (SELECT id FROM {$CFG->prefix}user WHERE deleted=1)";
+    }
+
+/// first unenrol from all roles in all contexts
+    // this is slow but modules/blocks might need to do some extra cleanup
+    if ($userid) {
+        $ok = role_unassign(0, $userid);
+    } else {
+        $ok = true;
+        $sql = "SELECT DISTINCT u.id
+                  FROM {$CFG->prefix}user u
+                       JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
+                 WHERE u.deleted=1";
+        if ($rs = get_recordset_sql($sql)) {
+            while ($u = rs_fetch_next_record($rs)) {
+                $ok = role_unassign(0, $u->id) && $ok;
+            }
+            rs_close($rs);
+        }
+    }
+    $status[] = array('component'=>$componentstr, 'item'=>get_string('roles', 'role'), 'error'=>($ok ? false : get_string('error')));
+
+/// delete user dir
+    if ($userid) {
+        $userdir = make_user_directory($userid, true);
+        $ok = remove_dir($userdir);
+    } else {
+        $ok = true;
+        $sql = "SELECT DISTINCT u.id
+                  FROM {$CFG->prefix}user u
+                 WHERE u.deleted=1 AND u.picture=1";
+        if ($rs = get_recordset_sql($sql)) {
+            while ($u = rs_fetch_next_record($rs)) {
+                $userdir = make_user_directory($u->id, true);
+                $ok = remove_dir($userdir) && $ok;
+            }
+            rs_close($rs);
+        }
+    }
+    $status[] = array('component'=>$componentstr, 'item'=>'User directory', 'error'=>($ok ? false : get_string('error')));
+
+/// give all modules a chance - including the disabled
+    if ($modules = get_records('modules')) {
+        foreach($modules as $module) {
+            @include_once("$CFG->dirroot/mod/$module->name/lib");
+            $functionname = $module->name.'_delete_user_cleanup'; // _delete_user() would collide with code in contrib :-(
+            if (function_exists($functionname)) {
+                $status = array_merge($status, $functionname($userid));
+            }
+        }
+    }
+
+/// give all blocks a chance - including the disabled
+    if ($blocks = get_records('block')) {
+        foreach($blocks as $block) {
+            $blockinstance = block_instance($block->name);
+            if (method_exists($blockinstance, 'delete_user_cleanup')) {
+                $status = array_merge($status, $blockinstance->deleted_user_cleanup($userid));
+            }
+        }
+    }
+
+/// remove blocks from my moodle page including contexts
+    $ok = true;
+    if ($userid) {
+        $sql = "SELECT bi.*
+                  FROM {$CFG->prefix}block_instance bi
+                 WHERE pagetype='my-index' AND pageid=$userid";
+    } else {
+        $sql = "SELECT bi.*
+                  FROM {$CFG->prefix}block_instance bi
+                       JOIN {$CFG->prefix}user u ON (u.id=bi.pageid AND bi.pagetype='my-index')
+                 WHERE u.deleted=1";
+    }
+    if ($rs = get_recordset_sql($sql)) {
+        while ($block = rs_fetch_next_record($rs)) {
+            $ok = blocks_delete_instance($block) && $ok;
+        }
+        rs_close($rs);
+    }
+    $status[] = array('component'=>$componentstr, 'item'=>'My Moodle blocks', 'error'=>($ok ? false : get_string('error'))); // TODO: localise
+
+/// delete all grades - backup is kept in grade_grades_history table
+    $status = array_merge($status, grade_user_deleted_cleanup($userid));
+
+/// delete user settings and info from other tables
+    $tables = array('course_display'    => 'userid',
+                    'course_request'    => 'requester',
+                    'event'             => 'userid',
+                    'groups_members'    => 'userid',
+                    'message_contacts'  => array('userid', 'contactid'),
+                    'user_info_data'    => 'userid',
+                    'user_lastaccess'   => 'userid',
+                    'user_preferences'  => 'userid',
+                    'user_private_key'  => 'userid',
+                    'webdav_locks'      => 'userid',
+                    'role_assignments'  => 'userid');
+    foreach ($tables as $tablename=>$fieldnames) {
+        if (!is_array($fieldnames)) {
+            $fieldnames = array($fieldnames);
+        }
+        $ok = true;
+        foreach ($fieldnames as $fieldname) {
+            $ok = delete_records_select($tablename, "$fieldname $useridcond") && $ok;
+        }
+
+        $status[] = array('component'=>$componentstr, 'item'=>'User data in table: '.$tablename, 'error'=>($ok ? false : get_string('error'))); // TODO: localise
+    }
+
+/// now do a final accesslib cleanup - removes all role assingments in user context and context itself
+    if ($userid) {
+        $ok = delete_context(CONTEXT_USER, $userid);
+    } else {
+        $ok = true;
+        $sql = "SELECT u.id
+                  FROM {$CFG->prefix}context c
+                       JOIN {$CFG->prefix}user u ON (u.id=c.instanceid AND c.contextlevel=".CONTEXT_USER.")
+                 WHERE u.deleted=1";
+        if ($rs = get_recordset_sql($sql)) {
+            while ($u = rs_fetch_next_record($rs)) {
+                $ok = delete_context(CONTEXT_USER, $u->id) && $ok;
+            }
+            rs_close($rs);
+        }
+    }
+    $status[] = array('component'=>$componentstr, 'item'=>'User context', 'error'=>($ok ? false : get_string('error'))); // TODO: localise
+
+    return $status;
+}
 ?>
Index: lib/datalib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/datalib.php,v
retrieving revision 1.439.2.24
diff -u -r1.439.2.24 datalib.php
--- lib/datalib.php	15 Apr 2008 21:42:51 -0000	1.439.2.24
+++ lib/datalib.php	27 Apr 2008 22:09:25 -0000
@@ -332,7 +332,7 @@
     }
 
 /// warning: will return UNCONFIRMED USERS
-    return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
+    return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid, auth
                               FROM {$CFG->prefix}user
                              WHERE $select $sort", $page, $recordsperpage);
 
Index: lib/moodlelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v
retrieving revision 1.960.2.74
diff -u -r1.960.2.74 moodlelib.php
--- lib/moodlelib.php	25 Apr 2008 18:52:47 -0000	1.960.2.74
+++ lib/moodlelib.php	27 Apr 2008 22:09:33 -0000
@@ -2950,30 +2950,11 @@
  * 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)
- * @return boolean success
+ * @param bool $verbose 
+ * @return mixed boolean success or status array depedniong on $verbose param
  */
-function delete_user($user) {
+function delete_user($user, $verbose=false) {
     global $CFG;
-    require_once($CFG->libdir.'/grouplib.php');
-    require_once($CFG->libdir.'/gradelib.php');
-
-    begin_sql();
-
-    // delete all grades - backup is kept in grade_grades_history table
-    if ($grades = grade_grade::fetch_all(array('userid'=>$user->id))) {
-        foreach ($grades as $grade) {
-            $grade->delete('userdelete');
-        }
-    }
-
-    // remove from all groups
-    delete_records('groups_members', 'userid', $user->id);
-
-    // unenrol from all roles in all contexts
-    role_unassign(0, $user->id); // this might be slow but it is really needed - modules might do some extra cleanup!
-
-    // now do a final accesslib cleanup - removes all role assingments in user context and context itself
-    delete_context(CONTEXT_USER, $user->id);
 
     // workaround for bulk deletes of users with the same email address
     $delname = addslashes("$user->email.".time());
@@ -2981,6 +2962,7 @@
         $delname++;
     }
 
+
     // mark internal user record as "deleted"
     $updateuser = new object();
     $updateuser->id           = $user->id;
@@ -2989,16 +2971,28 @@
     $updateuser->email        = '';               // Clear this field to free it up
     $updateuser->idnumber     = '';               // Clear this field to free it up
     $updateuser->timemodified = time();
+    $updateuser->picture      = 0;
 
     if (update_record('user', $updateuser)) {
-        commit_sql();
         // notify auth plugin - do not block the delete even when plugin fails
         $authplugin = get_auth_plugin($user->auth);
         $authplugin->user_delete($user);
-        return true;
 
+        require_once($CFG->libdir.'/authlib.php');
+        if ($verbose) {
+            $status = array(array('component'=>get_string('general'), 'item'=>get_string('user'), 'error'=>false));
+            $status = array_merge($status, deleted_user_cleanup($user->id));
+            return $status;
+        } else {
+            deleted_user_cleanup($user->id); //ignore result ;-)
+            return true;
+        }
+    } 
+
+    // error!
+    if ($verbose) {
+        return array(array('component'=>get_string('general'), 'item'=>get_string('user'), 'error'=>get_string('error')));
     } else {
-        rollback_sql();
         return false;
     }
 }
Index: lib/gradelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/gradelib.php,v
retrieving revision 1.120.2.25
diff -u -r1.120.2.25 gradelib.php
--- lib/gradelib.php	16 Mar 2008 23:21:35 -0000	1.120.2.25
+++ lib/gradelib.php	27 Apr 2008 22:09:27 -0000
@@ -1346,4 +1346,54 @@
     return (grade_floatval($f1) !== grade_floatval($f2));
 }
 
+/**
+ * Deal somehow with grades when user deleted
+ * Grades are deleted - backup is kept in grade_grades_history if enabled.
+ * @param int $userid, empty means all deleted users
+ * @return array status array
+ */
+function grade_user_deleted_cleanup($userid) {
+    global $CFG;
+
+    if ($userid) {
+        if (!empty($CFG->disablegradehistory)) {
+            $status = delete_records('grade_grades', 'userid', $userid);
+
+        } else {
+            $status = true;
+            if ($grades = grade_grade::fetch_all(array('userid'=>$userid))) {
+                foreach ($grades as $grade) {
+                    $status = $grade->delete('userdelete') && $status;
+                }
+            }
+        }
+
+    } else {
+        if (!empty($CFG->disablegradehistory)) {
+            $status = delete_records_select('grade_grades', "userid IN (SELECT id FROM {$CFG->prefix}user WHERE deleted=1)") && $status;
+
+        } else {
+            $sql = "SELECT g.*
+                      FROM {$CFG->prefix}grade_grades g
+                           JOIN {$CFG->prefix}user u ON u.id=g.userid
+                     WHERE u.deleted=1";
+            if ($rs = get_recordset_sql($sql)) {
+                $status = true;
+                while ($grade = rs_fetch_next_record($rs)) {
+                    $grade_grade = new grade_grade($grade, false);
+                    $status = $grade->delete('userdelete') && $status;
+                }
+                rs_close($rs);
+            }
+        }
+    }
+
+    if ($status) {
+        return array(array('component'=>get_string('gradebook', 'grades'), 'item'=>get_string('grades', 'grades'), 'error'=>false));
+    } else {
+        return array(array('component'=>get_string('gradebook', 'grades'), 'item'=>get_string('grades', 'grades'), 'error'=>get_string('error')));
+    }
+
+}
+
 ?>
Index: lib/blocklib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/blocklib.php,v
retrieving revision 1.129.2.1
diff -u -r1.129.2.1 blocklib.php
--- lib/blocklib.php	19 Oct 2007 17:52:03 -0000	1.129.2.1
+++ lib/blocklib.php	27 Apr 2008 22:09:25 -0000
@@ -196,6 +196,10 @@
         if($obj = block_instance($record->name, $instance)) {
             // Return value ignored
             $obj->instance_delete();
+            // remove context - pinned blocks use system context
+            if (!$pinned) {
+                delete_context(CONTEXT_BLOCK, $instance->id);
+            }
         }
     }
 
Index: lang/en_utf8/admin.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/admin.php,v
retrieving revision 1.154.2.40
diff -u -r1.154.2.40 admin.php
--- lang/en_utf8/admin.php	24 Apr 2008 03:01:31 -0000	1.154.2.40
+++ lang/en_utf8/admin.php	27 Apr 2008 22:09:21 -0000
@@ -288,6 +288,7 @@
 $string['defaultsettinginfo'] = 'Default: $a';
 $string['defaultuserroleid'] = 'Default role for all users';
 $string['defaultvalues'] = 'Default values';
+$string['deletedusers'] = 'Deleted users';
 $string['deleteerrors'] = 'Delete errors';
 $string['deleteunconfirmed'] = 'Delete unconfirmed users after';
 $string['deleteuser'] = 'Delete user';
@@ -599,6 +600,7 @@
 $string['requiredtemplate'] = 'Required. You may use template syntax here (%%l = lastname, %%f = firstname, %%u = username). See help for details and examples.';
 $string['restrictbydefault'] = 'Restrict modules by default';
 $string['restrictmodulesfor'] = 'Restrict modules for';
+$string['revive'] = 'Revive';
 $string['riskconfig'] = 'Users could change site configuration and behaviour';
 $string['riskconfigshort'] = 'Configuration risk';
 $string['riskmanagetrust'] = 'Users could change trust settings of other users';
@@ -658,6 +660,8 @@
 $string['supportemail'] = 'Support email';
 $string['supportname'] = 'Support name';
 $string['supportpage'] = 'Support page';
+$string['suspend'] = 'Suspend';
+$string['suspended'] = 'Suspended';
 $string['switchlang'] = 'Switch lang directory';
 $string['systempaths'] = 'System Paths';
 $string['tabselectedtofront'] = 'On tables with tabs, should the row with the currently selected tab be placed at the front';
@@ -714,9 +718,16 @@
 $string['usehtmleditor'] = 'Use HTML editor';
 $string['useraccountupdated'] = 'User updated';
 $string['userbulk'] = 'Bulk user actions';
+$string['userdeletecleanup'] = 'Delete cleanup';
+$string['userdeletecleanupuser'] = 'Delete cleanup of user $a';
+$string['userdeletecleanupall'] = 'Cleanup all deleted';
+$string['userdeletecleanupallconfirm'] = 'Do you really want to do cleanup after all deleted users?';
 $string['userdeleted'] = 'User deleted';
 $string['userlist'] = 'Browse list of users';
 $string['userpolicies'] = 'User policies';
+$string['userpurge'] = 'Purge';
+$string['userpurgeall'] = 'Purge all deleted';
+$string['userpurgenotimplemented'] = 'Purging not implemented';
 $string['userrenamed'] = 'User renamed';
 $string['users'] = 'Users';
 $string['userscreated'] = 'Users created';
Index: admin/settings/users.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/settings/users.php,v
retrieving revision 1.26.2.5
diff -u -r1.26.2.5 users.php
--- admin/settings/users.php	10 Mar 2008 17:21:33 -0000	1.26.2.5
+++ admin/settings/users.php	27 Apr 2008 22:09:17 -0000
@@ -74,6 +74,7 @@
     $ADMIN->add('accounts', new admin_externalpage('uploadusers', get_string('uploadusers'), "$CFG->wwwroot/$CFG->admin/uploaduser.php", 'moodle/site:uploadusers'));
     $ADMIN->add('accounts', new admin_externalpage('uploadpictures', get_string('uploadpictures','admin'), "$CFG->wwwroot/$CFG->admin/uploadpicture.php", 'moodle/site:uploadusers'));
     $ADMIN->add('accounts', new admin_externalpage('profilefields', get_string('profilefields','admin'), "$CFG->wwwroot/user/profile/index.php", 'moodle/site:config'));
+    $ADMIN->add('accounts', new admin_externalpage('deletedusers', get_string('deletedusers', 'admin'), $CFG->wwwroot.'/'.$CFG->admin.'/deletedusers.php', 'moodle/site:doanything', false)); //TODO: needs new capability
 
 
     // stuff under the "roles" subcategory
Index: auth/ldap/auth.php
===================================================================
RCS file: /cvsroot/moodle/moodle/auth/ldap/auth.php,v
retrieving revision 1.27.2.22
diff -u -r1.27.2.22 auth.php
--- auth/ldap/auth.php	25 Apr 2008 13:20:28 -0000	1.27.2.22
+++ auth/ldap/auth.php	27 Apr 2008 22:09:19 -0000
@@ -644,28 +644,26 @@
         // this is still not as scalable (but how often do we mass delete?)
         if (!empty($this->config->removeuser)) {
             $sql = "SELECT u.id, u.username, u.email, u.auth 
-                    FROM {$CFG->prefix}user u
-                        LEFT JOIN $temptable e ON u.username = e.username
-                    WHERE u.auth='ldap'
-                        AND u.deleted=0
-                        AND e.username IS NULL";
+                      FROM {$CFG->prefix}user u
+                           LEFT JOIN $temptable e ON u.username = e.username
+                     WHERE u.auth='ldap'
+                           AND u.deleted=0
+                           AND e.username IS NULL
+                           AND u.mnethostid = {$CFG->mnet_localhost_id}";
             $remove_users = get_records_sql($sql);
 
             if (!empty($remove_users)) {
                 print "User entries to remove: ". count($remove_users) . "\n";
 
                 foreach ($remove_users as $user) {
-                    if ($this->config->removeuser == 2) {
+                    if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
                         if (delete_user($user)) {
                             echo "\t"; print_string('auth_dbdeleteuser', 'auth', array($user->username, $user->id)); echo "\n";
                         } else {
                             echo "\t"; print_string('auth_dbdeleteusererror', 'auth', $user->username); echo "\n";
                         }
-                    } else if ($this->config->removeuser == 1) {
-                        $updateuser = new object();
-                        $updateuser->id = $user->id;
-                        $updateuser->auth = 'nologin';
-                        if (update_record('user', $updateuser)) {
+                    } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
+                        if (suspend_user($user)) {
                             echo "\t"; print_string('auth_dbsuspenduser', 'auth', array($user->username, $user->id)); echo "\n";
                         } else {
                             echo "\t"; print_string('auth_dbsuspendusererror', 'auth', $user->username); echo "\n";
@@ -679,11 +677,11 @@
         }
 
 /// Revive suspended users
-        if (!empty($this->config->removeuser) and $this->config->removeuser == 1) {
+        if (!empty($this->config->removeuser) and $this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
+            list($join, $where) = suspended_users_sql("e", 'ldap');
             $sql = "SELECT u.id, u.username
-                    FROM $temptable e, {$CFG->prefix}user u
-                    WHERE e.username=u.username
-                        AND u.auth='nologin'";
+                    FROM $temptable e $join
+                    WHERE $where";
             $revive_users = get_records_sql($sql);
 
             if (!empty($revive_users)) {
@@ -691,10 +689,7 @@
 
                 begin_sql();
                 foreach ($revive_users as $user) {
-                    $updateuser = new object();
-                    $updateuser->id = $user->id;
-                    $updateuser->auth = 'ldap';
-                    if (update_record('user', $updateuser)) {
+                    if (unsuspend_user($user->id)) {
                         echo "\t"; print_string('auth_dbreviveser', 'auth', array($user->username, $user->id)); echo "\n";
                     } else {
                         echo "\t"; print_string('auth_dbreviveusererror', 'auth', $user->username); echo "\n";
@@ -733,8 +728,10 @@
         }
         if ( $do_updates and !empty($updatekeys) ) { // run updates only if relevant
             $users = get_records_sql("SELECT u.username, u.id
-                                      FROM {$CFG->prefix}user u
-                                      WHERE u.deleted=0 AND u.auth='ldap'");
+                                        FROM {$CFG->prefix}user u
+                                       WHERE u.deleted = 0
+                                             AND u.auth = 'ldap'
+                                             AND u.mnethostid = {$CFG->mnet_localhost_id}");
             if (!empty($users)) {
                 print "User entries to update: ". count($users). "\n";
 
@@ -786,7 +783,7 @@
         // and gives me a nifty object I don't want.
         // note: we do not care about deleted accounts anymore, this feature was replaced by suspending to nologin auth plugin
         $sql = "SELECT e.username, e.username
-                FROM $temptable e LEFT JOIN {$CFG->prefix}user u ON e.username = u.username
+                FROM $temptable e LEFT JOIN {$CFG->prefix}user u ON (u.username = e.username AND u.mnethostid = {$CFG->mnet_localhost_id})
                 WHERE u.id IS NULL";
         $add_users = get_records_sql($sql); // get rid of the fat
 
@@ -889,7 +886,7 @@
         } else {
             return false;
         }
-        return get_record_select('user', "id = $userid AND deleted = 0");
+        return get_record_select('user', "id = $userid AND deleted = 0 AND u.mnethostid = {$CFG->mnet_localhost_id}");
     }
 
     /**
@@ -1968,7 +1965,7 @@
         if (!isset($config->changepasswordurl))
             {$config->changepasswordurl = ''; }
         if (!isset($config->removeuser))
-            {$config->removeuser = 0; }
+            {$config->removeuser = AUTH_REMOVEUSER_KEEP; }
         if (!isset($config->ntlmsso_enabled))
             {$config->ntlmsso_enabled = 0; }
         if (!isset($config->ntlmsso_subnet))
Index: admin/user.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/user.php,v
retrieving revision 1.109.2.2
diff -u -r1.109.2.2 user.php
--- admin/user.php	26 Jan 2008 16:56:23 -0000	1.109.2.2
+++ admin/user.php	27 Apr 2008 22:09:17 -0000
@@ -2,6 +2,7 @@
 
     require_once('../config.php');
     require_once($CFG->libdir.'/adminlib.php');
+    require_once($CFG->libdir.'/authlib.php');
     require_once($CFG->dirroot.'/user/filters/lib.php');
 
     $delete       = optional_param('delete', 0, PARAM_INT);
@@ -11,6 +12,8 @@
     $dir          = optional_param('dir', 'ASC', PARAM_ALPHA);
     $page         = optional_param('page', 0, PARAM_INT);
     $perpage      = optional_param('perpage', 30, PARAM_INT);        // how many per page
+    $suspend      = optional_param('suspend', 0, PARAM_INT);
+    $revive       = optional_param('revive', 0, PARAM_INT);
     $ru           = optional_param('ru', '2', PARAM_INT);            // show remote users
     $lu           = optional_param('lu', '2', PARAM_INT);            // show local users
     $acl          = optional_param('acl', '0', PARAM_INT);           // id of user to tweak mnet ACL (requires $access)
@@ -25,10 +28,13 @@
         error('You do not have the required permission to edit/delete users.');
     }
 
-    $stredit   = get_string('edit');
-    $strdelete = get_string('delete');
-    $strdeletecheck = get_string('deletecheck');
+    $stredit         = get_string('edit');
+    $strdelete       = get_string('delete');
+    $strdeletecheck  = get_string('deletecheck');
     $strshowallusers = get_string('showallusers');
+    $strsuspend      = get_string('suspend', 'admin');
+    $strsuspended    = get_string('suspended', 'admin');
+    $strrevive       = get_string('revive', 'admin');
 
     if (empty($CFG->loginhttps)) {
         $securewwwroot = $CFG->wwwroot;
@@ -39,6 +45,8 @@
     admin_externalpage_print_header();
 
     if ($confirmuser and confirm_sesskey()) {
+        require_capability('moodle/user:update', $sitecontext);
+
         if (!$user = get_record('user', 'id', $confirmuser)) {
             error("No such user!", '', true);
         }
@@ -55,9 +63,7 @@
 
     } else if ($delete and confirm_sesskey()) {              // Delete a selected user, after confirmation
 
-        if (!has_capability('moodle/user:delete', $sitecontext)) {
-            error('You do not have the required permission to delete a user.');
-        }
+        require_capability('moodle/user:delete', $sitecontext);
 
         if (!$user = get_record('user', 'id', $delete)) {
             error("No such user!", '', true);
@@ -74,18 +80,45 @@
             notice_yesno(get_string('deletecheckfull', '', "'$fullname'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
             admin_externalpage_print_footer();
             die;
+
         } else if (data_submitted() and !$user->deleted) {
-            if (delete_user($user)) {
-                notify(get_string('deletedactivity', '', fullname($user, true)) );
-            } else {
-                notify(get_string('deletednot', '', fullname($user, true)));
-            }
+            print_heading(get_string('deleteuser', 'admin'));
+
+            $status = delete_user($user, true);
+            $data = array();;
+            foreach ($status as $item) {
+                $line = array();
+                $line[] = $item['component'];
+                $line[] = $item['item'];
+                $line[] = ($item['error']===false) ? get_string('ok') : '<div class="notifyproblem">'.$item['error'].'</div>';
+                $data[] = $line;
+            }
+
+            $table = new object();
+            $table->head  = array(get_string('resetcomponent'), get_string('info'), get_string('status'));
+            $table->size  = array('20%', '40%', '40%');
+            $table->align = array('left', 'left', 'left');
+            $table->width = '80%';
+            $table->data  = $data;
+            print_table($table);
+            print_continue('user.php');
+            print_footer();
+            die;
         }
+
+    } else if ($suspend and confirm_sesskey()) {
+        require_capability('moodle/user:update', $sitecontext);
+        suspend_user($suspend, true);
+
+    } else if ($revive and confirm_sesskey()) {
+        require_capability('moodle/user:update', $sitecontext);
+
+        unsuspend_user($revive, true);
+
     } else if ($acl and confirm_sesskey()) {
-        if (!has_capability('moodle/user:delete', $sitecontext)) {
-            // TODO: this should be under a separate capability
-            error('You are not permitted to modify the MNET access control list.');
-        }
+        // TODO: this should be under a separate capability
+        require_capability('moodle/user:delete', $sitecontext);
+
         if (!$user = get_record('user', 'id', $acl)) {
             error("No such user.", '', true);
         }
@@ -202,8 +235,8 @@
 
         $mainadmin = get_admin();
 
-        $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
-        $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
+        $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "", "");
+        $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center", "center");
         $table->width = "95%";
         foreach ($users as $user) {
             if ($user->username == 'guest') {
@@ -266,6 +299,21 @@
             }
             $fullname = fullname($user, true);
 
+            if ($user->auth == 'nologin') {
+                if (has_capability('moodle/user:update', $sitecontext) and is_man_suspended($user)) {
+                    $suspended = "<a href=\"user.php?revive=$user->id&amp;sesskey=$USER->sesskey&amp;page=$page\">$strrevive</a>";
+                } else {
+                    $suspended = $strsuspended;
+                }
+                
+            } else {
+                if (has_capability('moodle/user:update', $sitecontext)) {
+                    $suspended = "<a href=\"user.php?suspend=$user->id&amp;sesskey=$USER->sesskey&amp;page=$page\">$strsuspend</a>";
+                } else {
+                    $suspended = '';
+                }
+            }
+
             $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
                                 "$user->email",
                                 "$user->city",
@@ -273,7 +321,8 @@
                                 $strlastaccess,
                                 $editbutton,
                                 $deletebutton,
-                                $confirmbutton);
+                                $confirmbutton,
+                                $suspended);
         }
     }
 
Index: admin/deletedusers.php
===================================================================
RCS file: admin/deletedusers.php
diff -N admin/deletedusers.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ admin/deletedusers.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,230 @@
+<?php // $Id: $
+
+    require_once('../config.php');
+    require_once($CFG->libdir.'/adminlib.php');
+    require_once($CFG->libdir.'/authlib.php');
+    require_once('deletedusers_purge_form.php');
+
+    $sort      = optional_param('sort', 'timemodified', PARAM_ALPHA);
+    $dir       = optional_param('dir', 'ASC', PARAM_ALPHA);
+    $page      = optional_param('page', 0, PARAM_INT);
+    $perpage   = optional_param('perpage', 30, PARAM_INT);
+    $delete    = optional_param('delete', 0, PARAM_BOOL);     // rerun delete cleanup
+    $purge     = optional_param('purge', 0, PARAM_BOOL);      // purge all user data
+    $confirm   = optional_param('confirm', 0, PARAM_BOOL);
+    $userid    = optional_param('userid', 0, PARAM_INT);
+
+
+    admin_externalpage_setup('deletedusers');
+
+    $stredit         = get_string('edit');
+    $strdelete       = get_string('userdeletecleanup', 'admin');
+    $strdeleteall    = get_string('userdeletecleanupall', 'admin');
+    $strpurge        = get_string('userpurge', 'admin');
+    $strpurgeall     = get_string('userpurgeall', 'admin');
+    $strdeletedusers = get_string('deletedusers', 'admin');
+
+    if ($purge) {
+        $mform = new purge_form(null, empty($userid));
+        $data = array('userid'=>$userid, 'dir'=>$dir, 'sort'=>$sort, 'page'=>$page, 'perpage'=>$perpage);
+        $mform->set_data($data);
+        $mform->load_defaults();
+
+        if ($mform->is_cancelled()) {
+            redirect("deletedusers.php?sort=$sort&amp;dir=$dir&amp;page=$page&amp;perpage=$perpage");
+        }
+
+        admin_externalpage_print_header();
+        if ($userid) {
+            if (!$user = get_record('user', 'id', $userid)) {
+                error("No such user!", 'deletedusers.php');
+            }
+            print_heading($strpurge.' '.format_username($user));
+        } else {
+            print_heading($strpurgeall);
+        }
+        if ($hints = $mform->get_data(false)) {
+            $status = purge_user($userid, $hints);
+            $data = array();;
+            foreach ($status as $item) {
+                $line = array();
+                $line[] = $item['component'];
+                $line[] = $item['item'];
+                $line[] = ($item['error']===false) ? get_string('ok') : '<div class="notifyproblem">'.$item['error'].'</div>';
+                $data[] = $line;
+            }
+
+            $table = get_result_table();
+            $table->data  = $data;
+            print_table($table);
+            print_continue('deletedusers.php');
+
+        } else {
+            $mform->display();
+        }
+        print_footer();
+        die;
+
+    } else if ($delete and $userid and data_submitted() and confirm_sesskey()) {
+        admin_externalpage_print_header();
+
+        if (!$user = get_record('user', 'id', $userid)) {
+            error("No such user!", 'deletedusers.php');
+        }
+
+        print_heading(get_string('userdeletecleanupuser', 'admin', format_username($user)));
+
+        $status = deleted_user_cleanup($userid);
+        $data = array();;
+        foreach ($status as $item) {
+            $line = array();
+            $line[] = $item['component'];
+            $line[] = $item['item'];
+            $line[] = ($item['error']===false) ? get_string('ok') : '<div class="notifyproblem">'.$item['error'].'</div>';
+            $data[] = $line;
+        }
+
+        $table = get_result_table();
+        $table->data  = $data;
+        print_table($table);
+        print_continue('deletedusers.php');
+        print_footer();
+        die;
+
+    } else if ($delete and !$userid) {
+        admin_externalpage_print_header();
+        print_heading($strdeleteall);
+
+        if ($confirm and data_submitted() and confirm_sesskey()) {
+
+            $status = deleted_user_cleanup(0);
+            $data = array();;
+            foreach ($status as $item) {
+                $line = array();
+                $line[] = $item['component'];
+                $line[] = $item['item'];
+                $line[] = ($item['error']===false) ? get_string('ok') : '<div class="notifyproblem">'.$item['error'].'</div>';
+                $data[] = $line;
+            }
+
+            $table = get_result_table();
+            $table->data  = $data;
+            print_table($table);
+            print_continue('deletedusers.php');
+            print_footer();
+            die;
+
+        } else {
+            $strconfirm = get_string('userdeletecleanupallconfirm', 'admin');
+            $optionsyes = array('delete'=>1, 'sesskey'=>sesskey(), 'confirm'=>1);
+            notice_yesno($strconfirm, 'deletedusers.php', 'deletedusers.php', $optionsyes, NULL, 'post', 'get');
+            print_footer();
+            die;
+        }
+
+    } else {
+        admin_externalpage_print_header();
+    }
+
+    print_heading($strdeletedusers);
+
+    $columns = array('id', 'username', 'firstname', 'lastname', 'city', 'country', 'timemodified');
+
+    foreach ($columns as $column) {
+        if ($column == 'id') {
+            $string[$column] = 'Id';
+        } else if ($column == 'timemodified') {
+            $string[$column] = get_string('modified');
+        } else {
+            $string[$column] = get_string($column);
+        }
+
+        if ($sort != $column) {
+            $columnicon = "";
+            if ($column == "timemodified") {
+                $columndir = "DESC";
+            } else {
+                $columndir = "ASC";
+            }
+        } else {
+            $columndir = $dir == "ASC" ? "DESC":"ASC";
+            if ($column == "timemodified") {
+                $columnicon = $dir == "ASC" ? "up":"down";
+            } else {
+                $columnicon = $dir == "ASC" ? "down":"up";
+            }
+            $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
+
+        }
+        $colname = 'col'.$column;
+        $$colname = "<a href=\"deletedusers.php?sort=$column&amp;dir=$columndir&amp;perpage=$perpage&amp;page=$page\">".$string[$column]."</a>$columnicon";
+    }
+
+    if ($sort) {
+        if ($sort == 'firstname') {
+            $sqlsort = " ORDER BY firstname $dir, lastname $dir";
+        } else if ($sort == 'lastname') {
+            $sqlsort = " ORDER BY lastname $dir, firstname $dir";
+        } else {
+            $sqlsort = " ORDER BY $sort $dir";
+        }
+    } else {
+        $sqlsort = "";
+    }
+
+    $users = get_records_sql("SELECT id, username, firstname, lastname, city, country, timemodified
+                                FROM {$CFG->prefix}user
+                               WHERE deleted=1
+                            $sqlsort",
+                             $page, $perpage);
+
+    $usercount = count_records('user', 'deleted', 1);
+
+    if ($users) {
+        print_heading("$usercount ".get_string('users'), '', 3);
+
+        print_paging_bar($usercount, $page, $perpage, "deletedusers.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;");
+
+        $cleanallbutton = print_single_button('deletedusers.php', array('delete'=>1), $strdeleteall, 'get', '_self', true);
+        $purgellbutton  = print_single_button('deletedusers.php', array('purge'=>1), $strpurgeall, 'get', '_self', true);
+
+        $table->head = array ($colid, $colusername, "$colfirstname / $collastname", $colcity, $colcountry, $coltimemodified, $cleanallbutton, $purgellbutton);
+        $table->align = array ('left', 'left', 'left', 'left', 'left', 'left', 'center', 'center');
+        $table->width = '95%';
+        $shared = array('page'=>$page, 'perpage'=>$perpage, 'sort'=>$sort, 'dir'=>$dir);
+        foreach ($users as $user) {
+            // buttons here for security reasons
+            $options = $shared+array('delete'=>1, 'userid'=>$user->id, 'sesskey'=>sesskey());
+            $delete = print_single_button('deletedusers.php', $options, $strdelete, 'post', '_self', true);
+            $options = $shared+array('purge'=>1, 'userid'=>$user->id,);
+            $purge  = print_single_button('deletedusers.php', $options, $strpurge, 'post', '_self', true);
+            $fullname = fullname($user, true);
+            $table->data[] = array ($user->id, $user->username, $fullname, $user->city, $user->country, userdate($user->timemodified), $delete, $purge);
+        }
+
+        print_table($table);
+        print_paging_bar($usercount, $page, $perpage, "deletedusers.php?sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;");
+
+    } else {
+        print_heading(get_string('nousersfound'));
+    }
+
+    print_footer();
+
+/////////////////////////
+/// Utility functions ///
+/////////////////////////
+
+function format_username($user) {
+    return 'Id: '.$user->id.' ('.$user->username.')';
+}
+
+function get_result_table() {
+    $table = new object();
+    $table->head  = array(get_string('resetcomponent'), get_string('info'), get_string('status'));
+    $table->size  = array('20%', '40%', '40%');
+    $table->align = array('left', 'left', 'left');
+    $table->width = '80%';
+    return $table;
+}
+?>
Index: admin/deletedusers_purge_form.php
===================================================================
RCS file: admin/deletedusers_purge_form.php
diff -N admin/deletedusers_purge_form.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ admin/deletedusers_purge_form.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,91 @@
+<?php // $Id: $
+require_once $CFG->libdir.'/formslib.php';
+
+class purge_form extends moodleform {
+    function definition (){
+        global $CFG;
+
+        $mform    =& $this->_form;
+        $purgeall = $this->_customdata;
+
+        $mform->addElement('header', 'generalheader', get_string('general'));
+        $mform->addElement('advcheckbox', 'purge_name', get_string('name'));
+        $mform->addElement('advcheckbox', 'purge_profile', get_string('profile'));
+        $mform->addElement('advcheckbox', 'purge_notes', get_string('notes', 'notes'));
+        $mform->addElement('advcheckbox', 'purge_tags', get_string('tags', 'tag'));
+        $mform->addElement('advcheckbox', 'purge_blog', get_string('blog', 'blog'));
+        $mform->addElement('advcheckbox', 'purge_messages', get_string('messages', 'message'));
+        $mform->addElement('advcheckbox', 'purge_logs', get_string('logs'));
+        $mform->addElement('advcheckbox', 'purge_stats', get_string('statistics'));
+
+        $unsupported = array();
+        if ($allmods = get_records('modules') ) {
+            foreach ($allmods as $mod) {
+                $modname = $mod->name;
+                if ($modname == 'label' or $modname == 'resource') {
+                    continue;
+                }
+                $modfile = $CFG->dirroot."/mod/$modname/lib.php";
+                $mod_purge_user_form_definition = $modname.'_purge_user_form_definition';
+                $mod_purge_user = $modname.'_purge_user';
+                if (file_exists($modfile)) {
+                    include_once($modfile);
+                    if (function_exists($mod_purge_user_form_definition)) {
+                        $mod_purge_user_form_definition($mform);
+                    } else if (!function_exists($mod_purge_user)) {
+                        $unsupported[] = get_string('modulenameplural', $modname);
+                    }
+                } else {
+                    debugging('Missing lib.php in '.$modname.' module');
+                }
+            }
+        }
+        // mention unsupported mods
+        if (!empty($unsupported)) {
+            $mform->addElement('header', 'activities', get_string('activities'));
+            $mform->addElement('static', 'unsupportedmods', get_string('userpurgenotimplemented', 'admin'), implode(', ', $unsupported));
+        }
+
+        $mform->addElement('hidden', 'purge', 1);
+        $mform->addElement('hidden', 'userid', 0);
+        $mform->addElement('hidden', 'dir');
+        $mform->addElement('hidden', 'sort');
+        $mform->addElement('hidden', 'page');
+        $mform->addElement('hidden', 'perpage');
+
+        $submitstr = $purgeall ? get_string('userpurgeall', 'admin') : get_string('userpurge', 'admin');
+
+        $buttonarray = array();
+        $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitstr);
+        $buttonarray[] = &$mform->createElement('cancel');
+        $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
+        $mform->closeHeaderBefore('buttonar');
+    }
+
+    function load_defaults() {
+        global $CFG;
+
+        $mform =& $this->_form;
+
+        $defaults = array ('purge_name'=>0, 'purge_profile'=>1, 'purge_notes'=>1, 'purge_tags'=>1, 'purge_blog'=>1,
+                           'purge_messages'=>1, 'purge_logs'=>1, 'purge_stats'=>1);
+
+        if ($allmods = get_records('modules') ) {
+            foreach ($allmods as $mod) {
+                $modname = $mod->name;
+                $modfile = $CFG->dirroot."/mod/$modname/lib.php";
+                $mod_purge_user_form_defaults = $modname.'_purge_user_form_defaults';
+                if (file_exists($modfile)) {
+                    @include_once($modfile);
+                    if (function_exists($mod_purge_user_form_defaults)) {
+                        $defaults = $defaults + $mod_purge_user_form_defaults();
+                    }
+                }
+            }
+        }
+
+        foreach ($defaults as $element=>$default) {
+            $mform->setDefault($element, $default);
+        }
+    }
+}
