MDL-71051 core_user: move edit profile category form to classes
[moodle.git] / user / classes / form / profile_category_form.php
blobffc07073aa048ee16288392ad571cdafd0f800d1
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 namespace core_user\form;
27 use moodleform;
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');
35 /**
36 * Class category_form
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 {
43 /**
44 * Define the form.
46 public function definition () {
47 global $USER, $CFG;
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);
67 /**
68 * Perform some moodle validation.
70 * @param array $data
71 * @param array $files
72 * @return array
74 public function validation($data, $files) {
75 global $CFG, $DB;
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;
91 if ($dupfound ) {
92 $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
95 return $errors;