You are here

function quiz_questions_form_submit in Quiz 6.6

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \quiz_questions_form_submit()
  2. 5.2 quiz.module \quiz_questions_form_submit()
  3. 5 quiz.module \quiz_questions_form_submit()
  4. 6.2 quiz.admin.inc \quiz_questions_form_submit()
  5. 6.3 quiz.admin.inc \quiz_questions_form_submit()
  6. 6.4 quiz.admin.inc \quiz_questions_form_submit()
  7. 6.5 quiz.admin.inc \quiz_questions_form_submit()
  8. 7.6 quiz.admin.inc \quiz_questions_form_submit()
  9. 7 quiz.admin.inc \quiz_questions_form_submit()
  10. 7.4 quiz.admin.inc \quiz_questions_form_submit()
  11. 7.5 quiz.admin.inc \quiz_questions_form_submit()

Submit function for quiz_questions.

Updates from the "manage questions" tab.

Parameters

$form_id: A string containing the form id.

$values: Array containing the form values.

2 string references to 'quiz_questions_form_submit'
qcollection_items_form in includes/qcollection/qcollection.inc
Handles "manage questions" tab.
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

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

Code

function quiz_questions_form_submit($form, &$form_state) {

  // This is ugly and should be fixed.
  $quiz = node_load(arg(1));
  $is_new_revision = (bool) $form_state['values']['new_revision'];
  $removed = explode(',', $form_state['values']['remove_from_quiz']);
  $weight_map = $form_state['values']['weights'];
  $questions = _quiz_update_items($quiz, $weight_map, $removed, $is_new_revision);
  $num_random = $form_state['values']['number_of_random_questions'];
  $term_id = isset($form_state['values']['random_term_id']) ? $form_state['values']['random_term_id'] : 0;

  // If using random questions and no term ID is specified, make sure we have enough.
  if (empty($term_id)) {
    $assigned_random = 0;
    foreach ($questions as $question) {
      if ($question->state == QUESTION_RANDOM) {
        ++$assigned_random;
      }
    }

    // Adjust number of random questions downward to match number of selected questions..
    if ($num_random > $assigned_random) {
      $num_random = $assigned_random;
      drupal_set_message(t('The number of random questions for this @quiz have been lowered to %anum to match the number of questions you assigned.', array(
        '@quiz' => QUIZ_NAME,
        '%anum' => $assigned_random,
      ), 'warning'));
    }
  }
  else {

    // Warn user if not enough questions available with this term_id.
    $available_random = count(_quiz_get_random_taxonomy_question_ids($term_id, $num_random));
    if ($num_random > $available_random) {
      $num_random = $available_random;
      drupal_set_message(t('There are currently not enough questions assigned to this term (@random). Please lower the number of random quetions or assign more questions to this taxonomy term before taking this @quiz.', array(
        '@random' => $available_random,
        '@quiz' => QUIZ_NAME,
      )), 'error');
    }
  }

  // Update the quiz properties.
  $sql = "UPDATE {quiz_node_properties} SET number_of_random_questions = %d, tid = %d WHERE vid = %d AND nid = %d";
  if (db_query($sql, $num_random, $term_id, $quiz->vid, $quiz->nid) == FALSE) {
    drupal_set_message(t('There was an error updating the @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )), 'error');
  }
  else {
    drupal_set_message(t('Questions updated successfully.'));
  }
}