Updated the 19 build version to 20081205
[moodle.git] / course / delete_category_form.php
blob82eb305c7c920ce43c4b56b25c37ef0db258c938
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 ensure_context_subobj_present($category, CONTEXT_COURSECAT);
15 $this->_category = $category;
17 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name)));
19 $displaylist = array();
20 $notused = array();
21 make_categories_list($displaylist, $notused, 'moodle/course:create', $category->id);
23 // Check permissions, to see if it OK to give the option to delete
24 // the contents, rather than move elsewhere.
25 $candeletecontent = true;
26 $tocheck = array($category);
27 while (!empty($tocheck)) {
28 $checkcat = array_pop($tocheck);
29 $tocheck = $tocheck + get_child_categories($checkcat->id);
30 if (!has_capability('moodle/category:manage', $checkcat->context)) {
31 $candeletecontent = false;
32 break;
36 // TODO check that the user is allowed to delete all the courses MDL-17502!
38 $options = array();
40 if ($displaylist) {
41 $options[0] = get_string('move');
44 if ($candeletecontent) {
45 $options[1] = get_string('delete');
48 if (empty($options)) {
49 print_error('nocategorydelete', 'error', 'index.php', format_string($category->name));
52 $mform->addElement('select', 'fulldelete', get_string('categorycontents'), $options);
53 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
54 $mform->setDefault('newparent', 0);
56 if ($displaylist) {
57 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
58 if (in_array($category->parent, $displaylist)) {
59 $mform->setDefault('newparent', $category->parent);
63 $mform->addElement('hidden', 'delete');
64 $mform->addElement('hidden', 'sure');
65 $mform->setDefault('sure', md5(serialize($category)));
67 //--------------------------------------------------------------------------------
68 $this->add_action_buttons(true, get_string('delete'));
72 /// perform some extra moodle validation
73 function validation($data, $files) {
74 $errors = parent::validation($data, $files);
76 if (!empty($data['fulldelete'])) {
77 // already verified
78 } else {
79 if (empty($data['newparent'])) {
80 $errors['newparent'] = get_string('required');
84 if ($data['sure'] != md5(serialize($this->_category))) {
85 $errors['categorylabel'] = get_string('categorymodifiedcancel');
88 return $errors;