MDL-39103 lesson: Fix lesson administration block
[moodle.git] / course / classes / deletecategory_form.php
blob0d171c45f234704a23d605f845f8c5f96830ef97
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');
29 require_once($CFG->libdir . '/coursecatlib.php');
31 /**
32 * Delete category moodleform.
33 * @package core_course
34 * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_course_deletecategory_form extends moodleform {
39 /**
40 * The coursecat object for that category being deleted.
41 * @var coursecat
43 protected $coursecat;
45 /**
46 * Defines the form.
48 public function definition() {
49 $mform = $this->_form;
50 $this->coursecat = $this->_customdata;
52 $categorycontext = context_coursecat::instance($this->coursecat->id);
53 $categoryname = $this->coursecat->get_formatted_name();
55 // Check permissions, to see if it OK to give the option to delete
56 // the contents, rather than move elsewhere.
57 $candeletecontent = $this->coursecat->can_delete_full();
59 // Get the list of categories we might be able to move to.
60 $displaylist = $this->coursecat->move_content_targets_list();
62 // Now build the options.
63 $options = array();
64 if ($displaylist) {
65 $options[0] = get_string('movecontentstoanothercategory');
67 if ($candeletecontent) {
68 $options[1] = get_string('deleteallcannotundo');
70 if (empty($options)) {
71 print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname);
74 // Now build the form.
75 $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname));
77 // Describe the contents of this category.
78 $contents = '';
79 if ($this->coursecat->has_children()) {
80 $contents .= '<li>' . get_string('subcategories') . '</li>';
82 if ($this->coursecat->has_courses()) {
83 $contents .= '<li>' . get_string('courses') . '</li>';
85 if (question_context_has_any_questions($categorycontext)) {
86 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
88 if (!empty($contents)) {
89 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
90 } else {
91 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
94 // Give the options for what to do.
95 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
96 if (count($options) == 1) {
97 $optionkeys = array_keys($options);
98 $option = reset($optionkeys);
99 $mform->hardFreeze('fulldelete');
100 $mform->setConstant('fulldelete', $option);
103 if ($displaylist) {
104 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
105 if (in_array($this->coursecat->parent, $displaylist)) {
106 $mform->setDefault('newparent', $this->coursecat->parent);
108 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
111 $mform->addElement('hidden', 'categoryid', $this->coursecat->id);
112 $mform->setType('categoryid', PARAM_ALPHANUM);
113 $mform->addElement('hidden', 'action', 'deletecategory');
114 $mform->setType('action', PARAM_ALPHANUM);
115 $mform->addElement('hidden', 'sure');
116 // This gets set by default to ensure that if the user changes it manually we can detect it.
117 $mform->setDefault('sure', md5(serialize($this->coursecat)));
118 $mform->setType('sure', PARAM_ALPHANUM);
120 $this->add_action_buttons(true, get_string('delete'));
124 * Perform some extra moodle validation.
126 * @param array $data
127 * @param array $files
128 * @return array An array of errors.
130 public function validation($data, $files) {
131 $errors = parent::validation($data, $files);
132 if (empty($data['fulldelete']) && empty($data['newparent'])) {
133 // When they have chosen the move option, they must specify a destination.
134 $errors['newparent'] = get_string('required');
137 if ($data['sure'] !== md5(serialize($this->coursecat))) {
138 $errors['categorylabel'] = get_string('categorymodifiedcancel');
141 return $errors;