Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * 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      public static function get_options_array(\core_customfield\field_controller $field) : array {
  65          if ($field->get_configdata_property('options')) {
  66              $options = preg_split("/\s*\n\s*/", trim($field->get_configdata_property('options')));
  67          } else {
  68              $options = array();
  69          }
  70          return array_merge([''], $options);
  71      }
  72  
  73      /**
  74       * Validate the data from the config form.
  75       * Sub classes must reimplement it.
  76       *
  77       * @param array $data from the add/edit profile field form
  78       * @param array $files
  79       * @return array associative array of error messages
  80       */
  81      public function config_form_validation(array $data, $files = array()) : array {
  82          $options = preg_split("/\s*\n\s*/", trim($data['configdata']['options']));
  83          $errors = [];
  84          if (!$options || count($options) < 2) {
  85              $errors['configdata[options]'] = get_string('errornotenoughoptions', 'customfield_select');
  86          } else if (!empty($data['configdata']['defaultvalue'])) {
  87              $defaultkey = array_search($data['configdata']['defaultvalue'], $options);
  88              if ($defaultkey === false) {
  89                  $errors['configdata[defaultvalue]'] = get_string('errordefaultvaluenotinlist', 'customfield_select');
  90              }
  91          }
  92          return $errors;
  93      }
  94  
  95      /**
  96       * Does this custom field type support being used as part of the block_myoverview
  97       * custom field grouping?
  98       * @return bool
  99       */
 100      public function supports_course_grouping(): bool {
 101          return true;
 102      }
 103  
 104      /**
 105       * If this field supports course grouping, then this function needs overriding to
 106       * return the formatted values for this.
 107       * @param array $values the used values that need formatting
 108       * @return array
 109       */
 110      public function course_grouping_format_values($values): array {
 111          $options = self::get_options_array($this);
 112          $ret = [];
 113          foreach ($values as $value) {
 114              if (isset($options[$value])) {
 115                  $ret[$value] = format_string($options[$value]);
 116              }
 117          }
 118          $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
 119              $this->get_formatted_name());
 120          return $ret;
 121      }
 122  
 123      /**
 124       * Locate the value parameter in the field options array, and return it's index
 125       *
 126       * @param string $value
 127       * @return int
 128       */
 129      public function parse_value(string $value) {
 130          return (int) array_search($value, self::get_options_array($this));
 131      }
 132  }