Differences Between: [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 * Select plugin data controller 19 * 20 * @package customfield_select 21 * @copyright 2018 Daniel Neis Araujo <daniel@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 data 31 * 32 * @package customfield_select 33 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class data_controller extends \core_customfield\data_controller { 37 38 /** 39 * Return the name of the field where the information is stored 40 * @return string 41 */ 42 public function datafield() : string { 43 return 'intvalue'; 44 } 45 46 /** 47 * Returns the default value as it would be stored in the database (not in human-readable format). 48 * 49 * @return mixed 50 */ 51 public function get_default_value() { 52 $defaultvalue = $this->get_field()->get_configdata_property('defaultvalue'); 53 if ('' . $defaultvalue !== '') { 54 $key = array_search($defaultvalue, $this->get_field()->get_options()); 55 if ($key !== false) { 56 return $key; 57 } 58 } 59 return 0; 60 } 61 62 /** 63 * Add fields for editing a textarea field. 64 * 65 * @param \MoodleQuickForm $mform 66 */ 67 public function instance_form_definition(\MoodleQuickForm $mform) { 68 $field = $this->get_field(); 69 $config = $field->get('configdata'); 70 $options = $field->get_options(); 71 $formattedoptions = array(); 72 $context = $this->get_field()->get_handler()->get_configuration_context(); 73 foreach ($options as $key => $option) { 74 // Multilang formatting with filters. 75 $formattedoptions[$key] = format_string($option, true, ['context' => $context]); 76 } 77 78 $elementname = $this->get_form_element_name(); 79 $mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $formattedoptions); 80 81 if (($defaultkey = array_search($config['defaultvalue'], $options)) !== false) { 82 $mform->setDefault($elementname, $defaultkey); 83 } 84 if ($field->get_configdata_property('required')) { 85 $mform->addRule($elementname, null, 'required', null, 'client'); 86 } 87 } 88 89 /** 90 * Validates data for this field. 91 * 92 * @param array $data 93 * @param array $files 94 * @return array 95 */ 96 public function instance_form_validation(array $data, array $files) : array { 97 $errors = parent::instance_form_validation($data, $files); 98 if ($this->get_field()->get_configdata_property('required')) { 99 // Standard required rule does not work on select element. 100 $elementname = $this->get_form_element_name(); 101 if (empty($data[$elementname])) { 102 $errors[$elementname] = get_string('err_required', 'form'); 103 } 104 } 105 return $errors; 106 } 107 108 /** 109 * Returns value in a human-readable format 110 * 111 * @return mixed|null value or null if empty 112 */ 113 public function export_value() { 114 $value = $this->get_value(); 115 116 if ($this->is_empty($value)) { 117 return null; 118 } 119 120 $options = $this->get_field()->get_options(); 121 if (array_key_exists($value, $options)) { 122 return format_string($options[$value], true, 123 ['context' => $this->get_field()->get_handler()->get_configuration_context()]); 124 } 125 126 return null; 127 } 128 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body