Merge branch 'MDL-66900-39' of https://github.com/paulholden/moodle into MOODLE_39_STABLE
[moodle.git] / course / classes / editcategory_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 * Edit 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');
5dc361e1
SH
28
29/**
30 * Edit category form.
31 *
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_editcategory_form extends moodleform {
37
38 /**
39 * The form definition.
40 */
41 public function definition() {
42 global $CFG, $DB;
43 $mform = $this->_form;
44 $categoryid = $this->_customdata['categoryid'];
45 $parent = $this->_customdata['parent'];
46
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');
51 }
52 if ($categoryid) {
53 // Editing an existing category.
442f12f8 54 $options += core_course_category::make_categories_list('moodle/category:manage', $categoryid);
5dc361e1
SH
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));
58 }
59 $strsubmit = get_string('savechanges');
60 } else {
61 // Making a new category.
442f12f8 62 $options += core_course_category::make_categories_list('moodle/category:manage');
5dc361e1
SH
63 $strsubmit = get_string('createcategory');
64 }
65
66 $mform->addElement('select', 'parent', get_string('parentcategory'), $options);
67
68 $mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
69 $mform->addRule('name', get_string('required'), 'required', null);
70 $mform->setType('name', PARAM_TEXT);
71
72 $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'), 'maxlength="100" size="10"');
73 $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
74 $mform->setType('idnumber', PARAM_RAW);
75
76 $mform->addElement('editor', 'description_editor', get_string('description'), null,
77 $this->get_description_editor_options());
a97c45a1 78 $mform->setType('description_editor', PARAM_RAW);
5dc361e1
SH
79
80 if (!empty($CFG->allowcategorythemes)) {
81 $themes = array(''=>get_string('forceno'));
82 $allthemes = get_list_of_themes();
83 foreach ($allthemes as $key => $theme) {
84 if (empty($theme->hidefromselector)) {
85 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
86 }
87 }
88 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
89 }
90
91 $mform->addElement('hidden', 'id', 0);
92 $mform->setType('id', PARAM_INT);
93 $mform->setDefault('id', $categoryid);
94
95 $this->add_action_buttons(true, $strsubmit);
96 }
97
98 /**
99 * Returns the description editor options.
100 * @return array
101 */
102 public function get_description_editor_options() {
103 global $CFG;
104 $context = $this->_customdata['context'];
105 $itemid = $this->_customdata['itemid'];
106 return array(
107 'maxfiles' => EDITOR_UNLIMITED_FILES,
108 'maxbytes' => $CFG->maxbytes,
a97c45a1
PH
109 'trusttext' => false,
110 'noclean' => true,
5dc361e1
SH
111 'context' => $context,
112 'subdirs' => file_area_contains_subdirs($context, 'coursecat', 'description', $itemid),
113 );
114 }
115
116 /**
117 * Validates the data submit for this form.
118 *
119 * @param array $data An array of key,value data pairs.
120 * @param array $files Any files that may have been submit as well.
121 * @return array An array of errors.
122 */
123 public function validation($data, $files) {
124 global $DB;
125 $errors = parent::validation($data, $files);
126 if (!empty($data['idnumber'])) {
127 if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
128 if (!$data['id'] || $existing->id != $data['id']) {
bd3c9587 129 $errors['idnumber'] = get_string('categoryidnumbertaken', 'error');
5dc361e1
SH
130 }
131 }
132 }
133 return $errors;
134 }
bd3c9587 135}