| 1 | 
           efrain | 
           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 | 
                * Return configured field options
  | 
        
        
            | 
            | 
           52 | 
                *
  | 
        
        
            | 
            | 
           53 | 
                * @return array
  | 
        
        
            | 
            | 
           54 | 
                */
  | 
        
        
            | 
            | 
           55 | 
               public function get_options(): array {
  | 
        
        
            | 
            | 
           56 | 
                   $optionconfig = $this->get_configdata_property('options');
  | 
        
        
            | 
            | 
           57 | 
                   if ($optionconfig) {
  | 
        
        
           | 11 | 
           efrain | 
           58 | 
                       $context = $this->get_handler()->get_configuration_context();
  | 
        
        
            | 
            | 
           59 | 
                       $options = array_map(
  | 
        
        
            | 
            | 
           60 | 
                           fn(string $option) => format_string($option, true, ['context' => $context]),
  | 
        
        
            | 
            | 
           61 | 
                           preg_split("/\s*\n\s*/", trim($optionconfig), -1, PREG_SPLIT_NO_EMPTY),
  | 
        
        
            | 
            | 
           62 | 
                       );
  | 
        
        
           | 1 | 
           efrain | 
           63 | 
                   } else {
  | 
        
        
            | 
            | 
           64 | 
                       $options = array();
  | 
        
        
            | 
            | 
           65 | 
                   }
  | 
        
        
            | 
            | 
           66 | 
                   return array_merge([''], $options);
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Validate the data from the config form.
  | 
        
        
            | 
            | 
           71 | 
                * Sub classes must reimplement it.
  | 
        
        
            | 
            | 
           72 | 
                *
  | 
        
        
            | 
            | 
           73 | 
                * @param array $data from the add/edit profile field form
  | 
        
        
            | 
            | 
           74 | 
                * @param array $files
  | 
        
        
            | 
            | 
           75 | 
                * @return array associative array of error messages
  | 
        
        
            | 
            | 
           76 | 
                */
  | 
        
        
            | 
            | 
           77 | 
               public function config_form_validation(array $data, $files = array()): array {
  | 
        
        
            | 
            | 
           78 | 
                   $options = preg_split("/\s*\n\s*/", trim($data['configdata']['options']));
  | 
        
        
            | 
            | 
           79 | 
                   $errors = [];
  | 
        
        
            | 
            | 
           80 | 
                   if (!$options || count($options) < 2) {
  | 
        
        
            | 
            | 
           81 | 
                       $errors['configdata[options]'] = get_string('errornotenoughoptions', 'customfield_select');
  | 
        
        
            | 
            | 
           82 | 
                   } else if (!empty($data['configdata']['defaultvalue'])) {
  | 
        
        
            | 
            | 
           83 | 
                       $defaultkey = array_search($data['configdata']['defaultvalue'], $options);
  | 
        
        
            | 
            | 
           84 | 
                       if ($defaultkey === false) {
  | 
        
        
            | 
            | 
           85 | 
                           $errors['configdata[defaultvalue]'] = get_string('errordefaultvaluenotinlist', 'customfield_select');
  | 
        
        
            | 
            | 
           86 | 
                       }
  | 
        
        
            | 
            | 
           87 | 
                   }
  | 
        
        
            | 
            | 
           88 | 
                   return $errors;
  | 
        
        
            | 
            | 
           89 | 
               }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
               /**
  | 
        
        
            | 
            | 
           92 | 
                * Does this custom field type support being used as part of the block_myoverview
  | 
        
        
            | 
            | 
           93 | 
                * custom field grouping?
  | 
        
        
            | 
            | 
           94 | 
                * @return bool
  | 
        
        
            | 
            | 
           95 | 
                */
  | 
        
        
            | 
            | 
           96 | 
               public function supports_course_grouping(): bool {
  | 
        
        
            | 
            | 
           97 | 
                   return true;
  | 
        
        
            | 
            | 
           98 | 
               }
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
               /**
  | 
        
        
            | 
            | 
           101 | 
                * If this field supports course grouping, then this function needs overriding to
  | 
        
        
            | 
            | 
           102 | 
                * return the formatted values for this.
  | 
        
        
            | 
            | 
           103 | 
                * @param array $values the used values that need formatting
  | 
        
        
            | 
            | 
           104 | 
                * @return array
  | 
        
        
            | 
            | 
           105 | 
                */
  | 
        
        
            | 
            | 
           106 | 
               public function course_grouping_format_values($values): array {
  | 
        
        
            | 
            | 
           107 | 
                   $options = $this->get_options();
  | 
        
        
            | 
            | 
           108 | 
                   $ret = [];
  | 
        
        
            | 
            | 
           109 | 
                   foreach ($values as $value) {
  | 
        
        
            | 
            | 
           110 | 
                       if (isset($options[$value])) {
  | 
        
        
           | 11 | 
           efrain | 
           111 | 
                           $ret[$value] = $options[$value];
  | 
        
        
           | 1 | 
           efrain | 
           112 | 
                       }
  | 
        
        
            | 
            | 
           113 | 
                   }
  | 
        
        
            | 
            | 
           114 | 
                   $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
  | 
        
        
            | 
            | 
           115 | 
                       $this->get_formatted_name());
  | 
        
        
            | 
            | 
           116 | 
                   return $ret;
  | 
        
        
            | 
            | 
           117 | 
               }
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
               /**
  | 
        
        
            | 
            | 
           120 | 
                * Locate the value parameter in the field options array, and return it's index
  | 
        
        
            | 
            | 
           121 | 
                *
  | 
        
        
            | 
            | 
           122 | 
                * @param string $value
  | 
        
        
            | 
            | 
           123 | 
                * @return int
  | 
        
        
            | 
            | 
           124 | 
                */
  | 
        
        
            | 
            | 
           125 | 
               public function parse_value(string $value) {
  | 
        
        
            | 
            | 
           126 | 
                   return (int) array_search($value, $this->get_options());
  | 
        
        
            | 
            | 
           127 | 
               }
  | 
        
        
            | 
            | 
           128 | 
           }
  |