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