MDL-73758 availability: fix info::is_available_for_all()
[moodle.git] / course / delete.php
blob66bb257276f5c1efcb04e8252a9bd3978bd8b734
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.
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/>.
17 /**
18 * Admin-only code to delete a course utterly.
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
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');
29 $id = required_param('id', PARAM_INT); // Course ID.
30 $delete = optional_param('delete', '', PARAM_ALPHANUM); // Confirmation hash.
32 $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
33 $coursecontext = context_course::instance($course->id);
35 require_login();
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');
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)));
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));
52 // Check if we've got confirmation.
53 if ($delete === md5($course->timemodified)) {
54 // We do - time to delete the course.
55 require_sesskey();
57 $strdeletingcourse = get_string("deletingcourse", "", $courseshortname);
59 $PAGE->navbar->add($strdeletingcourse);
60 $PAGE->set_title("$SITE->shortname: $strdeletingcourse");
61 $PAGE->set_heading($SITE->fullname);
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!!!
77 $strdeletecheck = get_string("deletecheck", "", $courseshortname);
79 $PAGE->navbar->add($strdeletecheck);
80 $PAGE->set_title("$SITE->shortname: $strdeletecheck");
81 $PAGE->set_heading($SITE->fullname);
82 echo $OUTPUT->header();
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})";
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);
99 echo $OUTPUT->footer();
100 exit;