Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402]

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