You are here

function quiz_questions_form_submit in Quiz 6.4

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.2 quiz.admin.inc \quiz_questions_form_submit()
  6. 6.3 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.

1 string reference to 'quiz_questions_form_submit'
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

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

Code

function quiz_questions_form_submit($form, &$form_state) {
  if (isset($form_state['#from_ahah'])) {
    return;
  }

  // Load the quiz node
  $quiz = node_load(intval(arg(1)));

  // Update the refresh latest quizzes table so that we know what the users latest quizzes are
  if (variable_get('quiz_auto_revisioning', 1)) {
    $is_new_revision = quiz_has_been_answered($quiz);
  }
  else {
    $is_new_revision = (bool) $form_state['values']['new_revision'];
  }
  _quiz_question_browser_submit($form, $form_state);
  $weight_map = $form_state['values']['weights'];
  $max_scores = $form_state['values']['max_scores'];
  $refreshes = isset($form_state['values']['revision']) ? $form_state['values']['revision'] : NULL;
  $stayers = $form_state['values']['stayers'];
  if (isset($form_state['values']['compulsories'])) {
    $compulsories = $form_state['values']['compulsories'];
  }
  $num_random = $form_state['values']['num_random_questions'];
  $quiz->max_score_for_random = isset($form_state['values']['max_score_for_random']) ? $form_state['values']['max_score_for_random'] : 1;
  $term_id = isset($form_state['values']['random_term_id']) ? $form_state['values']['random_term_id'] : 0;

  // Store what questions belong to the quiz
  $questions = _quiz_update_items($quiz, $weight_map, $max_scores, $is_new_revision, $refreshes, $stayers, $compulsories);

  // 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');
    }
  }
  $success = TRUE;

  // since we got this far
  if ($quiz->type == 'quiz') {

    // Update the quiz node properties.
    $sql = "UPDATE {quiz_node_properties} SET number_of_random_questions = %d, max_score_for_random = %d, tid = %d WHERE vid = %d AND nid = %d";
    $success = db_query($sql, $num_random, $quiz->max_score_for_random, $term_id, $quiz->vid, $quiz->nid);
    $sql = "UPDATE {quiz_node_properties} SET max_score = max_score_for_random  * number_of_random_questions + (\n              SELECT COALESCE(SUM(max_score), 0)\n              FROM {quiz_node_relationship}\n              WHERE parent_vid = %d AND question_status = %d\n            )\n            WHERE vid = %d";
    $success2 = db_query($sql, $quiz->vid, QUESTION_ALWAYS, $quiz->vid);
  }
  if ($success && $success2) {
    drupal_set_message(t('Questions updated successfully.'));
  }
  else {
    drupal_set_message(t('There was an error updating the @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )), 'error');
  }
}