You are here

function quiz_questions_form_submit in Quiz 6.2

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.6 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.

File

./quiz.admin.inc, line 193
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));
  $new_revision = $form_state['values']['new_revision'];

  // Update quiz with selected question options.
  if (!quiz_update_questions($quiz, $form_state['values']['question_status'], $new_revision)) {
    form_set_error('', t('Either no questions were selected, or there was a problem updating your @quiz. Please try again.', array(
      '@quiz' => QUIZ_NAME,
    )));
    return;
  }

  // Check if selecting random question from pool, and not via term.
  if (empty($form_state['values']['random_term_id'])) {
    $assigned_random = 0;
    if (is_array($form_state['values']['question_status'])) {
      foreach ($form_state['values']['question_status'] as $id => $status) {
        if (QUESTION_RANDOM == $status) {
          $assigned_random++;
        }
      }
    }
    if ($form_state['values']['number_of_random_questions'] > $assigned_random) {
      $form_state['values']['number_of_random_questions'] = $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_questions($form_state['values']['number_of_random_questions'], $form_state['values']['random_term_id']));
    $available_random = count(_quiz_get_random_taxonomy_question_ids($form_state['values']['random_term_id'], $form_state['values']['number_of_random_questions']));
    if ($form_state['values']['number_of_random_questions'] > $available_random) {
      $form_state['values']['number_of_random_questions'] = $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');
    }
  }
  $result = db_query("UPDATE {quiz_node_properties} SET number_of_random_questions = %d, tid = %d WHERE vid = %d AND nid = %d", $form_state['values']['number_of_random_questions'], $form_state['values']['random_term_id'], $quiz->vid, $quiz->nid);
  if (!$result) {
    drupal_set_message(t('There was an error updating the @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )), 'error');
  }
  else {
    drupal_set_message(t('Questions updated successfully.'));
  }
}