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 |
namespace editor_tiny\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_multiple_structure;
|
|
|
22 |
use core_external\external_single_structure;
|
|
|
23 |
use core_external\external_value;
|
|
|
24 |
use core_external\external_warnings;
|
|
|
25 |
use editor_tiny\manager;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* External function that returns the TinyMCE configuration for a context.
|
|
|
29 |
*
|
|
|
30 |
* @package editor_tiny
|
|
|
31 |
* @copyright 2025 Moodle Pty Ltd
|
|
|
32 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class get_configuration extends external_api {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Describes the parameters of the external function.
|
|
|
38 |
*
|
|
|
39 |
* @return external_function_parameters
|
|
|
40 |
*/
|
|
|
41 |
public static function execute_parameters(): external_function_parameters {
|
|
|
42 |
return new external_function_parameters([
|
|
|
43 |
'contextlevel' => new external_value(PARAM_ALPHA, 'Context level: system, user, coursecat, course, module or block'),
|
|
|
44 |
'instanceid' => new external_value(PARAM_INT, 'Instance ID of the context (e.g. course ID)'),
|
|
|
45 |
]);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Returns the TinyMCE configuration for a context.
|
|
|
50 |
*
|
|
|
51 |
* This function serves a similar purpose as \editor_tiny\editor::use_editor but with the following differences:
|
|
|
52 |
* - It does not receive editor or file picker options.
|
|
|
53 |
* - It only returns information that depends on site settings or permission checks.
|
|
|
54 |
*
|
|
|
55 |
* @param string $contextlevel Context level: system, user, coursecat, course, module or block.
|
|
|
56 |
* @param int $instanceid Instance ID of the context (e.g. course ID).
|
|
|
57 |
* @return array
|
|
|
58 |
*/
|
|
|
59 |
public static function execute(string $contextlevel, int $instanceid): array {
|
|
|
60 |
global $PAGE;
|
|
|
61 |
|
|
|
62 |
$params = self::validate_parameters(self::execute_parameters(), [
|
|
|
63 |
'contextlevel' => $contextlevel,
|
|
|
64 |
'instanceid' => $instanceid,
|
|
|
65 |
]);
|
|
|
66 |
|
|
|
67 |
$context = self::get_context_from_params($params);
|
|
|
68 |
self::validate_context($context);
|
|
|
69 |
|
|
|
70 |
$siteconfig = get_config('editor_tiny');
|
|
|
71 |
$branding = !empty($siteconfig->branding ?? true);
|
|
|
72 |
$extendedvalidelements = $siteconfig->extended_valid_elements ?? 'script[*],p[*],i[*]';
|
|
|
73 |
|
|
|
74 |
$installedlanguages = [];
|
|
|
75 |
foreach (get_string_manager()->get_list_of_translations(true) as $lang => $name) {
|
|
|
76 |
$installedlanguages[] = ['lang' => $lang, 'name' => $name];
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$manager = new manager();
|
|
|
80 |
$plugins = [];
|
|
|
81 |
foreach ($manager->get_plugin_configuration_for_external($context) as $name => $settings) {
|
|
|
82 |
$plugin = [
|
|
|
83 |
'name' => $name,
|
|
|
84 |
'settings' => [],
|
|
|
85 |
];
|
|
|
86 |
foreach ($settings as $name => $value) {
|
|
|
87 |
$plugin['settings'][] = [
|
|
|
88 |
'name' => $name,
|
|
|
89 |
'value' => $value,
|
|
|
90 |
];
|
|
|
91 |
}
|
|
|
92 |
$plugins[] = $plugin;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return [
|
|
|
96 |
'contextid' => $context->id,
|
|
|
97 |
'branding' => $branding,
|
|
|
98 |
'extendedvalidelements' => $extendedvalidelements,
|
|
|
99 |
'installedlanguages' => $installedlanguages,
|
|
|
100 |
'plugins' => $plugins,
|
|
|
101 |
'warnings' => [],
|
|
|
102 |
];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Describes the return structure of the external function.
|
|
|
107 |
*
|
|
|
108 |
* @return external_single_structure
|
|
|
109 |
*/
|
|
|
110 |
public static function execute_returns(): external_single_structure {
|
|
|
111 |
return new external_single_structure([
|
|
|
112 |
'contextid' => new external_value(PARAM_INT, 'Context id'),
|
|
|
113 |
'branding' => new external_value(PARAM_BOOL, 'Display the TinyMCE logo'),
|
|
|
114 |
'extendedvalidelements' => new external_value(PARAM_RAW, 'Extended valid elements'),
|
|
|
115 |
'installedlanguages' => new external_multiple_structure(
|
|
|
116 |
new external_single_structure([
|
|
|
117 |
'lang' => new external_value(PARAM_LANG, 'Language code'),
|
|
|
118 |
'name' => new external_value(PARAM_RAW, 'Language name'),
|
|
|
119 |
]),
|
|
|
120 |
'List of installed languages',
|
|
|
121 |
),
|
|
|
122 |
'plugins' => new external_multiple_structure(
|
|
|
123 |
new external_single_structure([
|
|
|
124 |
'name' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
|
|
|
125 |
'settings' => new external_multiple_structure(
|
|
|
126 |
new external_single_structure([
|
|
|
127 |
'name' => new external_value(PARAM_RAW, 'Name of the setting'),
|
|
|
128 |
'value' => new external_value(PARAM_RAW, 'Value of the setting'),
|
|
|
129 |
]),
|
|
|
130 |
'Settings of the plugin',
|
|
|
131 |
),
|
|
|
132 |
]),
|
|
|
133 |
'Configuration of enabled plugins for the context'),
|
|
|
134 |
'warnings' => new external_warnings(),
|
|
|
135 |
]);
|
|
|
136 |
}
|
|
|
137 |
}
|