Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/course/ -> delete.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  define('NO_OUTPUT_BUFFERING', true);
  26  
  27  require_once(__DIR__ . '/../config.php');
  28  require_once($CFG->dirroot . '/course/lib.php');
  29  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  30  
  31  $id = required_param('id', PARAM_INT); // Course ID.
  32  $delete = optional_param('delete', '', PARAM_ALPHANUM); // Confirmation hash.
  33  
  34  $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
  35  $coursecontext = context_course::instance($course->id);
  36  
  37  require_login();
  38  
  39  if ($SITE->id == $course->id || !can_delete_course($id)) {
  40      // Can not delete frontpage or don't have permission to delete the course.
  41      throw new \moodle_exception('cannotdeletecourse');
  42  }
  43  
  44  $categorycontext = context_coursecat::instance($course->category);
  45  $PAGE->set_url('/course/delete.php', array('id' => $id));
  46  $PAGE->set_context($categorycontext);
  47  $PAGE->set_pagelayout('admin');
  48  navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid'=>$course->category)));
  49  
  50  $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
  51  $coursefullname = format_string($course->fullname, true, array('context' => $coursecontext));
  52  $categoryurl = new moodle_url('/course/management.php', array('categoryid' => $course->category));
  53  
  54  // Check if we've got confirmation.
  55  if ($delete === md5($course->timemodified)) {
  56      // We do - time to delete the course.
  57      require_sesskey();
  58  
  59      $strdeletingcourse = get_string("deletingcourse", "", $courseshortname);
  60  
  61      $PAGE->navbar->add($strdeletingcourse);
  62      $PAGE->set_title($strdeletingcourse);
  63      $PAGE->set_heading($SITE->fullname);
  64  
  65      echo $OUTPUT->header();
  66      echo $OUTPUT->heading($strdeletingcourse);
  67      // This might take a while. Raise the execution time limit.
  68      core_php_time_limit::raise();
  69  
  70      // We do this here because it spits out feedback as it goes.
  71      echo $OUTPUT->footer();
  72      echo $OUTPUT->select_element_for_append();
  73  
  74      // Preemptively reset the navcache before closing, so it remains the same on shutdown.
  75      navigation_cache::destroy_volatile_caches();
  76      \core\session\manager::write_close();
  77  
  78      delete_course($course);
  79      echo $OUTPUT->heading( get_string("deletedcourse", "", $courseshortname) );
  80      // Update course count in categories.
  81      fix_course_sortorder();
  82      echo $OUTPUT->continue_button($categoryurl);
  83      exit; // We must exit here!!!
  84  }
  85  
  86  $strdeletecheck = get_string("deletecheck", "", $courseshortname);
  87  
  88  $PAGE->navbar->add($strdeletecheck);
  89  $PAGE->set_title($strdeletecheck);
  90  $PAGE->set_heading($SITE->fullname);
  91  echo $OUTPUT->header();
  92  
  93  // Only let user delete this course if there is not an async backup in progress.
  94  if (!async_helper::is_async_pending($id, 'course', 'backup')) {
  95      $strdeletecoursecheck = get_string("deletecoursecheck");
  96      $message = "{$strdeletecoursecheck}<br /><br />{$coursefullname} ({$courseshortname})";
  97  
  98      $continueurl = new moodle_url('/course/delete.php', array('id' => $course->id, 'delete' => md5($course->timemodified)));
  99      $continuebutton = new single_button($continueurl, get_string('delete'), 'post');
 100      echo $OUTPUT->confirm($message, $continuebutton, $categoryurl);
 101  } else {
 102      // Async backup is pending, don't let user delete course.
 103      echo $OUTPUT->notification(get_string('pendingasyncerror', 'backup'), 'error');
 104      echo $OUTPUT->container(get_string('pendingasyncdeletedetail', 'backup'));
 105      echo $OUTPUT->continue_button($categoryurl);
 106  }
 107  
 108  echo $OUTPUT->footer();
 109  exit;