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
 * AMD module for purposes actions.
18
 *
19
 * @module     tool_dataprivacy/purposesactions
20
 * @copyright  2018 David Monllao
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
// This file is part of Moodle - http://moodle.org/
24
//
25
// Moodle is free software: you can redistribute it and/or modify
26
// it under the terms of the GNU General Public License as published by
27
// the Free Software Foundation, either version 3 of the License, or
28
// (at your option) any later version.
29
//
30
// Moodle is distributed in the hope that it will be useful,
31
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
// GNU General Public License for more details.
34
//
35
// You should have received a copy of the GNU General Public License
36
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
37
 
38
/**
39
 * Module for purpose actions.
40
 *
41
 * @module     tool_dataprivacy/purposeactions
42
 * @copyright  2018 David Monllao
43
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
45
import * as Ajax from 'core/ajax';
46
import * as Notification from 'core/notification';
47
import * as Str from 'core/str';
48
import ModalEvents from 'core/modal_events';
49
import ModalSaveCancel from 'core/modal_save_cancel';
50
 
51
/**
52
 * List of action selectors.
53
 *
54
 * @type {{DELETE: string}}
55
 */
56
const ACTIONS = {
57
    DELETE: '[data-action="deletepurpose"]',
58
};
59
 
60
export default class PurposeActions {
61
 
62
    static init() {
63
        return new this();
64
    }
65
 
66
    constructor() {
67
        this.registerEvents();
68
    }
69
 
70
    deletePurpose(id) {
71
        return Ajax.call([{
72
            methodname: 'tool_dataprivacy_delete_purpose',
73
            args: {id}
74
        }])[0];
75
    }
76
 
77
    handleRemoval(id) {
78
        this.deletePurpose(id)
79
            .then((data) => {
80
                if (data.result) {
81
                    document.querySelector(`tr[data-purposeid="${id}"]`)?.remove();
82
                } else {
83
                    Notification.addNotification({
84
                        message: data.warnings[0].message,
85
                        type: 'error'
86
                    });
87
                }
88
 
89
                return;
90
            })
91
            .catch(Notification.exception);
92
 
93
    }
94
 
95
    /**
96
     * Register event listeners.
97
     */
98
    registerEvents() {
99
        document.addEventListener('click', (e) => {
100
            const target = e.target.closest(ACTIONS.DELETE);
101
            if (!target) {
102
                return;
103
            }
104
 
105
            e.preventDefault();
106
 
107
            this.confirmRemoval(target);
108
        });
109
    }
110
 
111
    confirmRemoval(target) {
112
        const id = target.dataset.id;
113
        var purposename = target.dataset.name;
114
        var stringkeys = [
115
            {
116
                key: 'deletepurpose',
117
                component: 'tool_dataprivacy'
118
            },
119
            {
120
                key: 'deletepurposetext',
121
                component: 'tool_dataprivacy',
122
                param: purposename
123
            },
124
            {
125
                key: 'delete'
126
            }
127
        ];
128
 
129
        Str.get_strings(stringkeys).then(([
130
            title,
131
            body,
132
            save,
133
        ]) => ModalSaveCancel.create({
134
            title,
135
            body,
136
            buttons: {
137
                save,
138
            },
139
            show: true,
140
            removeOnClose: true,
141
        }))
142
            .then((modal) => {
143
                // Handle save event.
144
                modal.getRoot().on(ModalEvents.save, () => this.handleRemoval(id));
145
 
146
                return modal;
147
            })
148
            .catch(Notification.exception);
149
    }
150
}