You are here

function multichoice_validate in Quiz 6.5

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

Implementation of hook_validate().

File

question_types/multichoice/multichoice.module, line 374
Multiple choice question type for the Quiz module.

Code

function multichoice_validate($node, &$form) {

  // 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 = user_access('allow multiple correct answers') ? 0 : 1;
  if ($node->scored_quiz) {
    foreach ($node->answers as $key => $answer) {
      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 that feedback is present without an answer.
        if (trim($answer['feedback']) != '') {
          form_set_error("answers][{$key}][feedback", t('Feedback is given without an answer.'));
        }

        // Warn about marking correct without an answer present.
        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).'));
    }

    // We want to allow multi-answer even if there is only one answer.
    if ($node->multiple_answers && $corrects < 1) {
      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.'));
    }
    elseif ($answers < 2) {
      form_set_error("answers][1]['answer'", t('Must have at least two answers.'));
    }
  }
  else {

    // Unscored quiz.
    foreach ($node->answers as $key => $answer) {
      if ($answer['answer'] && !$answer['result_option']) {
        form_set_error('answers][$key][result_option', t('You must select an association for each answer'));
      }
    }
  }

  // Validate number of answers.
  if (!user_access('allow any number of answers')) {

    // Users must create questions with exactly a certain number of answers.
    $required = variable_get('multichoice_default_answers', 5);
    if ($answers != $required) {
      form_set_error("answers[0]['answer'", t('You must have exactly @num answer choices. There are currently @answers answer choices.', array(
        '@num' => $required,
        '@answers' => $answers,
      )));
    }
  }
}