You are here

public function QuizResultEntityForm::submitForm in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Entity/QuizResultEntityForm.php \Drupal\quiz\Entity\QuizResultEntityForm::submitForm()

Additionally update the score and feedback of the questions in this result.

Overrides ContentEntityForm::submitForm

File

src/Entity/QuizResultEntityForm.php, line 61

Class

QuizResultEntityForm

Namespace

Drupal\quiz\Entity

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /* @var $quiz_result QuizResult */
  $quiz_result = $this->entity;
  if ($quiz_result
    ->isNew()) {
    $quiz_result->build_on_last = $form_state
      ->getValue('build_on_last');
  }
  else {
    $layout = $this->entity
      ->getLayout();

    // Update questions.
    foreach ($form_state
      ->getValue('question') as $layoutIdx => $question) {
      $qra = $layout[$layoutIdx];
      $qra
        ->set('points_awarded', $question['score']);
      $qra
        ->set('answer_feedback', $question['answer_feedback']);
      $qra
        ->set('is_evaluated', 1);
      $qra
        ->save();
    }

    // Finalize result.
    $quiz_result
      ->finalize();

    // Notify the user if results got deleted as a result of him scoring an
    // answer.
    $quiz = \Drupal::entityTypeManager()
      ->getStorage('quiz')
      ->loadRevision($quiz_result
      ->get('vid')
      ->getString());
    $results_got_deleted = $quiz_result
      ->maintainResults();
    $add = $quiz
      ->get('keep_results')
      ->getString() == QUIZ_KEEP_BEST && $results_got_deleted ? ' ' . t('Note that this @quiz is set to only keep each users best answer.', array(
      '@quiz' => _quiz_get_quiz_name(),
    )) : '';
    \Drupal::messenger()
      ->addMessage(t('The scoring data you provided has been saved.') . $add);
  }

  // Update the result.
  parent::submitForm($form, $form_state);
}