You are here

function quiz_validate in Quiz 7.6

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

Implements hook_validate().

1 call to quiz_validate()
quiz_admin_node_form_validate in ./quiz.admin.inc
Validation function for the quiz_admin_node_form form

File

./quiz.module, line 1416
quiz.module Main file for the Quiz module.

Code

function quiz_validate($node) {

  // Don't check dates if the quiz is always available.
  if (!$node->quiz_always) {
    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('"Close date" must be later than the "open date".'));
    }
  }
  if (!empty($node->pass_rate)) {
    if (!_quiz_is_int($node->pass_rate, 0, 100)) {
      form_set_error('pass_rate', t('"Passing rate" must be a number between 0 and 100.'));
    }
  }
  if (isset($node->time_limit)) {
    if (!_quiz_is_int($node->time_limit, 0)) {
      form_set_error('time_limit', t('"Time limit" must be a positive number.'));
    }
  }
  if (isset($node->resultoptions) && count($node->resultoptions) > 0) {
    $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('Range 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 (!_quiz_is_int($option[$bound], 0, 100)) {
              form_set_error($bound, t('The range %start value must be a number between 0 and 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 (!_quiz_is_empty_html($option['option_summary']['value'])) {
        form_set_error('option_summary', t('Range has a summary, but no name.'));
      }
    }
  }
  if ($node->allow_jumping && !$node->allow_skipping) {

    // @todo when we have pages of questions, we have to check that jumping is
    // not enabled, and randomization is not enabled unless there is only 1 page
    form_set_error('allow_skipping', t('If jumping is allowed, skipping must also be allowed.'));
  }
}