You are here

function quiz_options_form_validate in Quiz 8.4

Page callback for quiz options validate

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

File

./quiz.pages.inc, line 381
Page callback file for the quiz module.

Code

function quiz_options_form_validate($form, &$form_state) {
  $node = new \stdClass();
  $node->quiz_always = $form_state['values']['availability']['quiz_always'];
  $node->quiz_open = $form_state['values']['availability']['quiz_open'];

  //TODO: Need to verify
  $node->quiz_close = $form_state['values']['availability']['quiz_close'];

  //TODO: Need to verify
  $node->pass_rate = $form_state['values']['pass_fail']['pass_rate'];
  $node->resultoptions = $form_state['values']['resultoptions'];
  $node->allow_skipping = $form_state['values']['taking']['allow_skipping'];
  $node->allow_jumping = $form_state['values']['taking']['allow_jumping'];
  if (isset($form_state['values']['taking']['addons']['time_limit'])) {
    $node->time_limit = $form_state['values']['taking']['addons']['time_limit'];
  }
  $quiz_open = explode(' ', $node->quiz_open);
  $quiz_close = explode(' ', $node->quiz_close);

  // Don't check dates if the quiz is always available.
  if (!$node->quiz_always && count($quiz_open) == 3 && count($quiz_close) == 3) {
    if (mktime(0, 0, 0, $quiz_open[1], $quiz_open[0], $quiz_open[2]) > mktime(0, 0, 0, $quiz_close[1], $quiz_close[0], $quiz_close[2])) {
      form_set_error('quiz_close', $form_state, t('Please make sure the close date is after the open date.'));
    }
  }
  if (!empty($node->pass_rate)) {
    if (!_quiz_is_int($node->pass_rate, 0, 100)) {
      form_set_error('pass_rate', $form_state, t('The pass rate value 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', $form_state, t('Time limit must be a non negative interger'));
    }
  }
  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', $form_state, 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 (!_quiz_is_int($option[$bound], 0, 100)) {
              form_set_error($bound, $form_state, 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', $form_state, 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', $form_state, 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', $form_state, t('Option has a summary, but no name.'));
      }
    }
  }
  if ($node->allow_jumping && !$node->allow_skipping) {
    form_set_error('allow_skipping', $form_state, t('If jumping is allowed skipping also has to be allowed.'));
  }
}