Delete all data for all users in the specified context.

@param   context                 $context   The specific context to delete data for.
763260
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        $DB->delete_records('search_simpledb_index', ['contextid' => $context->id]);

        if ($context->contextlevel == CONTEXT_USER) {
            $select = "userid = :userid OR owneruserid = :owneruserid";
            $params = ['userid' => $context->instanceid, 'owneruserid' => $context->instanceid];
            $DB->delete_records_select('search_simpledb_index', $select, $params);
        }
    }
1190179
@Override
	public synchronized void deleteAllDataForContext(int contextId) throws DatabaseException {
    	try {
			psDeleteAllDataForContext.setInt(1, contextId);
			psDeleteAllDataForContext.executeUpdate();
		} catch (SQLException e) {
			throw new DatabaseException(e);
		}
    }
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]);
    }
958138
@Override
	public synchronized void deleteAllDataForContext(int contextId) throws DatabaseException {
    	SqlPreparedStatementWrapper psDeleteAllDataForContext = null;
    	try {
        	psDeleteAllDataForContext = DbSQL.getSingleton().getPreparedStatement("context.ps.deletealldataforcontext");
			psDeleteAllDataForContext.getPs().setInt(1, contextId);
			psDeleteAllDataForContext.getPs().executeUpdate();
		} catch (SQLException e) {
			throw new DatabaseException(e);
		} finally {
			DbSQL.getSingleton().releasePreparedStatement(psDeleteAllDataForContext);
		}
    }
959538
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;

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

        if (!$lessonid = static::get_lesson_id_from_context($context)) {
            return;
        }

        $DB->delete_records('lesson_attempts', ['lessonid' => $lessonid]);
        $DB->delete_records('lesson_branch', ['lessonid' => $lessonid]);
        $DB->delete_records('lesson_grades', ['lessonid' => $lessonid]);
        $DB->delete_records('lesson_timer', ['lessonid' => $lessonid]);
        $DB->delete_records_select('lesson_overrides', 'lessonid = :id AND userid IS NOT NULL', ['id' => $lessonid]);

        $fs = get_file_storage();
        $fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses');
        $fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers');
    }
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');
    }
717653
protected function _actionDelete($context)
    {
        $context->response->status = AnHttpResponse::NO_CONTENT;
        $this->getItem()->delete();
        $context->response->setRedirect(route($this->getItem()->owner->getURL()));
    }
784956
public static function delete_data_for_all_users_in_context(context $context) {
        global $DB;

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

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

        $chatid = $cm->instance;
        $DB->delete_records_select('chat_messages', 'chatid = :chatid', ['chatid' => $chatid]);
        $DB->delete_records_select('chat_messages_current', 'chatid = :chatid', ['chatid' => $chatid]);
        $DB->delete_records_select('chat_users', 'chatid = :chatid', ['chatid' => $chatid]);
    }
1279242
public function deleteContext($context) {
        global $DB;

        $contextid = $context->getRecordId();

        $params = ['id' => $contextid];

        // Delete any outstanding share keys for resource links for this context.
        $where = "resourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.contextid = :id
               )";
        $DB->delete_records_select($this->sharekeytable, $where, $params);

        // Delete any users in resource links for this context.
        $DB->delete_records_select($this->userresulttable, $where, $params);

        // Update any resource links for which this consumer is acting as a primary resource link.
        $where = "primaryresourcelinkid IN (
                    SELECT rl.id
                      FROM {{$this->resourcelinktable}} rl
                     WHERE rl.contextid = :id
               )";
        $updaterecords = $DB->get_records_select($this->resourcelinktable, $where, $params);
        foreach ($updaterecords as $record) {
            $record->primaryresourcelinkid = null;
            $record->shareapproved = null;
            $DB->update_record($this->resourcelinktable, $record);
        }

        // Delete any resource links for this context.
        $DB->delete_records($this->resourcelinktable, ['contextid' => $contextid]);

        // Delete context.
        $DB->delete_records($this->contexttable, $params);

        $context->initialize();

        return true;
    }
1009953
public static function delete_data_for_all_users_in_context(\context $context) {
        if ($context->contextlevel != CONTEXT_MODULE) {
            // Only quiz module will be handled.
            return;
        }

        $cm = get_coursemodule_from_id('quiz', $context->instanceid);
        if (!$cm) {
            // Only quiz module will be handled.
            return;
        }

        $quizobj = \quiz::create($cm->instance);
        $quiz = $quizobj->get_quiz();

        // Handle the 'quizaccess' subplugin.
        manager::plugintype_class_callback(
                'quizaccess',
                quizaccess_provider::class,
                'delete_subplugin_data_for_all_users_in_context',
                [$quizobj]
            );

        // Delete all overrides - do not log.
        quiz_delete_all_overrides($quiz, false);

        // This will delete all question attempts, quiz attempts, and quiz grades for this quiz.
        quiz_delete_all_attempts($quiz);
    }
345743
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if ($context->contextlevel == CONTEXT_MODULE) {
            $cm = get_coursemodule_from_id('assign', $context->instanceid);
            if ($cm) {
                // Get the assignment related to this context.
                $assign = new \assign($context, null, null);
                // What to do first... Get sub plugins to delete their stuff.
                $requestdata = new assign_plugin_request_data($context, $assign);
                manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
                    'delete_submission_for_context', [$requestdata]);
                $requestdata = new assign_plugin_request_data($context, $assign);
                manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
                    'delete_feedback_for_context', [$requestdata]);
                $DB->delete_records('assign_grades', ['assignment' => $assign->get_instance()->id]);

                // Delete advanced grading information.
                $gradingmanager = get_grading_manager($context, 'mod_assign', 'submissions');
                $controller = $gradingmanager->get_active_controller();
                if (isset($controller)) {
                    \core_grading\privacy\provider::delete_instance_data($context);
                }

                // Time to roll my own method for deleting overrides.
                static::delete_overrides_for_users($assign);
                $DB->delete_records('assign_submission', ['assignment' => $assign->get_instance()->id]);
                $DB->delete_records('assign_user_flags', ['assignment' => $assign->get_instance()->id]);
                $DB->delete_records('assign_user_mapping', ['assignment' => $assign->get_instance()->id]);
            }
        }
    }
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');
    }
503913
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        if ($context->contextlevel == CONTEXT_MODULE) {
            // Delete all assignment submissions for the assignment associated with the context module.
            $assignment = self::get_assignment_by_context($context);
            if ($assignment != null) {
                $DB->delete_records('assignment_submissions', ['assignment' => $assignment->id]);

                // Delete all file uploads associated with the assignment submission for the specified context.
                $fs = get_file_storage();
                $fs->delete_area_files($context->id, 'mod_assignment', 'submission');
            }
        }
    }
1231340
public static function delete_data_for_user(approved_contextlist $contextlist) {
        global $DB;

        // When we process user deletions and expiries, we always delete from the user context.
        // As a result the cohort role assignments would be deleted, which has a knock-on effect with courses
        // as roles may change and data may be removed earlier than it should be.

        // Remove contexts different from SYSTEM or COURSECAT.
        $contextids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
            if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) {
                $carry[] = $context->id;
            }
            return $carry;
        }, []);

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

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

        list($contextsql, $contextparams) =  $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
        $selectcontext = "contextid {$contextsql}";
        // Get the cohorts in the specified contexts.
        $cohortids = $DB->get_fieldset_select('cohort', 'id', $selectcontext, $contextparams);

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

        list($cohortsql, $cohortparams) =  $DB->get_in_or_equal($cohortids, SQL_PARAMS_NAMED);
        $selectcohort = "cohortid {$cohortsql} AND userid = :userid";
        $params = ['userid' => $userid] + $cohortparams;

        // Delete the tool_cohortroles records created for the userid.
        $DB->delete_records_select('tool_cohortroles', $selectcohort, $params);
    }
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?'));
    }
751907
public static function deleteAllMembersProcessBatch(&$context) {
    if (empty($context['sandbox'])) {
      $memberIds = \Drupal::entityQuery('openy_campaign_member')->execute();
      $memberCampaignIds = \Drupal::entityQuery('openy_campaign_member_campaign')->execute();

      $context['sandbox']['progress'] = 0;

      $context['sandbox']['members'] = array_values($memberIds);
      $context['sandbox']['member_campaigns'] = array_values($memberCampaignIds);
      $context['sandbox']['max'] = count($memberCampaignIds);
    }
    // Get Member and MemberCampaign ids.
    $memberId = $context['sandbox']['members'][$context['sandbox']['progress']];
    $memberCampaignId = $context['sandbox']['member_campaigns'][$context['sandbox']['progress']];

    // Get Member entity manager.
    $memberStorage = \Drupal::entityTypeManager()
      ->getStorage('openy_campaign_member');
    // Get MemberCampaign entity manager.
    $memberCampaignStorage = \Drupal::entityTypeManager()
      ->getStorage('openy_campaign_member_campaign');

    // Delete Member entity.
    $entities = $memberStorage->loadMultiple([$memberId]);
    $memberStorage->delete($entities);
    // Delete MemberCampaign entity.
    $entities = $memberCampaignStorage->loadMultiple([$memberCampaignId]);
    $memberCampaignStorage->delete($entities);

    // Save results.
    $context['results'][] = $memberId;
    $context['sandbox']['progress']++;
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
213680
public function updateUserContext(UserContext $context): void
    {
        if ($this->session->isStarted()) {
            $context->addParameter('sessionId', $this->session->getId());
        }
    }