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.
/course/ -> delete.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [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   * Admin-only code to delete a course utterly.
  19   *
  20   * @package core_course
  21   * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../config.php');
  26  require_once($CFG->dirroot . '/course/lib.php');
  27  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  28  
  29  $id = required_param('id', PARAM_INT); // Course ID.
  30  $delete = optional_param('delete', '', PARAM_ALPHANUM); // Confirmation hash.
  31  
  32  $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
  33  $coursecontext = context_course::instance($course->id);
  34  
  35  require_login();
  36  
  37  if ($SITE->id == $course->id || !can_delete_course($id)) {
  38      // Can not delete frontpage or don't have permission to delete the course.
  39      throw new \moodle_exception('cannotdeletecourse');
  40  }
  41  
  42  $categorycontext = context_coursecat::instance($course->category);
  43  $PAGE->set_url('/course/delete.php', array('id' => $id));
  44  $PAGE->set_context($categorycontext);
  45  $PAGE->set_pagelayout('admin');
  46  navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid'=>$course->category)));
  47  
  48  $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
  49  $coursefullname = format_string($course->fullname, true, array('context' => $coursecontext));
  50  $categoryurl = new moodle_url('/course/management.php', array('categoryid' => $course->category));
  51  
  52  // Check if we've got confirmation.
  53  if ($delete === md5($course->timemodified)) {
  54      // We do - time to delete the course.
  55      require_sesskey();
  56  
  57      $strdeletingcourse = get_string("deletingcourse", "", $courseshortname);
  58  
  59      $PAGE->navbar->add($strdeletingcourse);
  60      $PAGE->set_title($strdeletingcourse);
  61      $PAGE->set_heading($SITE->fullname);
  62  
  63      echo $OUTPUT->header();
  64      echo $OUTPUT->heading($strdeletingcourse);
  65      // This might take a while. Raise the execution time limit.
  66      core_php_time_limit::raise();
  67      // We do this here because it spits out feedback as it goes.
  68      delete_course($course);
  69      echo $OUTPUT->heading( get_string("deletedcourse", "", $courseshortname) );
  70      // Update course count in categories.
  71      fix_course_sortorder();
  72      echo $OUTPUT->continue_button($categoryurl);
  73      echo $OUTPUT->footer();
  74      exit; // We must exit here!!!
  75  }
  76  
  77  $strdeletecheck = get_string("deletecheck", "", $courseshortname);
  78  
  79  $PAGE->navbar->add($strdeletecheck);
  80  $PAGE->set_title($strdeletecheck);
  81  $PAGE->set_heading($SITE->fullname);
  82  echo $OUTPUT->header();
  83  
  84  // Only let user delete this course if there is not an async backup in progress.
  85  if (!async_helper::is_async_pending($id, 'course', 'backup')) {
  86      $strdeletecoursecheck = get_string("deletecoursecheck");
  87      $message = "{$strdeletecoursecheck}<br /><br />{$coursefullname} ({$courseshortname})";
  88  
  89      $continueurl = new moodle_url('/course/delete.php', array('id' => $course->id, 'delete' => md5($course->timemodified)));
  90      $continuebutton = new single_button(
  91          $continueurl,
  92          get_string('delete'), 'post', false, ['data-action' => 'delete']
  93      );
  94      echo $OUTPUT->confirm($message, $continuebutton, $categoryurl);
  95      // In the following script, we need to use setTimeout as disabling the
  96      // button in the event listener script prevent the click to be taken into account.
  97      $jsscript = <<<EOF
  98  const button = document.querySelector('button[data-action="delete"]');
  99  if (button) {
 100      button.addEventListener('click', () => {
 101          setTimeout(() => {
 102              button.disabled = true;
 103          }, 0);
 104      });
 105  }
 106  EOF;
 107      $PAGE->requires->js_amd_inline($jsscript);
 108  
 109  } else {
 110      // Async backup is pending, don't let user delete course.
 111      echo $OUTPUT->notification(get_string('pendingasyncerror', 'backup'), 'error');
 112      echo $OUTPUT->container(get_string('pendingasyncdeletedetail', 'backup'));
 113      echo $OUTPUT->continue_button($categoryurl);
 114  }
 115  
 116  echo $OUTPUT->footer();
 117  exit;