Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 context_system;
20
use core_table\dynamic as dynamic_table;
21
use flexible_table;
22
use moodle_url;
23
use html_writer;
24
 
25
/**
26
 * Table to manage AI 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_management_table extends flexible_table implements dynamic_table {
33
    /**
34
     * @var array $aiproviders List of configured provider instances.
35
     */
36
    protected array $aiproviders = [];
37
 
38
    /** @var int The number of enabled provider instances. */
39
    protected int $enabledprovidercount = 0;
40
 
41
    /**
42
     * Constructor for the AI provider table.
43
     */
44
    public function __construct() {
45
        parent::__construct($this->get_table_id());
46
 
47
        $this->aiproviders = $this->get_providers();
48
        $this->setup_column_configuration();
49
        $this->set_filterset(new aiprovider_management_table_filterset());
50
        $this->setup();
51
        $tableclasses = $this->attributes['class'] . ' ' . $this->get_table_id();
52
        $this->set_attribute('class', $tableclasses);
53
 
54
        $this->enabledprovidercount = count(array_filter($this->aiproviders, function ($provider) {
55
            return $provider->enabled;
56
        }));
57
    }
58
 
59
    /**
60
     * Get the plugin type for the table.
61
     *
62
     * @return string
63
     * @deprecated since 5.0
64
     */
65
    #[\core\attribute\deprecated(replacement: null, since: '5.0', mdl: 'MDL-82977')]
66
    protected function get_plugintype(): string {
67
        \core\deprecation::emit_deprecation(__FUNCTION__);
68
        return 'aiprovider';
69
    }
70
 
71
    #[\Override]
72
    public function get_context(): context_system {
73
        return context_system::instance();
74
    }
75
 
76
    #[\Override]
77
    public function has_capability(): bool {
78
        return has_capability('moodle/site:config', $this->get_context());
79
    }
80
 
81
    /**
82
     * Get the table id for the table.
83
     *
84
     * @return string
85
     */
86
    protected function get_table_id(): string {
87
        return 'aiproviders_table';
88
    }
89
 
90
    /**
91
     * Get the js module needed for the table.
92
     *
93
     * This module can include table specific ajax calls etc.
94
     *
95
     * @return string
96
     */
97
    protected function get_table_js_module(): string {
98
        return 'core_ai/aiprovider_instance_management_table';
99
    }
100
 
101
    /**
102
     * Webservice for toggle.
103
     *
104
     * @return string
105
     */
106
    protected function get_toggle_service(): string {
107
        return 'core_ai_set_provider_status';
108
    }
109
 
110
    /**
111
     * Webservice for delete.
112
     *
113
     * @return string
114
     */
115
    protected function get_delete_service(): string {
116
        return 'core_ai_delete_provider_instance';
117
    }
118
 
119
    /**
120
     * Get the action URL for the table.
121
     *
122
     * @param array $params The params to pass to the URL.
123
     * @return moodle_url
124
     * @deprecated since 5.0
125
     */
126
    #[\core\attribute\deprecated(replacement: null, since: '5.0', mdl: 'MDL-82977')]
127
    protected function get_action_url(array $params = []): moodle_url {
128
        \core\deprecation::emit_deprecation(__FUNCTION__);
129
        return new moodle_url('/admin/ai.php', $params);
130
    }
131
 
132
    #[\Override]
133
    protected function get_dynamic_table_html_end(): string {
134
        global $PAGE;
135
 
136
        $PAGE->requires->js_call_amd($this->get_table_js_module(), 'init');
137
        return parent::get_dynamic_table_html_end();
138
    }
139
 
140
    /**
141
     * Get the configured ai providers from the manager.
142
     *
143
     * @return array
144
     */
145
    protected function get_providers(): array {
146
        return \core\di::get(\core_ai\manager::class)->get_sorted_providers();
147
    }
148
 
149
    /**
150
     * Setup the column configs for the table.
151
     */
152
    protected function setup_column_configuration(): void {
153
        $columnlist = $this->get_column_list();
154
        $this->define_columns(array_keys($columnlist));
155
        $this->define_headers(array_values($columnlist));
156
    }
157
 
158
    #[\Override]
159
    public function guess_base_url(): void {
160
        $this->define_baseurl(new moodle_url('/admin/ai.php'));
161
    }
162
 
163
    /**
164
     * Get the column list for the table.
165
     *
166
     * @return array
167
     */
168
    protected function get_column_list(): array {
169
        return [
170
            'name' => get_string('name'),
171
            'provider' => get_string('provider', 'core_ai'),
172
            'enabled' => get_string('pluginenabled', 'core_plugin'),
173
            'order' => get_string('order', 'core'),
174
            'settings' => get_string('settings', 'core'),
175
            'delete' => get_string('delete'),
176
        ];
177
    }
178
 
179
    /**
180
     * Get the content of the table.
181
     *
182
     * @return string
183
     */
184
    public function get_content(): string {
185
        ob_start();
186
        $this->out();
187
        $content = ob_get_contents();
188
        ob_end_clean();
189
        return $content;
190
    }
191
 
192
    /**
193
     * Add the row data for the table.
194
     */
195
    public function out(): void {
196
        foreach ($this->aiproviders as $provider) {
197
            $rowdata = (object) [
198
                'id' => $provider->id,
199
                'name' => $provider->name,
200
                'provider' => $provider->provider,
201
                'enabled' => (int)$provider->enabled,
202
            ];
203
            $this->add_data_keyed(
204
                $this->format_row($rowdata),
205
                $this->get_row_class($rowdata)
206
            );
207
        }
208
 
209
        $this->finish_output(false);
210
    }
211
 
212
    /**
213
     * Get the class for row whether is dimmed or not according to enabled or disabled.
214
     *
215
     * @param \stdClass $row The row object
216
     * @return string
217
     */
218
    protected function get_row_class(\stdClass $row): string {
219
        if ($row->enabled) {
220
            return '';
221
        }
222
        return 'dimmed_text';
223
    }
224
 
225
    /**
226
     * The column for the provider instance Name.
227
     *
228
     * @param \stdClass $row The row object
229
     * @return string
230
     */
231
    public function col_name(\stdClass $row): string {
232
        return $row->name;
233
    }
234
 
235
    /**
236
     * The column for the provider plugin name.
237
     *
238
     * @param \stdClass $row The row object
239
     * @return string
240
     */
241
    public function col_provider(\stdClass $row): string {
242
        $component = \core\component::get_component_from_classname($row->provider);
243
        return get_string('pluginname', $component);
244
    }
245
 
246
    /**
247
     * The column for enabled or disabled status of the provider instance.
248
     *
249
     * @param \stdClass $row The row object
250
     * @return string
251
     */
252
    public function col_enabled(\stdClass $row): string {
253
        global $OUTPUT;
254
 
255
        $enabled = $row->enabled;
256
        if ($enabled) {
257
            $labelstr = get_string('disableplugin', 'core_admin', $row->name);
258
        } else {
259
            $labelstr = get_string('enableplugin', 'core_admin', $row->name);
260
        }
261
 
262
        $params = [
263
            'id' => 'ai-provider-toggle-' . $row->id,
264
            'checked' => $enabled,
265
            'dataattributes' => [
266
                'name' => 'id',
267
                'value' => $row->provider,
268
                'toggle-method' => $this->get_toggle_service(),
269
                'action' => 'togglestate',
270
                'plugin' => $row->id, // Set plugin attribute to provider ID.
271
                'state' => $enabled ? 1 : 0,
272
            ],
273
            'title' => $labelstr,
274
            'label' => $labelstr,
275
            'labelclasses' => 'visually-hidden',
276
        ];
277
 
278
        return $OUTPUT->render_from_template('core_admin/setting_configtoggle', $params);
279
    }
280
 
281
    /**
282
     * The column to show the settings of the provider instance.
283
     *
284
     * @param \stdClass $row The row object.
285
     * @return string
286
     */
287
    public function col_settings(\stdClass $row): string {
288
        $settingsurl = new moodle_url('/ai/configure.php', ['id' => $row->id]);
289
        return \html_writer::link($settingsurl, get_string('settings'));
290
 
291
    }
292
 
293
    /**
294
     * The column to show the delete action for the provider instance.
295
     *
296
     * @param \stdClass $row The row object.
297
     * @return string
298
     */
299
    public function col_delete(\stdClass $row): string {
300
        global $OUTPUT;
301
 
302
        // Render the delete button from the template.
303
        $component = \core\component::get_component_from_classname($row->provider);
304
        $provider = get_string('pluginname', $component);
305
        $params = [
306
            'id' => $row->id,
307
            'name' => $row->name,
308
            'provider' => $provider,
309
            'delete-method' => $this->get_delete_service(),
310
        ];
311
        return $OUTPUT->render_from_template('core_ai/admin_delete_provider', $params);
312
    }
313
 
314
    /**
315
     * Get the web service method used to order provider instances.
316
     *
317
     * @return null|string
318
     */
319
    protected function get_sortorder_service(): ?string {
320
        return 'core_ai_set_provider_order';
321
    }
322
 
323
    /**
324
     * Generates the HTML for the order column with up and down controls.
325
     *
326
     * @param \stdClass $row An object representing a row of data.
327
     * @return string The HTML string for the order controls, or an empty string if no controls are needed.
328
     */
329
    protected function col_order(\stdClass $row): string {
330
        global $OUTPUT;
331
 
332
        if (!$row->enabled) {
333
            return '';
334
        }
335
 
336
        if ($this->enabledprovidercount <= 1) {
337
            // There is only one row.
338
            return '';
339
        }
340
 
341
        $hasup = true;
342
        $hasdown = true;
343
 
344
        if (empty($this->currentrow)) {
345
            // This is the top row.
346
            $hasup = false;
347
        }
348
 
349
        if ($this->currentrow === ($this->enabledprovidercount - 1)) {
350
            // This is the last row.
351
            $hasdown = false;
352
        }
353
 
354
        $dataattributes = [
355
            'data-method' => $this->get_sortorder_service(),
356
            'data-action' => 'move',
357
            'data-plugin' => $row->id,
358
        ];
359
 
360
        if ($hasup) {
361
            $upicon = html_writer::link(
362
                $this->get_base_action_url([
363
                    'sesskey' => sesskey(),
364
                    'action' => \core\plugininfo\aiprovider::UP,
365
                    'id' => $row->id,
366
                ]),
367
                $OUTPUT->pix_icon(
368
                    pix: 't/up',
369
                    alt: '',
370
                ),
371
                array_merge($dataattributes, [
372
                    'data-direction' => \core\plugininfo\aiprovider::UP,
373
                    'role' => 'button',
374
                    'aria-label' => get_string('moveitemup', 'core', $row->name),
375
                    'title' => get_string('moveitemup', 'core', $row->name),
376
                    'class' => 'btn btn-link',
377
                ]),
378
            );
379
        } else {
380
            $upicon = '';
381
        }
382
 
383
        if ($hasdown) {
384
            $downicon = html_writer::link(
385
                $this->get_base_action_url([
386
                    'sesskey' => sesskey(),
387
                    'action' => \core\plugininfo\aiprovider::DOWN,
388
                    'id' => $row->id,
389
                ]),
390
                $OUTPUT->pix_icon(
391
                    pix: 't/down',
392
                    alt: '',
393
                ),
394
                array_merge($dataattributes, [
395
                    'data-direction' => \core\plugininfo\aiprovider::DOWN,
396
                    'role' => 'button',
397
                    'aria-label' => get_string('moveitemdown', 'core', $row->name),
398
                    'title' => get_string('moveitemdown', 'core', $row->name),
399
                    'class' => 'btn btn-link',
400
                ]),
401
            );
402
        } else {
403
            $downicon = '';
404
        }
405
 
406
        $spacer = ($hasup && $hasdown) ? $OUTPUT->spacer() : '';
407
        return html_writer::div($upicon . $spacer . $downicon, '', ['class' => 'w-25 d-flex justify-content-center']);
408
    }
409
 
410
    /**
411
     * Get the action URL for this table.
412
     *
413
     * The action URL is used to perform all actions when JS is not available.
414
     *
415
     * @param array $params
416
     * @return moodle_url
417
     */
418
    protected function get_base_action_url(array $params = []): moodle_url {
419
        return new moodle_url('/ai/configure_providers.php', $params);
420
    }
421
}