Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
17
 * Javascript module for deleting a database as a preset.
18
 *
19
 * @module      mod_data/deletepreset
20
 * @copyright   2022 Amaia Anabitarte <amaia@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import Notification from 'core/notification';
25
import {prefetchStrings} from 'core/prefetch';
26
import {getString} from 'core/str';
27
import Ajax from 'core/ajax';
28
import Url from 'core/url';
29
 
30
const selectors = {
31
    deletePresetButton: '[data-action="deletepreset"]',
32
};
33
 
34
/**
35
 * Initialize module
36
 */
37
export const init = () => {
38
    prefetchStrings('mod_data', [
39
        'deleteconfirm',
40
        'deletewarning',
41
    ]);
42
    prefetchStrings('core', [
43
        'delete',
44
    ]);
45
 
46
    registerEventListeners();
47
};
48
 
49
/**
50
 * Register events for delete preset option in action menu.
51
 */
52
const registerEventListeners = () => {
53
    document.addEventListener('click', (event) => {
54
        const deleteOption = event.target.closest(selectors.deletePresetButton);
55
        if (deleteOption) {
56
            event.preventDefault();
57
            deletePresetConfirm(deleteOption);
58
        }
59
    });
60
};
61
 
62
/**
63
 * Show the confirmation modal to delete the preset.
64
 *
65
 * @param {HTMLElement} deleteOption the element to delete.
66
 */
67
const deletePresetConfirm = (deleteOption) => {
68
    const presetName = deleteOption.getAttribute('data-presetname');
69
    const dataId = deleteOption.getAttribute('data-dataid');
70
 
71
    Notification.deleteCancelPromise(
72
        getString('deleteconfirm', 'mod_data', presetName),
73
        getString('deletewarning', 'mod_data'),
74
    ).then(() => {
75
        return deletePreset(dataId, presetName);
76
    }).catch(() => {
77
        return;
78
    });
79
};
80
 
81
/**
82
 * Delete site user preset.
83
 *
84
 * @param {int} dataId The id of the current database activity.
85
 * @param {string} presetName The preset name to delete.
86
 * @return {promise} Resolved with the result and warnings of deleting a preset.
87
 */
88
async function deletePreset(dataId, presetName) {
89
    var request = {
90
        methodname: 'mod_data_delete_saved_preset',
91
        args: {
92
            dataid: dataId,
93
            presetnames: {presetname: presetName},
94
        }
95
    };
96
    try {
97
        await Ajax.call([request])[0];
98
        window.location.href = Url.relativeUrl(
99
            'mod/data/preset.php',
100
            {
101
                d: dataId,
102
            },
103
            false
104
        );
105
    } catch (error) {
106
        Notification.exception(error);
107
    }
108
}