Merge branch 'MDL-34573_accessibility' of git://github.com/rwijaya/moodle
[moodle.git] / course / editcategory_form.php
blob3e3cfe32fad95c68b167e30b6e077bf054949e68
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
7 class editcategory_form extends moodleform {
9 // form definition
10 function definition() {
11 global $CFG, $DB;
12 $mform =& $this->_form;
13 $category = $this->_customdata['category'];
14 $editoroptions = $this->_customdata['editoroptions'];
16 // get list of categories to use as parents, with site as the first one
17 $options = array();
18 if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) {
19 $options[0] = get_string('top');
21 $parents = array();
22 if ($category->id) {
23 // Editing an existing category.
24 make_categories_list($options, $parents, 'moodle/category:manage', $category->id);
25 if (empty($options[$category->parent])) {
26 $options[$category->parent] = $DB->get_field('course_categories', 'name', array('id'=>$category->parent));
28 $strsubmit = get_string('savechanges');
29 } else {
30 // Making a new category
31 make_categories_list($options, $parents, 'moodle/category:manage');
32 $strsubmit = get_string('createcategory');
35 $mform->addElement('select', 'parent', get_string('parentcategory'), $options);
36 $mform->addElement('text', 'name', get_string('categoryname'), array('size'=>'30'));
37 $mform->addRule('name', get_string('required'), 'required', null);
38 $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'),'maxlength="100" size="10"');
39 $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
40 $mform->addElement('editor', 'description_editor', get_string('description'), null, $editoroptions);
41 $mform->setType('description_editor', PARAM_RAW);
42 if (!empty($CFG->allowcategorythemes)) {
43 $themes = array(''=>get_string('forceno'));
44 $allthemes = get_list_of_themes();
45 foreach ($allthemes as $key=>$theme) {
46 if (empty($theme->hidefromselector)) {
47 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
50 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
53 $mform->addElement('hidden', 'id', 0);
54 $mform->setType('id', PARAM_INT);
55 $mform->setDefault('id', $category->id);
57 $this->add_action_buttons(true, $strsubmit);
60 function validation($data, $files) {
61 global $DB;
62 $errors = parent::validation($data, $files);
63 if (!empty($data['idnumber'])) {
64 if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
65 if (!$data['id'] || $existing->id != $data['id']) {
66 $errors['idnumber']= get_string('idnumbertaken');
71 return $errors;