Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
import PluginManagementTable from 'core_admin/plugin_management_table';
17
import {call as fetchMany} from 'core/ajax';
18
import {refreshTableContent} from 'core_table/dynamic';
19
import Pending from 'core/pending';
20
import {fetchNotifications} from 'core/notification';
21
import {prefetchStrings} from 'core/prefetch';
22
import {getString} from 'core/str';
23
import DeleteCancelModal from 'core/modal_delete_cancel';
24
import ModalEvents from 'core/modal_events';
25
 
26
let watching = false;
27
 
28
/**
29
 * Handles setting plugin state for the AI provider management table.
30
 *
31
 * @module     core_ai/aiprovider_instance_management_table
32
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
export default class extends PluginManagementTable {
36
    constructor() {
37
        super();
38
        this.addClickHandler(this.handleDelete);
39
    }
40
 
41
    /**
42
     * Initialise an instance of the class.
43
     *
44
     * This is just a way of making it easier to initialise an instance of the class from PHP.
45
     */
46
    static init() {
47
        if (watching) {
48
            return;
49
        }
50
 
51
        prefetchStrings('core_ai', [
52
            'providerinstancedelete',
53
            'providerinstancedeleteconfirm',
54
        ]);
55
 
56
        watching = true;
57
        new this();
58
    }
59
 
60
    /**
61
     * Call the delete service.
62
     *
63
     * @param {string} methodname The web service to call
64
     * @param {number} providerid The provider id.
65
     * @return {Promise} The promise.
66
     */
67
    deleteProvider(methodname, providerid) {
68
        return fetchMany([{
69
            methodname,
70
            args: {
71
                providerid,
72
            },
73
        }])[0];
74
    }
75
 
76
    /**
77
     * Handle delete.
78
     *
79
     * @param {HTMLElement} tableRoot
80
     * @param {Event} e
81
     */
82
    async handleDelete(tableRoot, e) {
83
        const deleteElement = e.target.closest('[data-delete-method]');
84
        if (deleteElement) {
85
            e.preventDefault();
86
            const providerId = e.target.dataset.id;
87
            const deleteMethod = e.target.dataset.deleteMethod;
88
            const bodyParams = {
89
                provider: e.target.dataset.provider,
90
                name: e.target.dataset.name,
91
            };
92
            const modal = await DeleteCancelModal.create({
93
                title: getString('providerinstancedelete', 'core_ai'),
94
                body: getString('providerinstancedeleteconfirm', 'core_ai', bodyParams),
95
                show: true,
96
                removeOnClose: true,
97
            });
98
 
99
            // Handle delete event.
100
            modal.getRoot().on(ModalEvents.delete, async(e) => {
101
                e.preventDefault();
102
                const pendingPromise = new Pending('core_table/dynamic:deleteProvider');
103
                await this.deleteProvider(deleteMethod, providerId);
104
                // Reload the table, so we get the updated list of providers, and any messages.
105
                await Promise.all([
106
                    refreshTableContent(tableRoot),
107
                    fetchNotifications(),
108
                ]);
109
                modal.destroy();
110
                pendingPromise.resolve();
111
            });
112
        }
113
    }
114
}