See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
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 * Customfields textarea plugin 19 * 20 * @package customfield_textarea 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_textarea; 26 27 defined('MOODLE_INTERNAL') || die; 28 29 /** 30 * Class data 31 * 32 * @package customfield_textarea 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 'value'; 44 } 45 46 /** 47 * Options for the editor 48 * 49 * @return array 50 */ 51 protected function value_editor_options() { 52 /** @var field_controller $field */ 53 $field = $this->get_field(); 54 return $field->value_editor_options($this->get('id') ? $this->get_context() : null); 55 } 56 57 /** 58 * Returns the name of the field to be used on HTML forms. 59 * 60 * @return string 61 */ 62 public function get_form_element_name() : string { 63 return parent::get_form_element_name() . '_editor'; 64 } 65 66 /** 67 * Add fields for editing a textarea field. 68 * 69 * @param \MoodleQuickForm $mform 70 */ 71 public function instance_form_definition(\MoodleQuickForm $mform) { 72 $field = $this->get_field(); 73 $desceditoroptions = $this->value_editor_options(); 74 $elementname = $this->get_form_element_name(); 75 $mform->addElement('editor', $elementname, $this->get_field()->get_formatted_name(), null, $desceditoroptions); 76 if ($field->get_configdata_property('required')) { 77 $mform->addRule($elementname, null, 'required', null, 'client'); 78 } 79 } 80 81 /** 82 * Saves the data coming from form 83 * 84 * @param \stdClass $datanew data coming from the form 85 */ 86 public function instance_form_save(\stdClass $datanew) { 87 $fieldname = $this->get_form_element_name(); 88 if (!property_exists($datanew, $fieldname)) { 89 return; 90 } 91 $fromform = $datanew->$fieldname; 92 93 if (!$this->get('id')) { 94 $this->data->set('value', ''); 95 $this->data->set('valueformat', FORMAT_MOODLE); 96 $this->save(); 97 } 98 99 if (array_key_exists('text', $fromform)) { 100 $textoptions = $this->value_editor_options(); 101 $data = (object) ['field_editor' => $fromform]; 102 $data = file_postupdate_standard_editor($data, 'field', $textoptions, $textoptions['context'], 103 'customfield_textarea', 'value', $this->get('id')); 104 $this->data->set('value', $data->field); 105 $this->data->set('valueformat', $data->fieldformat); 106 107 $this->save(); 108 } 109 } 110 111 /** 112 * Prepares the custom field data related to the object to pass to mform->set_data() and adds them to it 113 * 114 * This function must be called before calling $form->set_data($object); 115 * 116 * @param \stdClass $instance the entity that has custom fields, if 'id' attribute is present the custom 117 * fields for this entity will be added, otherwise the default values will be added. 118 */ 119 public function instance_form_before_set_data(\stdClass $instance) { 120 $textoptions = $this->value_editor_options(); 121 if ($this->get('id')) { 122 $text = $this->get('value'); 123 $format = $this->get('valueformat'); 124 $temp = (object)['field' => $text, 'fieldformat' => $format]; 125 file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea', 126 'value', $this->get('id')); 127 $value = $temp->field_editor; 128 } else { 129 $text = $this->get_field()->get_configdata_property('defaultvalue'); 130 $format = $this->get_field()->get_configdata_property('defaultvalueformat'); 131 $temp = (object)['field' => $text, 'fieldformat' => $format]; 132 file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea', 133 'defaultvalue', $this->get_field()->get('id')); 134 $value = $temp->field_editor; 135 } 136 $instance->{$this->get_form_element_name()} = $value; 137 } 138 139 /** 140 * Checks if the value is empty, overriding the base method to ensure it's the "text" element of our value being compared 141 * 142 * @param mixed $value 143 * @return bool 144 */ 145 protected function is_empty($value): bool { 146 if (is_array($value)) { 147 $value = $value['text']; 148 } 149 return html_is_blank($value); 150 } 151 152 /** 153 * Checks if the value is unique, overriding the base method to ensure it's the "text" element of our value being compared 154 * 155 * @param mixed $value 156 * @return bool 157 */ 158 protected function is_unique($value): bool { 159 return parent::is_unique($value['text']); 160 } 161 162 /** 163 * Delete data 164 * 165 * @return bool 166 */ 167 public function delete() { 168 get_file_storage()->delete_area_files($this->get('contextid'), 'customfield_textarea', 169 'value', $this->get('id')); 170 return parent::delete(); 171 } 172 173 /** 174 * Returns the default value as it would be stored in the database (not in human-readable format). 175 * 176 * @return mixed 177 */ 178 public function get_default_value() { 179 return $this->get_field()->get_configdata_property('defaultvalue'); 180 } 181 182 /** 183 * Returns value in a human-readable format 184 * 185 * @return mixed|null value or null if empty 186 */ 187 public function export_value() { 188 global $CFG; 189 require_once($CFG->libdir . '/filelib.php'); 190 191 $value = $this->get_value(); 192 193 if ($dataid = $this->get('id')) { 194 $context = $this->get_context(); 195 $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php', 196 $context->id, 'customfield_textarea', 'value', $dataid); 197 $value = format_text($processed, $this->get('valueformat'), ['context' => $context]); 198 } else { 199 $fieldid = $this->get_field()->get('id'); 200 $configcontext = $this->get_field()->get_handler()->get_configuration_context(); 201 $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php', 202 $configcontext->id, 'customfield_textarea', 'defaultvalue', $fieldid); 203 $valueformat = $this->get_field()->get_configdata_property('defaultvalueformat'); 204 $value = format_text($processed, $valueformat, ['context' => $configcontext]); 205 } 206 207 return $value; 208 } 209 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body