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 Marina Glancy
  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  // Get cli options.
  32  list($options, $unrecognized) = cli_get_params(
  33      array(
  34          'courses'           => false,
  35          'fix'               => false,
  36          'help'              => false
  37      ),
  38      array(
  39          'h' => 'help',
  40          'c' => 'courses',
  41          'f' => 'fix'
  42      )
  43  );
  44  
  45  if ($options['help'] || empty($options['courses'])) {
  46      $help =
  47  "Checks and fixes that course modules and sections reference each other correctly.
  48  
  49  Compares DB fields course_sections.sequence and course_modules.section
  50  checking that:
  51  - course_sections.sequence contains each module id not more than once in the course
  52  - for each moduleid from course_sections.sequence the field course_modules.section
  53    refers to the same section id (this means course_sections.sequence is more
  54    important if they are different)
  55  - each module in the course is present in one of course_sections.sequence
  56  - section sequences do not contain non-existing course modules
  57  
  58  If there are any mismatches, the message is displayed. If --fix is specified,
  59  the records in DB are corrected.
  60  
  61  This script may run for a long time on big systems if called for all courses.
  62  
  63  Avoid executing the script when another user may simultaneously edit any of the
  64  courses being checked (recommended to run in mainenance mode).
  65  
  66  Options:
  67  -c, --courses         List courses that need to be checked (comma-separated
  68                        values or * for all). Required
  69  -f, --fix             Fix the mismatches in DB. If not specified check only and
  70                        report problems to STDERR
  71  -h, --help            Print out this help
  72  
  73  Example:
  74  \$sudo -u www-data /usr/bin/php admin/cli/fix_course_sequence.php --courses=*
  75  \$sudo -u www-data /usr/bin/php admin/cli/fix_course_sequence.php --courses=2,3,4 --fix
  76  ";
  77  
  78      echo $help;
  79      die;
  80  }
  81  
  82  $courseslist = preg_split('/\s*,\s*/', $options['courses'], -1, PREG_SPLIT_NO_EMPTY);
  83  if (in_array('*', $courseslist)) {
  84      $where = '';
  85      $params = array();
  86  } else {
  87      list($sql, $params) = $DB->get_in_or_equal($courseslist, SQL_PARAMS_NAMED, 'id');
  88      $where = 'WHERE id '. $sql;
  89  }
  90  $coursescount = $DB->get_field_sql('SELECT count(id) FROM {course} '. $where, $params);
  91  
  92  if (!$coursescount) {
  93      cli_error('No courses found');
  94  }
  95  echo "Checking $coursescount courses...\n\n";
  96  
  97  require_once($CFG->dirroot. '/course/lib.php');
  98  
  99  $problems = array();
 100  $courses = $DB->get_fieldset_sql('SELECT id FROM {course} '. $where, $params);
 101  foreach ($courses as $courseid) {
 102      $errors = course_integrity_check($courseid, null, null, true, empty($options['fix']));
 103      if ($errors) {
 104          if (!empty($options['fix'])) {
 105              // Reset the course cache to make sure cache is recalculated next time the course is viewed.
 106              rebuild_course_cache($courseid, true);
 107          }
 108          foreach ($errors as $error) {
 109              cli_problem($error);
 110          }
 111          $problems[] = $courseid;
 112      } else {
 113          echo "Course [$courseid] is OK\n";
 114      }
 115  }
 116  if (!count($problems)) {
 117      echo "\n...All courses are OK\n";
 118  } else {
 119      if (!empty($options['fix'])) {
 120          echo "\n...Found and fixed ".count($problems)." courses with problems". "\n";
 121      } else {
 122          echo "\n...Found ".count($problems)." courses with problems. To fix run:\n";
 123          echo "\$sudo -u www-data /usr/bin/php admin/cli/fix_course_sequence.php --courses=".join(',', $problems)." --fix". "\n";
 124      }
 125  }