Created
March 21, 2018 14:39
-
-
Save mhughes2k/ede8db9739b5faed55203902b536a3cb to your computer and use it in GitHub Desktop.
Moodle SAR developer script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A Script to help work with Moodle's Privacy API. | |
* @author Michael Hughes <michaelhughes@strath.ac.uk>, University of Strathclyde, | |
* Creates and SAR for specified user. | |
* Optionally can | |
* * unpack the resulting SAR to a specified location | |
* * add the SAR request to the cron queue (instead of processing it immediately). | |
*/ | |
define('CLI_SCRIPT', true); | |
require_once(__DIR__ . '/../../../../config.php'); | |
require_once($CFG->libdir.'/clilib.php'); | |
use tool_dataprivacy\api; | |
use tool_dataprivacy\data_request; | |
if (!debugging(null, DEBUG_DEVELOPER)) { | |
echo "This requires DEBUG_DEVELOPER to be set on."; | |
} | |
$workdir = null; | |
$help = "Requests a SAR for a user | |
Options: | |
--userid User ID | |
-q, --queue Queue the task to run in cron, otherwise run right now | |
-u, --unpack=DIR Unpack the resulting file to DIR. | |
-h, --help Display this help | |
"; | |
list($options, $unrecognized) = cli_get_params( | |
[ | |
'userid' => false, | |
'queue' => false, | |
'unpack' => false, | |
'help' => false | |
], | |
[ | |
'h' => 'help', | |
'u' => 'unpack' | |
] | |
); | |
if ($options['help']) { | |
echo $help; | |
exit(0); | |
} | |
if ($options['unpack']!== false && !is_string($options['unpack'])) { | |
cli_writeln("Unpack must be a valid path"); | |
exit(0); | |
} | |
$workdir = $options['unpack']; | |
if ($workdir!==false && !file_exists(realpath($workdir))) { | |
cli_writeln("{$workdir} doesn't exist"); | |
exit(0); | |
} | |
if($options['userid'] === false) { | |
echo 'userid is required'; | |
exit(0); | |
} | |
$queue = true; | |
if ($options['queue'] === false) { | |
$queue = false; | |
} | |
$userid = $options['userid']; | |
// Fix $OUTPUT not being initialised | |
$PAGE->get_renderer('core'); | |
$u = core_user::get_user($userid); | |
$datarequest = new data_request(); | |
// The user the request is being made for. | |
$datarequest->set('userid', $u->id); | |
// The user making the request. | |
$datarequest->set('requestedby', $u->id); | |
// Set status. | |
$datarequest->set('status', api::DATAREQUEST_STATUS_PENDING); | |
// Set request type. | |
$datarequest->set('type', api::DATAREQUEST_TYPE_EXPORT); | |
// Set request comments. | |
$datarequest->set('comments', 'Automated generation'); | |
// Store subject access request. | |
$datarequest->create(); | |
$rqid = $datarequest->get('id'); | |
//api::approve_data_request($rq->id); | |
$admin = get_admin(); | |
api::update_request_status($rqid, api::DATAREQUEST_STATUS_APPROVED, $admin->id); | |
$rq = api::get_request($rqid); | |
$task = new \tool_dataprivacy\task\process_data_request_task(); | |
$task->set_custom_data(['requestid' => $rqid]); | |
use core\task\manager; | |
if ($queue) { | |
cli_writeln("Queuing task to run in cron"); | |
manager::queue_adhoc_task($task, true); | |
} else { | |
cli_write("Executing task..."); | |
$result = $task->execute(); | |
cli_writeln("Done"); | |
if ($workdir !== false) { | |
cli_write("Unpacking to \"{$workdir}\"..."); | |
$usercontext = \context_user::instance($u->id); | |
$filerecord = new \stdClass; | |
$filerecord->component = 'tool_dataprivacy'; | |
$filerecord->contextid = $usercontext->id; | |
$filerecord->userid = $u->id; | |
$filerecord->filearea = 'export'; | |
$filerecord->filename = 'export.zip'; | |
$filerecord->filepath = '/'; | |
$filerecord->itemid = $rqid; | |
$filerecord->license = $CFG->sitedefaultlicense; | |
$filerecord->author = fullname($u); | |
// Save somewhere. | |
//$thing = $fs->create_file_from_pathname($filerecord, $exportedcontent); | |
$fs = get_file_storage(); | |
$f = $fs->get_file( | |
$filerecord->contextid, | |
$filerecord->component, | |
$filerecord->filearea, | |
$filerecord->itemid, | |
$filerecord->filepath, | |
$filerecord->filename | |
); | |
$packer = new zip_packer(); | |
$f->extract_to_pathname($packer, $workdir); | |
cli_writeln("Done"); | |
} | |
} | |
cli_writeln("Finished SAR process"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment