You are here

function quiz_validate in Quiz 6.6

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_validate()
  2. 5 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 798
Quiz Module

Code

function quiz_validate($node) {
  if (!$node->nid && empty($_POST)) {
    return;
  }
  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%.'));
  }
  if (isset($node->time_limit)) {
    if ($node->time_limit < 0 || !is_numeric($node->time_limit)) {
      form_set_error('time_limit', t('Time limit must be a non negative interger '));
    }
  }
  $taken_values = array();
  $num_options = 0;
  foreach ($node->resultoptions as $option) {
    if (!empty($option['option_name'])) {
      $num_options++;
      if (empty($option['option_summary'])) {
        form_set_error('option_summary', t('Option has no summary text.'));
      }
      if ($node->pass_rate && (isset($option['option_start']) || isset($option['option_end']))) {

        // Check for a number between 0-100.
        foreach (array(
          'option_start' => 'start',
          'option_end' => 'end',
        ) as $bound => $bound_text) {
          if (!is_numeric($option[$bound])) {
            form_set_error($bound, t('The range %start value must be a number between 0% and 100%.', array(
              '%start' => $bound_text,
            )));
          }
          if ($option[$bound] < 0) {
            form_set_error($bound, t('The range %start value must not be less than 0%.', array(
              '%start' => $bound_text,
            )));
          }
          if ($option[$bound] > 100) {
            form_set_error($bound, t('The range %start value must not be more than 100%.', array(
              '%start' => $bound_text,
            )));
          }
        }

        // Check that range end >= start.
        if ($option['option_start'] > $option['option_end']) {
          form_set_error('option_start', t('The start must be less than the end of the range.'));
        }

        // Check that range doesn't collide with any other range.
        $option_range = range($option['option_start'], $option['option_end']);
        if ($intersect = array_intersect($taken_values, $option_range)) {
          form_set_error('option_start', t('The ranges must not overlap each other. (%intersect)', array(
            '%intersect' => implode(',', $intersect),
          )));
        }
        else {
          $taken_values = array_merge($taken_values, $option_range);
        }
      }
    }
    elseif (!empty($option['option_summary'])) {
      form_set_error('option_summary', t('Option has a summary, but no name.'));
    }
  }
  if ($node->pass_rate == 0 && !$num_options) {
    form_set_error('pass_rate', t('Unscored quiz, but no result options defined.'));
  }
}