"MDL-12304, fix double text"
[moodle-linuxchix.git] / course / delete_category_form.php
blobf25194fedfa700e66ef18099871653a4e03cf78a
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class delete_category_form extends moodleform {
7 var $_category;
9 function definition() {
10 global $CFG;
12 $mform =& $this->_form;
13 $category = $this->_customdata;
14 $this->_category = $category;
16 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name)));
18 $displaylist = array();
19 $parentlist = array();
20 $children = array();
21 make_categories_list($displaylist, $parentlist);
22 unset($displaylist[$category->id]);
23 foreach ($displaylist as $catid=>$unused) {
24 // remove all children of $category
25 if (isset($parentlist[$catid]) and in_array($category->id, $parentlist[$catid])) {
26 $children[] = $catid;
27 unset($displaylist[$catid]);
28 continue;
30 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $catid))) {
31 unset($displaylist[$catid]);
35 $candeletecontent = true;
36 foreach ($children as $catid) {
37 $context = get_context_instance(CONTEXT_COURSECAT, $catid);
38 if (!has_capability('moodle/category:delete', $context)) {
39 $candeletecontent = false;
40 break;
44 $options = array();
46 if ($displaylist) {
47 $options[0] = get_string('move');
50 if ($candeletecontent) {
51 $options[1] =get_string('delete');
54 if (empty($options)) {
55 print_error('nocategorydelete', 'error', 'index.php', format_string($category->name));
58 $mform->addElement('select', 'fulldelete', get_string('categorycontents'), $options);
59 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
60 $mform->setDefault('newparent', 0);
62 if ($displaylist) {
63 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
64 if (in_array($category->parent, $displaylist)) {
65 $mform->setDefault('newparent', $category->parent);
69 $mform->addElement('hidden', 'delete');
70 $mform->addElement('hidden', 'sure');
71 $mform->setDefault('sure', md5(serialize($category)));
73 //--------------------------------------------------------------------------------
74 $this->add_action_buttons(true, get_string('delete'));
78 /// perform some extra moodle validation
79 function validation($data, $files) {
80 $errors = parent::validation($data, $files);
82 if (!empty($data['fulldelete'])) {
83 // already verified
84 } else {
85 if (empty($data['newparent'])) {
86 $errors['newparent'] = get_string('required');
90 if ($data['sure'] != md5(serialize($this->_category))) {
91 $errors['categorylabel'] = get_string('categorymodifiedcancel');
94 return $errors;