You are here

public function MultichoiceQuestion::validateNode in Quiz 8.4

Implementation of validate

QuizQuestion#validate()

Overrides QuizQuestion::validateNode

File

question_types/multichoice/lib/Drupal/multichoice/MultichoiceQuestion.php, line 260
The main classes for the multichoice question type.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Namespace

Drupal\multichoice

Code

public function validateNode(array &$form_state) {
  if ($form_state['values']['choice_multi'] == 0) {
    $found_one_correct = FALSE;
    for ($i = 0; isset($form_state['values']['alternatives'][$i]) && is_array($form_state['values']['alternatives'][$i]); $i++) {
      $short = $form_state['values']['alternatives'][$i];
      if (drupal_strlen($this
        ->checkMarkup($form_state, $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', $form_state, 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($form_state['values']['alternatives'][$i]); $i++) {
      $short = $form_state['values']['alternatives'][$i];
      if (strlen($this
        ->checkMarkup($form_state, $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", $form_state, 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", $form_state, t('The alternative is not marked as correct, but gives more points if you select it.'));
      }
    }
  }
}