Delete all data for all users in the specified context.

@param \context $context the context to delete in.
558412
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

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

        if (!$cm = get_coursemodule_from_id('glossary', $context->instanceid)) {
            return;
        }

        $instanceid = $cm->instance;

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

        // 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 entry and attachment files.
        get_file_storage()->delete_area_files($context->id, 'mod_glossary', 'entry');
        get_file_storage()->delete_area_files($context->id, 'mod_glossary', 'attachment');

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

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

        // Delete tags.
        \core_tag\privacy\provider::delete_item_tags($context, 'mod_glossary', 'glossary_entries');

        // Now delete all user related entries.
        $DB->delete_records('glossary_entries', ['glossaryid' => $instanceid]);
    }
780284
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;
        list($sql, $params) = static::get_sql_where_from_contexts([$context]);
        if (empty($sql)) {
            return;
        }
        $DB->delete_records_select('log', $sql, $params);
    }
1296626
protected function _afterEntityDelete(AnCommandContext $context)
    {
        $total = $this->set->photos->reset()->getTotal();

        if ($total > 0) {
            $this->set->setValue('photo_count', $this->set->photos->reset()->getTotal());
        } else {
            $this->set->delete();
        }
    }
478623
public static function delete_data_for_all_users_in_context(\context $context) {
        global $CFG, $DB;
        require_once($CFG->libdir.'/gradelib.php');

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

        $cm = get_coursemodule_from_id('workshop', $context->instanceid, 0, false, IGNORE_MISSING);

        if (!$cm) {
            // Probably some kind of expired context.
            return;
        }

        $workshop = $DB->get_record('workshop', ['id' => $cm->instance], 'id, course', MUST_EXIST);

        $submissions = $DB->get_records('workshop_submissions', ['workshopid' => $workshop->id], '', 'id');
        $assessments = $DB->get_records_list('workshop_assessments', 'submissionid', array_keys($submissions), '', 'id');

        $DB->delete_records('workshop_aggregations', ['workshopid' => $workshop->id]);
        $DB->delete_records_list('workshop_grades', 'assessmentid', array_keys($assessments));
        $DB->delete_records_list('workshop_assessments', 'id', array_keys($assessments));
        $DB->delete_records_list('workshop_submissions', 'id', array_keys($submissions));

        $fs = get_file_storage();
        $fs->delete_area_files($context->id, 'mod_workshop', 'submission_content');
        $fs->delete_area_files($context->id, 'mod_workshop', 'submission_attachment');
        $fs->delete_area_files($context->id, 'mod_workshop', 'overallfeedback_content');
        $fs->delete_area_files($context->id, 'mod_workshop', 'overallfeedback_attachment');

        grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 0, null, ['reset' => true]);
        grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 1, null, ['reset' => true]);

        \core_plagiarism\privacy\provider::delete_plagiarism_for_context($context);
    }
587850
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

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

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

            $idssql = "SELECT ap.id FROM {analytics_predictions} ap
                        WHERE ap.contextid = :contextid AND ap.modelid = :modelid";
            $idsparams = ['contextid' => $context->id, 'modelid' => $modelid];
            $predictionids = $DB->get_fieldset_sql($idssql, $idsparams);
            if ($predictionids) {
                list($predictionidssql, $params) = $DB->get_in_or_equal($predictionids, SQL_PARAMS_NAMED);

                $DB->delete_records_select('analytics_prediction_actions', "predictionid IN ($idssql)", $idsparams);
                $DB->delete_records_select('analytics_predictions', "id $predictionidssql", $params);
            }
        }

        // We delete them all this table is just a cache and we don't know which model filled it.
        $DB->delete_records('analytics_indicator_calc', ['contextid' => $context->id]);
    }
48252
protected function _actionDelete(AnCommandContext $context)
    {
        if ($session = $this->actor->sessions->find(array('api' => $this->getIdentifier()->name))) {
            $session->delete()->save();
        }
    }
970835
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        foreach ($contextlist as $context) {
            // Let's be super certain that we have the right information for this user here.
            if ($context->contextlevel == CONTEXT_USER && $contextlist->get_user()->id == $context->instanceid) {
                $DB->delete_records('block_recentlyaccesseditems', ['userid' => $context->instanceid]);
            }
        }
    }
695319
protected function _actionDelete(AnCommandContext $context)
    {
        dispatch_plugin('user.onBeforeDeletePerson', array('data' => $context->data));

        $person = parent::_actionDelete($context);
        $person_id = $person->id;

        $this->getService('repos:sessions.session')->destroy(array('nodeId' => $person->id));

        dispatch_plugin('user.onAfterDeletePerson', array('id' => $person_id));

        return $person;
    }
344147
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;
        list($sql, $params) = static::get_sql_where_from_contexts($contextlist->get_contexts());
        if (empty($sql)) {
            return;
        }
        $userid = $contextlist->get_user()->id;
        $DB->delete_records_select('log', "$sql AND userid = :userid", array_merge($params, ['userid' => $userid]));
    }
123464
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        // If the user has repository_instances data, then only the User context should be present so get the first context.
        $contexts = $contextlist->get_contexts();
        if (count($contexts) == 0) {
            return;
        }
        $context = reset($contexts);

        // Sanity check that context is at the User context level, then get the userid.
        if ($context->contextlevel !== CONTEXT_USER) {
            return;
        }
        $userid = $context->instanceid;

        // Delete the repository_instances records created for the userid.
        $DB->delete_records('repository_instances', ['userid' => $userid]);
    }
643225
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if ($context instanceof \context_course) {
            $sectionsql = "itemid IN (SELECT id FROM {course_sections} WHERE course = ?) AND type = ?";
            $DB->delete_records_select('backup_controllers', $sectionsql, [$context->instanceid, \backup::TYPE_1SECTION]);
            $DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1COURSE]);
        }
        if ($context instanceof \context_module) {
            $DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1ACTIVITY]);
        }
        return;
    }
304667
protected function _afterEntityDelete(AnCommandContext $context)
    {
        $this->parent->getRepository()
                     ->getBehavior('commentable')
                     ->resetStats(array($this->parent));
    }
688245
public static function delete_instance_data(\context $context, int $itemid = null) {
        if (is_null($itemid)) {
            self::delete_data_for_instances($context);
        } else {
            self::delete_data_for_instances($context, [$itemid]);
        }
    }
738258
public function setContext($context)
    {
        if ($context === self::CONTEXT_APP || $context === self::CONTEXT_USER) {
            $this->context = $context;
        } else {
            throw new InvalidArgumentException('Incorrect context');
        }
    }
265143
public function updateUserContext(UserContext $context): void
    {
        $context->addParameter(
            'userId',
            $this->repository->getPermissionResolver()->getCurrentUserReference()->getUserId()
        );
    }
1075459
public function deleteContext($context)
    {

        $id = $context->getRecordId();

// Delete any outstanding share keys for resource links for this context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete any users in resource links for this context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Update any resource links for which this consumer is acting as a primary resource link
        $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
               'SET primary_resource_link_pk = null, share_approved = null ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete any resource links for this consumer
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
               'WHERE context_pk = :id';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
               'WHERE context_pk = :id';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $ok = $query->execute();

        if ($ok) {
            $context->initialize();
        }

        return $ok;

    }
26180
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

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

        if ($cm = get_coursemodule_from_id('choice', $context->instanceid)) {
            $DB->delete_records('choice_answers', ['choiceid' => $cm->instance]);
        }
    }