You are here

function quiz_calculate_score in Quiz 5

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

Calculates the score user received on quiz

Parameters

$rid: Quiz result ID

Return value

array Contains three elements: question_count, num_correct and percentage_score

4 calls to quiz_calculate_score()
quiz_admin_results in ./quiz.module
quiz_start_actions in ./quiz.module
Actions to take place at the start of a quiz
quiz_take_quiz in ./quiz.module
Handles quiz taking
quiz_user_results in ./quiz.module

File

./quiz.module, line 812
Quiz Module

Code

function quiz_calculate_score($rid) {

  // initialize our variables
  $question_count = 0;
  $num_correct = 0;
  $percentage_score = 0;

  // Get the all answers from the database
  $result = db_query("SELECT\n                       qqr.answer answer,\n                       qqr.question_nid qnid,\n                       n.type type\n                     FROM {quiz_question_results} qqr, {node} n\n                     WHERE qqr.result_rid = %d AND n.nid = qqr.question_nid", $rid);
  while ($r = db_fetch_array($result)) {
    $question_count++;
    $r['answer'] = unserialize($r['answer']);
    $s = module_invoke($r['type'], 'calculate_result', $r['answer']['answers'], $r['answer']['tried']);
    $num_correct += $s;
    $r['score'] = $s;

    // I think this is legacy
  }

  // calculate the percentage score
  if ($question_count > 0) {
    $percentage_score = round($num_correct * 100 / $question_count);
  }

  // build the score array
  $score = array(
    'question_count' => $question_count,
    'num_correct' => $num_correct,
    'percentage_score' => $percentage_score,
  );

  // return the array
  return $score;
}