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 |
/**
|
|
|
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 |
use backup_nested_element;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Class data
|
|
|
33 |
*
|
|
|
34 |
* @package customfield_textarea
|
|
|
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 'value';
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Options for the editor
|
|
|
50 |
*
|
|
|
51 |
* @return array
|
|
|
52 |
*/
|
|
|
53 |
protected function value_editor_options() {
|
|
|
54 |
/** @var field_controller $field */
|
|
|
55 |
$field = $this->get_field();
|
|
|
56 |
return $field->value_editor_options($this->get('id') ? $this->get_context() : null);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Returns the name of the field to be used on HTML forms.
|
|
|
61 |
*
|
|
|
62 |
* @return string
|
|
|
63 |
*/
|
|
|
64 |
public function get_form_element_name(): string {
|
|
|
65 |
return parent::get_form_element_name() . '_editor';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Add fields for editing a textarea field.
|
|
|
70 |
*
|
|
|
71 |
* @param \MoodleQuickForm $mform
|
|
|
72 |
*/
|
|
|
73 |
public function instance_form_definition(\MoodleQuickForm $mform) {
|
|
|
74 |
$field = $this->get_field();
|
|
|
75 |
$desceditoroptions = $this->value_editor_options();
|
|
|
76 |
$elementname = $this->get_form_element_name();
|
|
|
77 |
$mform->addElement('editor', $elementname, $this->get_field()->get_formatted_name(), null, $desceditoroptions);
|
|
|
78 |
if ($field->get_configdata_property('required')) {
|
|
|
79 |
$mform->addRule($elementname, null, 'required', null, 'client');
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Saves the data coming from form
|
|
|
85 |
*
|
|
|
86 |
* @param \stdClass $datanew data coming from the form
|
|
|
87 |
*/
|
|
|
88 |
public function instance_form_save(\stdClass $datanew) {
|
|
|
89 |
$fieldname = $this->get_form_element_name();
|
|
|
90 |
if (!property_exists($datanew, $fieldname)) {
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Normalise form data, for cases it's come from an external source.
|
|
|
95 |
$fromform = $datanew->$fieldname;
|
|
|
96 |
if (!is_array($fromform)) {
|
|
|
97 |
$fromform = ['text' => $fromform];
|
|
|
98 |
$fromform['format'] = $this->get('id') ? $this->get('valueformat') :
|
|
|
99 |
$this->get_field()->get_configdata_property('defaultvalueformat');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if (!$this->get('id')) {
|
|
|
103 |
$this->data->set('value', '');
|
|
|
104 |
$this->data->set('valueformat', FORMAT_MOODLE);
|
|
|
105 |
$this->data->set('valuetrust', false);
|
|
|
106 |
$this->save();
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if (array_key_exists('text', $fromform)) {
|
|
|
110 |
$textoptions = $this->value_editor_options();
|
|
|
111 |
$context = $textoptions['context'];
|
|
|
112 |
|
|
|
113 |
$data = (object) ['field_editor' => $fromform];
|
|
|
114 |
$data = file_postupdate_standard_editor($data, 'field', $textoptions, $context,
|
|
|
115 |
'customfield_textarea', 'value', $this->get('id'));
|
|
|
116 |
|
|
|
117 |
$this->data->set('value', $data->field);
|
|
|
118 |
$this->data->set('valueformat', $data->fieldformat);
|
|
|
119 |
$this->data->set('valuetrust', trusttext_trusted($context));
|
|
|
120 |
$this->save();
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Prepares the custom field data related to the object to pass to mform->set_data() and adds them to it
|
|
|
126 |
*
|
|
|
127 |
* This function must be called before calling $form->set_data($object);
|
|
|
128 |
*
|
|
|
129 |
* @param \stdClass $instance the entity that has custom fields, if 'id' attribute is present the custom
|
|
|
130 |
* fields for this entity will be added, otherwise the default values will be added.
|
|
|
131 |
*/
|
|
|
132 |
public function instance_form_before_set_data(\stdClass $instance) {
|
|
|
133 |
$textoptions = $this->value_editor_options();
|
|
|
134 |
$context = $textoptions['context'];
|
|
|
135 |
if ($this->get('id')) {
|
|
|
136 |
$text = $this->get('value');
|
|
|
137 |
$format = $this->get('valueformat');
|
|
|
138 |
$temp = (object) ['field' => $text, 'fieldformat' => $format, 'fieldtrust' => trusttext_trusted($context)];
|
|
|
139 |
file_prepare_standard_editor($temp, 'field', $textoptions, $context, 'customfield_textarea',
|
|
|
140 |
'value', $this->get('id'));
|
|
|
141 |
$value = $temp->field_editor;
|
|
|
142 |
} else {
|
|
|
143 |
$text = $this->get_field()->get_configdata_property('defaultvalue');
|
|
|
144 |
$format = $this->get_field()->get_configdata_property('defaultvalueformat');
|
|
|
145 |
$temp = (object) ['field' => $text, 'fieldformat' => $format, 'fieldtrust' => trusttext_trusted($context)];
|
|
|
146 |
file_prepare_standard_editor($temp, 'field', $textoptions, $context, 'customfield_textarea',
|
|
|
147 |
'defaultvalue', $this->get_field()->get('id'));
|
|
|
148 |
$value = $temp->field_editor;
|
|
|
149 |
}
|
|
|
150 |
$instance->{$this->get_form_element_name()} = $value;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Checks if the value is empty, overriding the base method to ensure it's the "text" element of our value being compared
|
|
|
155 |
*
|
|
|
156 |
* @param string|string[] $value
|
|
|
157 |
* @return bool
|
|
|
158 |
*/
|
|
|
159 |
protected function is_empty($value): bool {
|
|
|
160 |
if (is_array($value)) {
|
|
|
161 |
$value = $value['text'];
|
|
|
162 |
}
|
|
|
163 |
return html_is_blank($value);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Checks if the value is unique, overriding the base method to ensure it's the "text" element of our value being compared
|
|
|
168 |
*
|
|
|
169 |
* @param mixed $value
|
|
|
170 |
* @return bool
|
|
|
171 |
*/
|
|
|
172 |
protected function is_unique($value): bool {
|
|
|
173 |
return parent::is_unique($value['text']);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Delete data
|
|
|
178 |
*
|
|
|
179 |
* @return bool
|
|
|
180 |
*/
|
|
|
181 |
public function delete() {
|
|
|
182 |
get_file_storage()->delete_area_files($this->get('contextid'), 'customfield_textarea',
|
|
|
183 |
'value', $this->get('id'));
|
|
|
184 |
return parent::delete();
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Returns the default value as it would be stored in the database (not in human-readable format).
|
|
|
189 |
*
|
|
|
190 |
* @return mixed
|
|
|
191 |
*/
|
|
|
192 |
public function get_default_value() {
|
|
|
193 |
return $this->get_field()->get_configdata_property('defaultvalue');
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Implement the backup callback for the custom field element.
|
|
|
198 |
* This includes any embedded files in the custom field element.
|
|
|
199 |
*
|
|
|
200 |
* @param \backup_nested_element $customfieldelement The custom field element to be backed up.
|
|
|
201 |
*/
|
|
|
202 |
public function backup_define_structure(backup_nested_element $customfieldelement): void {
|
|
|
203 |
$annotations = $customfieldelement->get_file_annotations();
|
|
|
204 |
|
|
|
205 |
if (!isset($annotations['customfield_textarea']['value'])) {
|
|
|
206 |
$customfieldelement->annotate_files('customfield_textarea', 'value', 'id');
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Implement the restore callback for the custom field element.
|
|
|
212 |
* This includes restoring any embedded files in the custom field element.
|
|
|
213 |
*
|
|
|
214 |
* @param \restore_structure_step $step The restore step instance.
|
|
|
215 |
* @param int $newid The new ID for the custom field data after restore.
|
|
|
216 |
* @param int $oldid The original ID of the custom field data before backup.
|
|
|
217 |
*/
|
|
|
218 |
public function restore_define_structure(\restore_structure_step $step, int $newid, int $oldid): void {
|
|
|
219 |
if (!$step->get_mappingid('customfield_data', $oldid)) {
|
|
|
220 |
$step->set_mapping('customfield_data', $oldid, $newid, true);
|
|
|
221 |
$step->add_related_files('customfield_textarea', 'value', 'customfield_data');
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Returns value in a human-readable format
|
|
|
227 |
*
|
|
|
228 |
* @return mixed|null value or null if empty
|
|
|
229 |
*/
|
|
|
230 |
public function export_value() {
|
|
|
231 |
global $CFG;
|
|
|
232 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
233 |
|
|
|
234 |
$value = $this->get_value();
|
|
|
235 |
if ($this->is_empty($value)) {
|
|
|
236 |
return null;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
if ($dataid = $this->get('id')) {
|
|
|
240 |
$context = $this->get_context();
|
|
|
241 |
$processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
|
|
|
242 |
$context->id, 'customfield_textarea', 'value', $dataid);
|
|
|
243 |
$value = format_text($processed, $this->get('valueformat'), [
|
|
|
244 |
'context' => $context,
|
|
|
245 |
'trusted' => $this->get('valuetrust'),
|
|
|
246 |
]);
|
|
|
247 |
} else {
|
|
|
248 |
$field = $this->get_field();
|
|
|
249 |
|
|
|
250 |
$context = $field->get_handler()->get_configuration_context();
|
|
|
251 |
$processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
|
|
|
252 |
$context->id, 'customfield_textarea', 'defaultvalue', $field->get('id'));
|
|
|
253 |
$value = format_text($processed, $field->get_configdata_property('defaultvalueformat'), [
|
|
|
254 |
'context' => $context,
|
|
|
255 |
'trusted' => true,
|
|
|
256 |
]);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
return $value;
|
|
|
260 |
}
|
|
|
261 |
}
|