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 |
* Customfield component data controller abstract class
|
|
|
19 |
*
|
|
|
20 |
* @package core_customfield
|
|
|
21 |
* @copyright 2018 Toni Barbera <toni@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_customfield;
|
|
|
26 |
|
|
|
27 |
use backup_nested_element;
|
|
|
28 |
use core_customfield\output\field_data;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Base class for custom fields data controllers
|
|
|
34 |
*
|
|
|
35 |
* This class is a wrapper around the persistent data class that allows to define
|
|
|
36 |
* how the element behaves in the instance edit forms.
|
|
|
37 |
*
|
|
|
38 |
* Custom field plugins must define a class
|
|
|
39 |
* \{pluginname}\data_controller extends \core_customfield\data_controller
|
|
|
40 |
*
|
|
|
41 |
* @package core_customfield
|
|
|
42 |
* @copyright 2018 Toni Barbera <toni@moodle.com>
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
abstract class data_controller {
|
|
|
46 |
/**
|
|
|
47 |
* Data persistent
|
|
|
48 |
*
|
|
|
49 |
* @var data
|
|
|
50 |
*/
|
|
|
51 |
protected $data;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Field that this data belongs to.
|
|
|
55 |
*
|
|
|
56 |
* @var field_controller
|
|
|
57 |
*/
|
|
|
58 |
protected $field;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* data_controller constructor.
|
|
|
62 |
*
|
|
|
63 |
* @param int $id
|
|
|
64 |
* @param \stdClass|null $record
|
|
|
65 |
*/
|
|
|
66 |
public function __construct(int $id, \stdClass $record) {
|
|
|
67 |
$this->data = new data($id, $record);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Creates an instance of data_controller
|
|
|
72 |
*
|
|
|
73 |
* Parameters $id, $record and $field can complement each other but not conflict.
|
|
|
74 |
* If $id is not specified, fieldid must be present either in $record or in $field.
|
|
|
75 |
* If $id is not specified, instanceid must be present in $record
|
|
|
76 |
*
|
|
|
77 |
* No DB queries are performed if both $record and $field are specified.
|
|
|
78 |
|
|
|
79 |
* @param int $id
|
|
|
80 |
* @param \stdClass|null $record
|
|
|
81 |
* @param field_controller|null $field
|
|
|
82 |
* @return data_controller
|
|
|
83 |
* @throws \coding_exception
|
|
|
84 |
* @throws \moodle_exception
|
|
|
85 |
*/
|
|
|
86 |
public static function create(int $id, \stdClass $record = null, field_controller $field = null): data_controller {
|
|
|
87 |
global $DB;
|
|
|
88 |
if ($id && $record) {
|
|
|
89 |
// This warning really should be in persistent as well.
|
|
|
90 |
debugging('Too many parameters, either id need to be specified or a record, but not both.',
|
|
|
91 |
DEBUG_DEVELOPER);
|
|
|
92 |
}
|
|
|
93 |
if ($id) {
|
|
|
94 |
$record = $DB->get_record(data::TABLE, array('id' => $id), '*', MUST_EXIST);
|
|
|
95 |
} else if (!$record) {
|
|
|
96 |
$record = new \stdClass();
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (!$field && empty($record->fieldid)) {
|
|
|
100 |
throw new \coding_exception('Not enough parameters to initialise data_controller - unknown field');
|
|
|
101 |
}
|
|
|
102 |
if (!$field) {
|
|
|
103 |
$field = field_controller::create($record->fieldid);
|
|
|
104 |
}
|
|
|
105 |
if (empty($record->fieldid)) {
|
|
|
106 |
$record->fieldid = $field->get('id');
|
|
|
107 |
}
|
|
|
108 |
if ($field->get('id') != $record->fieldid) {
|
|
|
109 |
throw new \coding_exception('Field id from the record does not match field from the parameter');
|
|
|
110 |
}
|
|
|
111 |
$type = $field->get('type');
|
|
|
112 |
$customfieldtype = "\\customfield_{$type}\\data_controller";
|
|
|
113 |
if (!class_exists($customfieldtype) || !is_subclass_of($customfieldtype, self::class)) {
|
|
|
114 |
throw new \moodle_exception('errorfieldtypenotfound', 'core_customfield', '', s($type));
|
|
|
115 |
}
|
|
|
116 |
$datacontroller = new $customfieldtype(0, $record);
|
|
|
117 |
$datacontroller->field = $field;
|
|
|
118 |
return $datacontroller;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Returns the name of the field to be used on HTML forms.
|
|
|
123 |
*
|
|
|
124 |
* @return string
|
|
|
125 |
*/
|
|
|
126 |
public function get_form_element_name(): string {
|
|
|
127 |
return 'customfield_' . $this->get_field()->get('shortname');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Persistent getter parser.
|
|
|
132 |
*
|
|
|
133 |
* @param string $property
|
|
|
134 |
* @return mixed
|
|
|
135 |
*/
|
|
|
136 |
final public function get($property) {
|
|
|
137 |
return $this->data->get($property);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Persistent setter parser.
|
|
|
142 |
*
|
|
|
143 |
* @param string $property
|
|
|
144 |
* @param mixed $value
|
|
|
145 |
* @return data
|
|
|
146 |
*/
|
|
|
147 |
final public function set($property, $value) {
|
|
|
148 |
return $this->data->set($property, $value);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Return the name of the field in the db table {customfield_data} where the data is stored
|
|
|
153 |
*
|
|
|
154 |
* Must be one of the following:
|
|
|
155 |
* intvalue - can store integer values, this field is indexed
|
|
|
156 |
* decvalue - can store decimal values
|
|
|
157 |
* shortcharvalue - can store character values up to 255 characters long, this field is indexed
|
|
|
158 |
* charvalue - can store character values up to 1333 characters long, this field is not indexed but
|
|
|
159 |
* full text search is faster than on field 'value'
|
|
|
160 |
* value - can store character values of unlimited length ("text" field in the db)
|
|
|
161 |
*
|
|
|
162 |
* @return string
|
|
|
163 |
*/
|
|
|
164 |
abstract public function datafield(): string;
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Delete data. Element can override it if related information needs to be deleted as well (such as files)
|
|
|
168 |
*
|
|
|
169 |
* @return bool
|
|
|
170 |
*/
|
|
|
171 |
public function delete() {
|
|
|
172 |
return $this->data->delete();
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Persistent save parser.
|
|
|
177 |
*
|
|
|
178 |
* @return void
|
|
|
179 |
*/
|
|
|
180 |
public function save() {
|
|
|
181 |
$this->data->save();
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Field associated with this data
|
|
|
186 |
*
|
|
|
187 |
* @return field_controller
|
|
|
188 |
*/
|
|
|
189 |
public function get_field(): field_controller {
|
|
|
190 |
return $this->field;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Saves the data coming from form
|
|
|
195 |
*
|
|
|
196 |
* @param \stdClass $datanew data coming from the form
|
|
|
197 |
*/
|
|
|
198 |
public function instance_form_save(\stdClass $datanew) {
|
|
|
199 |
$elementname = $this->get_form_element_name();
|
|
|
200 |
if (!property_exists($datanew, $elementname)) {
|
|
|
201 |
return;
|
|
|
202 |
}
|
|
|
203 |
$value = $datanew->$elementname;
|
|
|
204 |
$this->data->set($this->datafield(), $value);
|
|
|
205 |
$this->data->set('value', $value);
|
|
|
206 |
$this->save();
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Prepares the custom field data related to the object to pass to mform->set_data() and adds them to it
|
|
|
211 |
*
|
|
|
212 |
* This function must be called before calling $form->set_data($object);
|
|
|
213 |
*
|
|
|
214 |
* @param \stdClass $instance the instance that has custom fields, if 'id' attribute is present the custom
|
|
|
215 |
* fields for this instance will be added, otherwise the default values will be added.
|
|
|
216 |
*/
|
|
|
217 |
public function instance_form_before_set_data(\stdClass $instance) {
|
|
|
218 |
$instance->{$this->get_form_element_name()} = $this->get_value();
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Checks if the value is empty
|
|
|
223 |
*
|
|
|
224 |
* @param mixed $value
|
|
|
225 |
* @return bool
|
|
|
226 |
*/
|
|
|
227 |
protected function is_empty($value): bool {
|
|
|
228 |
if ($this->datafield() === 'value' || $this->datafield() === 'charvalue' || $this->datafield() === 'shortcharvalue') {
|
|
|
229 |
return '' . $value === '';
|
|
|
230 |
}
|
|
|
231 |
return empty($value);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Checks if the value is unique
|
|
|
236 |
*
|
|
|
237 |
* @param mixed $value
|
|
|
238 |
* @return bool
|
|
|
239 |
*/
|
|
|
240 |
protected function is_unique($value): bool {
|
|
|
241 |
global $DB;
|
|
|
242 |
|
|
|
243 |
// Ensure the "value" datafield can be safely compared across all databases.
|
|
|
244 |
$datafield = $this->datafield();
|
|
|
245 |
if ($datafield === 'value') {
|
|
|
246 |
$datafield = $DB->sql_cast_to_char($datafield);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
$where = "fieldid = ? AND {$datafield} = ?";
|
|
|
250 |
$params = [$this->get_field()->get('id'), $value];
|
|
|
251 |
if ($this->get('id')) {
|
|
|
252 |
$where .= ' AND id <> ?';
|
|
|
253 |
$params[] = $this->get('id');
|
|
|
254 |
}
|
|
|
255 |
return !$DB->record_exists_select('customfield_data', $where, $params);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* Called from instance edit form in validation()
|
|
|
260 |
*
|
|
|
261 |
* @param array $data
|
|
|
262 |
* @param array $files
|
|
|
263 |
* @return array array of errors
|
|
|
264 |
*/
|
|
|
265 |
public function instance_form_validation(array $data, array $files): array {
|
|
|
266 |
$errors = [];
|
|
|
267 |
$elementname = $this->get_form_element_name();
|
|
|
268 |
if ($this->get_field()->get_configdata_property('uniquevalues') == 1) {
|
|
|
269 |
$value = $data[$elementname];
|
|
|
270 |
if (!$this->is_empty($value) && !$this->is_unique($value)) {
|
|
|
271 |
$errors[$elementname] = get_string('erroruniquevalues', 'core_customfield');
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
return $errors;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Called from instance edit form in definition_after_data()
|
|
|
279 |
*
|
|
|
280 |
* @param \MoodleQuickForm $mform
|
|
|
281 |
*/
|
|
|
282 |
public function instance_form_definition_after_data(\MoodleQuickForm $mform) {
|
|
|
283 |
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Used by handlers to display data on various places.
|
|
|
288 |
*
|
|
|
289 |
* @return string
|
|
|
290 |
*/
|
|
|
291 |
public function display(): string {
|
|
|
292 |
global $PAGE;
|
|
|
293 |
$output = $PAGE->get_renderer('core_customfield');
|
|
|
294 |
return $output->render(new field_data($this));
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
/**
|
|
|
298 |
* Returns the default value as it would be stored in the database (not in human-readable format).
|
|
|
299 |
*
|
|
|
300 |
* @return mixed
|
|
|
301 |
*/
|
|
|
302 |
abstract public function get_default_value();
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* Returns the value as it is stored in the database or default value if data record is not present
|
|
|
306 |
*
|
|
|
307 |
* @return mixed
|
|
|
308 |
*/
|
|
|
309 |
public function get_value() {
|
|
|
310 |
if (!$this->get('id')) {
|
|
|
311 |
return $this->get_default_value();
|
|
|
312 |
}
|
|
|
313 |
return $this->get($this->datafield());
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Return the context of the field
|
|
|
318 |
*
|
|
|
319 |
* @return \context
|
|
|
320 |
*/
|
|
|
321 |
public function get_context(): \context {
|
|
|
322 |
if ($this->get('contextid')) {
|
|
|
323 |
return \context::instance_by_id($this->get('contextid'));
|
|
|
324 |
} else if ($this->get('instanceid')) {
|
|
|
325 |
return $this->get_field()->get_handler()->get_instance_context($this->get('instanceid'));
|
|
|
326 |
} else {
|
|
|
327 |
// Context is not yet known (for example, entity is not yet created).
|
|
|
328 |
return \context_system::instance();
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Add a field to the instance edit form.
|
|
|
334 |
*
|
|
|
335 |
* @param \MoodleQuickForm $mform
|
|
|
336 |
*/
|
|
|
337 |
abstract public function instance_form_definition(\MoodleQuickForm $mform);
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Returns value in a human-readable format or default value if data record is not present
|
|
|
341 |
*
|
|
|
342 |
* This is the default implementation that most likely needs to be overridden
|
|
|
343 |
*
|
|
|
344 |
* @return mixed|null value or null if empty
|
|
|
345 |
*/
|
|
|
346 |
public function export_value() {
|
|
|
347 |
$value = $this->get_value();
|
|
|
348 |
|
|
|
349 |
if ($this->is_empty($value)) {
|
|
|
350 |
return null;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
if ($this->datafield() === 'intvalue') {
|
|
|
354 |
return (int)$value;
|
|
|
355 |
} else if ($this->datafield() === 'decvalue') {
|
|
|
356 |
return (float)$value;
|
|
|
357 |
} else if ($this->datafield() === 'value') {
|
|
|
358 |
return format_text($value, $this->get('valueformat'), [
|
|
|
359 |
'context' => $this->get_context(),
|
|
|
360 |
'trusted' => $this->get('valuetrust'),
|
|
|
361 |
]);
|
|
|
362 |
} else {
|
|
|
363 |
return format_string($value, true, ['context' => $this->get_context()]);
|
|
|
364 |
}
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Callback for backup, allowing custom fields to add additional data to the backup.
|
|
|
369 |
* It is not an abstract method for backward compatibility reasons.
|
|
|
370 |
*
|
|
|
371 |
* @param \backup_nested_element $customfieldelement The custom field element to be backed up.
|
|
|
372 |
*/
|
|
|
373 |
public function backup_define_structure(backup_nested_element $customfieldelement): void {
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
/**
|
|
|
377 |
* Callback for restore, allowing custom fields to restore additional data from the backup.
|
|
|
378 |
* It is not an abstract method for backward compatibility reasons.
|
|
|
379 |
*
|
|
|
380 |
* @param \restore_structure_step $step The restore step instance.
|
|
|
381 |
* @param int $newid The new ID for the custom field data after restore.
|
|
|
382 |
* @param int $oldid The original ID of the custom field data before backup.
|
|
|
383 |
*/
|
|
|
384 |
public function restore_define_structure(\restore_structure_step $step, int $newid, int $oldid): void {
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* Persistent to_record parser.
|
|
|
389 |
*
|
|
|
390 |
* @return \stdClass
|
|
|
391 |
*/
|
|
|
392 |
final public function to_record() {
|
|
|
393 |
return $this->data->to_record();
|
|
|
394 |
}
|
|
|
395 |
}
|