You are here

function quiz_store_question_result in Quiz 7

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.4 quiz.module \quiz_store_question_result()
  7. 6.5 quiz.module \quiz_store_question_result()
  8. 7.6 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 2132
Quiz Module

Code

function quiz_store_question_result($quiz, $result, $options) {

  //drupal_set_message('<pre>'. print_r($result, 1) .'</pre>');
  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_query("SELECT (max_score / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = :nid AND vid = :vid\n                )) as scale\n                FROM {quiz_node_relationship}\n                WHERE parent_nid = :parent_nid\n                AND parent_vid = :parent_vid\n                AND child_nid = :child_nid\n                AND child_vid = :child_vid\n               ", array(
      ':nid' => $result->nid,
      ':vid' => $result->vid,
      ':parent_nid' => $quiz->nid,
      ':parent_vid' => $quiz->vid,
      ':child_nid' => $result->nid,
      ':child_vid' => $result->vid,
    ))
      ->fetchField();
  }
  elseif ($quiz->randomization == 2) {
    $scale = db_query("SELECT (max_score_for_random / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = :nid AND vid = :vid\n                )) as scale\n                FROM {quiz_node_properties}\n                WHERE vid = :vid\n               ", array(
      ':nid' => $result->nid,
      ':vid' => $result->vid,
      ':vid' => $quiz->vid,
    ))
      ->fetchField();
  }
  elseif ($quiz->randomization == 3) {
    if (isset($options['question_data']['tid'])) {
      $result->tid = $options['question_data']['tid'];
    }
    $scale = db_query("SELECT (max_score / (\n                  SELECT max_score\n                  FROM {quiz_question_properties}\n                  WHERE nid = :nid AND vid = :vid\n                )) as scale\n                FROM {quiz_terms}\n                WHERE vid = :vid\n                AND tid = :tid\n               ", array(
      ':nid' => $result->nid,
      ':vid' => $result->vid,
      ':vid' => $quiz->vid,
      ':tid' => $result->tid,
    ))
      ->fetchField();
  }
  $points = round($result->score * $scale);

  // Insert result data, or update existing data.
  $rid_count = db_query("SELECT COUNT('result_id') AS count\n              FROM {quiz_node_results_answers}\n              WHERE question_nid = :question_nid\n              AND question_vid = :question_vid\n              AND result_id = :result_id", array(
    ':question_nid' => $result->nid,
    ':question_vid' => $result->vid,
    ':result_id' => $result->rid,
  ))
    ->fetchField();
  if ($rid_count > 0) {
    $update = db_update('quiz_node_results_answers')
      ->fields(array(
      'is_correct' => $result->is_correct,
      'points_awarded' => $points,
      'answer_timestamp' => REQUEST_TIME,
      'is_skipped' => (int) $result->is_skipped,
      'tid' => $quiz->randomization == 3 && $result->tid ? $result->tid : 0,
    ));

    //$sql = "UPDATE {quiz_node_results_answers}

    //        SET is_correct = %d, points_awarded = %d, answer_timestamp = %d, is_skipped = %d";
    $args = array(
      $result->is_correct,
      $points,
      REQUEST_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";
    $update
      ->condition('question_nid', $result->nid);
    $update
      ->condition('question_vid', $result->vid);
    $update
      ->condition('result_id', $result->rid);
    $update
      ->execute();

    //array_push($args, $result->nid, $result->vid, $result->rid);

    //db_query($sql, $args);
  }
  else {

    /*$sql = "INSERT INTO {quiz_node_results_answers}
              (question_nid, question_vid, result_id, is_correct, points_awarded,
              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, REQUEST_TIME, $result->is_skipped, $options['question_data']['number']);
      if ($quiz->randomization == 3) {
        $sql .= ", %d";
        $args[] = $result->tid;
      }
      $sql .= ')';
      db_query($sql, $args);
      */
    $insert = db_insert('quiz_node_results_answers')
      ->fields(array(
      'question_nid',
      'question_vid',
      'result_id',
      'is_correct',
      'points_awarded',
      'answer_timestamp',
      'is_skipped',
      'number',
      'tid',
    ))
      ->values(array(
      'question_nid' => $result->nid,
      'question_vid' => $result->vid,
      'result_id' => $result->rid,
      'is_correct' => (int) $result->is_correct,
      'points_awarded' => $points,
      'answer_timestamp' => REQUEST_TIME,
      'is_skipped' => (int) $result->is_skipped,
      'number' => $options['question_data']['number'],
      'tid' => $quiz->randomization == 3 && $result->tid ? $result->tid : 0,
    ))
      ->execute();
  }
}