Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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  namespace core_user\form;
  18  
  19  use context;
  20  use core_form\dynamic_form;
  21  use moodle_url;
  22  
  23  /**
  24   * Modal form to edit profile category
  25   *
  26   * @package core_user
  27   * @copyright 2021 Marina Glancy
  28   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class profile_category_form extends dynamic_form {
  31  
  32      /**
  33       * Form definition
  34       */
  35      protected function definition() {
  36          $mform = $this->_form;
  37  
  38          $strrequired = get_string('required');
  39  
  40          // Add some extra hidden fields.
  41          $mform->addElement('hidden', 'id');
  42          $mform->setType('id', PARAM_INT);
  43          $mform->addElement('hidden', 'action', 'editcategory');
  44          $mform->setType('action', PARAM_ALPHANUMEXT);
  45  
  46          $mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
  47          $mform->setType('name', PARAM_TEXT);
  48          $mform->addRule('name', $strrequired, 'required', null, 'client');
  49      }
  50  
  51      /**
  52       * Perform some moodle validation.
  53       *
  54       * @param array $data
  55       * @param array $files
  56       * @return array
  57       */
  58      public function validation($data, $files) {
  59          global $DB;
  60          $errors = parent::validation($data, $files);
  61  
  62          $duplicate = $DB->get_field('user_info_category', 'id', ['name' => $data['name']]);
  63  
  64          // Check the name is unique.
  65          if (!empty($data['id'])) { // We are editing an existing record.
  66              $olddata = $DB->get_record('user_info_category', ['id' => $data['id']]);
  67              // Name has changed, new name in use, new name in use by another record.
  68              $dupfound = (($olddata->name !== $data['name']) && $duplicate && ($data['id'] != $duplicate));
  69          } else { // New profile category.
  70              $dupfound = $duplicate;
  71          }
  72  
  73          if ($dupfound ) {
  74              $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
  75          }
  76  
  77          return $errors;
  78      }
  79  
  80      /**
  81       * Returns context where this form is used
  82       *
  83       * @return context
  84       */
  85      protected function get_context_for_dynamic_submission(): context {
  86          return \context_system::instance();
  87      }
  88  
  89      /**
  90       * Checks if current user has access to this form, otherwise throws exception
  91       */
  92      protected function check_access_for_dynamic_submission(): void {
  93          require_capability('moodle/site:config', $this->get_context_for_dynamic_submission());
  94      }
  95  
  96      /**
  97       * Process the form submission, used if form was submitted via AJAX
  98       */
  99      public function process_dynamic_submission() {
 100          global $CFG;
 101          require_once($CFG->dirroot.'/user/profile/definelib.php');
 102          profile_save_category($this->get_data());
 103      }
 104  
 105      /**
 106       * Load in existing data as form defaults
 107       */
 108      public function set_data_for_dynamic_submission(): void {
 109          global $DB;
 110          if ($id = $this->optional_param('id', 0, PARAM_INT)) {
 111              $this->set_data($DB->get_record('user_info_category', ['id' => $id], '*', MUST_EXIST));
 112          }
 113      }
 114  
 115      /**
 116       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 117       *
 118       * @return moodle_url
 119       */
 120      protected function get_page_url_for_dynamic_submission(): moodle_url {
 121          $id = $this->optional_param('id', 0, PARAM_INT);
 122          return new moodle_url('/user/profile/index.php',
 123              ['action' => 'editcategory', 'id' => $id]);
 124      }
 125  }