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  use profile_define_base;
  23  
  24  /**
  25   * Class field_form used for profile fields.
  26   *
  27   * @package core_user
  28   * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class profile_field_form extends dynamic_form {
  32  
  33      /** @var profile_define_base $field */
  34      public $field;
  35      /** @var \stdClass */
  36      protected $fieldrecord;
  37  
  38      /**
  39       * Define the form
  40       */
  41      public function definition () {
  42          global $CFG;
  43          require_once($CFG->dirroot.'/user/profile/definelib.php');
  44  
  45          $mform = $this->_form;
  46  
  47          // Everything else is dependant on the data type.
  48          $datatype = $this->get_field_record()->datatype;
  49          require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
  50          $newfield = 'profile_define_'.$datatype;
  51          $this->field = new $newfield();
  52  
  53          // Add some extra hidden fields.
  54          $mform->addElement('hidden', 'id');
  55          $mform->setType('id', PARAM_INT);
  56          $mform->addElement('hidden', 'action', 'editfield');
  57          $mform->setType('action', PARAM_ALPHANUMEXT);
  58          $mform->addElement('hidden', 'datatype', $datatype);
  59          $mform->setType('datatype', PARAM_ALPHA);
  60  
  61          $this->field->define_form($mform);
  62      }
  63  
  64  
  65      /**
  66       * Alter definition based on existing or submitted data
  67       */
  68      public function definition_after_data () {
  69          $mform = $this->_form;
  70          $this->field->define_after_data($mform);
  71      }
  72  
  73  
  74      /**
  75       * Perform some moodle validation.
  76       * @param array $data
  77       * @param array $files
  78       * @return array
  79       */
  80      public function validation($data, $files) {
  81          return $this->field->define_validate($data, $files);
  82      }
  83  
  84      /**
  85       * Returns the defined editors for the field.
  86       * @return array
  87       */
  88      public function editors(): array {
  89          $editors = $this->field->define_editors();
  90          return is_array($editors) ? $editors : [];
  91      }
  92  
  93      /**
  94       * Returns context where this form is used
  95       *
  96       * @return context
  97       */
  98      protected function get_context_for_dynamic_submission(): context {
  99          return \context_system::instance();
 100      }
 101  
 102      /**
 103       * Checks if current user has access to this form, otherwise throws exception
 104       */
 105      protected function check_access_for_dynamic_submission(): void {
 106          require_capability('moodle/site:config', $this->get_context_for_dynamic_submission());
 107      }
 108  
 109      /**
 110       * Process the form submission, used if form was submitted via AJAX
 111       */
 112      public function process_dynamic_submission() {
 113          global $CFG;
 114          require_once($CFG->dirroot.'/user/profile/definelib.php');
 115          profile_save_field($this->get_data(), $this->editors());
 116      }
 117  
 118      /**
 119       * Load in existing data as form defaults
 120       */
 121      public function set_data_for_dynamic_submission(): void {
 122          $field = $this->get_field_record();
 123  
 124          // Clean and prepare description for the editor.
 125          $description = clean_text($field->description, $field->descriptionformat);
 126          $field->description = ['text' => $description, 'format' => $field->descriptionformat, 'itemid' => 0];
 127          // Convert the data format for.
 128          if (is_array($this->editors())) {
 129              foreach ($this->editors() as $editor) {
 130                  if (isset($field->$editor)) {
 131                      $editordesc = clean_text($field->$editor, $field->{$editor.'format'});
 132                      $field->$editor = ['text' => $editordesc, 'format' => $field->{$editor.'format'}, 'itemid' => 0];
 133                  }
 134              }
 135          }
 136  
 137          $this->set_data($field);
 138      }
 139  
 140      /**
 141       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 142       *
 143       * @return moodle_url
 144       */
 145      protected function get_page_url_for_dynamic_submission(): moodle_url {
 146          $id = $this->optional_param('id', 0, PARAM_INT);
 147          $datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN);
 148          return new moodle_url('/user/profile/index.php',
 149              ['action' => 'editfield', 'id' => $id, 'datatype' => $id ? null : $datatype]);
 150      }
 151  
 152      /**
 153       * Record for the field from the database (or generic record for a new field)
 154       *
 155       * @return false|mixed|\stdClass
 156       * @throws \coding_exception
 157       * @throws \dml_exception
 158       */
 159      public function get_field_record() {
 160          global $DB;
 161  
 162          if (!$this->fieldrecord) {
 163              $id = $this->optional_param('id', 0, PARAM_INT);
 164              if (!$id || !($this->fieldrecord = $DB->get_record('user_info_field', ['id' => $id]))) {
 165                  $datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN);
 166                  $this->fieldrecord = new \stdClass();
 167                  $this->fieldrecord->datatype = $datatype;
 168                  $this->fieldrecord->description = '';
 169                  $this->fieldrecord->descriptionformat = FORMAT_HTML;
 170                  $this->fieldrecord->defaultdata = '';
 171                  $this->fieldrecord->defaultdataformat = FORMAT_HTML;
 172                  $this->fieldrecord->categoryid = $this->optional_param('categoryid', 0, PARAM_INT);
 173              }
 174              if (!\core_component::get_component_directory('profilefield_'.$this->fieldrecord->datatype)) {
 175                  throw new \moodle_exception('fieldnotfound', 'customfield');
 176              }
 177          }
 178  
 179          return $this->fieldrecord;
 180      }
 181  }
 182  
 183