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');
9 require_once($CFG->libdir
. '/coursecatlib.php');
11 class delete_category_form
extends moodleform
{
15 function definition() {
16 $mform = & $this->_form
;
17 $this->_category
= $this->_customdata
;
18 $categorycontext = context_coursecat
::instance($this->_category
->id
);
20 // Check permissions, to see if it OK to give the option to delete
21 // the contents, rather than move elsewhere.
22 $candeletecontent = $this->_category
->can_delete_full();
24 // Get the list of categories we might be able to move to.
25 $displaylist = $this->_category
->move_content_targets_list();
27 // Now build the options.
30 $options[0] = get_string('movecontentstoanothercategory');
32 if ($candeletecontent) {
33 $options[1] = get_string('deleteallcannotundo');
35 if (empty($options)) {
36 print_error('youcannotdeletecategory', 'error', 'index.php', $this->_category
->get_formatted_name());
39 // Now build the form.
40 $mform->addElement('header','general', get_string('categorycurrentcontents', '', $this->_category
->get_formatted_name()));
42 // Describe the contents of this category.
44 if ($this->_category
->has_children()) {
45 $contents .= '<li>' . get_string('subcategories') . '</li>';
47 if ($this->_category
->has_courses()) {
48 $contents .= '<li>' . get_string('courses') . '</li>';
50 if (question_context_has_any_questions($categorycontext)) {
51 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
53 if (!empty($contents)) {
54 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer
::tag('ul', $contents));
56 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
59 // Give the options for what to do.
60 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
61 if (count($options) == 1) {
62 $optionkeys = array_keys($options);
63 $option = reset($optionkeys);
64 $mform->hardFreeze('fulldelete');
65 $mform->setConstant('fulldelete', $option);
69 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
70 if (in_array($this->_category
->parent
, $displaylist)) {
71 $mform->setDefault('newparent', $this->_category
->parent
);
73 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
76 $mform->addElement('hidden', 'deletecat');
77 $mform->setType('deletecat', PARAM_ALPHANUM
);
78 $mform->addElement('hidden', 'sure');
79 $mform->setType('sure', PARAM_ALPHANUM
);
80 $mform->setDefault('sure', md5(serialize($this->_category
)));
82 //--------------------------------------------------------------------------------
83 $this->add_action_buttons(true, get_string('delete'));
85 $this->set_data(array('deletecat' => $this->_category
->id
));
88 /// perform some extra moodle validation
89 function validation($data, $files) {
90 $errors = parent
::validation($data, $files);
92 if (empty($data['fulldelete']) && empty($data['newparent'])) {
93 /// When they have chosen the move option, they must specify a destination.
94 $errors['newparent'] = get_string('required');
97 if ($data['sure'] != md5(serialize($this->_category
))) {
98 $errors['categorylabel'] = get_string('categorymodifiedcancel');