MDL-38886 course: missing setType calls when adding a new category
[moodle.git] / course / editcategory_form.php
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
4 }
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
7 require_once ($CFG->libdir.'/coursecatlib.php');
8 class editcategory_form extends moodleform {
10     // form definition
11     function definition() {
12         global $CFG, $DB;
13         $mform =& $this->_form;
14         $category = $this->_customdata['category'];
15         $editoroptions = $this->_customdata['editoroptions'];
17         // get list of categories to use as parents, with site as the first one
18         $options = array();
19         if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) {
20             $options[0] = get_string('top');
21         }
22         if ($category->id) {
23             // Editing an existing category.
24             $options += coursecat::make_categories_list('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));
27             }
28             $strsubmit = get_string('savechanges');
29         } else {
30             // Making a new category
31             $options += coursecat::make_categories_list('moodle/category:manage');
32             $strsubmit = get_string('createcategory');
33         }
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->setType('name', PARAM_TEXT);
39         $mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'),'maxlength="100"  size="10"');
40         $mform->addHelpButton('idnumber', 'idnumbercoursecategory');
41         $mform->setType('idnumber', PARAM_RAW);
42         $mform->addElement('editor', 'description_editor', get_string('description'), null, $editoroptions);
43         $mform->setType('description_editor', PARAM_RAW);
44         if (!empty($CFG->allowcategorythemes)) {
45             $themes = array(''=>get_string('forceno'));
46             $allthemes = get_list_of_themes();
47             foreach ($allthemes as $key=>$theme) {
48                 if (empty($theme->hidefromselector)) {
49                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
50                 }
51             }
52             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
53         }
55         $mform->addElement('hidden', 'id', 0);
56         $mform->setType('id', PARAM_INT);
57         $mform->setDefault('id', $category->id);
59         $this->add_action_buttons(true, $strsubmit);
60     }
62     function validation($data, $files) {
63         global $DB;
64         $errors = parent::validation($data, $files);
65         if (!empty($data['idnumber'])) {
66             if ($existing = $DB->get_record('course_categories', array('idnumber' => $data['idnumber']))) {
67                 if (!$data['id'] || $existing->id != $data['id']) {
68                     $errors['idnumber']= get_string('idnumbertaken');
69                 }
70             }
71         }
73         return $errors;
74     }
75 }