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
{
14 function definition() {
17 $mform =& $this->_form
;
18 $category = $this->_customdata
;
19 ensure_context_subobj_present($category, CONTEXT_COURSECAT
);
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 if ($candeletecontent && !has_capability('moodle/category:manage', $checkcat->context
)) {
34 $candeletecontent = false;
38 /// Are there any courses in here, can they be deleted?
39 $containedcourses = get_records_sql("
40 SELECT id,1 FROM {$CFG->prefix}course c
41 WHERE c.category IN (" . implode(',', $categoryids) . ")");
42 $containscourses = false;
43 if ($containedcourses) {
44 $containscourses = true;
45 foreach ($containedcourses as $courseid => $notused) {
46 if ($candeletecontent && !can_delete_course($courseid)) {
47 $candeletecontent = false;
53 /// Are there any questions in the question bank here?
54 $containsquestions = question_context_has_any_questions($category->context
);
56 /// Get the list of categories we might be able to move to.
58 if ($containscourses) {
59 $testcaps[] = 'moodle/course:create';
61 if ($containscategories ||
$containsquestions) {
62 $testcaps[] = 'moodle/category:manage';
64 $displaylist = array();
66 if (!empty($testcaps)) {
67 make_categories_list($displaylist, $notused, $testcaps, $category->id
);
70 /// Now build the options.
73 $options[0] = get_string('movecontentstoanothercategory');
75 if ($candeletecontent) {
76 $options[1] = get_string('deleteallcannotundo');
79 /// Now build the form.
80 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name
)));
82 if ($containscourses ||
$containscategories ||
$containsquestions) {
83 if (empty($options)) {
84 print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name
));
87 /// Describe the contents of this category.
89 if ($containscategories) {
90 $contents .= '<li>' . get_string('subcategories') . '</li>';
92 if ($containscourses) {
93 $contents .= '<li>' . get_string('courses') . '</li>';
95 if ($containsquestions) {
96 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
99 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);
101 /// Give the options for what to do.
102 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
103 if (count($options) == 1) {
104 $mform->hardFreeze('fulldelete');
105 $mform->setConstant('fulldelete', reset(array_keys($options)));
109 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
110 if (in_array($category->parent
, $displaylist)) {
111 $mform->setDefault('newparent', $category->parent
);
113 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
116 $mform->addElement('hidden', 'fulldelete', 1);
117 $mform->setType('fulldelete', PARAM_INT
);
118 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
121 $mform->addElement('hidden', 'delete');
122 $mform->setType('delete', PARAM_ALPHANUM
);
123 $mform->addElement('hidden', 'sure');
124 $mform->setType('sure', PARAM_ALPHANUM
);
125 $mform->setDefault('sure', md5(serialize($category)));
127 //--------------------------------------------------------------------------------
128 $this->add_action_buttons(true, get_string('delete'));
132 /// perform some extra moodle validation
133 function validation($data, $files) {
134 $errors = parent
::validation($data, $files);
136 if (empty($data['fulldelete']) && empty($data['newparent'])) {
137 /// When they have chosen the move option, they must specify a destination.
138 $errors['newparent'] = get_string('required');
141 if ($data['sure'] != md5(serialize($this->_category
))) {
142 $errors['categorylabel'] = get_string('categorymodifiedcancel');