MDL-64557 category: switch disabledIf to hideIf on delete form
[moodle.git] / course / classes / deletecategory_form.php
CommitLineData
5dc361e1
SH
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.
13//
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/>.
16
17/**
18 * Delete category form.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die;
26
27require_once($CFG->libdir . '/formslib.php');
28require_once($CFG->libdir . '/questionlib.php');
5dc361e1
SH
29
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
35 */
36class core_course_deletecategory_form extends moodleform {
37
38 /**
442f12f8
MG
39 * The core_course_category object for that category being deleted.
40 * @var core_course_category
5dc361e1
SH
41 */
42 protected $coursecat;
43
44 /**
45 * Defines the form.
46 */
47 public function definition() {
48 $mform = $this->_form;
49 $this->coursecat = $this->_customdata;
50
51 $categorycontext = context_coursecat::instance($this->coursecat->id);
52 $categoryname = $this->coursecat->get_formatted_name();
53
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();
57
58 // Get the list of categories we might be able to move to.
59 $displaylist = $this->coursecat->move_content_targets_list();
60
61 // Now build the options.
62 $options = array();
63 if ($displaylist) {
64 $options[0] = get_string('movecontentstoanothercategory');
65 }
66 if ($candeletecontent) {
67 $options[1] = get_string('deleteallcannotundo');
68 }
69 if (empty($options)) {
70 print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname);
71 }
72
73 // Now build the form.
74 $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname));
75
76 // Describe the contents of this category.
77 $contents = '';
78 if ($this->coursecat->has_children()) {
79 $contents .= '<li>' . get_string('subcategories') . '</li>';
80 }
81 if ($this->coursecat->has_courses()) {
82 $contents .= '<li>' . get_string('courses') . '</li>';
83 }
84 if (question_context_has_any_questions($categorycontext)) {
85 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
86 }
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'));
91 }
92
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);
100 }
101
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);
106 }
30968654 107 $mform->hideIf('newparent', 'fulldelete', 'eq', '1');
5dc361e1
SH
108 }
109
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);
118
119 $this->add_action_buttons(true, get_string('delete'));
120 }
121
122 /**
123 * Perform some extra moodle validation.
124 *
125 * @param array $data
126 * @param array $files
127 * @return array An array of errors.
128 */
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');
134 }
135
136 if ($data['sure'] !== md5(serialize($this->coursecat))) {
137 $errors['categorylabel'] = get_string('categorymodifiedcancel');
138 }
139
140 return $errors;
141 }
142}