MDL-36209 Allow teachers to assess submissions during the grading evaluation phase
[moodle.git] / course / delete_category_form.php
blob8c32405e5ca66a7a3194a88f3183dfe9a5b3d229
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 = context_coursecat::instance($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 = context_coursecat::instance($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 $optionkeys = array_keys($options);
106 $option = reset($optionkeys);
107 $mform->hardFreeze('fulldelete');
108 $mform->setConstant('fulldelete', $option);
111 if ($displaylist) {
112 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
113 if (in_array($category->parent, $displaylist)) {
114 $mform->setDefault('newparent', $category->parent);
116 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
118 } else {
119 $mform->addElement('hidden', 'fulldelete', 1);
120 $mform->setType('fulldelete', PARAM_INT);
121 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
124 $mform->addElement('hidden', 'delete');
125 $mform->setType('delete', PARAM_ALPHANUM);
126 $mform->addElement('hidden', 'sure');
127 $mform->setType('sure', PARAM_ALPHANUM);
128 $mform->setDefault('sure', md5(serialize($category)));
130 //--------------------------------------------------------------------------------
131 $this->add_action_buttons(true, get_string('delete'));
135 /// perform some extra moodle validation
136 function validation($data, $files) {
137 $errors = parent::validation($data, $files);
139 if (empty($data['fulldelete']) && empty($data['newparent'])) {
140 /// When they have chosen the move option, they must specify a destination.
141 $errors['newparent'] = get_string('required');
144 if ($data['sure'] != md5(serialize($this->_category))) {
145 $errors['categorylabel'] = get_string('categorymodifiedcancel');
148 return $errors;