MDL-20306 Add course_category idnumber field
[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 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');
20         }
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));
27             }
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');
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->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);
48                 }
49             }
50             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
51         }
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);
58     }
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');
67                 }
68             }
69         }
71         return $errors;
72     }
73 }