You are here

public function MultichoiceQuestion::validateNode in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::validateNode()
  2. 6.x question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::validateNode()

Implementation of validateNode().

See also

QuizQuestion::validateNode()

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 288

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

public function validateNode(array &$form) {
  if ($this->node->choice_multi == 0) {
    $found_one_correct = FALSE;
    for ($i = 0; isset($this->node->alternatives[$i]) && is_array($this->node->alternatives[$i]); $i++) {
      $short = $this->node->alternatives[$i];
      if (drupal_strlen($this
        ->checkMarkup($i, 'answer')) < 1) {
        continue;
      }
      if ($short['correct'] == 1) {
        if ($found_one_correct) {

          // We don't display an error message here since we allow
          // alternatives to be partially correct.
        }
        else {
          $found_one_correct = TRUE;
        }
      }
    }
    if (!$found_one_correct) {
      form_set_error('choice_multi', t('You have not marked any alternatives as correct. If there are no correct alternatives you should allow multiple answers.'));
    }
  }
  else {
    for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
      $short = $this->node->alternatives[$i];
      if (strlen($this
        ->checkMarkup($i, 'answer')) < 1) {
        continue;
      }
      if ($short['score_if_chosen'] < $short['score_if_not_chosen'] && $short['correct']) {
        form_set_error("alternatives][{$i}][score_if_not_chosen", t("The alternative is marked as correct, but gives more points if you don't select it."));
      }
      elseif ($short['score_if_chosen'] > $short['score_if_not_chosen'] && !$short['correct']) {
        form_set_error("alternatives][{$i}][score_if_chosen", t('The alternative is not marked as correct, but gives more points if you select it.'));
      }
    }
  }
}