Merge branch 'MDL-51969-master' of https://github.com/dmitriim/moodle
[moodle.git] / user / profile / index_category_form.php
blob551e7fb2879507177d3445cb3384a1d0d1fe4eba
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * This file contains the profile field category form.
20 * @package core_user
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 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
29 require_once($CFG->dirroot.'/lib/formslib.php');
31 /**
32 * Class category_form
34 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class category_form extends moodleform {
39 /**
40 * Define the form.
42 public function definition () {
43 global $USER, $CFG;
45 $mform = $this->_form;
47 $strrequired = get_string('required');
49 // Add some extra hidden fields.
50 $mform->addElement('hidden', 'id');
51 $mform->setType('id', PARAM_INT);
52 $mform->addElement('hidden', 'action', 'editcategory');
53 $mform->setType('action', PARAM_ALPHANUMEXT);
55 $mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
56 $mform->setType('name', PARAM_TEXT);
57 $mform->addRule('name', $strrequired, 'required', null, 'client');
59 $this->add_action_buttons(true);
63 /**
64 * Perform some moodle validation.
66 * @param array $data
67 * @param array $files
68 * @return array
70 public function validation($data, $files) {
71 global $CFG, $DB;
72 $errors = parent::validation($data, $files);
74 $data = (object)$data;
76 $duplicate = $DB->get_field('user_info_category', 'id', array('name' => $data->name));
78 // Check the name is unique.
79 if (!empty($data->id)) { // We are editing an existing record.
80 $olddata = $DB->get_record('user_info_category', array('id' => $data->id));
81 // Name has changed, new name in use, new name in use by another record.
82 $dupfound = (($olddata->name !== $data->name) && $duplicate && ($data->id != $duplicate));
83 } else { // New profile category.
84 $dupfound = $duplicate;
87 if ($dupfound ) {
88 $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
91 return $errors;