It looks like I need to add each of these to the $collection variable. And, each table and relevant field will require a language string, as shown in the documentation example. To start with, I’ll implement just the questionnaire_attemptstable.

Adding this table to the get_metadata function means defining the relevant fields. In this case, this table stores the user id, the question id, the response id and the time stamp of when the latest submission for this attempt occurred. Each of these fields can be considered private data, although the question id points to the actual question which really only provides context for a specific question response. I’ll stay on the side of providing too much information rather than too little and include it. My function now looks like:

$string[‘privacy:metadata:questionnaire_attempts’] = ‘Details about each submission of a questionnaire by a user.’;
$string[‘privacy:metadata:questionnaire_attempts:userid’] = ‘The ID of the user for this attempt.’;
$string[‘privacy:metadata:questionnaire_attempts:rid’] = ‘The ID of the user\’s response record for this attempt.’;
$string[‘privacy:metadata:questionnaire_attempts:qid’] = ‘The ID of the questionnaire record for this attempt.’;
$string[‘privacy:metadata:questionnaire_attempts:timemodified’] = ‘The timestamp for the latest submission of this attempt.’;

Now that I have added the metadata, I should be able to see them at the “Plugin privacy registry” page of the site. Navigating to that page, and opening the section on questionnaire, I do indeed see the definitions I just added:

Next, I need to provide a way to retrieve and return the list of contexts for which my plugin stores user data. For my plugin, the only context is CONTEXT_MODULE. And I can determine the context module id for each questionnaire a user has responded to by the qid field in the questionnaire_attempts table and joining tables back through the course_modules table to the context table using SQL. My function looks like this:

Next, I need to provide a way to export user data. The documentation doesn’t provide an example, but I can find examples in the core code.

There are a number of data types that must be exported mentioned in the documentation, but questionnaire only needs to worry about the “data” part. The documentation section also describes using the \core_privacy\local\request\content_writer but the code examples in the documentation use \core_privacy\local\request\writer. Looking at the /privacy/classes/local/request/content_writer.php file, I can see that is an interface, while the /privacy/classes/local/request/writer.php is a class described as a “factory class used to fetch and work with the content_writer“. So I think the “writer” class has been provided as a shortcut.

Looking at the exporter code for choice and forum, it appears that there is no specific format for the output of a module. The data is structured as JSON, but the elements seem to be up to the plugin. This makes sense, since any plugin can have very different data.

Before I implement an exporter, I will need to decide what the data should look like. I’ll stick with my simple attempts data for now. Since any questionnaire instance can have multiple attempts by a user, it makes sense to create a structure organized by the instance; in this case the course module id. So my structure should look like this:

Looking at the choice activity code for the exporter, I create a function to create the JSON structure I am aiming for. You can see the code here. This code uses several functions provided by the API that are not documented in the wiki. The documentation is really in the class files themselves.

Now, to test this, Moodle has provided some scripts that can be created and executed from the CLI. The one I want to use is the “Test of exporting user data” script, provided on that page. So, I create that script on my test site, and execute it. When I execute it, there is a lot of output. Scanning through the output, I see: