MDL-61899 tool_dataprivacy: Implement data export for the tool

This commit is contained in:
Jun Pataleta 2018-04-13 12:17:13 +08:00 committed by Eloy Lafuente (stronk7)
parent c504ba4332
commit 2076d34b8e

View File

@ -25,10 +25,19 @@
namespace tool_dataprivacy\privacy;
defined('MOODLE_INTERNAL') || die();
use coding_exception;
use context;
use context_user;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\context;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\helper;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use dml_exception;
use stdClass;
use tool_dataprivacy\api;
use tool_dataprivacy\local\helper as tool_helper;
/**
* Privacy class for requesting user data.
@ -71,23 +80,79 @@ class provider implements
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
return new contextlist();
$sql = "SELECT id
FROM {context}
WHERE instanceid = :userid
AND contextlevel = :contextlevel";
$contextlist = new contextlist();
$contextlist->set_component('tool_dataprivacy');
$contextlist->add_from_sql($sql, ['userid' => $userid, 'contextlevel' => CONTEXT_USER]);
return $contextlist;
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
* @param approved_contextlist $contextlist The approved contexts to export information for.
* @throws coding_exception
* @throws dml_exception
* @throws \moodle_exception
*/
public static function export_user_data(approved_contextlist $contextlist) {
if (empty($contextlist->count())) {
return;
}
$user = $contextlist->get_user();
$datarequests = api::get_data_requests($user->id);
$context = context_user::instance($user->id);
$contextdatatowrite = [];
foreach ($datarequests as $request) {
$record = $request->to_record();
$data = new stdClass();
// The user ID that made the request/the request is made for.
if ($record->requestedby != $record->userid) {
if ($user->id != $record->requestedby) {
// This request is done by this user for another user.
$data->userid = fullname($user);
} else if ($user->id != $record->userid) {
// This request was done by another user on behalf of this user.
$data->requestedby = fullname($user);
}
}
// Request type.
$data->type = tool_helper::get_shortened_request_type_string($record->type);
// Status.
$data->status = tool_helper::get_request_status_string($record->status);
// Comments.
$data->comments = $record->comments;
// The DPO's comment about this request.
$data->dpocomment = $record->dpocomment;
// The date and time this request was lodged.
$data->timecreated = transform::datetime($record->timecreated);
$contextdatatowrite[] = $data;
}
// {User context} / Privacy and policies / Data requests.
$subcontext = [
get_string('privacyandpolicies', 'admin'),
get_string('datarequests', 'tool_dataprivacy'),
];
writer::with_context($context)->export_data($subcontext, (object)$contextdatatowrite);
// Write generic module intro files.
helper::export_context_files($context, $user);
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
public static function delete_data_for_all_users_in_context(context $context) {
}
/**