MDL-30514 registration: table aliases don't use AS keyword
[moodle.git] / course / delete_category_form.php
blobf2d394cd90754dcd8d694f9bec4d37b220ac51fb
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->libdir.'/formslib.php');
8 require_once($CFG->libdir.'/questionlib.php');
10 class delete_category_form extends moodleform {
12 var $_category;
14 function definition() {
15 global $CFG, $DB;
17 $mform =& $this->_form;
18 $category = $this->_customdata;
19 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
20 $this->_category = $category;
22 /// Check permissions, to see if it OK to give the option to delete
23 /// the contents, rather than move elsewhere.
24 /// Are there any subcategories of this one, can they be deleted?
25 $candeletecontent = true;
26 $tocheck = get_child_categories($category->id);
27 $containscategories = !empty($tocheck);
28 $categoryids = array($category->id);
29 while (!empty($tocheck)) {
30 $checkcat = array_pop($tocheck);
31 $childcategoryids[] = $checkcat->id;
32 $tocheck = $tocheck + get_child_categories($checkcat->id);
33 $chcontext = get_context_instance(CONTEXT_COURSECAT, $checkcat->id);
34 if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) {
35 $candeletecontent = false;
39 /// Are there any courses in here, can they be deleted?
40 list($test, $params) = $DB->get_in_or_equal($categoryids);
41 $containedcourses = $DB->get_records_sql(
42 "SELECT id,1 FROM {course} c WHERE c.category $test", $params);
43 $containscourses = false;
44 if ($containedcourses) {
45 $containscourses = true;
46 foreach ($containedcourses as $courseid => $notused) {
47 if ($candeletecontent && !can_delete_course($courseid)) {
48 $candeletecontent = false;
49 break;
54 /// Are there any questions in the question bank here?
55 $containsquestions = question_context_has_any_questions($categorycontext);
57 /// Get the list of categories we might be able to move to.
58 $testcaps = array();
59 if ($containscourses) {
60 $testcaps[] = 'moodle/course:create';
62 if ($containscategories || $containsquestions) {
63 $testcaps[] = 'moodle/category:manage';
65 $displaylist = array();
66 $notused = array();
67 if (!empty($testcaps)) {
68 make_categories_list($displaylist, $notused, $testcaps, $category->id);
71 /// Now build the options.
72 $options = array();
73 if ($displaylist) {
74 $options[0] = get_string('movecontentstoanothercategory');
76 if ($candeletecontent) {
77 $options[1] = get_string('deleteallcannotundo');
80 /// Now build the form.
81 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name, true, array('context' => $categorycontext))));
83 if ($containscourses || $containscategories || $containsquestions) {
84 if (empty($options)) {
85 print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name, true, array('context' => $categorycontext)));
88 /// Describe the contents of this category.
89 $contents = '<ul>';
90 if ($containscategories) {
91 $contents .= '<li>' . get_string('subcategories') . '</li>';
93 if ($containscourses) {
94 $contents .= '<li>' . get_string('courses') . '</li>';
96 if ($containsquestions) {
97 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
99 $contents .= '</ul>';
100 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);
102 /// Give the options for what to do.
103 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
104 if (count($options) == 1) {
105 $mform->hardFreeze('fulldelete');
106 $mform->setConstant('fulldelete', reset(array_keys($options)));
109 if ($displaylist) {
110 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
111 if (in_array($category->parent, $displaylist)) {
112 $mform->setDefault('newparent', $category->parent);
114 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
116 } else {
117 $mform->addElement('hidden', 'fulldelete', 1);
118 $mform->setType('fulldelete', PARAM_INT);
119 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
122 $mform->addElement('hidden', 'delete');
123 $mform->setType('delete', PARAM_ALPHANUM);
124 $mform->addElement('hidden', 'sure');
125 $mform->setType('sure', PARAM_ALPHANUM);
126 $mform->setDefault('sure', md5(serialize($category)));
128 //--------------------------------------------------------------------------------
129 $this->add_action_buttons(true, get_string('delete'));
133 /// perform some extra moodle validation
134 function validation($data, $files) {
135 $errors = parent::validation($data, $files);
137 if (empty($data['fulldelete']) && empty($data['newparent'])) {
138 /// When they have chosen the move option, they must specify a destination.
139 $errors['newparent'] = get_string('required');
142 if ($data['sure'] != md5(serialize($this->_category))) {
143 $errors['categorylabel'] = get_string('categorymodifiedcancel');
146 return $errors;