Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/course/ -> delete.php (source)

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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      print_error('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("$SITE->shortname: $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("$SITE->shortname: $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($continueurl, get_string('delete'), 'post');
  91      echo $OUTPUT->confirm($message, $continuebutton, $categoryurl);
  92  } else {
  93      // Async backup is pending, don't let user delete course.
  94      echo $OUTPUT->notification(get_string('pendingasyncerror', 'backup'), 'error');
  95      echo $OUTPUT->container(get_string('pendingasyncdeletedetail', 'backup'));
  96      echo $OUTPUT->continue_button($categoryurl);
  97  }
  98  
  99  echo $OUTPUT->footer();
 100  exit;