weekly release 3.11.12+
[moodle.git] / course / classes / editcategory_form.php
blob1006fb4e572a11f18c7cfdebb892dd2022b02ad9
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 * Edit 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');
29 /**
30 * Edit category form.
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_editcategory_form extends moodleform {
38 /**
39 * The form definition.
41 public function definition() {
42 global $CFG, $DB;
43 $mform = $this->_form;
44 $categoryid = $this->_customdata['categoryid'];
45 $parent = $this->_customdata['parent'];
47 // Get list of categories to use as parents, with site as the first one.
48 $options = array();
49 if (has_capability('moodle/category:manage', context_system::instance()) || $parent == 0) {
50 $options[0] = get_string('top');
52 if ($categoryid) {
53 // Editing an existing category.
54 $options += core_course_category::make_categories_list('moodle/category:manage', $categoryid);
55 if (empty($options[$parent])) {
56 // Ensure the the category parent has been included in the options.
57 $options[$parent] = $DB->get_field('course_categories', 'name', array('id'=>$parent));
59 $strsubmit = get_string('savechanges');
60 } else {
61 // Making a new category.
62 $options += core_course_category::make_categories_list('moodle/category:manage');
63 $strsubmit = get_string('createcategory');
66 $mform->addElement('autocomplete', 'parent', get_string('parentcategory'), $options);
67 $mform->addRule('parent', null, 'required', null, 'client');
69 $mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
70 $mform->addRule('name', get_string('required'), 'required', null);
71 $mform->setType('name', PARAM_TEXT);
73 $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'), 'maxlength="100" size="10"');
74 $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
75 $mform->setType('idnumber', PARAM_RAW);
77 $mform->addElement('editor', 'description_editor', get_string('description'), null,
78 $this->get_description_editor_options());
79 $mform->setType('description_editor', PARAM_RAW);
81 if (!empty($CFG->allowcategorythemes)) {
82 $themes = array(''=>get_string('forceno'));
83 $allthemes = get_list_of_themes();
84 foreach ($allthemes as $key => $theme) {
85 if (empty($theme->hidefromselector)) {
86 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
89 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
92 $mform->addElement('hidden', 'id', 0);
93 $mform->setType('id', PARAM_INT);
94 $mform->setDefault('id', $categoryid);
96 $this->add_action_buttons(true, $strsubmit);
99 /**
100 * Returns the description editor options.
101 * @return array
103 public function get_description_editor_options() {
104 global $CFG;
105 $context = $this->_customdata['context'];
106 $itemid = $this->_customdata['itemid'];
107 return array(
108 'maxfiles' => EDITOR_UNLIMITED_FILES,
109 'maxbytes' => $CFG->maxbytes,
110 'trusttext' => false,
111 'noclean' => true,
112 'context' => $context,
113 'subdirs' => file_area_contains_subdirs($context, 'coursecat', 'description', $itemid),
118 * Validates the data submit for this form.
120 * @param array $data An array of key,value data pairs.
121 * @param array $files Any files that may have been submit as well.
122 * @return array An array of errors.
124 public function validation($data, $files) {
125 global $DB;
126 $errors = parent::validation($data, $files);
127 if (!empty($data['idnumber'])) {
128 if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
129 if (!$data['id'] || $existing->id != $data['id']) {
130 $errors['idnumber'] = get_string('categoryidnumbertaken', 'error');
134 return $errors;