Merge branch 'MDL-76246-master' of https://github.com/rezaies/moodle
[moodle.git] / admin / cli / fix_deleted_users.php
blobcc052b4e702d90748228771e457c9bca89c597ba
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This script fixed incorrectly deleted users.
20 * @package core
21 * @subpackage cli
22 * @copyright 2013 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__.'/../../config.php');
29 require_once($CFG->libdir.'/clilib.php');
32 // Now get cli options.
33 list($options, $unrecognized) = cli_get_params(array('help'=>false),
34 array('h'=>'help'));
36 if ($unrecognized) {
37 $unrecognized = implode("\n ", $unrecognized);
38 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
41 if ($options['help']) {
42 $help =
43 "Fix incorrectly deleted users.
45 This scripts detects users that are marked as deleted instead
46 of calling delete_user().
48 Deleted users do not have original username, idnumber or email,
49 we must also delete all roles, enrolments, group memberships, etc.
51 Please note this script does not delete any public information
52 such as forum posts.
54 Options:
55 -h, --help Print out this help
57 Example:
58 \$sudo -u www-data /usr/bin/php admin/cli/fix_deleted_users.php
61 echo $help;
62 die;
65 cli_heading('Looking for sloppy user deletes');
67 // Look for sloppy deleted users where somebody only flipped the deleted flag.
68 $sql = "SELECT *
69 FROM {user}
70 WHERE deleted = 1 AND email LIKE '%@%' AND username NOT LIKE '%@%'";
71 $rs = $DB->get_recordset_sql($sql);
72 foreach ($rs as $user) {
73 echo "Redeleting user $user->id: $user->username ($user->email)\n";
74 delete_user($user);
76 $rs->close();
78 cli_heading('Deleting all leftovers');
80 $DB->set_field('user', 'idnumber', '', array('deleted'=>1));
82 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
83 $DB->delete_records_select('cohort_members', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
84 $DB->delete_records_select('groups_members', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
85 $DB->delete_records_select('user_enrolments', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
86 $DB->delete_records_select('user_preferences', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
87 $DB->delete_records_select('user_info_data', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
88 $DB->delete_records_select('user_lastaccess', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
89 $DB->delete_records_select('external_tokens', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
90 $DB->delete_records_select('external_services_users', "userid IN (SELECT id FROM {user} WHERE deleted = 1)");
92 exit(0);