Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge branch 'MDL-62554-34' of git://github.com/junpataleta/moodle in…
…to MOODLE_34_STABLE
- Loading branch information
Showing
with
2,528 additions
and 138 deletions.
- +1 −0 admin/tool/dataprivacy/amd/build/defaultsactions.min.js
- +315 −0 admin/tool/dataprivacy/amd/src/defaultsactions.js
- +88 −1 admin/tool/dataprivacy/classes/api.php
- +44 −17 admin/tool/dataprivacy/classes/data_registry.php
- +281 −0 admin/tool/dataprivacy/classes/external.php
- +23 −0 admin/tool/dataprivacy/classes/external/category_exporter.php
- +22 −0 admin/tool/dataprivacy/classes/external/purpose_exporter.php
- +0 −81 admin/tool/dataprivacy/classes/form/defaults.php
- +2 −2 admin/tool/dataprivacy/classes/output/data_registry_page.php
- +178 −0 admin/tool/dataprivacy/classes/output/defaults_page.php
- +40 −0 admin/tool/dataprivacy/db/services.php
- +27 −35 admin/tool/dataprivacy/defaults.php
- +8 −1 admin/tool/dataprivacy/lang/en/tool_dataprivacy.php
- +4 −0 admin/tool/dataprivacy/styles.css
- +99 −0 admin/tool/dataprivacy/templates/category_purpose_form.mustache
- +74 −0 admin/tool/dataprivacy/templates/defaults_display.mustache
- +147 −0 admin/tool/dataprivacy/templates/defaults_page.mustache
- +41 −0 admin/tool/dataprivacy/templates/delete_activity_defaults.mustache
- +158 −0 admin/tool/dataprivacy/tests/api_test.php
- +284 −0 admin/tool/dataprivacy/tests/behat/behat_tool_dataprivacy.php
- +298 −0 admin/tool/dataprivacy/tests/behat/manage_defaults.feature
- +278 −0 admin/tool/dataprivacy/tests/external_test.php
- +115 −0 admin/tool/dataprivacy/tests/generator/lib.php
- +1 −1 admin/tool/dataprivacy/version.php
@@ -0,0 +1,315 @@ | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* AMD module for data registry defaults actions. | ||
* | ||
* @module tool_dataprivacy/defaultsactions | ||
* @package tool_dataprivacy | ||
* @copyright 2018 Jun Pataleta | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
define([ | ||
'jquery', | ||
'core/ajax', | ||
'core/notification', | ||
'core/str', | ||
'core/modal_factory', | ||
'core/modal_events', | ||
'core/templates'], | ||
function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates) { | ||
|
||
/** | ||
* List of action selectors. | ||
* | ||
* @type {{EDIT_LEVEL_DEFAULTS: string}} | ||
* @type {{NEW_ACTIVITY_DEFAULTS: string}} | ||
* @type {{EDIT_ACTIVITY_DEFAULTS: string}} | ||
* @type {{DELETE_ACTIVITY_DEFAULTS: string}} | ||
*/ | ||
var ACTIONS = { | ||
EDIT_LEVEL_DEFAULTS: '[data-action="edit-level-defaults"]', | ||
NEW_ACTIVITY_DEFAULTS: '[data-action="new-activity-defaults"]', | ||
EDIT_ACTIVITY_DEFAULTS: '[data-action="edit-activity-defaults"]', | ||
DELETE_ACTIVITY_DEFAULTS: '[data-action="delete-activity-defaults"]' | ||
}; | ||
|
||
/** @type {{INHERIT: Number}} **/ | ||
var INHERIT = -1; | ||
|
||
/** | ||
* DefaultsActions class. | ||
*/ | ||
var DefaultsActions = function() { | ||
this.registerEvents(); | ||
}; | ||
|
||
/** | ||
* Register event listeners. | ||
*/ | ||
DefaultsActions.prototype.registerEvents = function() { | ||
$(ACTIONS.EDIT_LEVEL_DEFAULTS).click(function(e) { | ||
e.preventDefault(); | ||
|
||
var button = $(this); | ||
var contextLevel = button.data('contextlevel'); | ||
var category = button.data('category'); | ||
var purpose = button.data('purpose'); | ||
|
||
// Get options. | ||
var requests = [ | ||
{methodname: 'tool_dataprivacy_get_category_options', args: {}}, | ||
{methodname: 'tool_dataprivacy_get_purpose_options', args: {}} | ||
]; | ||
|
||
var promises = Ajax.call(requests); | ||
var titlePromise = Str.get_string('editdefaults', 'tool_dataprivacy', $('#defaults-header').text()); | ||
$.when(promises[0], promises[1], titlePromise).then(function(categoryResponse, purposeResponse, title) { | ||
var categories = categoryResponse.options; | ||
var purposes = purposeResponse.options; | ||
showDefaultsFormModal(title, contextLevel, category, purpose, null, categories, purposes, null); | ||
|
||
return true; | ||
}).catch(Notification.exception); | ||
}); | ||
|
||
$(ACTIONS.NEW_ACTIVITY_DEFAULTS).click(function(e) { | ||
e.preventDefault(); | ||
|
||
var button = $(this); | ||
var contextLevel = button.data('contextlevel'); | ||
|
||
// Get options. | ||
var requests = [ | ||
{methodname: 'tool_dataprivacy_get_category_options', args: {}}, | ||
{methodname: 'tool_dataprivacy_get_purpose_options', args: {}}, | ||
{methodname: 'tool_dataprivacy_get_activity_options', args: {'nodefaults': true}} | ||
]; | ||
|
||
var promises = Ajax.call(requests); | ||
var titlePromise = Str.get_string('addnewdefaults', 'tool_dataprivacy'); | ||
|
||
$.when(promises[0], promises[1], promises[2], titlePromise).then( | ||
function(categoryResponse, purposeResponse, activityResponse, title) { | ||
var categories = categoryResponse.options; | ||
var purposes = purposeResponse.options; | ||
var activities = activityResponse.options; | ||
|
||
showDefaultsFormModal(title, contextLevel, null, null, null, categories, purposes, activities); | ||
|
||
return true; | ||
|
||
}).catch(Notification.exception); | ||
} | ||
); | ||
|
||
$(ACTIONS.EDIT_ACTIVITY_DEFAULTS).click(function(e) { | ||
e.preventDefault(); | ||
|
||
var button = $(this); | ||
var contextLevel = button.data('contextlevel'); | ||
var category = button.data('category'); | ||
var purpose = button.data('purpose'); | ||
var activity = button.data('activityname'); | ||
|
||
// Get options. | ||
var requests = [ | ||
{methodname: 'tool_dataprivacy_get_category_options', args: {}}, | ||
{methodname: 'tool_dataprivacy_get_purpose_options', args: {}}, | ||
{methodname: 'tool_dataprivacy_get_activity_options', args: {}} | ||
]; | ||
|
||
var promises = Ajax.call(requests); | ||
var titlePromise = Str.get_string('editmoduledefaults', 'tool_dataprivacy'); | ||
|
||
$.when(promises[0], promises[1], promises[2], titlePromise).then( | ||
function(categoryResponse, purposeResponse, activityResponse, title) { | ||
var categories = categoryResponse.options; | ||
var purposes = purposeResponse.options; | ||
var activities = activityResponse.options; | ||
|
||
showDefaultsFormModal(title, contextLevel, category, purpose, activity, categories, purposes, activities); | ||
|
||
return true; | ||
|
||
}).catch(Notification.exception); | ||
} | ||
); | ||
|
||
$(ACTIONS.DELETE_ACTIVITY_DEFAULTS).click(function(e) { | ||
e.preventDefault(); | ||
|
||
var button = $(this); | ||
var contextLevel = button.data('contextlevel'); | ||
var activity = button.data('activityname'); | ||
var activityDisplayName = button.data('activitydisplayname'); | ||
// Set category and purpose to inherit (-1). | ||
var category = INHERIT; | ||
var purpose = INHERIT; | ||
|
||
ModalFactory.create({ | ||
title: Str.get_string('deletedefaults', 'tool_dataprivacy', activityDisplayName), | ||
body: Templates.render('tool_dataprivacy/delete_activity_defaults', {"activityname": activityDisplayName}), | ||
type: ModalFactory.types.SAVE_CANCEL, | ||
large: true | ||
}).then(function(modal) { | ||
modal.setSaveButtonText(Str.get_string('delete')); | ||
|
||
// Handle save event. | ||
modal.getRoot().on(ModalEvents.save, function() { | ||
setContextDefaults(contextLevel, category, purpose, activity, false); | ||
}); | ||
|
||
// Handle hidden event. | ||
modal.getRoot().on(ModalEvents.hidden, function() { | ||
// Destroy when hidden. | ||
modal.destroy(); | ||
}); | ||
|
||
modal.show(); | ||
|
||
return true; | ||
}).catch(Notification.exception); | ||
}); | ||
}; | ||
|
||
/** | ||
* Prepares and renders the modal for setting the defaults for the given context level/plugin. | ||
* | ||
* @param {String} title The modal's title. | ||
* @param {Number} contextLevel The context level to set defaults for. | ||
* @param {Number} category The current category ID. | ||
* @param {Number} purpose The current purpose ID. | ||
* @param {String} activity The plugin name of the activity. Optional. | ||
* @param {Array} categoryOptions The list of category options. | ||
* @param {Array} purposeOptions The list of purpose options. | ||
* @param {Array} activityOptions The list of activity options. Optional. | ||
*/ | ||
function showDefaultsFormModal(title, contextLevel, category, purpose, activity, | ||
categoryOptions, purposeOptions, activityOptions) { | ||
|
||
if (category !== null) { | ||
categoryOptions.forEach(function(currentValue) { | ||
if (currentValue.id === category) { | ||
currentValue.selected = true; | ||
} | ||
}); | ||
} | ||
|
||
if (purpose !== null) { | ||
purposeOptions.forEach(function(currentValue) { | ||
if (currentValue.id === purpose) { | ||
currentValue.selected = true; | ||
} | ||
}); | ||
} | ||
|
||
var templateContext = { | ||
"contextlevel": contextLevel, | ||
"categoryoptions": categoryOptions, | ||
"purposeoptions": purposeOptions | ||
}; | ||
|
||
// Check the activityOptions parameter that was passed. | ||
if (activityOptions !== null && activityOptions.length) { | ||
// Check the activity parameter that was passed. | ||
if (activity === null) { | ||
// We're setting a new defaults for a module. | ||
templateContext.newactivitydefaults = true; | ||
|
||
} else { | ||
// Edit mode. Set selection. | ||
activityOptions.forEach(function(currentValue) { | ||
if (activity === currentValue.name) { | ||
currentValue.selected = true; | ||
} | ||
}); | ||
} | ||
|
||
templateContext.modemodule = true; | ||
templateContext.activityoptions = activityOptions; | ||
} | ||
|
||
ModalFactory.create({ | ||
title: title, | ||
body: Templates.render('tool_dataprivacy/category_purpose_form', templateContext), | ||
type: ModalFactory.types.SAVE_CANCEL, | ||
large: true | ||
}).then(function(modal) { | ||
|
||
// Handle save event. | ||
modal.getRoot().on(ModalEvents.save, function() { | ||
var activity = $('#activity'); | ||
var activityVal = typeof activity !== 'undefined' ? activity.val() : null; | ||
var override = $('#override'); | ||
var overrideVal = typeof override !== 'undefined' ? override.is(':checked') : false; | ||
|
||
setContextDefaults($('#contextlevel').val(), $('#category').val(), $('#purpose').val(), activityVal, overrideVal); | ||
}); | ||
|
||
// Handle hidden event. | ||
modal.getRoot().on(ModalEvents.hidden, function() { | ||
// Destroy when hidden. | ||
modal.destroy(); | ||
}); | ||
|
||
modal.show(); | ||
|
||
return modal; | ||
}).catch(Notification.exception); | ||
} | ||
|
||
/** | ||
* Calls a the tool_dataprivacy_set_context_defaults WS function. | ||
* | ||
* @param {Number} contextLevel The context level. | ||
* @param {Number} category The category ID. | ||
* @param {Number} purpose The purpose ID. | ||
* @param {String} activity The plugin name of the activity module. | ||
* @param {Boolean} override Whether to override custom instances. | ||
*/ | ||
function setContextDefaults(contextLevel, category, purpose, activity, override) { | ||
var request = { | ||
methodname: 'tool_dataprivacy_set_context_defaults', | ||
args: { | ||
'contextlevel': contextLevel, | ||
'category': category, | ||
'purpose': purpose, | ||
'override': override, | ||
'activity': activity | ||
} | ||
}; | ||
|
||
Ajax.call([request])[0].done(function(data) { | ||
if (data.result) { | ||
window.location.reload(); | ||
} | ||
}); | ||
} | ||
|
||
return /** @alias module:tool_dataprivacy/defaultsactions */ { | ||
// Public variables and functions. | ||
|
||
/** | ||
* Initialise the module. | ||
* | ||
* @method init | ||
* @return {DefaultsActions} | ||
*/ | ||
'init': function() { | ||
return new DefaultsActions(); | ||
} | ||
}; | ||
}); |
Oops, something went wrong.