You are here

function quiz_validate in Quiz 5

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_validate()
  2. 6.6 quiz.module \quiz_validate()
  3. 6.2 quiz.module \quiz_validate()
  4. 6.3 quiz.module \quiz_validate()
  5. 6.4 quiz.module \quiz_validate()
  6. 6.5 quiz.module \quiz_validate()
  7. 7.6 quiz.module \quiz_validate()
  8. 7 quiz.module \quiz_validate()
  9. 7.4 quiz.module \quiz_validate()
  10. 7.5 quiz.module \quiz_validate()

Implementation of hook_validate().

File

./quiz.module, line 353
Quiz Module

Code

function quiz_validate($node) {
  if (!$node->nid && empty($_POST)) {
    return;
  }

  // validate the number of questions against the actual questions assigned to this quiz
  if ($node->number_of_questions < 1) {
    form_set_error('number_of_questions', t('Number of questions must be a positive number.'));
  }
  else {
    if ($node->nid) {

      // get the number of each kind of question
      $anum_random = quiz_get_num_questions($node->nid, QUESTION_RANDOM);
      $anum_always = quiz_get_num_questions($node->nid, QUESTION_ALWAYS);
      $anum_total = $anum_always + $anum_random;

      // If we have random number, add one to the low range.
      if ($anum_random > 0) {
        $anum_always++;
      }

      // If we have more than one random number, lower the high range by one.
      if ($anum_random > 1) {
        $anum_total--;
      }

      // format the valid range
      if ($anum_always != $anum_total) {
        $range = t('between %low and %high', array(
          '%low' => $anum_always,
          '%high' => $anum_total,
        ));
      }
      else {
        $range = $anum_total;
      }

      // If there are not enough questions to support this number.
      if ($anum_total < $node->number_of_questions) {
        form_set_error('number_of_questions', t("You don't currently have enough questions assigned to this @quiz to support that many questions. Either change the number of questions to %range or !action to this @quiz.", array(
          '@quiz' => QUIZ_NAME,
          '%range' => $range,
          '!action' => l(t('add more questions'), 'node/' . $node->nid . '/questions'),
        )));
      }
      else {
        if ($anum_always > $node->number_of_questions) {
          form_set_error('number_of_questions', t('There are too many questions assigned to this @quiz to support that low of a number. Either change the number of questions to %range or !action from this @quiz.', array(
            '@quiz' => QUIZ_NAME,
            '%range' => $range,
            '!action' => l(t('remove some questions'), 'node/' . $node->nid . '/questions'),
          )));
        }
      }
    }
  }
  if (mktime(0, 0, 0, $node->quiz_open['month'], $node->quiz_open['day'], $node->quiz_open['year']) > mktime(0, 0, 0, $node->quiz_close['month'], $node->quiz_close['day'], $node->quiz_close['year'])) {
    form_set_error('quiz_close', t('Please make sure the close date is after the open date.'));
  }
  if (!is_numeric($node->pass_rate)) {
    form_set_error('pass_rate', t('The pass rate value must be a number between 0% and 100%.'));
  }
  if ($node->pass_rate > 100) {
    form_set_error('pass_rate', t('The pass rate value must not be more than 100%.'));
  }
  if ($node->pass_rate < 0) {
    form_set_error('pass_rate', t('The pass rate value must not be less than 0%.'));
  }
}