Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Class field
  19   *
  20   * @package   customfield_select
  21   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace customfield_select;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Class field
  31   *
  32   * @package customfield_select
  33   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class field_controller extends \core_customfield\field_controller {
  37      /**
  38       * Customfield type
  39       */
  40      const TYPE = 'select';
  41  
  42      /**
  43       * Add fields for editing a select field.
  44       *
  45       * @param \MoodleQuickForm $mform
  46       */
  47      public function config_form_definition(\MoodleQuickForm $mform) {
  48          $mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_select'));
  49          $mform->setExpanded('header_specificsettings', true);
  50  
  51          $mform->addElement('textarea', 'configdata[options]', get_string('menuoptions', 'customfield_select'));
  52          $mform->setType('configdata[options]', PARAM_TEXT);
  53  
  54          $mform->addElement('text', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'), 'size="50"');
  55          $mform->setType('configdata[defaultvalue]', PARAM_TEXT);
  56      }
  57  
  58      /**
  59       * Returns the options available as an array.
  60       *
  61       * @param \core_customfield\field_controller $field
  62       * @return array
  63       *
  64       * @deprecated since Moodle 3.10 - MDL-68569 please use $field->get_options
  65       */
  66      public static function get_options_array(\core_customfield\field_controller $field) : array {
  67          debugging('get_options_array() is deprecated, please use $field->get_options() instead', DEBUG_DEVELOPER);
  68  
  69          return $field->get_options();
  70      }
  71  
  72      /**
  73       * Return configured field options
  74       *
  75       * @return array
  76       */
  77      public function get_options(): array {
  78          $optionconfig = $this->get_configdata_property('options');
  79          if ($optionconfig) {
  80              $options = preg_split("/\s*\n\s*/", trim($optionconfig));
  81          } else {
  82              $options = array();
  83          }
  84          return array_merge([''], $options);
  85      }
  86  
  87      /**
  88       * Validate the data from the config form.
  89       * Sub classes must reimplement it.
  90       *
  91       * @param array $data from the add/edit profile field form
  92       * @param array $files
  93       * @return array associative array of error messages
  94       */
  95      public function config_form_validation(array $data, $files = array()) : array {
  96          $options = preg_split("/\s*\n\s*/", trim($data['configdata']['options']));
  97          $errors = [];
  98          if (!$options || count($options) < 2) {
  99              $errors['configdata[options]'] = get_string('errornotenoughoptions', 'customfield_select');
 100          } else if (!empty($data['configdata']['defaultvalue'])) {
 101              $defaultkey = array_search($data['configdata']['defaultvalue'], $options);
 102              if ($defaultkey === false) {
 103                  $errors['configdata[defaultvalue]'] = get_string('errordefaultvaluenotinlist', 'customfield_select');
 104              }
 105          }
 106          return $errors;
 107      }
 108  
 109      /**
 110       * Does this custom field type support being used as part of the block_myoverview
 111       * custom field grouping?
 112       * @return bool
 113       */
 114      public function supports_course_grouping(): bool {
 115          return true;
 116      }
 117  
 118      /**
 119       * If this field supports course grouping, then this function needs overriding to
 120       * return the formatted values for this.
 121       * @param array $values the used values that need formatting
 122       * @return array
 123       */
 124      public function course_grouping_format_values($values): array {
 125          $options = $this->get_options();
 126          $ret = [];
 127          foreach ($values as $value) {
 128              if (isset($options[$value])) {
 129                  $ret[$value] = format_string($options[$value]);
 130              }
 131          }
 132          $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
 133              $this->get_formatted_name());
 134          return $ret;
 135      }
 136  
 137      /**
 138       * Locate the value parameter in the field options array, and return it's index
 139       *
 140       * @param string $value
 141       * @return int
 142       */
 143      public function parse_value(string $value) {
 144          return (int) array_search($value, $this->get_options());
 145      }
 146  }