Trouble implementing delete_data_for_user

Trouble implementing delete_data_for_user

by Megan Bohland -
Number of replies: 3
Hi Everyone!

I am working on a plugin for Moodle and it was pointed out to me we need to use the Privacy API for GDPR compliance. I implemented it and ran the test suites it has just to see how I was doing. It seems to find and export data alright, and even deletes most of it. The problem I have is the DB tables specific to my plugin. I can't seem to delete those. 


I've tried writing SQL join queries but that didn't seem to work and in another forum I saw someone mention delete records works better on one table. The big issue I seem to have is not getting the right context list for the user. What's really odd is the contextlist Moodle passes to this function seems off by 1, for instance it shows the course_module instance as 121, when it should be 122. Where does Moodle get the context info from? And for some users it doesn't even seem to create a context. Can anyone point me toward any examples or forums this is touched on?

This is the full function in the provider.php file:
public static function delete_data_for_user(approved_contextlist $contextlist) {

    global $DB;

    if (empty($contextlist->count())) {
        return;
    }
    $userid = $contextlist->get_user()->id;

    foreach ($contextlist->get_contexts() as $context) {

        $instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);

    // Tables to delete from with same key if context matches.
    $tables = ['cmi5launch_usercourse', 'cmi5launch_sessions', 'cmi5launch_aus'];

    foreach ($tables as $table) {

        $sql = array("moodlecourseid" => $instanceid, "userid" => $userid);

        $record = $DB->delete_records($table, $sql);

    }
}
Again it seems to work mostly, it deletes the user from Moodle contexts, just not the plugin tables, which I am attempting to do to be GDPR compliant. Any thoughts?
I am running on Moodle 4.3.2 and use MySQL as DB. The full project is here: https://github.com/adlnet/Moodle-mod_cmi5launch, and I am working on issue #25. Appreciate it!
Average of ratings: -
In reply to Megan Bohland

Re: Trouble implementing delete_data_for_user

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
The field moodlecourseid does not exist in your tables, you have:
  • cmi5launch: course & courseid (twice the same info?)
  • cmi5launch_course : course
  • cmi5launch_usercourse:  This table does not exist
  • cmi5launch_sessions: courseid
  • cmi5launch_aus: courseid



In reply to Renaat Debleu

Re: Trouble implementing delete_data_for_user

by Megan Bohland -
Hi! Thank you for you're reply, I am so sorry but I forgot to mention the branch I am working on is "development". There the tables have been upgraded. I apologize, I should have mentioned that.
In reply to Megan Bohland

Re: Trouble implementing delete_data_for_user

by Megan Bohland -
Update: I think I may have solved the issue. It was not in the delete_data_for_user() function but rather in the function called before it get_contexts_for_userid(). The SQL query was silently failing, but I didn't notice because other contexts were still returned. I corrected the SQL, it now pulls contexts for my plugin tables, and the next function is deleting them!