You are here

function multichoice_validate in Quiz 5

Same name and namespace in other branches
  1. 5.2 multichoice.module \multichoice_validate()
  2. 6.6 question_types/multichoice/multichoice.module \multichoice_validate()
  3. 6.2 multichoice.module \multichoice_validate()
  4. 6.3 question_types/multichoice/multichoice.module \multichoice_validate()
  5. 6.5 question_types/multichoice/multichoice.module \multichoice_validate()

Implementation of hook_validate().

File

./multichoice.module, line 165
Multiple choice question type for quiz module

Code

function multichoice_validate(&$node) {

  // Hard-code questions to have no teaser and to not be promoted to front page
  $node->teaser = 0;
  $node->promote = 0;
  if (!$node->nid && empty($_POST)) {
    return;
  }

  // Validate body
  if (!$node->body) {
    form_set_error('body', t('Question text is empty'));
  }

  // Validate answers
  $answers = 0;
  $corrects = 0;
  while (list($key, $answer) = each($node->answers)) {
    if (trim($answer['answer']) != '') {
      $answers++;
      if ($answer['correct'] > 0) {
        if ($corrects && !$node->multiple_answers) {
          form_set_error('multiple_answers', t('Single choice yet multiple correct answers are present'));
        }
        $corrects++;
      }
    }
    else {

      // warn feedback is present without an answer
      if (trim($answer['feedback']) != '') {
        form_set_error("answers][{$key}][feedback", t('Feedback is given without an answer.'));
      }

      // warn marking correct without answer
      if ($answer['correct'] > 0) {
        form_set_error("answers][{$key}][correct", t('Empty answer marked as correct choice.'));
      }
    }
  }
  if (!$corrects) {
    form_set_error("answers][0]['correct'", t('No correct choice(s).'));
  }
  if ($node->multiple_answers && $corrects < 2) {
    form_set_error('multiple_answers', t('Multiple answers selected, but only %count correct answer(s) present.', array(
      '%count' => $corrects,
    )));
  }
  if (!$answers) {
    form_set_error("answers][0]['answer'", t('No answers.'));
  }
  if ($answers < 2) {
    form_set_error("answers][1]['answer'", t('Must have at least two answers.'));
  }
}