Delete all user data for the specified user, in the specified contexts.

@param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
548673
public static function delete_data_for_user(approved_contextlist $contextlist) {
        // The only way to delete data for the html block is to delete the block instance itself.
        foreach ($contextlist as $context) {

            if (!$context instanceof \context_block) {
                continue;
            }
            if ($blockinstance = static::get_instance_from_context($context)) {
                blocks_delete_instance($blockinstance);
            }
        }
    }
813435
public static function export_user_data(approved_contextlist $contextlist) {
        $context = $contextlist->current();
        $user = \core_user::get_user($contextlist->get_user()->id);
        static::export_recentitems($user->id, $context);
    }
1202309
public static function delete_data_for_user(approved_contextlist $contextlist) {
        if (empty($contextlist->count())) {
            return;
        }
        \core_group\privacy\provider::delete_groups_for_user($contextlist, 'enrol_meta');
    }
859350
public static function export_user_data(approved_contextlist $contextlist) {
        $contexts = $contextlist->get_contexts();

        foreach ($contexts as $context) {
            static::export_groups($context, '');
        }
    }
287929
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        $userid = $contextlist->get_user()->id;
        $cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
            if ($context->contextlevel == CONTEXT_MODULE) {
                $carry[] = $context->instanceid;
            }
            return $carry;
        }, []);
        if (empty($cmids)) {
            return;
        }

        // Find the lesson IDs.
        $lessonidstocmids = static::get_lesson_ids_to_cmids_from_cmids($cmids);
        $lessonids = array_keys($lessonidstocmids);
        if (empty($lessonids)) {
            return;
        }

        // Prepare the SQL we'll need below.
        list($insql, $inparams) = $DB->get_in_or_equal($lessonids, SQL_PARAMS_NAMED);
        $sql = "lessonid $insql AND userid = :userid";
        $params = array_merge($inparams, ['userid' => $userid]);

        // Delete the attempt files.
        $fs = get_file_storage();
        $recordset = $DB->get_recordset_select('lesson_attempts', $sql, $params, '', 'id, lessonid');
        foreach ($recordset as $record) {
            $cmid = $lessonidstocmids[$record->lessonid];
            $context = context_module::instance($cmid);
            $fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses', $record->id);
            $fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers', $record->id);
        }
        $recordset->close();

        // Delete all the things.
        $DB->delete_records_select('lesson_attempts', $sql, $params);
        $DB->delete_records_select('lesson_branch', $sql, $params);
        $DB->delete_records_select('lesson_grades', $sql, $params);
        $DB->delete_records_select('lesson_timer', $sql, $params);
        $DB->delete_records_select('lesson_overrides', $sql, $params);
    }
1016032
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();
        // If the context isn't for a module then return early.
        if ($context->contextlevel != CONTEXT_MODULE) {
            return;
        }
        // Fetch the assignment.
        $assignment = self::get_assignment_by_context($context);
        $userids = $userlist->get_userids();

        list($inorequalsql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
        $params['assignmentid'] = $assignment->id;

        // Get submission ids.
        $sql = "
            SELECT s.id
            FROM {assignment_submissions} s
            JOIN {assignment} a ON s.assignment = a.id
            WHERE a.id = :assignmentid
            AND s.userid $inorequalsql
        ";

        $submissionids = $DB->get_records_sql($sql, $params);
        list($submissionidsql, $submissionparams) = $DB->get_in_or_equal(array_keys($submissionids), SQL_PARAMS_NAMED);
        $fs = get_file_storage();
        $fs->delete_area_files_select($context->id, 'mod_assignment', 'submission', $submissionidsql, $submissionparams);
        // Delete related tables.
        $DB->delete_records_list('assignment_submissions', 'id', array_keys($submissionids));
    }
1326590
public static function delete_data_for_users(approved_userlist $userlist) {
        $context = $userlist->get_context();

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

        // Remove invalid users. If it ends up empty then early return.
        $userids = array_filter($userlist->get_userids(), function($userid) use($context) {
            return $context->instanceid == $userid;
        });

        if (empty($userids)) {
            return;
        }

        static::delete_user_data($context->instanceid);
    }
231499
public static function delete_data_for_user(approved_contextlist $contextlist) {
        // Only delete data from contexts which are at the CONTEXT_COURSE contextlevel.
        $contexts = self::validate_contextlist_contexts($contextlist);
        if (empty($contexts)) {
            return;
        }

        // Get the course ids based on the provided contexts.
        $contextinstanceids = array_map(function($context) {
            return $context->instanceid;
        }, $contextlist->get_contexts());

        global $DB;
        $user = $contextlist->get_user();
        list($insql, $inparams) = $DB->get_in_or_equal($contextinstanceids, SQL_PARAMS_NAMED);
        $params = array_merge(['userid' => $user->id], $inparams);
        $sql = "userid = :userid AND courseid $insql";
        $DB->delete_records_select('enrol_flatfile', $sql, $params);
    }
690443
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        $userid = intval($contextlist->get_user()->id);

        $models = self::get_models_with_user_data();
        $modelids = array_keys($models);

        list ($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);

        // Analytics prediction actions.
        list($sql, $apaparams) = self::analytics_prediction_actions_user_sql($userid, $modelids, $contextsql);
        $sql = "SELECT apa.id " . $sql;

        $predictionactionids = $DB->get_fieldset_sql($sql, $apaparams + $contextparams);
        if ($predictionactionids) {
            list ($predictionactionidssql, $params) = $DB->get_in_or_equal($predictionactionids);
            $DB->delete_records_select('analytics_prediction_actions', "id {$predictionactionidssql}", $params);
        }

        foreach ($models as $modelid => $model) {

            $analyser = $model->get_analyser(['notimesplitting' => true]);

            // Analytics predictions.
            $joinusersql = $analyser->join_sample_user('ap');
            $sql = "SELECT DISTINCT ap.id FROM {analytics_predictions} ap
                      {$joinusersql}
                     WHERE u.id = :userid AND ap.modelid = :modelid AND ap.contextid {$contextsql}";

            $predictionids = $DB->get_fieldset_sql($sql, ['userid' => $userid, 'modelid' => $modelid] + $contextparams);
            if ($predictionids) {
                list($predictionidssql, $params) = $DB->get_in_or_equal($predictionids, SQL_PARAMS_NAMED);
                $DB->delete_records_select('analytics_predictions', "id $predictionidssql", $params);
            }

            // Indicator calculations.
            $joinusersql = $analyser->join_sample_user('aic');
            $sql = "SELECT DISTINCT aic.id FROM {analytics_indicator_calc} aic
                      {$joinusersql}
                     WHERE u.id = :userid AND aic.sampleorigin = :analysersamplesorigin AND aic.contextid {$contextsql}";

            $params = ['userid' => $userid, 'analysersamplesorigin' => $analyser->get_samples_origin()] + $contextparams;
            $indicatorcalcids = $DB->get_fieldset_sql($sql, $params);
            if ($indicatorcalcids) {
                list ($indicatorcalcidssql, $params) = $DB->get_in_or_equal($indicatorcalcids, SQL_PARAMS_NAMED);
                $DB->delete_records_select('analytics_indicator_calc', "id $indicatorcalcidssql", $params);
            }
        }
    }
739170
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;
        $userid = $contextlist->get_user()->id;
        $prefnames = array_reduce($contextlist->get_contexts(), function($carry, $context) {
            if ($context->contextlevel == CONTEXT_BLOCK) {
                $carry[] = "block{$context->instanceid}hidden";
                $carry[] = "docked_block_instance_{$context->instanceid}";
            }
            return $carry;
        }, []);

        if (empty($prefnames)) {
            return;
        }

        list($insql, $inparams) = $DB->get_in_or_equal($prefnames, SQL_PARAMS_NAMED);
        $sql = "userid = :userid AND name $insql";
        $params = array_merge($inparams, ['userid' => $userid]);
        $DB->delete_records_select('user_preferences', $sql, $params);
    }
386618
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;
        $userid = $contextlist->get_user()->id;
        $associationcontextids = [];

        foreach ($contextlist as $context) {
            if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) {
                static::delete_all_user_data($context);
                \core_comment\privacy\provider::delete_comments_for_all_users($context, 'blog', 'format_blog');
            } else if ($context->contextlevel == CONTEXT_COURSE) {
                // Only delete the course associations.
                $associationcontextids[] = $context->id;
            } else if ($context->contextlevel == CONTEXT_MODULE) {
                // Only delete the module associations.
                $associationcontextids[] = $context->id;
            } else {
                \core_comment\privacy\provider::delete_comments_for_user($contextlist, 'blog', 'format_blog');
            }
        }

        // Delete the associations.
        if (!empty($associationcontextids)) {
            list($insql, $inparams) = $DB->get_in_or_equal($associationcontextids, SQL_PARAMS_NAMED);
            $sql = "SELECT ba.id
                      FROM {blog_association} ba
                      JOIN {post} p
                        ON p.id = ba.blogid
                     WHERE ba.contextid $insql
                       AND p.userid = :userid";
            $params = array_merge($inparams, ['userid' => $userid]);
            $associds = $DB->get_fieldset_sql($sql, $params);

            list($insql, $inparams) = $DB->get_in_or_equal($associds, SQL_PARAMS_NAMED, 'param', true);
            $DB->delete_records_select('blog_association', "id $insql", $inparams);
        }
    }
278814
public static function delete_data_for_users(approved_userlist $userlist) {
        global $DB;

        $context = $userlist->get_context();

        if ($context->contextlevel != CONTEXT_COURSE) {
            return;
        }

        $userids = $userlist->get_userids();

        list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
        $params = array_merge(['courseid' => $context->instanceid], $inparams);
        $sql = "courseid = :courseid AND userid $insql";
        $DB->delete_records_select('enrol_flatfile', $sql, $params);
    }
263056
public static function export_user_data(approved_contextlist $contextlist) {
        self::export_user_data_lti_submissions($contextlist);

        self::export_user_data_lti_types($contextlist);

        self::export_user_data_lti_tool_proxies($contextlist);
    }
1024737
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) {
            if ($context->contextlevel == CONTEXT_MODULE) {

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

                $entries = $DB->get_records('glossary_entries', ['glossaryid' => $instanceid, 'userid' => $userid],
                        '', 'id');

                if (!$entries) {
                    continue;
                }

                list($insql, $inparams) = $DB->get_in_or_equal(array_keys($entries), SQL_PARAMS_NAMED);
                // Delete related entry aliases.
                $DB->delete_records_list('glossary_alias', 'entryid', array_keys($entries));

                // Delete related entry categories.
                $DB->delete_records_list('glossary_entries_categories', 'entryid', array_keys($entries));

                // Delete related entry and attachment files.
                get_file_storage()->delete_area_files_select($context->id, 'mod_glossary', 'entry', $insql, $inparams);
                get_file_storage()->delete_area_files_select($context->id, 'mod_glossary', 'attachment', $insql, $inparams);

                // Delete user tags related to this glossary.
                \core_tag\privacy\provider::delete_item_tags_select($context, 'mod_glossary', 'glossary_entries', $insql, $inparams);

                // Delete related ratings.
                \core_rating\privacy\provider::delete_ratings_select($context, 'mod_glossary', 'entry', $insql, $inparams);

                // Delete comments.
                \core_comment\privacy\provider::delete_comments_for_user($contextlist, 'mod_glossary', 'glossary_entry');

                // Now delete all user related entries.
                $DB->delete_records('glossary_entries', ['glossaryid' => $instanceid, 'userid' => $userid]);
            }
        }
    }
1206335
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        $contextids = $contextlist->get_contextids();

        if (!$contextids) {
            return;
        }

        // Remove only individual subwikis. Contributions to collaborative wikis is not considered personal contents.
        list($ctxsql, $ctxparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
        $subwikis = $DB->get_records_sql_menu('SELECT s.id, ctx.id AS ctxid
              FROM {context} ctx
              JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextmod
              JOIN {modules} m ON m.name = :wiki AND cm.module = m.id
              JOIN {wiki_subwikis} s ON s.wikiid = cm.instance AND s.userid = :userid
             WHERE ctx.id ' . $ctxsql,
            ['userid' => (int)$contextlist->get_user()->id, 'wiki' => 'wiki', 'contextmod' => CONTEXT_MODULE] + $ctxparams);

        if ($subwikis) {
            // We found individual subwikis that need to be deleted completely.

            $fs = get_file_storage();
            foreach ($subwikis as $subwikiid => $contextid) {
                $fs->delete_area_files($contextid, 'mod_wiki', 'attachments', $subwikiid);
                \core_comment\privacy\provider::delete_comments_for_all_users_select(context::instance_by_id($contextid),
                    'mod_wiki', 'wiki_page', "IN (SELECT id FROM {wiki_pages} WHERE subwikiid=:subwikiid)",
                    ['subwikiid' => $subwikiid]);
            }

            list($sql, $params) = $DB->get_in_or_equal(array_keys($subwikis), SQL_PARAMS_NAMED);

            $DB->execute("DELETE FROM {tag_instance} WHERE component=:component AND itemtype=:itemtype AND itemid IN
                (SELECT id FROM {wiki_pages} WHERE subwikiid $sql)",
                ['component' => 'mod_wiki', 'itemtype' => 'page'] + $params);

            $DB->delete_records_select('wiki_locks', 'pageid IN (SELECT id FROM {wiki_pages} WHERE subwikiid ' . $sql . ')',
                $params);
            $DB->delete_records_select('wiki_versions', 'pageid IN (SELECT id FROM {wiki_pages} WHERE subwikiid ' . $sql . ')',
                $params);
            $DB->delete_records_select('wiki_synonyms', 'subwikiid ' . $sql, $params);
            $DB->delete_records_select('wiki_links', 'subwikiid ' . $sql, $params);
            $DB->delete_records_select('wiki_pages', 'subwikiid ' . $sql, $params);
            $DB->delete_records_select('wiki_subwikis', 'id ' . $sql, $params);
        }

        // Remove comments made by this user on all other wiki pages.
        \core_comment\privacy\provider::delete_comments_for_user($contextlist, 'mod_wiki', 'wiki_page');
    }
840555
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        $user = $contextlist->get_user();

        foreach ($contextlist as $context) {
            if ($context->contextlevel != CONTEXT_MODULE) {
                continue;
            }
            // Get the assign object.
            $assign = new \assign($context, null, null);
            $assignid = $assign->get_instance()->id;

            $submissions = $DB->get_records('assign_submission', ['assignment' => $assignid, 'userid' => $user->id]);
            foreach ($submissions as $submission) {
                $requestdata = new assign_plugin_request_data($context, $assign, $submission, [], $user);
                manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
                        'delete_submission_for_userid', [$requestdata]);
            }

            $grades = $DB->get_records('assign_grades', ['assignment' => $assignid, 'userid' => $user->id]);
            $gradingmanager = get_grading_manager($context, 'mod_assign', 'submissions');
            $controller = $gradingmanager->get_active_controller();
            foreach ($grades as $grade) {
                $requestdata = new assign_plugin_request_data($context, $assign, $grade, [], $user);
                manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
                        'delete_feedback_for_grade', [$requestdata]);
                // Delete advanced grading information.
                if (isset($controller)) {
                    \core_grading\privacy\provider::delete_instance_data($context, $grade->id);
                }
            }

            static::delete_overrides_for_users($assign, [$user->id]);
            $DB->delete_records('assign_user_flags', ['assignment' => $assignid, 'userid' => $user->id]);
            $DB->delete_records('assign_user_mapping', ['assignment' => $assignid, 'userid' => $user->id]);
            $DB->delete_records('assign_grades', ['assignment' => $assignid, 'userid' => $user->id]);
            $DB->delete_records('assign_submission', ['assignment' => $assignid, 'userid' => $user->id]);
        }
    }
17551
public static function delete_data_for_user(approved_contextlist $contextlist) {
        if (empty($contextlist->count())) {
            return;
        }

        $userid = $contextlist->get_user()->id;
        foreach ($contextlist->get_contexts() as $context) {
            if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
                continue;
            }
            static::delete_data($context, $userid);
        }
    }