You are here

function quiz_store_question_result in Quiz 6.4

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

Store a quiz question result.

Parameters

$quiz: The quiz node

$result: Object with data about the result for a question.

$options: Array with options that affect the behavior of this function. ['set_msg'] - Sets a message if the last question was skipped.

Related topics

1 call to quiz_store_question_result()
quiz_take_quiz in ./quiz.module
Handles quiz taking.

File

./quiz.module, line 2021
Quiz Module

Code

function quiz_store_question_result($quiz, $result, $options) {
  if (isset($result->is_skipped) && $result->is_skipped == TRUE) {
    if ($options['set_msg']) {
      drupal_set_message(t('Last question skipped.'), 'status');
    }
    $result->is_correct = FALSE;
    $result->score = 0;
  }
  else {

    // Make sure this is set.
    $result->is_skipped = FALSE;
  }
  if (!isset($result->score)) {
    $result->score = $result->is_correct ? 1 : 0;
  }

  // Points are stored pre-scaled in the quiz_node_results_answers table. We get the scale.
  if ($quiz->randomization < 2) {
    $scale = db_result(db_query("SELECT (max_score / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = %d AND vid = %d\n                )) as scale\n                FROM {quiz_node_relationship}\n                WHERE parent_nid = %d\n                AND parent_vid = %d\n                AND child_nid = %d\n                AND child_vid = %d\n               ", $result->nid, $result->vid, $quiz->nid, $quiz->vid, $result->nid, $result->vid));
  }
  elseif ($quiz->randomization == 2) {
    $scale = db_result(db_query("SELECT (max_score_for_random / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = %d AND vid = %d\n                )) as scale\n                FROM {quiz_node_properties}\n                WHERE vid = %d\n               ", $result->nid, $result->vid, $quiz->vid));
  }
  elseif ($quiz->randomization == 3) {
    if (isset($options['question_data']['tid'])) {
      $result->tid = $options['question_data']['tid'];
    }
    $scale = db_result(db_query("SELECT (max_score / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = %d AND vid = %d\n                )) as scale\n                FROM {quiz_terms}\n                WHERE vid = %d\n                AND tid = %d\n               ", $result->nid, $result->vid, $quiz->vid, $result->tid));
  }
  $points = round($result->score * $scale);

  // Insert result data, or update existing data.
  $rid_count = db_result(db_query("SELECT COUNT('result_id') AS count\n              FROM {quiz_node_results_answers}\n              WHERE question_nid = %d\n              AND question_vid = %d\n              AND result_id = %d", $result->nid, $result->vid, $result->rid));
  if ($rid_count > 0) {
    $sql = "UPDATE {quiz_node_results_answers}\n            SET is_correct = %d, points_awarded = %d, answer_timestamp = %d, is_skipped = %d";
    $args = array(
      $result->is_correct,
      $points,
      time(),
      $result->is_skipped,
    );
    if ($quiz->randomization == 3) {
      $sql .= ", tid = %d";
      $args[] = $result->tid;
    }
    $sql .= " WHERE question_nid = %d AND question_vid = %d AND result_id = %d";
    array_push($args, $result->nid, $result->vid, $result->rid);
    db_query($sql, $args);
  }
  else {
    $sql = "INSERT INTO {quiz_node_results_answers}\n            (question_nid, question_vid, result_id, is_correct, points_awarded,\n            answer_timestamp, is_skipped, number";
    if ($quiz->randomization == 3) {
      $sql .= ", tid";
    }
    $sql .= ") VALUES (%d, %d, %d, %d, %d, %d, %d, %d";
    $args = array(
      $result->nid,
      $result->vid,
      $result->rid,
      $result->is_correct,
      $points,
      time(),
      $result->is_skipped,
      $options['question_data']['number'],
    );
    if ($quiz->randomization == 3) {
      $sql .= ", %d";
      $args[] = $result->tid;
    }
    $sql .= ')';
    db_query($sql, $args);
  }
}