Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

   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   * Menu profile field.
  19   *
  20   * @package    profilefield_menu
  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  /**
  26   * Class profile_field_menu
  27   *
  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_menu extends profile_field_base {
  32  
  33      /** @var array $options */
  34      public $options;
  35  
  36      /** @var int $datakey */
  37      public $datakey;
  38  
  39      /**
  40       * Constructor method.
  41       *
  42       * Pulls out the options for the menu from the database and sets the the corresponding key for the data if it exists.
  43       *
  44       * @param int $fieldid
  45       * @param int $userid
  46       * @param object $fielddata
  47       */
  48      public function __construct($fieldid = 0, $userid = 0, $fielddata = null) {
  49          // First call parent constructor.
  50          parent::__construct($fieldid, $userid, $fielddata);
  51  
  52          // Param 1 for menu type is the options.
  53          if (isset($this->field->param1)) {
  54              $options = explode("\n", $this->field->param1);
  55          } else {
  56              $options = array();
  57          }
  58          $this->options = array();
  59          if (!empty($this->field->required)) {
  60              $this->options[''] = get_string('choose').'...';
  61          }
  62          foreach ($options as $key => $option) {
  63              // Multilang formatting with filters.
  64              $this->options[$option] = format_string($option, true, ['context' => context_system::instance()]);
  65          }
  66  
  67          // Set the data key.
  68          if ($this->data !== null) {
  69              $key = $this->data;
  70              if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false) {
  71                  $this->data = $key;
  72                  $this->datakey = $key;
  73              }
  74          }
  75      }
  76  
  77      /**
  78       * Create the code snippet for this field instance
  79       * Overwrites the base class method
  80       * @param moodleform $mform Moodle form instance
  81       */
  82      public function edit_field_add($mform) {
  83          $mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
  84      }
  85  
  86      /**
  87       * Set the default value for this field instance
  88       * Overwrites the base class method.
  89       * @param moodleform $mform Moodle form instance
  90       */
  91      public function edit_field_set_default($mform) {
  92          $key = $this->field->defaultdata;
  93          if (isset($this->options[$key]) || ($key = array_search($key, $this->options)) !== false){
  94              $defaultkey = $key;
  95          } else {
  96              $defaultkey = '';
  97          }
  98          $mform->setDefault($this->inputname, $defaultkey);
  99      }
 100  
 101      /**
 102       * The data from the form returns the key.
 103       *
 104       * This should be converted to the respective option string to be saved in database
 105       * Overwrites base class accessor method.
 106       *
 107       * @param mixed $data The key returned from the select input in the form
 108       * @param stdClass $datarecord The object that will be used to save the record
 109       * @return mixed Data or null
 110       */
 111      public function edit_save_data_preprocess($data, $datarecord) {
 112          return isset($this->options[$data]) ? $data : null;
 113      }
 114  
 115      /**
 116       * When passing the user object to the form class for the edit profile page
 117       * we should load the key for the saved data
 118       *
 119       * Overwrites the base class method.
 120       *
 121       * @param stdClass $user User object.
 122       */
 123      public function edit_load_user_data($user) {
 124          $user->{$this->inputname} = $this->datakey;
 125      }
 126  
 127      /**
 128       * HardFreeze the field if locked.
 129       * @param moodleform $mform instance of the moodleform class
 130       */
 131      public function edit_field_set_locked($mform) {
 132          if (!$mform->elementExists($this->inputname)) {
 133              return;
 134          }
 135          if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) {
 136              $mform->hardFreeze($this->inputname);
 137              $mform->setConstant($this->inputname, format_string($this->datakey));
 138          }
 139      }
 140      /**
 141       * Convert external data (csv file) from value to key for processing later by edit_save_data_preprocess
 142       *
 143       * @param string $value one of the values in menu options.
 144       * @return int options key for the menu
 145       */
 146      public function convert_external_data($value) {
 147          if (isset($this->options[$value])) {
 148              $retval = $value;
 149          } else {
 150              $retval = array_search($value, $this->options);
 151          }
 152  
 153          // If value is not found in options then return null, so that it can be handled
 154          // later by edit_save_data_preprocess.
 155          if ($retval === false) {
 156              $retval = null;
 157          }
 158          return $retval;
 159      }
 160  
 161      /**
 162       * Return the field type and null properties.
 163       * This will be used for validating the data submitted by a user.
 164       *
 165       * @return array the param type and null property
 166       * @since Moodle 3.2
 167       */
 168      public function get_field_properties() {
 169          return array(PARAM_TEXT, NULL_NOT_ALLOWED);
 170      }
 171  }
 172  
 173