You are here

function quiz_question_answering_form_submit in Quiz 7.6

Same name and namespace in other branches
  1. 7.5 question_types/quiz_question/quiz_question.module \quiz_question_answering_form_submit()

Submit handler for the question answering form.

There is no validation code here, but there may be feedback code for correct feedback.

File

question_types/quiz_question/quiz_question.module, line 829
Quiz Question module. This module provides the basic facilities for adding quiz question types to a quiz.

Code

function quiz_question_answering_form_submit(&$form, &$form_state) {
  $feedback_count = 0;
  $quiz_result = quiz_result_load($_SESSION['quiz'][arg(1)]['result_id']);
  $quiz = node_load($quiz_result->nid, $quiz_result->vid);
  $time_reached = $quiz->time_limit && REQUEST_TIME > $quiz_result->time_start + $quiz->time_limit;
  if ($time_reached) {

    // Too late.
    // @todo move to quiz_question_answering_form_validate(), and then put all
    // the "quiz end" logic in a sharable place. We just need to not fire the
    // logic that saves all the users answers.
    drupal_set_message(t('The last answer was not submitted, as the time ran out.'), 'error');
  }
  else {
    $questions = $quiz_result->layout;
    if (!empty($form_state['values']['question'])) {
      foreach ($form_state['values']['question'] as $nid => $response) {
        $answer = $response['answer'];
        foreach ($questions as $question) {
          if ($question['nid'] == $nid) {
            $question_array = $question;
            $current_question = node_load($question['nid'], $question['vid']);
          }
        }
        $qi_instance = _quiz_question_response_get_instance($_SESSION['quiz'][$quiz->nid]['result_id'], $current_question, $form_state['values']['question'][$current_question->nid]['answer']);
        $qi_instance
          ->delete();
        $qi_instance
          ->save();
        $result = $qi_instance
          ->toBareObject();
        $result->is_doubtful = !empty($response['is_doubtful']);
        quiz_store_question_result($quiz, $result, array(
          'set_msg' => TRUE,
          'question_data' => $question_array,
        ));

        // Increment the counter.
        quiz_question_goto($quiz, $_SESSION['quiz'][$quiz->nid]['current'] + 1);
        $feedback_count += $qi_instance->quizQuestion
          ->hasFeedback();
      }
    }
  }

  // Wat do?
  if (!empty($quiz->review_options['question']) && array_filter($quiz->review_options['question']) && $feedback_count) {

    // We have question feedback.
    $form_state['redirect'] = "node/{$quiz->nid}/take/" . ($_SESSION['quiz'][$quiz->nid]['current'] - 1) . '/feedback';
    $form_state['feedback'] = TRUE;
  }
  else {

    // No question feedback. Go to next question.
    $form_state['redirect'] = "node/{$quiz->nid}/take/" . $_SESSION['quiz'][$quiz->nid]['current'];
  }
  if ($time_reached || !isset($quiz_result->layout[$_SESSION['quiz'][$quiz->nid]['current']])) {

    // If this is the last question, finalize the quiz.
    quiz_question_answering_form_finalize($form, $form_state);
  }
}