Merge branch MDL-29187-master of git://github.com/samhemelryk/moodle
[moodle.git] / course / delete.php
1 <?php
2       // Admin-only code to delete a course utterly
4     require_once(dirname(__FILE__) . '/../config.php');
5     require_once($CFG->dirroot . '/course/lib.php');
7     $id     = required_param('id', PARAM_INT);              // course id
8     $delete = optional_param('delete', '', PARAM_ALPHANUM); // delete confirmation hash
10     $PAGE->set_url('/course/delete.php', array('id' => $id));
11     $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
12     require_login();
14     $site = get_site();
16     $strdeletecourse = get_string("deletecourse");
17     $stradministration = get_string("administration");
18     $strcategories = get_string("categories");
20     if (! $course = $DB->get_record("course", array("id"=>$id))) {
21         print_error("invalidcourseid", 'error', '', $id);
22     }
23     if ($site->id == $course->id) {
24         // can not delete frontpage!
25         print_error("invalidcourseid", 'error', '', $id);
26     }
28     if (!can_delete_course($id)) {
29         print_error('cannotdeletecourse');
30     }
32     $category = $DB->get_record("course_categories", array("id"=>$course->category));
33     $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
34     $categoryname = format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id)));
36     $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php/'));
37     $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
38     $PAGE->navbar->add($categoryname, new moodle_url('/course/category.php', array('id'=>$course->category)));
39     if (! $delete) {
40         $strdeletecheck = get_string("deletecheck", "", $courseshortname);
41         $strdeletecoursecheck = get_string("deletecoursecheck");
43         $PAGE->navbar->add($strdeletecheck);
44         $PAGE->set_title("$site->shortname: $strdeletecheck");
45         $PAGE->set_heading($site->fullname);
46         echo $OUTPUT->header();
48         $message = "$strdeletecoursecheck<br /><br />" . format_string($course->fullname) .  " (" . $courseshortname . ")";
49         echo $OUTPUT->confirm($message, "delete.php?id=$course->id&delete=".md5($course->timemodified), "category.php?id=$course->category");
51         echo $OUTPUT->footer();
52         exit;
53     }
55     if ($delete != md5($course->timemodified)) {
56         print_error("invalidmd5");
57     }
59     if (!confirm_sesskey()) {
60         print_error('confirmsesskeybad', 'error');
61     }
63     // OK checks done, delete the course now.
65     add_to_log(SITEID, "course", "delete", "view.php?id=$course->id", "$course->fullname (ID $course->id)");
67     $strdeletingcourse = get_string("deletingcourse", "", $courseshortname);
69     $PAGE->navbar->add($strdeletingcourse);
70     $PAGE->set_title("$site->shortname: $strdeletingcourse");
71     $PAGE->set_heading($site->fullname);
72     echo $OUTPUT->header();
73     echo $OUTPUT->heading($strdeletingcourse);
75     delete_course($course);
76     fix_course_sortorder(); //update course count in catagories
78     echo $OUTPUT->heading( get_string("deletedcourse", "", $courseshortname) );
80     echo $OUTPUT->continue_button("category.php?id=$course->category");
82     echo $OUTPUT->footer();