Skip to content

Commit

Permalink
MDL-62799 tool_dataprivacy: Allow manual completion of general enquiries
Browse files Browse the repository at this point in the history
Patch originally from MDL-62026.
  • Loading branch information
junpataleta committed Aug 13, 2018
1 parent aaa58a1 commit b6168a9
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 87 deletions.
2 changes: 1 addition & 1 deletion admin/tool/dataprivacy/amd/build/data_request_modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/tool/dataprivacy/amd/build/events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/tool/dataprivacy/amd/build/requestactions.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions admin/tool/dataprivacy/amd/src/data_request_modal.js
Expand Up @@ -29,6 +29,7 @@ define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/m
var SELECTORS = {
APPROVE_BUTTON: '[data-action="approve"]',
DENY_BUTTON: '[data-action="deny"]',
COMPLETE_BUTTON: '[data-action="complete"]'
};

/**
Expand All @@ -38,14 +39,6 @@ define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/m
*/
var ModalDataRequest = function(root) {
Modal.call(this, root);

if (!this.getFooter().find(SELECTORS.APPROVE_BUTTON).length) {
Notification.exception({message: 'No approve button found'});
}

if (!this.getFooter().find(SELECTORS.DENY_BUTTON).length) {
Notification.exception({message: 'No deny button found'});
}
};

ModalDataRequest.TYPE = 'tool_dataprivacy-data_request';
Expand Down Expand Up @@ -80,6 +73,16 @@ define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/m
data.originalEvent.preventDefault();
}
}.bind(this));

this.getModal().on(CustomEvents.events.activate, SELECTORS.COMPLETE_BUTTON, function(e, data) {
var completeEvent = $.Event(DataPrivacyEvents.complete);
this.getRoot().trigger(completeEvent, this);

if (!completeEvent.isDefaultPrevented()) {
this.hide();
data.originalEvent.preventDefault();
}
}.bind(this));
};

// Automatically register with the modal registry the first time this module is imported so that you can create modals
Expand Down
1 change: 1 addition & 0 deletions admin/tool/dataprivacy/amd/src/events.js
Expand Up @@ -26,5 +26,6 @@ define([], function() {
return {
approve: 'tool_dataprivacy-data_request:approve',
deny: 'tool_dataprivacy-data_request:deny',
complete: 'tool_dataprivacy-data_request:complete'
};
});
155 changes: 99 additions & 56 deletions admin/tool/dataprivacy/amd/src/requestactions.js
Expand Up @@ -39,11 +39,13 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
* @type {{APPROVE_REQUEST: string}}
* @type {{DENY_REQUEST: string}}
* @type {{VIEW_REQUEST: string}}
* @type {{MARK_COMPLETE: string}}
*/
var ACTIONS = {
APPROVE_REQUEST: '[data-action="approve"]',
DENY_REQUEST: '[data-action="deny"]',
VIEW_REQUEST: '[data-action="view"]'
VIEW_REQUEST: '[data-action="view"]',
MARK_COMPLETE: '[data-action="complete"]'
};

/**
Expand Down Expand Up @@ -73,16 +75,9 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
};

var promises = Ajax.call([request]);
var modalTitle = '';
var modalType = ModalFactory.types.DEFAULT;
$.when(promises[0]).then(function(data) {
if (data.result) {
// Check if the status is awaiting approval.
if (data.result.status == 2) {
modalType = ModalDataRequest.TYPE;
}
modalTitle = data.result.typename;
return Templates.render('tool_dataprivacy/request_details', data.result);
return data.result;
}
// Fail.
Notification.addNotification({
Expand All @@ -91,35 +86,51 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
});
return false;

}).then(function(html) {
}).then(function(data) {
var body = Templates.render('tool_dataprivacy/request_details', data);
var templateContext = {
approvedeny: data.approvedeny,
canmarkcomplete: data.canmarkcomplete
};
return ModalFactory.create({
title: modalTitle,
body: html,
type: modalType,
large: true
}).then(function(modal) {
// Handle approve event.
modal.getRoot().on(DataPrivacyEvents.approve, function() {
showConfirmation(DataPrivacyEvents.approve, requestId);
});

// Handle deny event.
modal.getRoot().on(DataPrivacyEvents.deny, function() {
showConfirmation(DataPrivacyEvents.deny, requestId);
});

// Handle hidden event.
modal.getRoot().on(ModalEvents.hidden, function() {
// Destroy when hidden.
modal.destroy();
});

return modal;
title: data.typename,
body: body,
type: ModalDataRequest.TYPE,
large: true,
templateContext: templateContext
});

}).then(function(modal) {
// Handle approve event.
modal.getRoot().on(DataPrivacyEvents.approve, function() {
showConfirmation(DataPrivacyEvents.approve, requestId);
});

// Handle deny event.
modal.getRoot().on(DataPrivacyEvents.deny, function() {
showConfirmation(DataPrivacyEvents.deny, requestId);
});
}).done(function(modal) {

// Handle send event.
modal.getRoot().on(DataPrivacyEvents.complete, function() {
var params = {
'requestid': requestId
};
handleSave('tool_dataprivacy_mark_complete', params);
});

// Handle hidden event.
modal.getRoot().on(ModalEvents.hidden, function() {
// Destroy when hidden.
modal.destroy();
});

// Show the modal!
modal.show();
}).fail(Notification.exception);

return;

}).catch(Notification.exception);
});

$(ACTIONS.APPROVE_REQUEST).click(function(e) {
Expand All @@ -135,6 +146,11 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
var requestId = $(this).data('requestid');
showConfirmation(DataPrivacyEvents.deny, requestId);
});

$(ACTIONS.MARK_COMPLETE).click(function(e) {
e.preventDefault();
showConfirmation(DataPrivacyEvents.complete, $(this).data('requestid'));
});
};

/**
Expand All @@ -146,6 +162,9 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
function showConfirmation(action, requestId) {
var keys = [];
var wsfunction = '';
var params = {
'requestid': requestId
};
switch (action) {
case DataPrivacyEvents.approve:
keys = [
Expand Down Expand Up @@ -173,6 +192,19 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
];
wsfunction = 'tool_dataprivacy_deny_data_request';
break;
case DataPrivacyEvents.complete:
keys = [
{
key: 'markcomplete',
component: 'tool_dataprivacy'
},
{
key: 'confirmcompletion',
component: 'tool_dataprivacy'
}
];
wsfunction = 'tool_dataprivacy_mark_complete';
break;
}

var modalTitle = '';
Expand All @@ -189,26 +221,7 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal

// Handle save event.
modal.getRoot().on(ModalEvents.save, function() {
// Confirm the request.
var params = {
'requestid': requestId
};

var request = {
methodname: wsfunction,
args: params
};

Ajax.call([request])[0].done(function(data) {
if (data.result) {
window.location.reload();
} else {
Notification.addNotification({
message: data.warnings[0].message,
type: 'error'
});
}
}).fail(Notification.exception);
handleSave(wsfunction, params);
});

// Handle hidden event.
Expand All @@ -217,9 +230,39 @@ function($, Ajax, Notification, Str, ModalFactory, ModalEvents, Templates, Modal
modal.destroy();
});

return modal;
}).done(function(modal) {
modal.show();

return;

}).catch(Notification.exception);
}

/**
* Calls a web service function and reloads the page on success and shows a notification.
* Displays an error notification, otherwise.
*
* @param {String} wsfunction The web service function to call.
* @param {Object} params The parameters for the web service functoon.
*/
function handleSave(wsfunction, params) {
// Confirm the request.
var request = {
methodname: wsfunction,
args: params
};

Ajax.call([request])[0].done(function(data) {
if (data.result) {
// On success, reload the page so that the data request table will be updated.
// TODO: Probably in the future, better to reload the table or the target data request via AJAX.
window.location.reload();
} else {
// Add the notification.
Notification.addNotification({
message: data.warnings[0].message,
type: 'error'
});
}
}).fail(Notification.exception);
}

Expand Down
19 changes: 17 additions & 2 deletions admin/tool/dataprivacy/classes/api.php
Expand Up @@ -24,6 +24,7 @@
namespace tool_dataprivacy;

use coding_exception;
use context_course;
use context_system;
use core\invalid_persistent_exception;
use core\message\message;
Expand Down Expand Up @@ -426,7 +427,22 @@ public static function update_request_status($requestid, $status, $dpoid = 0, $c
if ($dpoid) {
$datarequest->set('dpo', $dpoid);
}
$datarequest->set('dpocomment', $comment);
// Update the comment if necessary.
if (!empty(trim($comment))) {
$params = [
'date' => userdate(time()),
'comment' => $comment
];
$commenttosave = get_string('datecomment', 'tool_dataprivacy', $params);
// Check if there's an existing DPO comment.
$currentcomment = trim($datarequest->get('dpocomment'));
if ($currentcomment) {
// Append the new comment to the current comment and give them 1 line space in between.
$commenttosave = $currentcomment . PHP_EOL . PHP_EOL . $commenttosave;
}
$datarequest->set('dpocomment', $commenttosave);
}

return $datarequest->update();
}

Expand Down Expand Up @@ -521,7 +537,6 @@ public static function deny_data_request($requestid) {
* @param data_request $request The data request
* @return int|false
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public static function notify_dpo($dpo, data_request $request) {
Expand Down

0 comments on commit b6168a9

Please sign in to comment.