Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 fixes orphaned question categories.
  19   *
  20   * Orphaned question categories have had their associated context deleted
  21   * but the category itself remains in the database with an invalid context.
  22   *
  23   * @package    core
  24   * @subpackage cli
  25   * @copyright  2013 Tyler Bannister (tyler.bannister@remote-learner.net)
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  define('CLI_SCRIPT', true);
  30  
  31  require(__DIR__.'/../../config.php');
  32  require_once($CFG->libdir.'/clilib.php');
  33  require_once($CFG->libdir.'/questionlib.php');
  34  
  35  $long = array('fix'  => false, 'help' => false);
  36  $short = array('f' => 'fix', 'h' => 'help');
  37  
  38  // Now get cli options.
  39  list($options, $unrecognized) = cli_get_params($long, $short);
  40  
  41  if ($unrecognized) {
  42      $unrecognized = implode("\n  ", $unrecognized);
  43      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  44  }
  45  
  46  if ($options['help']) {
  47      $help =
  48          "Fix orphaned question categories.
  49  
  50          This scripts detects question categories that have had their
  51          context deleted, thus severing them from their original purpose.
  52  
  53          This script will find the orphaned categories and delete the unused
  54          questions in each category found.  Used questions will not be
  55          deleted, instead they will be moved to a rescue question category.
  56  
  57          Options:
  58          -h, --help            Print out this help
  59          -f, --fix             Fix the orphaned question categories in the DB.
  60                                If not specified only check and report problems to STDERR.
  61          Example:
  62          \$sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_question_categories.php
  63          \$sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_question_categories.php -f
  64          ";
  65  
  66      echo $help;
  67      die;
  68  }
  69  
  70  cli_heading('Checking for orphaned categories');
  71  
  72  
  73  $sql = 'SELECT qc.id, qc.contextid, qc.name
  74            FROM {question_categories} qc
  75       LEFT JOIN {context} c ON qc.contextid = c.id
  76           WHERE c.id IS NULL';
  77  $categories = $DB->get_recordset_sql($sql);
  78  
  79  $i = 0;
  80  foreach ($categories as $category) {
  81      $i += 1;
  82      echo "Found orphaned category: {$category->name}\n";
  83      if (!empty($options['fix'])) {
  84          echo "Cleaning...";
  85          // One transaction per category.
  86          $transaction = $DB->start_delegated_transaction();
  87          question_category_delete_safe($category);
  88          $transaction->allow_commit();
  89          echo "  Done!\n";
  90      }
  91  }
  92  
  93  if (($i > 0) && !empty($options['fix'])) {
  94      echo "Found and removed {$i} orphaned question categories\n";
  95  } else if ($i > 0) {
  96      echo "Found {$i} orphaned question categories. To fix, run:\n";
  97      echo "\$sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_question_categories.php --fix\n";
  98  } else {
  99      echo "No orphaned question categories found.\n";
 100  }
 101  
 102  
 103  $categories->close();