Skip to content

Commit 79e321c

Browse files
author
David Monllao
committed
Merge branch 'MDL-62554-master' of git://github.com/junpataleta/moodle
2 parents 771665a + 01f098a commit 79e321c

24 files changed

+2528
-138
lines changed

admin/tool/dataprivacy/amd/build/defaultsactions.min.js

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

0 commit comments

Comments
 (0)