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 data registry defaults actions.
18
 *
19
 * @module     tool_dataprivacy/defaultsactions
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
function($, Ajax, Notification, Str, ModalSaveCancel, ModalEvents, Templates) {
32
 
33
    /**
34
     * List of action selectors.
35
     *
36
     * @type {{EDIT_LEVEL_DEFAULTS: string}}
37
     * @type {{NEW_ACTIVITY_DEFAULTS: string}}
38
     * @type {{EDIT_ACTIVITY_DEFAULTS: string}}
39
     * @type {{DELETE_ACTIVITY_DEFAULTS: string}}
40
     */
41
    var ACTIONS = {
42
        EDIT_LEVEL_DEFAULTS: '[data-action="edit-level-defaults"]',
43
        NEW_ACTIVITY_DEFAULTS: '[data-action="new-activity-defaults"]',
44
        EDIT_ACTIVITY_DEFAULTS: '[data-action="edit-activity-defaults"]',
45
        DELETE_ACTIVITY_DEFAULTS: '[data-action="delete-activity-defaults"]'
46
    };
47
 
48
    /** @type {{INHERIT: Number}} **/
49
    var INHERIT = -1;
50
 
51
    /**
52
     * DefaultsActions class.
53
     */
54
    var DefaultsActions = function() {
55
        this.registerEvents();
56
    };
57
 
58
    /**
59
     * Register event listeners.
60
     */
61
    DefaultsActions.prototype.registerEvents = function() {
62
        $(ACTIONS.EDIT_LEVEL_DEFAULTS).click(function(e) {
63
            e.preventDefault();
64
 
65
            var button = $(this);
66
            var contextLevel = button.data('contextlevel');
67
            var category = button.data('category');
68
            var purpose = button.data('purpose');
69
 
70
            // Get options.
71
            var requests = [
72
                {methodname: 'tool_dataprivacy_get_category_options', args: {}},
73
                {methodname: 'tool_dataprivacy_get_purpose_options', args: {}}
74
            ];
75
 
76
            var promises = Ajax.call(requests);
77
            var titlePromise = Str.get_string('editdefaults', 'tool_dataprivacy', $('#defaults-header').text());
78
            $.when(promises[0], promises[1], titlePromise).then(function(categoryResponse, purposeResponse, title) {
79
                var categories = categoryResponse.options;
80
                var purposes = purposeResponse.options;
81
                showDefaultsFormModal(title, contextLevel, category, purpose, null, categories, purposes, null);
82
 
83
                return true;
84
            }).catch(Notification.exception);
85
        });
86
 
87
        $(ACTIONS.NEW_ACTIVITY_DEFAULTS).click(function(e) {
88
            e.preventDefault();
89
 
90
            var button = $(this);
91
            var contextLevel = button.data('contextlevel');
92
 
93
            // Get options.
94
            var requests = [
95
                {methodname: 'tool_dataprivacy_get_category_options', args: {}},
96
                {methodname: 'tool_dataprivacy_get_purpose_options', args: {}},
97
                {methodname: 'tool_dataprivacy_get_activity_options', args: {'nodefaults': true}}
98
            ];
99
 
100
            var promises = Ajax.call(requests);
101
            var titlePromise = Str.get_string('addnewdefaults', 'tool_dataprivacy');
102
 
103
            $.when(promises[0], promises[1], promises[2], titlePromise).then(
104
                function(categoryResponse, purposeResponse, activityResponse, title) {
105
                    var categories = categoryResponse.options;
106
                    var purposes = purposeResponse.options;
107
                    var activities = activityResponse.options;
108
 
109
                    showDefaultsFormModal(title, contextLevel, null, null, null, categories, purposes, activities);
110
 
111
                    return true;
112
 
113
                }).catch(Notification.exception);
114
            }
115
        );
116
 
117
        $(ACTIONS.EDIT_ACTIVITY_DEFAULTS).click(function(e) {
118
            e.preventDefault();
119
 
120
            var button = $(this);
121
            var contextLevel = button.data('contextlevel');
122
            var category = button.data('category');
123
            var purpose = button.data('purpose');
124
            var activity = button.data('activityname');
125
 
126
            // Get options.
127
            var requests = [
128
                {methodname: 'tool_dataprivacy_get_category_options', args: {}},
129
                {methodname: 'tool_dataprivacy_get_purpose_options', args: {}},
130
                {methodname: 'tool_dataprivacy_get_activity_options', args: {}}
131
            ];
132
 
133
            var promises = Ajax.call(requests);
134
            var titlePromise = Str.get_string('editmoduledefaults', 'tool_dataprivacy');
135
 
136
            $.when(promises[0], promises[1], promises[2], titlePromise).then(
137
                function(categoryResponse, purposeResponse, activityResponse, title) {
138
                    var categories = categoryResponse.options;
139
                    var purposes = purposeResponse.options;
140
                    var activities = activityResponse.options;
141
 
142
                    showDefaultsFormModal(title, contextLevel, category, purpose, activity, categories, purposes, activities);
143
 
144
                    return true;
145
 
146
                }).catch(Notification.exception);
147
            }
148
        );
149
 
150
        $(ACTIONS.DELETE_ACTIVITY_DEFAULTS).click(function(e) {
151
            e.preventDefault();
152
 
153
            var button = $(this);
154
            var contextLevel = button.data('contextlevel');
155
            var activity = button.data('activityname');
156
            var activityDisplayName = button.data('activitydisplayname');
157
            // Set category and purpose to inherit (-1).
158
            var category = INHERIT;
159
            var purpose = INHERIT;
160
 
161
            ModalSaveCancel.create({
162
                title: Str.get_string('deletedefaults', 'tool_dataprivacy', activityDisplayName),
163
                body: Templates.render('tool_dataprivacy/delete_activity_defaults', {"activityname": activityDisplayName}),
164
                large: true,
165
                show: true,
166
                removeOnClose: true,
167
            }).then(function(modal) {
168
                modal.setSaveButtonText(Str.get_string('delete'));
169
 
170
                // Handle save event.
171
                modal.getRoot().on(ModalEvents.save, function() {
172
                    setContextDefaults(contextLevel, category, purpose, activity, false);
173
                });
174
 
175
                return true;
176
            }).catch(Notification.exception);
177
        });
178
    };
179
 
180
    /**
181
     * Prepares and renders the modal for setting the defaults for the given context level/plugin.
182
     *
183
     * @param {String} title The modal's title.
184
     * @param {Number} contextLevel The context level to set defaults for.
185
     * @param {Number} category The current category ID.
186
     * @param {Number} purpose The current purpose ID.
187
     * @param {String} activity The plugin name of the activity. Optional.
188
     * @param {Array} categoryOptions The list of category options.
189
     * @param {Array} purposeOptions The list of purpose options.
190
     * @param {Array} activityOptions The list of activity options. Optional.
191
     */
192
    function showDefaultsFormModal(title, contextLevel, category, purpose, activity,
193
                                   categoryOptions, purposeOptions, activityOptions) {
194
 
195
        if (category !== null) {
196
            categoryOptions.forEach(function(currentValue) {
197
                if (currentValue.id === category) {
198
                    currentValue.selected = true;
199
                }
200
            });
201
        }
202
 
203
        if (purpose !== null) {
204
            purposeOptions.forEach(function(currentValue) {
205
                if (currentValue.id === purpose) {
206
                    currentValue.selected = true;
207
                }
208
            });
209
        }
210
 
211
        var templateContext = {
212
            "contextlevel": contextLevel,
213
            "categoryoptions": categoryOptions,
214
            "purposeoptions": purposeOptions
215
        };
216
 
217
        // Check the activityOptions parameter that was passed.
218
        if (activityOptions !== null && activityOptions.length) {
219
            // Check the activity parameter that was passed.
220
            if (activity === null) {
221
                // We're setting a new defaults for a module.
222
                templateContext.newactivitydefaults = true;
223
 
224
            } else {
225
                // Edit mode. Set selection.
226
                activityOptions.forEach(function(currentValue) {
227
                    if (activity === currentValue.name) {
228
                        currentValue.selected = true;
229
                    }
230
                });
231
            }
232
 
233
            templateContext.modemodule = true;
234
            templateContext.activityoptions = activityOptions;
235
        }
236
 
237
        ModalSaveCancel.create({
238
            title: title,
239
            body: Templates.render('tool_dataprivacy/category_purpose_form', templateContext),
240
            large: true,
241
            show: true,
242
            removeOnClose: true,
243
        }).then(function(modal) {
244
 
245
            // Handle save event.
246
            modal.getRoot().on(ModalEvents.save, function() {
247
                var activity = $('#activity');
248
                var activityVal = typeof activity !== 'undefined' ? activity.val() : null;
249
                var override = $('#override');
250
                var overrideVal = typeof override !== 'undefined' ? override.is(':checked') : false;
251
 
252
                setContextDefaults($('#contextlevel').val(), $('#category').val(), $('#purpose').val(), activityVal, overrideVal);
253
            });
254
 
255
            return modal;
256
        }).catch(Notification.exception);
257
    }
258
 
259
    /**
260
     * Calls a the tool_dataprivacy_set_context_defaults WS function.
261
     *
262
     * @param {Number} contextLevel The context level.
263
     * @param {Number} category The category ID.
264
     * @param {Number} purpose The purpose ID.
265
     * @param {String} activity The plugin name of the activity module.
266
     * @param {Boolean} override Whether to override custom instances.
267
     */
268
    function setContextDefaults(contextLevel, category, purpose, activity, override) {
269
        var request = {
270
            methodname: 'tool_dataprivacy_set_context_defaults',
271
            args: {
272
                'contextlevel': contextLevel,
273
                'category': category,
274
                'purpose': purpose,
275
                'override': override,
276
                'activity': activity
277
            }
278
        };
279
 
280
        Ajax.call([request])[0].done(function(data) {
281
            if (data.result) {
282
                window.location.reload();
283
            }
284
        });
285
    }
286
 
287
    return /** @alias module:tool_dataprivacy/defaultsactions */ {
288
        // Public variables and functions.
289
 
290
        /**
291
         * Initialise the module.
292
         *
293
         * @method init
294
         * @return {DefaultsActions}
295
         */
296
        'init': function() {
297
            return new DefaultsActions();
298
        }
299
    };
300
});