}
}
if (!$success) {
    print_error('errorprocessingresponses', 'question', $attemptobj->attempt_url(0, $page));
}
/// If we do not have to finish the attempts (if we are only processing responses)
/// save the attempt and redirect to the next page.
if (!$finishattempt) {
    $attempt->timemodified = $timenow;
    $DB->update_record('quiz_attempts', $attempt);
    redirect($nexturl);
}
/// We have been asked to finish attempt, so do that //////////////////////
/// Now load the state of every question, reloading the ones we messed around
/// with above.
$attemptobj->preload_question_states();
$attemptobj->load_question_states();
/// Move each question to the closed state.
$success = true;
$attempt = $attemptobj->get_attempt();
foreach ($attemptobj->get_questions() as $id => $question) {
    $state = $attemptobj->get_question_state($id);
    $action = new stdClass();
    $action->event = QUESTION_EVENTCLOSE;
    $action->responses = $state->responses;
    $action->responses['_flagged'] = $state->flagged;
    $action->timestamp = $state->timestamp;
    if (question_process_responses($attemptobj->get_question($id), $state, $action, $attemptobj->get_quiz(), $attempt)) {
        save_question_session($attemptobj->get_question($id), $state);
    } else {
        $success = false;