1441 |
ariadna |
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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_customfield\external;
|
|
|
20 |
|
|
|
21 |
use core_customfield\handler;
|
|
|
22 |
use core\external\exporter;
|
|
|
23 |
use core\output\renderer_base;
|
|
|
24 |
use core_customfield\output\field_data;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Custom field data exporter
|
|
|
28 |
*
|
|
|
29 |
* @package core_customfield
|
|
|
30 |
* @copyright 2025 Paul Holden <paulh@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class field_data_exporter extends exporter {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Return a list of objects that are related to the exporter
|
|
|
37 |
*
|
|
|
38 |
* @return array
|
|
|
39 |
*/
|
|
|
40 |
protected static function define_related(): array {
|
|
|
41 |
return [
|
|
|
42 |
'component' => 'string',
|
|
|
43 |
'area' => 'string',
|
|
|
44 |
'itemid' => 'int?',
|
|
|
45 |
'instanceid' => 'int',
|
|
|
46 |
];
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Return the list of additional properties for read structure and export
|
|
|
51 |
*
|
|
|
52 |
* @return array[]
|
|
|
53 |
*/
|
|
|
54 |
protected static function define_other_properties(): array {
|
|
|
55 |
return [
|
|
|
56 |
'data' => [
|
|
|
57 |
'type' => [
|
|
|
58 |
'value' => ['type' => PARAM_TEXT, 'null' => NULL_ALLOWED],
|
|
|
59 |
'type' => ['type' => PARAM_TEXT],
|
|
|
60 |
'shortname' => ['type' => PARAM_TEXT],
|
|
|
61 |
'name' => ['type' => PARAM_TEXT],
|
|
|
62 |
'hasvalue' => ['type' => PARAM_BOOL],
|
|
|
63 |
'instanceid' => ['type' => PARAM_INT],
|
|
|
64 |
],
|
|
|
65 |
'multiple' => true,
|
|
|
66 |
],
|
|
|
67 |
];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Get additional values to inject while exporting
|
|
|
72 |
*
|
|
|
73 |
* @param renderer_base $output
|
|
|
74 |
* @return array
|
|
|
75 |
*/
|
|
|
76 |
protected function get_other_values(renderer_base $output): array {
|
|
|
77 |
|
|
|
78 |
/** @var string $component */
|
|
|
79 |
$component = $this->related['component'];
|
|
|
80 |
|
|
|
81 |
/** @var string $area */
|
|
|
82 |
$area = $this->related['area'];
|
|
|
83 |
|
|
|
84 |
/** @var int $itemid */
|
|
|
85 |
$itemid = (int) $this->related['itemid'];
|
|
|
86 |
|
|
|
87 |
/** @var int $instanceid */
|
|
|
88 |
$instanceid = $this->related['instanceid'];
|
|
|
89 |
|
|
|
90 |
$handler = handler::get_handler($component, $area, $itemid);
|
|
|
91 |
|
|
|
92 |
$data = array_map(
|
|
|
93 |
fn(field_data $fielddata): array => (array) $fielddata->export_for_template($output),
|
|
|
94 |
$handler->export_instance_data($instanceid),
|
|
|
95 |
);
|
|
|
96 |
|
|
|
97 |
return [
|
|
|
98 |
'data' => $data,
|
|
|
99 |
];
|
|
|
100 |
}
|
|
|
101 |
}
|