protected function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\sql_join $allowedjoins) {
        global $DB;

        foreach ($attemptids as $attemptid) {
            if (empty($allowedjoins->joins)) {
                $sql = "SELECT quiza.*
                          FROM {quiz_attempts} quiza
                          JOIN {user} u ON u.id = quiza.userid
                         WHERE quiza.id = :attemptid";
            } else {
                $sql = "SELECT quiza.*
                          FROM {quiz_attempts} quiza
                          JOIN {user} u ON u.id = quiza.userid
                        {$allowedjoins->joins}
                         WHERE {$allowedjoins->wheres} AND quiza.id = :attemptid";
            }
            $params = $allowedjoins->params + array('attemptid' => $attemptid);
            $attempt = $DB->get_record_sql($sql, $params);
            if (!$attempt || $attempt->quiz != $quiz->id || $attempt->preview != 0) {
                // Ensure the attempt exists, belongs to this quiz and belongs to
                // a student included in the report. If not skip.
                continue;
            }

            // Set the course module id before calling quiz_delete_attempt().
            $quiz->cmid = $cm->id;
            quiz_delete_attempt($attempt, $quiz);
        }
    }