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/requestactions
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
    'core/templates',
31
    'tool_dataprivacy/data_request_modal',
32
    'tool_dataprivacy/events',
33
    'tool_dataprivacy/selectedcourses'
34
], function(
35
    $, Ajax, Notification, Str, ModalSaveCancel, ModalEvents, Templates, ModalDataRequest, DataPrivacyEvents, SelectedCourses
36
) {
37
 
38
    /**
39
     * List of action selectors.
40
     *
41
     * @type {{APPROVE_REQUEST: string}}
42
     * @type {{DENY_REQUEST: string}}
43
     * @type {{VIEW_REQUEST: string}}
44
     * @type {{MARK_COMPLETE: string}}
45
     * @type {{CHANGE_BULK_ACTION: string}}
46
     * @type {{CONFIRM_BULK_ACTION: string}}
47
     * @type {{SELECT_ALL: string}}
48
     */
49
    var ACTIONS = {
50
        APPROVE_REQUEST: '[data-action="approve"]',
51
        DENY_REQUEST: '[data-action="deny"]',
52
        VIEW_REQUEST: '[data-action="view"]',
53
        MARK_COMPLETE: '[data-action="complete"]',
54
        CHANGE_BULK_ACTION: '[id="bulk-action"]',
55
        CONFIRM_BULK_ACTION: '[id="confirm-bulk-action"]',
56
        SELECT_ALL: '[data-action="selectall"]',
57
        APPROVE_REQUEST_SELECT_COURSE: '[data-action="approve-selected-courses"]',
58
    };
59
 
60
    /**
61
     * List of available bulk actions.
62
     *
63
     * @type {{APPROVE: number}}
64
     * @type {{DENY: number}}
65
     */
66
    var BULK_ACTIONS = {
67
        APPROVE: 1,
68
        DENY: 2
69
    };
70
 
71
    /**
72
     * List of selectors.
73
     *
74
     * @type {{SELECT_REQUEST: string}}
75
     */
76
    var SELECTORS = {
77
        SELECT_REQUEST: '.selectrequests'
78
    };
79
 
80
    /**
81
     * RequestActions class.
82
     */
83
    var RequestActions = function() {
84
        this.registerEvents();
85
    };
86
 
87
    /**
88
     * Register event listeners.
89
     */
90
    RequestActions.prototype.registerEvents = function() {
91
        $(ACTIONS.VIEW_REQUEST).click(function(e) {
92
            e.preventDefault();
93
 
94
            var requestId = $(this).data('requestid');
95
            var contextId = $(this).data('contextid');
96
 
97
            // Cancel the request.
98
            var params = {
99
                'requestid': requestId
100
            };
101
 
102
            var request = {
103
                methodname: 'tool_dataprivacy_get_data_request',
104
                args: params
105
            };
106
 
107
            var promises = Ajax.call([request]);
108
            $.when(promises[0]).then(function(data) {
109
                if (data.result) {
110
                    return data.result;
111
                }
112
                // Fail.
113
                Notification.addNotification({
114
                    message: data.warnings[0].message,
115
                    type: 'error'
116
                });
117
                return false;
118
 
119
            }).then(function(data) {
120
                var body = Templates.render('tool_dataprivacy/request_details', data);
121
                var templateContext = {
122
                    approvedeny: data.approvedeny,
123
                    canmarkcomplete: data.canmarkcomplete,
124
                    allowfiltering: data.allowfiltering
125
                };
126
                return ModalDataRequest.create({
127
                    title: data.typename,
128
                    body: body,
129
                    large: true,
130
                    templateContext: templateContext
131
                });
132
 
133
            }).then(function(modal) {
134
                // Handle approve event.
135
                modal.getRoot().on(DataPrivacyEvents.approve, function() {
136
                    showConfirmation(DataPrivacyEvents.approve, approveEventWsData(requestId));
137
                });
138
 
139
                // Handle deny event.
140
                modal.getRoot().on(DataPrivacyEvents.deny, function() {
141
                    showConfirmation(DataPrivacyEvents.deny, denyEventWsData(requestId));
142
                });
143
 
144
                // Handle send event.
145
                modal.getRoot().on(DataPrivacyEvents.complete, function() {
146
                    var params = {
147
                        'requestid': requestId
148
                    };
149
                    handleSave('tool_dataprivacy_mark_complete', params);
150
                });
151
 
152
                // Handle hidden event.
153
                modal.getRoot().on(ModalEvents.hidden, function() {
154
                    // Destroy when hidden.
155
                    modal.destroy();
156
                });
157
 
158
                modal.getRoot().on(DataPrivacyEvents.approveSelectCourses, function() {
159
                    new SelectedCourses(contextId, requestId);
160
                });
161
 
162
                // Show the modal!
163
                modal.show();
164
 
165
                return;
166
 
167
            }).catch(Notification.exception);
168
        });
169
 
170
        $(ACTIONS.APPROVE_REQUEST_SELECT_COURSE).click(function(e) {
171
            e.preventDefault();
172
 
173
            var requestId = $(this).data('requestid');
174
            var contextId = $(this).data('contextid');
175
 
176
            new SelectedCourses(contextId, requestId);
177
        });
178
 
179
        $(ACTIONS.APPROVE_REQUEST).click(function(e) {
180
            e.preventDefault();
181
 
182
            var requestId = $(this).data('requestid');
183
            showConfirmation(DataPrivacyEvents.approve, approveEventWsData(requestId));
184
        });
185
 
186
        $(ACTIONS.DENY_REQUEST).click(function(e) {
187
            e.preventDefault();
188
 
189
            var requestId = $(this).data('requestid');
190
            showConfirmation(DataPrivacyEvents.deny, denyEventWsData(requestId));
191
        });
192
 
193
        $(ACTIONS.MARK_COMPLETE).click(function(e) {
194
            e.preventDefault();
195
 
196
            var requestId = $(this).data('requestid');
197
            showConfirmation(DataPrivacyEvents.complete, completeEventWsData(requestId));
198
        });
199
 
200
        $(ACTIONS.CONFIRM_BULK_ACTION).click(function() {
201
            var requestIds = [];
202
            var actionEvent = '';
203
            var wsdata = {};
204
            var bulkActionKeys = [
205
                {
206
                    key: 'selectbulkaction',
207
                    component: 'tool_dataprivacy'
208
                },
209
                {
210
                    key: 'selectdatarequests',
211
                    component: 'tool_dataprivacy'
212
                },
213
                {
214
                    key: 'ok'
215
                }
216
            ];
217
 
218
            var bulkaction = parseInt($('#bulk-action').val());
219
 
220
            if (bulkaction != BULK_ACTIONS.APPROVE && bulkaction != BULK_ACTIONS.DENY) {
221
                Str.get_strings(bulkActionKeys).done(function(langStrings) {
222
                    Notification.alert('', langStrings[0], langStrings[2]);
223
                }).fail(Notification.exception);
224
 
225
                return;
226
            }
227
 
228
            $(".selectrequests:checked").each(function() {
229
                requestIds.push($(this).val());
230
            });
231
 
232
            if (requestIds.length < 1) {
233
                Str.get_strings(bulkActionKeys).done(function(langStrings) {
234
                    Notification.alert('', langStrings[1], langStrings[2]);
235
                }).fail(Notification.exception);
236
 
237
                return;
238
            }
239
 
240
            switch (bulkaction) {
241
                case BULK_ACTIONS.APPROVE:
242
                    actionEvent = DataPrivacyEvents.bulkApprove;
243
                    wsdata = bulkApproveEventWsData(requestIds);
244
                    break;
245
                case BULK_ACTIONS.DENY:
246
                    actionEvent = DataPrivacyEvents.bulkDeny;
247
                    wsdata = bulkDenyEventWsData(requestIds);
248
            }
249
 
250
            showConfirmation(actionEvent, wsdata);
251
        });
252
 
253
        $(ACTIONS.SELECT_ALL).change(function(e) {
254
            e.preventDefault();
255
 
256
            var selectAll = $(this).is(':checked');
257
            $(SELECTORS.SELECT_REQUEST).prop('checked', selectAll);
258
        });
259
    };
260
 
261
    /**
262
     * Return the webservice data for the approve request action.
263
     *
264
     * @param {Number} requestId The ID of the request.
265
     * @return {Object}
266
     */
267
    function approveEventWsData(requestId) {
268
        return {
269
            'wsfunction': 'tool_dataprivacy_approve_data_request',
270
            'wsparams': {'requestid': requestId}
271
        };
272
    }
273
 
274
    /**
275
     * Return the webservice data for the bulk approve request action.
276
     *
277
     * @param {Array} requestIds The array of request ID's.
278
     * @return {Object}
279
     */
280
    function bulkApproveEventWsData(requestIds) {
281
        return {
282
            'wsfunction': 'tool_dataprivacy_bulk_approve_data_requests',
283
            'wsparams': {'requestids': requestIds}
284
        };
285
    }
286
 
287
    /**
288
     * Return the webservice data for the deny request action.
289
     *
290
     * @param {Number} requestId The ID of the request.
291
     * @return {Object}
292
     */
293
    function denyEventWsData(requestId) {
294
        return {
295
            'wsfunction': 'tool_dataprivacy_deny_data_request',
296
            'wsparams': {'requestid': requestId}
297
        };
298
    }
299
 
300
    /**
301
     * Return the webservice data for the bulk deny request action.
302
     *
303
     * @param {Array} requestIds The array of request ID's.
304
     * @return {Object}
305
     */
306
    function bulkDenyEventWsData(requestIds) {
307
        return {
308
            'wsfunction': 'tool_dataprivacy_bulk_deny_data_requests',
309
            'wsparams': {'requestids': requestIds}
310
        };
311
    }
312
 
313
    /**
314
     * Return the webservice data for the complete request action.
315
     *
316
     * @param {Number} requestId The ID of the request.
317
     * @return {Object}
318
     */
319
    function completeEventWsData(requestId) {
320
        return {
321
            'wsfunction': 'tool_dataprivacy_mark_complete',
322
            'wsparams': {'requestid': requestId}
323
        };
324
    }
325
 
326
    /**
327
     * Show the confirmation dialogue.
328
     *
329
     * @param {String} action The action name.
330
     * @param {Object} wsdata Object containing ws data.
331
     */
332
    function showConfirmation(action, wsdata) {
333
        var keys = [];
334
 
335
        switch (action) {
336
            case DataPrivacyEvents.approve:
337
                keys = [
338
                    {
339
                        key: 'approverequest',
340
                        component: 'tool_dataprivacy'
341
                    },
342
                    {
343
                        key: 'confirmapproval',
344
                        component: 'tool_dataprivacy'
345
                    }
346
                ];
347
                break;
348
            case DataPrivacyEvents.bulkApprove:
349
                keys = [
350
                    {
351
                        key: 'bulkapproverequests',
352
                        component: 'tool_dataprivacy'
353
                    },
354
                    {
355
                        key: 'confirmbulkapproval',
356
                        component: 'tool_dataprivacy'
357
                    }
358
                ];
359
                break;
360
            case DataPrivacyEvents.deny:
361
                keys = [
362
                    {
363
                        key: 'denyrequest',
364
                        component: 'tool_dataprivacy'
365
                    },
366
                    {
367
                        key: 'confirmdenial',
368
                        component: 'tool_dataprivacy'
369
                    }
370
                ];
371
                break;
372
            case DataPrivacyEvents.bulkDeny:
373
                keys = [
374
                    {
375
                        key: 'bulkdenyrequests',
376
                        component: 'tool_dataprivacy'
377
                    },
378
                    {
379
                        key: 'confirmbulkdenial',
380
                        component: 'tool_dataprivacy'
381
                    }
382
                ];
383
                break;
384
            case DataPrivacyEvents.complete:
385
                keys = [
386
                    {
387
                        key: 'markcomplete',
388
                        component: 'tool_dataprivacy'
389
                    },
390
                    {
391
                        key: 'confirmcompletion',
392
                        component: 'tool_dataprivacy'
393
                    }
394
                ];
395
                break;
396
        }
397
 
398
        var modalTitle = '';
399
        Str.get_strings(keys).then(function(langStrings) {
400
            modalTitle = langStrings[0];
401
            var confirmMessage = langStrings[1];
402
            return ModalSaveCancel.create({
403
                title: modalTitle,
404
                body: confirmMessage,
405
            });
406
        }).then(function(modal) {
407
            modal.setSaveButtonText(modalTitle);
408
 
409
            // Handle save event.
410
            modal.getRoot().on(ModalEvents.save, function() {
411
                handleSave(wsdata.wsfunction, wsdata.wsparams);
412
            });
413
 
414
            // Handle hidden event.
415
            modal.getRoot().on(ModalEvents.hidden, function() {
416
                // Destroy when hidden.
417
                modal.destroy();
418
            });
419
 
420
            modal.show();
421
 
422
            return;
423
 
424
        }).catch(Notification.exception);
425
    }
426
 
427
    /**
428
     * Calls a web service function and reloads the page on success and shows a notification.
429
     * Displays an error notification, otherwise.
430
     *
431
     * @param {String} wsfunction The web service function to call.
432
     * @param {Object} params The parameters for the web service functoon.
433
     */
434
    function handleSave(wsfunction, params) {
435
        // Confirm the request.
436
        var request = {
437
            methodname: wsfunction,
438
            args: params
439
        };
440
 
441
        Ajax.call([request])[0].done(function(data) {
442
            if (data.result) {
443
                // On success, reload the page so that the data request table will be updated.
444
                // TODO: Probably in the future, better to reload the table or the target data request via AJAX.
445
                window.location.reload();
446
            } else {
447
                // Add the notification.
448
                Notification.addNotification({
449
                    message: data.warnings[0].message,
450
                    type: 'error'
451
                });
452
            }
453
        }).fail(Notification.exception);
454
    }
455
 
456
    return RequestActions;
457
});