You are here

public function QuizQuestionAnsweringForm::submitBlank in Quiz 6.x

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

Submit action for "leave blank".

File

src/Form/QuizQuestionAnsweringForm.php, line 287

Class

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

Namespace

Drupal\quiz\Form

Code

public function submitBlank(array $form, FormStateInterface $form_state) {
  $quiz_result = $form_state
    ->getBuildInfo()['args'][1];

  /* @var $quiz_session \Drupal\quiz\Services\QuizSessionInterface */
  $quiz_session = \Drupal::service('quiz.session');
  $quiz = \Drupal::entityTypeManager()
    ->getStorage('quiz')
    ->loadRevision($quiz_result
    ->get('vid')
    ->getString());
  if (!empty($form_state
    ->getUserInput()['question'])) {

    // Loop over all question inputs provided, and record them as skipped.
    foreach (array_keys($form_state
      ->getUserInput()['question']) as $qqid) {
      foreach ($quiz_result
        ->getLayout() as $idx => $qra) {

        // Find the blank submitted question in the current layout.
        if ($qra
          ->get('question_id')
          ->getString() == $qqid) {

          // Reset the question and mark it as taken, so restrictions like
          // backwards navigation function correctly.
          $qra
            ->set('is_skipped', TRUE);
          $qra
            ->set('is_correct', NULL);
          $qra
            ->set('points_awarded', FALSE);
          $qra
            ->set('answer_timestamp', \Drupal::time()
            ->getRequestTime());
          $qra
            ->save();
        }
      }
      $quiz_result
        ->setQuestion($quiz_session
        ->getCurrentQuestion($quiz) + 1);
    }
  }
  else {

    // Advance to next question, no input here.
    $quiz_result
      ->setQuestion($quiz, $quiz_session
      ->getCurrentQuestion($quiz) + 1);
  }

  // Advance to next question.
  $form_state
    ->setRedirect('quiz.question.take', [
    'quiz' => $quiz
      ->id(),
    'question_number' => $quiz_session
      ->getCurrentQuestion($quiz),
  ]);
  $layout = $quiz_result
    ->getLayout();
  if (!isset($layout[$quiz_session
    ->getCurrentQuestion($quiz)])) {

    // If this is the last question, finalize the quiz.
    $this
      ->submitFinalize($form, $form_state);
  }
}