You are here

function quiz_questions_form_validate in Quiz 8.4

Same name and namespace in other branches
  1. 6.6 quiz.admin.inc \quiz_questions_form_validate()
  2. 6.3 quiz.admin.inc \quiz_questions_form_validate()
  3. 6.4 quiz.admin.inc \quiz_questions_form_validate()
  4. 6.5 quiz.admin.inc \quiz_questions_form_validate()
  5. 7.6 quiz.admin.inc \quiz_questions_form_validate()
  6. 7 quiz.admin.inc \quiz_questions_form_validate()
  7. 7.4 quiz.admin.inc \quiz_questions_form_validate()
  8. 7.5 quiz.admin.inc \quiz_questions_form_validate()

Validate that the supplied questions are real.

File

./quiz.admin.inc, line 1204
Administrator interface for Quiz module.

Code

function quiz_questions_form_validate($form, &$form_state) {
  if (_quiz_is_int(arg(1))) {
    if (node_last_changed(intval(arg(1))) > $form_state['values']['timestamp']) {
      form_set_error('changed', $form_state, t('This content has been modified by another user, changes cannot be saved.'));
    }
  }
  else {
    form_set_error('changed', $form_state, t('A critical error has occured. Please report error code 28 on the quiz project page.'));
    return;
  }
  $already_checked = array();
  $weight_map = isset($form_state['values']['weights']) ? $form_state['values']['weights'] : '';

  // Make sure the number of random questions is a positive number
  if (isset($form_state['values']['num_random_questions']) && !_quiz_is_int($form_state['values']['num_random_questions'], 0)) {
    form_set_error('num_random_questions', $form_state, 'The number of random questions needs to be a positive number');
  }

  // Make sure the max score for random questions is a positive number
  if (isset($form_state['values']['max_score_for_random']) && !_quiz_is_int($form_state['values']['max_score_for_random'], 0)) {
    form_set_error('max_score_for_random', $form_state, 'The max score for random questions needs to be a positive number');
  }
  if (empty($weight_map)) {
    form_set_error('none', $form_state, 'No questions were included.');
    return;
  }
  $question_types = array_keys(_quiz_get_question_types());
  foreach ($weight_map as $id => $weight) {
    if ($form_state['values']['stayers'][$id] == 0) {
      continue;
    }

    // The question isn't to be added...
    list($nid, $vid) = explode('-', $id, 2);

    // If a node isn't one of the questionstypes we remove it from the question list
    $has_questions = (bool) db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('type', $question_types, 'IN')
      ->addTag('node_access')
      ->condition('n.nid', $nid)
      ->execute()
      ->fetchField();
    if (!$has_questions) {
      form_set_error('none', $form_state, 'One of the supplied questions was invalid. It has been removed from the quiz.');
      unset($form_state['values']['weights'][$id]);
    }
    elseif (in_array($nid, $already_checked)) {
      form_set_error('none', $form_state, 'A duplicate question has been removed. You can only ask a question once per quiz.');
      unset($form_state['values']['weights'][$id]);
    }
    else {
      $already_checked[] = $nid;
    }
  }

  // We make sure max score is a positive number
  $max_scores = $form_state['values']['max_scores'];
  foreach ($max_scores as $id => $max_score) {
    if ($form_state['values']['stayers'][$id] == 0) {
      continue;
    }
    if (!_quiz_is_int($max_score, 0)) {
      form_set_error("max_scores][{$id}", $form_state, t('Max score needs to be a positive number'));
    }
  }
}