Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * This script fixed incorrectly deleted users.
  19   *
  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
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__.'/../../config.php');
  29  require_once($CFG->libdir.'/clilib.php');
  30  
  31  
  32  // Now get cli options.
  33  list($options, $unrecognized) = cli_get_params(array('help'=>false),
  34      array('h'=>'help'));
  35  
  36  if ($unrecognized) {
  37      $unrecognized = implode("\n  ", $unrecognized);
  38      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  39  }
  40  
  41  if ($options['help']) {
  42      $help =
  43          "Fix incorrectly deleted users.
  44  
  45          This scripts detects users that are marked as deleted instead
  46          of calling delete_user().
  47  
  48          Deleted users do not have original username, idnumber or email,
  49          we must also delete all roles, enrolments, group memberships, etc.
  50  
  51          Please note this script does not delete any public information
  52          such as forum posts.
  53  
  54          Options:
  55          -h, --help            Print out this help
  56  
  57          Example:
  58          \$sudo -u www-data /usr/bin/php admin/cli/fix_deleted_users.php
  59          ";
  60  
  61      echo $help;
  62      die;
  63  }
  64  
  65  cli_heading('Looking for sloppy user deletes');
  66  
  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);
  75  }
  76  $rs->close();
  77  
  78  cli_heading('Deleting all leftovers');
  79  
  80  $DB->set_field('user', 'idnumber', '', array('deleted'=>1));
  81  
  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)");
  91  
  92  exit(0);