fix_course_sequence.php 4.3 KB

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