/user/profile/index_category_form.php
PHP | 60 lines | 37 code | 18 blank | 5 comment | 7 complexity | abfcbe87e6c283b07ddeeaf77440e227 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
- <?php
- if (!defined('MOODLE_INTERNAL')) {
- die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
- }
- require_once($CFG->dirroot.'/lib/formslib.php');
- class category_form extends moodleform {
- // Define the form
- function definition () {
- global $USER, $CFG;
- $mform =& $this->_form;
- $strrequired = get_string('required');
- /// Add some extra hidden fields
- $mform->addElement('hidden', 'id');
- $mform->setType('id', PARAM_INT);
- $mform->addElement('hidden', 'action', 'editcategory');
- $mform->setType('action', PARAM_ALPHANUMEXT);
- $mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
- $mform->setType('name', PARAM_TEXT);
- $mform->addRule('name', $strrequired, 'required', null, 'client');
- $this->add_action_buttons(true);
- } /// End of function
- /// perform some moodle validation
- function validation($data, $files) {
- global $CFG, $DB;
- $errors = parent::validation($data, $files);
- $data = (object)$data;
- $duplicate = $DB->get_field('user_info_category', 'id', array('name' => $data->name));
- /// Check the name is unique
- if (!empty($data->id)) { // we are editing an existing record
- $olddata = $DB->get_record('user_info_category', array('id'=>$data->id));
- // name has changed, new name in use, new name in use by another record
- $dupfound = (($olddata->name !== $data->name) && $duplicate && ($data->id != $duplicate));
- }
- else { // new profile category
- $dupfound = $duplicate;
- }
- if ($dupfound ) {
- $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
- }
- return $errors;
- }
- }