You are here

public static function QuizQuestionEntityTrait::getAnsweringFormValidate in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/Entity/QuizQuestionEntityTrait.php \Drupal\quiz\Entity\QuizQuestionEntityTrait::getAnsweringFormValidate()
  2. 6.x src/Entity/QuizQuestionEntityTrait.php \Drupal\quiz\Entity\QuizQuestionEntityTrait::getAnsweringFormValidate()

Validate a user's answer.

Parameters

array $element: The form element of this question.

mixed $form_state: Form state.

3 calls to QuizQuestionEntityTrait::getAnsweringFormValidate()
LongAnswerQuestion::getAnsweringFormValidate in question_types/quiz_long_answer/src/Plugin/quiz/QuizQuestion/LongAnswerQuestion.php
Validate a user's answer.
MultichoiceQuestion::getAnsweringFormValidate in question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php
Validate a user's answer.
TrueFalseQuestion::getAnsweringFormValidate in question_types/quiz_truefalse/src/Plugin/quiz/QuizQuestion/TrueFalseQuestion.php
Validate a user's answer.
5 methods override QuizQuestionEntityTrait::getAnsweringFormValidate()
LongAnswerQuestion::getAnsweringFormValidate in question_types/quiz_long_answer/src/Plugin/quiz/QuizQuestion/LongAnswerQuestion.php
Validate a user's answer.
MatchingQuestion::getAnsweringFormValidate in question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingQuestion.php
Question response validator.
MultichoiceQuestion::getAnsweringFormValidate in question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php
Validate a user's answer.
ShortAnswerQuestion::getAnsweringFormValidate in question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerQuestion.php
Validate a user's answer.
TrueFalseQuestion::getAnsweringFormValidate in question_types/quiz_truefalse/src/Plugin/quiz/QuizQuestion/TrueFalseQuestion.php
Validate a user's answer.

File

src/Entity/QuizQuestionEntityTrait.php, line 227

Class

QuizQuestionEntityTrait
A trait all Quiz question strongly typed entity bundles must use.

Namespace

Drupal\quiz\Entity

Code

public static function getAnsweringFormValidate(array &$element, FormStateInterface $form_state) {
  $quiz = \Drupal::entityTypeManager()
    ->getStorage('quiz')
    ->loadRevision($form_state
    ->getCompleteForm()['#quiz']['vid']
    ->getString());
  $qqid = $element['#array_parents'][1];

  // There was an answer submitted.

  /* @var $qra QuizResultAnswer */
  $qra = $element['#quiz_result_answer'];

  // Temporarily score the answer.
  $score = $qra
    ->score($form_state
    ->getValue('question')[$qqid]);

  // @todo kinda hacky here, we have to scale it temporarily so isCorrect()
  // works
  $qra
    ->set('points_awarded', $qra
    ->getWeightedRatio() * $score);
  if ($quiz
    ->get('repeat_until_correct')
    ->getString() && !$qra
    ->isCorrect() && $qra
    ->isEvaluated()) {
    $form_state
      ->setErrorByName('', t('The answer was incorrect. Please try again.'));

    // Show feedback after incorrect answer.
    $view_builder = Drupal::entityTypeManager()
      ->getViewBuilder('quiz_result_answer');
    $element['feedback'] = $view_builder
      ->view($qra);
    $element['feedback']['#weight'] = 100;
    $element['feedback']['#parents'] = [];
  }
}