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 core_ai\table;
|
|
|
18 |
|
|
|
19 |
use core_ai\manager;
|
|
|
20 |
use core_table\dynamic as dynamic_table;
|
|
|
21 |
use flexible_table;
|
|
|
22 |
use moodle_url;
|
|
|
23 |
use stdClass;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Table to manage AI actions used in provider plugins.
|
|
|
27 |
*
|
|
|
28 |
* @package core_ai
|
|
|
29 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class aiprovider_action_management_table extends flexible_table implements dynamic_table {
|
|
|
33 |
/** @var string The name of the plugin these actions related too */
|
|
|
34 |
protected string $pluginname;
|
|
|
35 |
|
|
|
36 |
/** @var int the provider instance id */
|
|
|
37 |
protected int $providerid;
|
|
|
38 |
|
|
|
39 |
/** @var array The list of actions this manager covers */
|
|
|
40 |
protected array $actions;
|
|
|
41 |
|
|
|
42 |
/** @var \core_ai\manager The AI manager */
|
|
|
43 |
protected \core_ai\manager $manager;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Constructor.
|
|
|
47 |
*
|
|
|
48 |
* @param string $uniqueid The table unique id.
|
|
|
49 |
*/
|
|
|
50 |
public function __construct(string $uniqueid) {
|
|
|
51 |
$parseuniqueid = explode('-', $uniqueid);
|
|
|
52 |
$this->pluginname = reset($parseuniqueid);
|
|
|
53 |
$this->providerid = (int)end($parseuniqueid);
|
|
|
54 |
|
|
|
55 |
// Get the list of actions that this provider supports.
|
|
|
56 |
$this->actions = \core_ai\manager::get_supported_actions($this->pluginname);
|
|
|
57 |
|
|
|
58 |
parent::__construct($uniqueid);
|
|
|
59 |
|
|
|
60 |
$this->setup_column_configuration();
|
|
|
61 |
$this->set_filterset(new aiprovider_action_management_table_filterset());
|
|
|
62 |
$this->manager = \core\di::get(manager::class);
|
|
|
63 |
$this->setup();
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
#[\Override]
|
|
|
67 |
public function get_context(): \context_system {
|
|
|
68 |
return \context_system::instance();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Set up the column configuration for this table.
|
|
|
73 |
*/
|
|
|
74 |
protected function setup_column_configuration(): void {
|
|
|
75 |
$columnlist = $this->get_column_list();
|
|
|
76 |
$this->define_columns(array_keys($columnlist));
|
|
|
77 |
$this->define_headers(array_values($columnlist));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get the ID of the table.
|
|
|
82 |
*
|
|
|
83 |
* @return string
|
|
|
84 |
*/
|
|
|
85 |
protected function get_table_id(): string {
|
|
|
86 |
return "aiprovider_action_management_table-{$this->pluginname}";
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get the web service method used to toggle state.
|
|
|
91 |
*
|
|
|
92 |
* @return null|string
|
|
|
93 |
*/
|
|
|
94 |
protected function get_toggle_service(): ?string {
|
|
|
95 |
return 'core_ai_set_action';
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Get the JS module used to manage this table.
|
|
|
100 |
*
|
|
|
101 |
* This should be a class which extends 'core_admin/plugin_management_table'.
|
|
|
102 |
*
|
|
|
103 |
* @return string
|
|
|
104 |
*/
|
|
|
105 |
protected function get_table_js_module(): string {
|
|
|
106 |
return 'core_ai/aiprovider_action_management_table';
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
#[\Override]
|
|
|
110 |
protected function get_dynamic_table_html_end(): string {
|
|
|
111 |
global $PAGE;
|
|
|
112 |
|
|
|
113 |
$PAGE->requires->js_call_amd(
|
|
|
114 |
$this->get_table_js_module(),
|
|
|
115 |
'init',
|
|
|
116 |
[$this->providerid]
|
|
|
117 |
);
|
|
|
118 |
return parent::get_dynamic_table_html_end();
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Get a list of the column titles
|
|
|
123 |
*
|
|
|
124 |
* @return string[]
|
|
|
125 |
*/
|
|
|
126 |
protected function get_column_list(): array {
|
|
|
127 |
return [
|
|
|
128 |
'namedesc' => get_string('action', 'core_ai'),
|
|
|
129 |
'enabled' => get_string('pluginenabled', 'core_plugin'),
|
|
|
130 |
'settings' => get_string('settings', 'core'),
|
|
|
131 |
];
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Show the name column content.
|
|
|
136 |
*
|
|
|
137 |
* @param stdClass $row
|
|
|
138 |
* @return string
|
|
|
139 |
*/
|
|
|
140 |
protected function col_namedesc(stdClass $row): string {
|
|
|
141 |
global $OUTPUT;
|
|
|
142 |
|
|
|
143 |
$params = [
|
|
|
144 |
'name' => $row->action::get_name(),
|
|
|
145 |
'description' => $row->action::get_description(),
|
|
|
146 |
];
|
|
|
147 |
|
|
|
148 |
return $OUTPUT->render_from_template('core_admin/table/namedesc', $params);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Show the enable/disable column content.
|
|
|
153 |
*
|
|
|
154 |
* @param stdClass $row
|
|
|
155 |
* @return string
|
|
|
156 |
*/
|
|
|
157 |
protected function col_enabled(stdClass $row): string {
|
|
|
158 |
global $OUTPUT;
|
|
|
159 |
|
|
|
160 |
$enabled = $row->enabled;
|
|
|
161 |
$identifier = $enabled ? 'disableplugin' : 'enableplugin';
|
|
|
162 |
$labelstr = get_string($identifier, 'core_admin', $row->action::get_name());
|
|
|
163 |
|
|
|
164 |
$params = [
|
|
|
165 |
'id' => 'admin-toggle-' . $row->action::get_basename(),
|
|
|
166 |
'checked' => $enabled,
|
|
|
167 |
'dataattributes' => [
|
|
|
168 |
'name' => 'id',
|
|
|
169 |
'value' => $row->action,
|
|
|
170 |
'toggle-method' => $this->get_toggle_service(),
|
|
|
171 |
'action' => 'togglestate',
|
|
|
172 |
'plugin' => $this->pluginname . "-" . $row->action::get_basename(),
|
|
|
173 |
'state' => $enabled ? 1 : 0,
|
|
|
174 |
],
|
|
|
175 |
'title' => $labelstr,
|
|
|
176 |
'label' => $labelstr,
|
|
|
177 |
'labelclasses' => 'visually-hidden',
|
|
|
178 |
];
|
|
|
179 |
|
|
|
180 |
return $OUTPUT->render_from_template('core_admin/setting_configtoggle', $params);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Show the settings column content.
|
|
|
185 |
*
|
|
|
186 |
* @param stdClass $row
|
|
|
187 |
* @return string
|
|
|
188 |
*/
|
|
|
189 |
protected function col_settings(stdClass $row): string {
|
|
|
190 |
$providerclass = "\\$this->pluginname\\provider";
|
|
|
191 |
$mform = $providerclass::get_action_settings($row->action);
|
|
|
192 |
|
|
|
193 |
// Only show the per action settings options if we have a form to display.
|
|
|
194 |
if ($mform) {
|
|
|
195 |
$urlparams = [
|
|
|
196 |
'provider' => $this->pluginname,
|
|
|
197 |
'action' => $row->action,
|
|
|
198 |
'providerid' => $this->providerid,
|
|
|
199 |
];
|
|
|
200 |
$url = new \moodle_url('/ai/configure_actions.php', $urlparams);
|
|
|
201 |
return \html_writer::link($url, get_string('settings'));
|
|
|
202 |
} else {
|
|
|
203 |
return '';
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Get any class to add to the row.
|
|
|
209 |
*
|
|
|
210 |
* @param mixed $row
|
|
|
211 |
* @return string
|
|
|
212 |
*/
|
|
|
213 |
protected function get_row_class($row): string {
|
|
|
214 |
if (!$row->enabled) {
|
|
|
215 |
return 'dimmed_text';
|
|
|
216 |
}
|
|
|
217 |
return '';
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Get the table content.
|
|
|
222 |
*/
|
|
|
223 |
public function get_content(): string {
|
|
|
224 |
ob_start();
|
|
|
225 |
$this->out();
|
|
|
226 |
$content = ob_get_contents();
|
|
|
227 |
ob_end_clean();
|
|
|
228 |
return $content;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Print the table.
|
|
|
233 |
*/
|
|
|
234 |
public function out(): void {
|
|
|
235 |
foreach ($this->actions as $actionclass) {
|
|
|
236 |
// Construct the row data.
|
|
|
237 |
$rowdata = (object) [
|
|
|
238 |
'action' => $actionclass,
|
|
|
239 |
'enabled' => $this->manager->is_action_enabled(
|
|
|
240 |
plugin: $this->pluginname,
|
|
|
241 |
actionclass: $actionclass,
|
|
|
242 |
instanceid: $this->providerid),
|
|
|
243 |
];
|
|
|
244 |
$this->add_data_keyed(
|
|
|
245 |
$this->format_row($rowdata),
|
|
|
246 |
$this->get_row_class($rowdata),
|
|
|
247 |
);
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
$this->finish_output(false);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
#[\Override]
|
|
|
254 |
public function is_downloadable($downloadable = null): bool {
|
|
|
255 |
return false;
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
#[\Override]
|
|
|
259 |
public function guess_base_url(): void {
|
|
|
260 |
$url = new moodle_url('/');
|
|
|
261 |
$this->define_baseurl($url);
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* Check capability for users accessing the action management table.
|
|
|
266 |
*
|
|
|
267 |
* @return bool
|
|
|
268 |
*/
|
|
|
269 |
public function has_capability(): bool {
|
|
|
270 |
return has_capability('moodle/site:config', $this->get_context());
|
|
|
271 |
}
|
|
|
272 |
}
|