You are here

function quiz_questions_form_validate in Quiz 7

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \quiz_questions_form_validate()
  2. 6.6 quiz.admin.inc \quiz_questions_form_validate()
  3. 6.3 quiz.admin.inc \quiz_questions_form_validate()
  4. 6.4 quiz.admin.inc \quiz_questions_form_validate()
  5. 6.5 quiz.admin.inc \quiz_questions_form_validate()
  6. 7.6 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 1510
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', t('This content has been modified by another user, changes cannot be saved.'));
    }
  }
  else {
    form_set_error('changed', t('A critical error has occured. Please report error code 28 on the quiz project page.'));
    return;
  }
  $already_checked = array();
  $weight_map = $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', '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', 'The max score for random questions needs to be a positive number');
  }
  if (empty($weight_map)) {
    form_set_error('none', 'No questions were included.');
    return;
  }
  $question_types = array_keys(_quiz_get_question_types());

  //$placeholders = db_placeholders($question_types, 'text');

  //$sql = 'SELECT COUNT(nid) FROM {node} WHERE type IN (' . $placeholders . ') AND nid = %d';
  $questions_count = db_select('node', 'n')
    ->condition('type', array_keys(_quiz_get_question_types()), 'IN')
    ->countQuery()
    ->addTag('node_access')
    ->execute()
    ->fetchField();
  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
    $params = $question_types;

    // Copy array.
    $params[] = $nid;

    // TODO Please convert this statement to the D7 database API syntax.
    if ($questions_count == 0) {
      form_set_error('none', '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', '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}", t('Max score needs to be a positive number'));
    }
  }
}