You are here

function QuizQuestionAnsweringForm::submitFinalize in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/Form/QuizQuestionAnsweringForm.php \Drupal\quiz\Form\QuizQuestionAnsweringForm::submitFinalize()
  2. 8.5 src/Form/QuizQuestionAnsweringForm.php \Drupal\quiz\Form\QuizQuestionAnsweringForm::submitFinalize()

Helper function to finalize a quiz attempt.

See also

quiz_question_answering_form_submit()

quiz_question_answering_form_submit_blank()

2 calls to QuizQuestionAnsweringForm::submitFinalize()
QuizQuestionAnsweringForm::submitBlank in src/Form/QuizQuestionAnsweringForm.php
Submit action for "leave blank".
QuizQuestionAnsweringForm::submitForm in src/Form/QuizQuestionAnsweringForm.php
Submit handler for the question answering form.

File

src/Form/QuizQuestionAnsweringForm.php, line 338

Class

QuizQuestionAnsweringForm
The form used to deliver questions to users and capture their responses.

Namespace

Drupal\quiz\Form

Code

function submitFinalize(array $form, FormStateInterface $form_state) {
  $quiz_result = $form_state
    ->getBuildInfo()['args'][1];
  $quiz = \Drupal::entityTypeManager()
    ->getStorage('quiz')
    ->loadRevision($quiz_result
    ->get('vid')
    ->getString());

  /* @var $quiz_session \Drupal\quiz\Services\QuizSessionInterface */
  $quiz_session = \Drupal::service('quiz.session');

  // No more questions. Score quiz and finalize result.
  $quiz_result
    ->finalize();
  if (empty($quiz->review_options['question']) || !array_filter($quiz->review_options['question']) || empty($form_state['feedback'])) {

    // Only redirect to question results if there is not question feedback.
    // /** @todo D8*/
    $form_state
      ->setRedirect('entity.quiz_result.canonical', [
      'quiz' => $quiz
        ->id(),
      'quiz_result' => $quiz_result
        ->id(),
    ]);
  }

  // Remove all information about this quiz from the session.
  // @todo but for anon, we might have to keep some so they could access
  // results
  // When quiz is completed we need to make sure that even though the quiz has
  // been removed from the session, that the user can still access the
  // feedback for the last question, THEN go to the results page.
  $quiz_session
    ->setTemporaryResult($quiz_result);
  $quiz_session
    ->removeQuiz($quiz);
}