Help adding Privacy API to local plugin

Help adding Privacy API to local plugin

by Mike Churchward -
Number of replies: 4
Picture of Core developers Picture of Plugin developers Picture of Testers
Hi -

I'm trying to implement the privacy API into my local plugin. The local plugin allows for extra data to be defined and assigned to a specific user. I have the definition working fine, and I have created an export function that I can see is executed, but none of the data ends up in the export that I can find.

I believe the problem is that the context I want to associate with in export_user_data is the user context. Perhaps that is wrong? Should I be writing to the system context? Something like:

$context = \context_system::instance();
\core_privacy\local\request\writer::with_context($context)->export_data([], $exportdata);

mike

Average of ratings: -
In reply to Mike Churchward

Re: Help adding Privacy API to local plugin

by Dorel Manolescu -
Picture of Plugin developers

Hi 

Using https://docs.moodle.org/dev/Privacy_API/Utilities : Test of exporting user data

Did you do a var_dump of exportdata just to see if you have records to be exported?

Another way of debugging it is:

if (!empty($whatever)) {
\core_privacy\local\request\writer::with_context($context)
->export_data($subcontext, (object) [
'yourstringidentifier' => $data,
]);
}
and after that grep the moodledata export folder by  'yourstringidentifier'


Regards

In reply to Dorel Manolescu

Re: Help adding Privacy API to local plugin

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Yes. There is data. But I don't think using the user context for $context may be wrong? Maybe I should be using the system context?

When I use the system context as the '$context', I see the data at the top level in the 'data.json' file. But it seems to overwrite what was there before.

Before my plugin, the data.json had:

{
"lti_tool_proxies": []
}
After my plugin, the data.json has:

{
"metadata": [
{
"name": "I accept the site policy",
"description": "",
"data": "1"
},
{
"name": "Highschool name",
"description": "",
"data": "Sky High"
}
]
}
I just noticed you used '$subcontext'. What is that?

mike

In reply to Mike Churchward

Re: Help adding Privacy API to local plugin

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Ah! I see. If I add a subcontext like ['usermetadata'] to the export_data function, and use the system context, it creates a new folder at the top level of the export called 'usermetadata' and the exported data is contained with the data.json file in that folder. That seems like what I want. I'll try that at the user context.

...

There we go! If I add that subcontext to the user's context then the data shows up in the correct user's folder. Perfect!

mike

In reply to Mike Churchward

Re: Help adding Privacy API to local plugin

by Dorel Manolescu -
Picture of Plugin developers

Hi Mike,

I used user context for same type of plugin : local with user metadata. 

Sub-context is more like informal (i noticed it is used in the same way in a couple of core Moodle plugins):

if (!$context instanceof \context_user) {
return;
}

$subcontext[] = get_string('pluginname', 'local_yourplugin');
It helps to create a sub-folder  with the name of your plugin where the json information is stored for that particular user.

Regards