timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / course / delete_category_form.php
blob8e63d9f18da890a7bd0ab8bd990aa17f0bb82c97
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
4 require_once($CFG->libdir . '/questionlib.php');
6 class delete_category_form extends moodleform {
8 var $_category;
10 function definition() {
11 global $CFG;
13 $mform =& $this->_form;
14 $category = $this->_customdata;
15 ensure_context_subobj_present($category, CONTEXT_COURSECAT);
16 $this->_category = $category;
18 /// Check permissions, to see if it OK to give the option to delete
19 /// the contents, rather than move elsewhere.
20 /// Are there any subcategories of this one, can they be deleted?
21 $candeletecontent = true;
22 $tocheck = get_child_categories($category->id);
23 $containscategories = !empty($tocheck);
24 $categoryids = array($category->id);
25 while (!empty($tocheck)) {
26 $checkcat = array_pop($tocheck);
27 $childcategoryids[] = $checkcat->id;
28 $tocheck = $tocheck + get_child_categories($checkcat->id);
29 if ($candeletecontent && !has_capability('moodle/category:manage', $checkcat->context)) {
30 $candeletecontent = false;
34 /// Are there any courses in here, can they be deleted?
35 $containedcourses = get_records_sql("
36 SELECT id,1 FROM {$CFG->prefix}course c
37 WHERE c.category IN (" . implode(',', $categoryids) . ")");
38 $containscourses = false;
39 if ($containedcourses) {
40 $containscourses = true;
41 foreach ($containedcourses as $courseid => $notused) {
42 if ($candeletecontent && !can_delete_course($courseid)) {
43 $candeletecontent = false;
44 break;
49 /// Are there any questions in the question bank here?
50 $containsquestions = question_context_has_any_questions($category->context);
52 /// Get the list of categories we might be able to move to.
53 $testcaps = array();
54 if ($containscourses) {
55 $testcaps[] = 'moodle/course:create';
57 if ($containscategories || $containsquestions) {
58 $testcaps[] = 'moodle/category:manage';
60 $displaylist = array();
61 $notused = array();
62 if (!empty($testcaps)) {
63 make_categories_list($displaylist, $notused, $testcaps, $category->id);
66 /// Now build the options.
67 $options = array();
68 if ($displaylist) {
69 $options[0] = get_string('movecontentstoanothercategory');
71 if ($candeletecontent) {
72 $options[1] = get_string('deleteallcannotundo');
75 /// Now build the form.
76 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name)));
78 if ($containscourses || $containscategories || $containsquestions) {
79 if (empty($options)) {
80 print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name));
83 /// Describe the contents of this category.
84 $contents = '<ul>';
85 if ($containscategories) {
86 $contents .= '<li>' . get_string('subcategories') . '</li>';
88 if ($containscourses) {
89 $contents .= '<li>' . get_string('courses') . '</li>';
91 if ($containsquestions) {
92 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
94 $contents .= '</ul>';
95 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);
97 /// Give the options for what to do.
98 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
99 if (count($options) == 1) {
100 $mform->hardFreeze('fulldelete');
101 $mform->setConstant('fulldelete', reset(array_keys($options)));
104 if ($displaylist) {
105 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
106 if (in_array($category->parent, $displaylist)) {
107 $mform->setDefault('newparent', $category->parent);
109 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
111 } else {
112 $mform->addElement('hidden', 'fulldelete', 1);
113 $mform->setType('fulldelete', PARAM_INT);
114 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
117 $mform->addElement('hidden', 'delete');
118 $mform->setType('delete', PARAM_ALPHANUM);
119 $mform->addElement('hidden', 'sure');
120 $mform->setType('sure', PARAM_ALPHANUM);
121 $mform->setDefault('sure', md5(serialize($category)));
123 //--------------------------------------------------------------------------------
124 $this->add_action_buttons(true, get_string('delete'));
128 /// perform some extra moodle validation
129 function validation($data, $files) {
130 $errors = parent::validation($data, $files);
132 if (empty($data['fulldelete']) && empty($data['newparent'])) {
133 /// When they have chosen the move option, they must specify a destination.
134 $errors['newparent'] = get_string('required');
137 if ($data['sure'] != md5(serialize($this->_category))) {
138 $errors['categorylabel'] = get_string('categorymodifiedcancel');
141 return $errors;