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
 * Request actions.
18
 *
19
 * @module     tool_dataprivacy/data_deletion
20
 * @copyright  2018 Jun Pataleta
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define([
24
    'jquery',
25
    'core/ajax',
26
    'core/notification',
27
    'core/str',
28
    'core/modal_save_cancel',
29
    'core/modal_events'],
30
function($, Ajax, Notification, Str, ModalSaveCancel, ModalEvents) {
31
 
32
    /**
33
     * List of action selectors.
34
     *
35
     * @type {{MARK_FOR_DELETION: string}}
36
     * @type {{SELECT_ALL: string}}
37
     */
38
    var ACTIONS = {
39
        MARK_FOR_DELETION: '[data-action="markfordeletion"]',
40
        SELECT_ALL: '[data-action="selectall"]',
41
    };
42
 
43
    /**
44
     * List of selectors.
45
     *
46
     * @type {{SELECTCONTEXT: string}}
47
     */
48
    var SELECTORS = {
49
        SELECTCONTEXT: '.selectcontext',
50
    };
51
 
52
    /**
53
     * DataDeletionActions class.
54
     */
55
    var DataDeletionActions = function() {
56
        this.registerEvents();
57
    };
58
 
59
    /**
60
     * Register event listeners.
61
     */
62
    DataDeletionActions.prototype.registerEvents = function() {
63
        $(ACTIONS.MARK_FOR_DELETION).click(function(e) {
64
            e.preventDefault();
65
 
66
            var selectedIds = [];
67
            $(SELECTORS.SELECTCONTEXT).each(function() {
68
                var checkbox = $(this);
69
                if (checkbox.is(':checked')) {
70
                    selectedIds.push(checkbox.val());
71
                }
72
            });
73
            showConfirmation(selectedIds);
74
        });
75
 
76
        $(ACTIONS.SELECT_ALL).change(function(e) {
77
            e.preventDefault();
78
 
79
            var selectallnone = $(this);
80
            if (selectallnone.is(':checked')) {
81
                $(SELECTORS.SELECTCONTEXT).attr('checked', 'checked');
82
            } else {
83
                $(SELECTORS.SELECTCONTEXT).removeAttr('checked');
84
            }
85
        });
86
    };
87
 
88
    /**
89
     * Show the confirmation dialogue.
90
     *
91
     * @param {Array} ids The array of expired context record IDs.
92
     */
93
    function showConfirmation(ids) {
94
        var keys = [
95
            {
96
                key: 'confirm',
97
                component: 'moodle'
98
            },
99
            {
100
                key: 'confirmcontextdeletion',
101
                component: 'tool_dataprivacy'
102
            }
103
        ];
104
        var wsfunction = 'tool_dataprivacy_confirm_contexts_for_deletion';
105
 
106
        var modalTitle = '';
107
        Str.get_strings(keys).then(function(langStrings) {
108
            modalTitle = langStrings[0];
109
            var confirmMessage = langStrings[1];
110
            return ModalSaveCancel.create({
111
                title: modalTitle,
112
                body: confirmMessage,
113
            });
114
        }).then(function(modal) {
115
            modal.setSaveButtonText(modalTitle);
116
 
117
            // Handle save event.
118
            modal.getRoot().on(ModalEvents.save, function() {
119
                // Confirm the request.
120
                var params = {
121
                    'ids': ids
122
                };
123
 
124
                var request = {
125
                    methodname: wsfunction,
126
                    args: params
127
                };
128
 
129
                Ajax.call([request])[0].done(function(data) {
130
                    if (data.result) {
131
                        window.location.reload();
132
                    } else {
133
                        Notification.addNotification({
134
                            message: data.warnings[0].message,
135
                            type: 'error'
136
                        });
137
                    }
138
                }).fail(Notification.exception);
139
            });
140
 
141
            // Handle hidden event.
142
            modal.getRoot().on(ModalEvents.hidden, function() {
143
                // Destroy when hidden.
144
                modal.destroy();
145
            });
146
 
147
            return modal;
148
        }).done(function(modal) {
149
            modal.show();
150
        }).fail(Notification.exception);
151
    }
152
 
153
    return DataDeletionActions;
154
});