2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the profile field category form.
21 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_user\form
;
29 if (!defined('MOODLE_INTERNAL')) {
30 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
33 require_once($CFG->dirroot
.'/lib/formslib.php');
38 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class profile_category_form
extends moodleform
{
46 public function definition () {
49 $mform = $this->_form
;
51 $strrequired = get_string('required');
53 // Add some extra hidden fields.
54 $mform->addElement('hidden', 'id');
55 $mform->setType('id', PARAM_INT
);
56 $mform->addElement('hidden', 'action', 'editcategory');
57 $mform->setType('action', PARAM_ALPHANUMEXT
);
59 $mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
60 $mform->setType('name', PARAM_TEXT
);
61 $mform->addRule('name', $strrequired, 'required', null, 'client');
63 $this->add_action_buttons(true);
68 * Perform some moodle validation.
74 public function validation($data, $files) {
76 $errors = parent
::validation($data, $files);
78 $data = (object)$data;
80 $duplicate = $DB->get_field('user_info_category', 'id', array('name' => $data->name
));
82 // Check the name is unique.
83 if (!empty($data->id
)) { // We are editing an existing record.
84 $olddata = $DB->get_record('user_info_category', array('id' => $data->id
));
85 // Name has changed, new name in use, new name in use by another record.
86 $dupfound = (($olddata->name
!== $data->name
) && $duplicate && ($data->id
!= $duplicate));
87 } else { // New profile category.
88 $dupfound = $duplicate;
92 $errors['name'] = get_string('profilecategorynamenotunique', 'admin');