forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_category_form.php
104 lines (84 loc) · 3.94 KB
/
delete_category_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');
class delete_category_form extends moodleform {
var $_category;
function definition() {
$mform = & $this->_form;
$this->_category = $this->_customdata;
$categorycontext = context_coursecat::instance($this->_category->id);
// Check permissions, to see if it OK to give the option to delete
// the contents, rather than move elsewhere.
$candeletecontent = $this->_category->can_delete_full();
// Get the list of categories we might be able to move to.
$displaylist = $this->_category->move_content_targets_list();
// Now build the options.
$options = array();
if ($displaylist) {
$options[0] = get_string('movecontentstoanothercategory');
}
if ($candeletecontent) {
$options[1] = get_string('deleteallcannotundo');
}
if (empty($options)) {
print_error('youcannotdeletecategory', 'error', 'index.php', $this->_category->get_formatted_name());
}
// Now build the form.
$mform->addElement('header','general', get_string('categorycurrentcontents', '', $this->_category->get_formatted_name()));
// Describe the contents of this category.
$contents = '';
if ($this->_category->has_children()) {
$contents .= '<li>' . get_string('subcategories') . '</li>';
}
if ($this->_category->has_courses()) {
$contents .= '<li>' . get_string('courses') . '</li>';
}
if (question_context_has_any_questions($categorycontext)) {
$contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
}
if (!empty($contents)) {
$mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
} else {
$mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
}
// Give the options for what to do.
$mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
if (count($options) == 1) {
$optionkeys = array_keys($options);
$option = reset($optionkeys);
$mform->hardFreeze('fulldelete');
$mform->setConstant('fulldelete', $option);
}
if ($displaylist) {
$mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
if (in_array($this->_category->parent, $displaylist)) {
$mform->setDefault('newparent', $this->_category->parent);
}
$mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
}
$mform->addElement('hidden', 'deletecat');
$mform->setType('deletecat', PARAM_ALPHANUM);
$mform->addElement('hidden', 'sure');
$mform->setType('sure', PARAM_ALPHANUM);
$mform->setDefault('sure', md5(serialize($this->_category)));
//--------------------------------------------------------------------------------
$this->add_action_buttons(true, get_string('delete'));
$this->set_data(array('deletecat' => $this->_category->id));
}
/// perform some extra moodle validation
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['fulldelete']) && empty($data['newparent'])) {
/// When they have chosen the move option, they must specify a destination.
$errors['newparent'] = get_string('required');
}
if ($data['sure'] != md5(serialize($this->_category))) {
$errors['categorylabel'] = get_string('categorymodifiedcancel');
}
return $errors;
}
}