Delete all data for all users in the specified context.

@param context $context The specific context to delete data for.
546517
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;
        if ($context->contextlevel != CONTEXT_BLOCK) {
            return;
        }

        // Delete the user preferences.
        $instanceid = $context->instanceid;
        $DB->delete_records_list('user_preferences', 'name', [
            "block{$instanceid}hidden",
            "docked_block_instance_{$instanceid}"
        ]);
    }
1196107
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        // Sanity check that context is at the User context level.
        if ($context->contextlevel == CONTEXT_COURSE) {
            $sql = "SELECT ue.id
                      FROM {user_enrolments} ue
                      JOIN {enrol} e ON e.id = ue.enrolid
                     WHERE e.courseid = :courseid";
            $params = ['courseid' => $context->instanceid];
            $enrolsids = $DB->get_fieldset_sql($sql, $params);
            if (!empty($enrolsids)) {
                list($insql, $inparams) = $DB->get_in_or_equal($enrolsids, SQL_PARAMS_NAMED);
                static::delete_user_data($insql, $inparams);
            }
        }
    }
1135978
public static function delete_data_for_all_users_in_context(\context $context) {
        if ($context->contextlevel != CONTEXT_COURSE) {
            return;
        }
        global $DB;
        $DB->delete_records('enrol_flatfile', ['courseid' => $context->instanceid]);
    }
602288
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;
        switch ($context->contextlevel) {
            case CONTEXT_USER:
                static::delete_all_user_data($context);
                break;

            case CONTEXT_COURSE:
            case CONTEXT_MODULE:
                // We only delete associations here.
                $DB->delete_records('blog_association', ['contextid' => $context->id]);
                break;
        }
        // Delete all the comments.
        \core_comment\privacy\provider::delete_comments_for_all_users($context, 'blog', 'format_blog');
    }
213680
public function updateUserContext(UserContext $context): void
    {
        if ($this->session->isStarted()) {
            $context->addParameter('sessionId', $this->session->getId());
        }
    }
1053926
public function decrement($context = null)
    {
        $poolUsage = $this->cache->get(self::CACHE_KEY) ?: 0;
        $this->logPoolUsage($poolUsage, $context);
        if ($poolUsage > 1) {
            $this->cache->set(self::CACHE_KEY, $poolUsage - 1, $this->lifetime);
        } else {
            $this->cache->delete(self::CACHE_KEY);
        }
    }
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);
    }
1342114
protected function terminate($context)
    {
        $handler = $context->getMessageHandler();
        $handler->handle($context->getMessageBeingHandled(), $context);
    }
639737
protected function _setContext($context)
    {
        $this->context = ($context !== null)
            ? $this->_normalizeContainer($context)
            : null;
    }
892643
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;
        if (!static::approved_contextlist_contains_my_context($contextlist)) {
            // We only care about the user's user context.
            return;
        }

        static::delete_user_data($contextlist->get_user()->id);
    }
701903
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;
        // Context could be anything, BEWARE!
        if ($context->contextlevel == CONTEXT_USER) {
            $DB->delete_records('portfolio_instance_user', ['userid' => $context->instanceid]);
            $DB->delete_records('portfolio_tempdata', ['userid' => $context->instanceid]);
            $DB->delete_records('portfolio_log', ['userid' => $context->instanceid]);
        }
    }
130019
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        // Don't remove data from role_capabilities.
        // Because this data affects the whole Moodle, there are override capabilities.
        // Don't belong to the modifier user.

        // Remove data from role_assignments.
        if (empty($contextlist->count())) {
            return;
        }
        $userid = $contextlist->get_user()->id;
        $contextids = $contextlist->get_contextids();

        list($contextsql, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
        $params = ['userid' => $userid] + $contextparams;

        // Only delete the roles assignments where the user is assigned in all contexts.
        $DB->delete_records_select('role_assignments',
            "userid = :userid AND contextid {$contextsql}", $params);
    }
1085779
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 instanceof \context_module) {
                continue;
            }
            $instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);
            $DB->delete_records('lti_submission', ['ltiid' => $instanceid, 'userid' => $userid]);
        }
    }
312584
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;

        switch ($context->contextlevel) {
            case CONTEXT_USER:
                // The user context is only reported when there are orphan historical grades, so we only delete those.
                static::delete_orphan_historical_grades($context->instanceid);
                break;

            case CONTEXT_COURSE:
                // We must not change the structure of the course, so we only delete user content.
                $itemids = static::get_item_ids_from_course_ids([$context->instanceid]);
                if (empty($itemids)) {
                    return;
                }

                self::delete_files($itemids, true);
                self::delete_files($itemids, false);

                list($insql, $inparams) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
                $DB->delete_records_select('grade_grades', "itemid $insql", $inparams);
                $DB->delete_records_select('grade_grades_history', "itemid $insql", $inparams);
                break;
        }

    }
1135400
protected static function delete_data(\context $context, int $userid = null) {
        global $DB;

        $cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid', ['contextid' => $context->id]);
        foreach ($cohortids as $cohortid) {
            $params = ['cohortid' => $cohortid];
            if (!empty($userid)) {
                $params['userid'] = $userid;
            }
            $DB->delete_records('cohort_members', $params);
        }
    }
150187
public static function delete_data_for_all_users_in_context(\context $context) {
        // Check what context we've been delivered.
        if (!$context instanceof \context_course) {
            return;
        }
        // Delete course completion data.
        \core_completion\privacy\provider::delete_completion(null, $context->instanceid);
        // Delete course favourite data.
        \core_favourites\privacy\provider::delete_favourites_for_all_users($context, 'core_course',
                'courses');
    }
418475
protected function _actionDelete(AnCommandContext $context)
    {
        $viewer = $this->_state->viewer;
        $person_id = $viewer->id;
        dispatch_plugin('user.onBeforeLogoutPerson', array('person' => $viewer));
        $this->getService('com:people.helper.person')->logout($viewer);
        dispatch_plugin('user.onAfterLogoutPerson', array('id' => $person_id));
        $context->response->setRedirect(route('index.php?'));
    }