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 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 placement 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 aiplacement_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 array The list of actions this manager covers */
37
    protected array $actions;
38
 
39
    /** @var \core_ai\manager The AI manager */
40
    protected \core_ai\manager $manager;
41
 
42
    /**
43
     * Constructor.
44
     *
45
     * @param string $uniqueid The table unique id.
46
     */
47
    public function __construct(string $uniqueid) {
48
        // Parse the unique id and get the plugin name.
49
        $parseuniqueid = explode('-', $uniqueid);
50
        $pluginname = end($parseuniqueid);
51
        $this->pluginname = $pluginname;
52
 
53
        // Get the list of actions that this provider supports.
54
        $this->actions = manager::get_supported_actions($this->pluginname);
55
 
56
        parent::__construct($this->get_table_id());
57
 
58
        $this->setup_column_configuration();
59
        $this->set_filterset(new aiplacement_action_management_table_filterset());
60
        $this->manager = \core\di::get(manager::class);
61
        $this->setup();
62
    }
63
 
64
    #[\Override]
65
    public function get_context(): \context_system {
66
        return \context_system::instance();
67
    }
68
 
69
    /**
70
     * Set up the column configuration for this table.
71
     */
72
    protected function setup_column_configuration(): void {
73
        $columnlist = $this->get_column_list();
74
        $this->define_columns(array_keys($columnlist));
75
        $this->define_headers(array_values($columnlist));
76
    }
77
 
78
    /**
79
     * Get the ID of the table.
80
     *
81
     * @return string
82
     */
83
    protected function get_table_id(): string {
84
        return "aiplacementaction_management_table-{$this->pluginname}";
85
    }
86
 
87
    /**
88
     * Get the web service method used to toggle state.
89
     *
90
     * @return null|string
91
     */
92
    protected function get_toggle_service(): ?string {
93
        return 'core_ai_set_action';
94
    }
95
 
96
    /**
97
     * Get the JS module used to manage this table.
98
     *
99
     * This should be a class which extends 'core_admin/plugin_management_table'.
100
     *
101
     * @return string
102
     */
103
    protected function get_table_js_module(): string {
104
        return 'core_admin/plugin_management_table';
105
    }
106
 
107
    #[\Override]
108
    protected function get_dynamic_table_html_end(): string {
109
        global $PAGE;
110
 
111
        $PAGE->requires->js_call_amd($this->get_table_js_module(), 'init');
112
        return parent::get_dynamic_table_html_end();
113
    }
114
 
115
    /**
116
     * Get a list of the column titles
117
     * @return string[]
118
     */
119
    protected function get_column_list(): array {
120
        return [
121
            'namedesc' => get_string('name', 'core'),
122
            'enabled' => get_string('pluginenabled', 'core_plugin'),
123
        ];
124
    }
125
 
126
    /**
127
     * Show the name column content.
128
     *
129
     * @param stdClass $row
130
     * @return string
131
     */
132
    protected function col_namedesc(stdClass $row): string {
133
        global $OUTPUT;
134
 
135
        $params = [
136
            'name' => $row->action::get_name(),
137
            'description' => $row->action::get_description(),
138
        ];
139
        $output = $OUTPUT->render_from_template('core_admin/table/namedesc', $params);
140
 
141
        if (!$this->manager->is_action_available($row->action)) {
142
            $providerurl = new moodle_url('/admin/settings.php', ['section' => 'aiprovider']);
143
            $output .= $OUTPUT->render_from_template('core_ai/admin_noproviders', [
144
                'providerurl' => $providerurl->out(),
145
            ]);
146
        }
147
 
148
        return $output;
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
     * Get any class to add to the row.
185
     *
186
     * @param mixed $row
187
     * @return string
188
     */
189
    protected function get_row_class($row): string {
190
        if (!$row->enabled) {
191
            return 'dimmed_text';
192
        }
193
        return '';
194
    }
195
 
196
    /**
197
     * Get the table content.
198
     */
199
    public function get_content(): string {
200
        ob_start();
201
        $this->out();
202
        $content = ob_get_contents();
203
        ob_end_clean();
204
        return $content;
205
    }
206
 
207
    /**
208
     * Print the table.
209
     */
210
    public function out(): void {
211
        foreach ($this->actions as $actionclass) {
212
            // Construct the row data.
213
            $rowdata = (object) [
214
                'action' => $actionclass,
215
                'enabled' => $this->manager->is_action_enabled($this->pluginname, $actionclass),
216
            ];
217
            $this->add_data_keyed(
218
                $this->format_row($rowdata),
219
                $this->get_row_class($rowdata)
220
            );
221
        }
222
 
223
        $this->finish_output(false);
224
    }
225
 
226
    #[\Override]
227
    public function is_downloadable($downloadable = null): bool {
228
        return false;
229
    }
230
 
231
    #[\Override]
232
    public function guess_base_url(): void {
233
        $url = new moodle_url('/');
234
        $this->define_baseurl($url);
235
    }
236
 
237
    #[\Override]
238
    public function has_capability(): bool {
239
        return has_capability('moodle/site:config', $this->get_context());
240
    }
241
}