weekly release 4.5dev
[moodle.git] / course / classes / deletecategory_form.php
blob713d9c4b8ac43c75b4aabfd87273f015671bf301
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 throw new \moodle_exception('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 .= html_writer::tag('li', get_string('subcategories'));
81 if ($this->coursecat->has_courses()) {
82 $contents .= html_writer::tag('li', get_string('courses'));
84 if (question_context_has_any_questions($categorycontext)) {
85 $contents .= html_writer::tag('li', get_string('questionsinthequestionbank'));
88 // Check if plugins can provide more info.
89 $pluginfunctions = $this->coursecat->get_plugins_callback_function('get_course_category_contents');
90 foreach ($pluginfunctions as $pluginfunction) {
91 if ($plugincontents = $pluginfunction($this->coursecat)) {
92 $contents .= html_writer::tag('li', $plugincontents);
96 if (!empty($contents)) {
97 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
98 } else {
99 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
102 // Give the options for what to do.
103 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
105 if (count($options) == 1) {
106 // Freeze selector if only one option available.
107 $optionkeys = array_keys($options);
108 $option = reset($optionkeys);
109 $mform->hardFreeze('fulldelete');
110 $mform->setConstant('fulldelete', $option);
113 if ($displaylist) {
114 $mform->addElement('autocomplete', 'newparent', get_string('movecategorycontentto'), $displaylist);
115 if (in_array($this->coursecat->parent, $displaylist)) {
116 $mform->setDefault('newparent', $this->coursecat->parent);
118 $mform->hideIf('newparent', 'fulldelete', 'eq', '1');
121 $mform->addElement('hidden', 'categoryid', $this->coursecat->id);
122 $mform->setType('categoryid', PARAM_ALPHANUM);
123 $mform->addElement('hidden', 'action', 'deletecategory');
124 $mform->setType('action', PARAM_ALPHANUM);
126 $this->add_action_buttons(true, get_string('delete'));
130 * Perform some extra moodle validation.
132 * @param array $data
133 * @param array $files
134 * @return array An array of errors.
136 public function validation($data, $files) {
137 $errors = parent::validation($data, $files);
138 if (empty($data['fulldelete']) && empty($data['newparent'])) {
139 // When they have chosen the move option, they must specify a destination.
140 $errors['newparent'] = get_string('required');
141 return $errors;
144 if (!empty($data['newparent']) && !$this->coursecat->can_move_content_to($data['newparent'])) {
145 $errors['newparent'] = get_string('movecatcontentstoselected', 'error');
148 return $errors;