Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * This file contains the profile field category form.
  19   *
  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
  23   */
  24  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    //  It must be included from a Moodle page.
  27  }
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  
  31  /**
  32   * Class category_form
  33   *
  34   * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class category_form extends moodleform {
  38  
  39      /**
  40       * Define the form.
  41       */
  42      public function definition () {
  43          global $USER, $CFG;
  44  
  45          $mform = $this->_form;
  46  
  47          $strrequired = get_string('required');
  48  
  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);
  54  
  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');
  58  
  59          $this->add_action_buttons(true);
  60  
  61      }
  62  
  63      /**
  64       * Perform some moodle validation.
  65       *
  66       * @param array $data
  67       * @param array $files
  68       * @return array
  69       */
  70      public function validation($data, $files) {
  71          global $CFG, $DB;
  72          $errors = parent::validation($data, $files);
  73  
  74          $data  = (object)$data;
  75  
  76          $duplicate = $DB->get_field('user_info_category', 'id', array('name' => $data->name));
  77  
  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;
  85          }
  86  
  87          if ($dupfound ) {
  88              $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
  89          }
  90  
  91          return $errors;
  92      }
  93  }
  94  
  95