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.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * CLI script to delete a course.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @author     Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
  23   * @copyright  2022 Catalyst IT
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  define('CLI_SCRIPT', true);
  28  
  29  require(__DIR__ . '/../../config.php');
  30  require_once($CFG->libdir . '/clilib.php');
  31  require_once($CFG->libdir . '/cronlib.php');
  32  
  33  list($options, $unrecognized) = cli_get_params(
  34      [
  35          'courseid' => false,
  36          'help' => false,
  37          'showsql' => false,
  38          'showdebugging' => false,
  39          'disablerecyclebin' => false,
  40          'non-interactive' => false,
  41      ], [
  42          'c' => 'courseid',
  43          'h' => 'help',
  44      ]
  45  );
  46  
  47  if ($unrecognized) {
  48      $unrecognized = implode("\n  ", $unrecognized);
  49      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  50  }
  51  
  52  if ($options['help'] || empty($options['courseid'])) {
  53      $help = <<<EOT
  54  CLI script to delete a course.
  55  
  56  Options:
  57   -h, --help                Print out this help
  58       --showsql             Show sql queries before they are executed
  59       --showdebugging       Show developer level debugging information
  60       --disablerecyclebin   Skip backing up the course
  61       --non-interactive     No interactive questions or confirmations
  62   -c, --courseid            Course id to be deleted
  63  
  64  Example:
  65  \$sudo -u www-data /usr/bin/php admin/cli/delete_course.php --courseid=123456
  66  \$sudo -u www-data /usr/bin/php admin/cli/delete_course.php --courseid=123456 --showdebugging
  67  \$sudo -u www-data /usr/bin/php admin/cli/delete_course.php --courseid=123456 --disablerecyclebin
  68  
  69  EOT;
  70  
  71      echo $help;
  72      die;
  73  }
  74  
  75  $interactive = empty($options['non-interactive']);
  76  
  77  if ($options['showdebugging']) {
  78      mtrace('Enabling debugging...');
  79      set_debugging(DEBUG_DEVELOPER, true);
  80  }
  81  
  82  if ($options['showsql']) {
  83      mtrace('Enabling SQL debugging...');
  84      $DB->set_debug(true);
  85  }
  86  
  87  if (CLI_MAINTENANCE) {
  88      cli_error('CLI maintenance mode active, CLI execution suspended');
  89  }
  90  
  91  if (moodle_needs_upgrading()) {
  92      cli_error('Moodle upgrade pending, CLI execution suspended');
  93  }
  94  
  95  $course = $DB->get_record('course', array('id' => $options['courseid']));
  96  if (empty($course)) {
  97      cli_error('Course not found');
  98  }
  99  
 100  mtrace('Deleting course id ' . $course->id);
 101  mtrace('Course name: ' . $course->fullname);
 102  mtrace('Short name: ' . $course->shortname);
 103  
 104  if ($interactive) {
 105      mtrace('');
 106      $input = cli_input('Are you sure you wish to delete this course? (y/N)', 'N', ['y', 'Y', 'n', 'N']);
 107      if (strtolower($input) != 'y') {
 108          exit(0);
 109      }
 110  }
 111  
 112  if ($options['disablerecyclebin']) {
 113      mtrace('Disabling recycle bin...');
 114      $overrideconfig = ['tool_recyclebin' => ['coursebinenable' => false, 'categorybinenable' => false]];
 115      $CFG->forced_plugin_settings = array_merge($CFG->forced_plugin_settings, $overrideconfig);
 116  }
 117  
 118  core_php_time_limit::raise();
 119  delete_course($course);
 120  
 121  mtrace('Updating course count in categories...');
 122  fix_course_sortorder();
 123  
 124  mtrace('Done!');