Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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