on-demand release 3.8dev+
[moodle.git] / course / classes / deletecategory_form.php
blobb75f19f0fc8123d7ac58aac4f0521d82912a222d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Delete category form.
20 * @package core_course
21 * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 require_once($CFG->libdir . '/formslib.php');
28 require_once($CFG->libdir . '/questionlib.php');
30 /**
31 * Delete category moodleform.
32 * @package core_course
33 * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_course_deletecategory_form extends moodleform {
38 /**
39 * The core_course_category object for that category being deleted.
40 * @var core_course_category
42 protected $coursecat;
44 /**
45 * Defines the form.
47 public function definition() {
48 $mform = $this->_form;
49 $this->coursecat = $this->_customdata;
51 $categorycontext = context_coursecat::instance($this->coursecat->id);
52 $categoryname = $this->coursecat->get_formatted_name();
54 // Check permissions, to see if it OK to give the option to delete
55 // the contents, rather than move elsewhere.
56 $candeletecontent = $this->coursecat->can_delete_full();
58 // Get the list of categories we might be able to move to.
59 $displaylist = $this->coursecat->move_content_targets_list();
61 // Now build the options.
62 $options = array();
63 if ($displaylist) {
64 $options[0] = get_string('movecontentstoanothercategory');
66 if ($candeletecontent) {
67 $options[1] = get_string('deleteallcannotundo');
69 if (empty($options)) {
70 print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname);
73 // Now build the form.
74 $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname));
76 // Describe the contents of this category.
77 $contents = '';
78 if ($this->coursecat->has_children()) {
79 $contents .= '<li>' . get_string('subcategories') . '</li>';
81 if ($this->coursecat->has_courses()) {
82 $contents .= '<li>' . get_string('courses') . '</li>';
84 if (question_context_has_any_questions($categorycontext)) {
85 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
87 if (!empty($contents)) {
88 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
89 } else {
90 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
93 // Give the options for what to do.
94 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
95 if (count($options) == 1) {
96 $optionkeys = array_keys($options);
97 $option = reset($optionkeys);
98 $mform->hardFreeze('fulldelete');
99 $mform->setConstant('fulldelete', $option);
102 if ($displaylist) {
103 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
104 if (in_array($this->coursecat->parent, $displaylist)) {
105 $mform->setDefault('newparent', $this->coursecat->parent);
107 $mform->hideIf('newparent', 'fulldelete', 'eq', '1');
110 $mform->addElement('hidden', 'categoryid', $this->coursecat->id);
111 $mform->setType('categoryid', PARAM_ALPHANUM);
112 $mform->addElement('hidden', 'action', 'deletecategory');
113 $mform->setType('action', PARAM_ALPHANUM);
114 $mform->addElement('hidden', 'sure');
115 // This gets set by default to ensure that if the user changes it manually we can detect it.
116 $mform->setDefault('sure', md5(serialize($this->coursecat)));
117 $mform->setType('sure', PARAM_ALPHANUM);
119 $this->add_action_buttons(true, get_string('delete'));
123 * Perform some extra moodle validation.
125 * @param array $data
126 * @param array $files
127 * @return array An array of errors.
129 public function validation($data, $files) {
130 $errors = parent::validation($data, $files);
131 if (empty($data['fulldelete']) && empty($data['newparent'])) {
132 // When they have chosen the move option, they must specify a destination.
133 $errors['newparent'] = get_string('required');
136 if ($data['sure'] !== md5(serialize($this->coursecat))) {
137 $errors['categorylabel'] = get_string('categorymodifiedcancel');
140 return $errors;