General developer forum

Help adding Privacy API to local plugin

 
Picture of Mike Churchward
Help adding Privacy API to local plugin
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters
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: -
Picture of Dorel Manolescu
Re: Help adding Privacy API to local plugin
 

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

 
Average of ratings: -
Picture of Mike Churchward
Re: Help adding Privacy API to local plugin
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

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

 
Average of ratings: -
Picture of Mike Churchward
Re: Help adding Privacy API to local plugin
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

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

 
Average of ratings: -
Picture of Dorel Manolescu
Re: Help adding Privacy API to local plugin
 

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

 
Average of ratings: -