You are here

function long_answer_score_an_answer in Quiz 6.5

Same name and namespace in other branches
  1. 8.4 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  2. 6.6 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  3. 6.3 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  4. 6.4 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  5. 7.6 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  6. 7 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  7. 7.4 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  8. 7.5 question_types/long_answer/long_answer.module \long_answer_score_an_answer()

Set a score for a long answer question.

This stores a score for a long answer question and marks that question as having been evaluated. The function updates all of the necessary data sources so that the individual answer results should be reflected in the total scoring table.

Parameters

$quiz: Quiz node.

$nid: Node ID of question.

$vid: Version ID of question.

$rid: Result ID for the quiz results.

$score: The numeric score to assign the result.

Return value

int Number of scores adjusted. If a change was made, this should be 1.

2 calls to long_answer_score_an_answer()
LongAnswerUnitTest::unitTestScoreAnswer in question_types/long_answer/long_answer.test
Test modifying (scoring) an answer.
long_answer_score_form_submit in question_types/long_answer/long_answer.admin.inc

File

question_types/long_answer/long_answer.module, line 130
This module defines a long answer question type for quizzes.

Code

function long_answer_score_an_answer($quiz, $nid, $vid, $rid, $score) {

  // Quiz scoring information is spread out across three tables:
  // 1. The module should retain its own scoring information in any case where scoring is non-trivial.
  // 2. The Quiz module (needlessly?) retains a limited amount of scoring information. This appears to be used only
  //    infrequently.
  // 3. The Quiz module retains an overall score for a quiz. This is the percentage score for the combination of all
  //    questions on the quiz.
  //
  // We update all three.
  // First, we update the long answer table
  db_query("UPDATE {quiz_long_answer_user_answers} SET score = %d, is_evaluated = 1 WHERE question_nid = %d AND question_vid = %d AND result_id = %d", $score, $nid, $vid, $rid);
  $changed = db_affected_rows();
  if ($changed > 0) {

    // Second, we update the main quiz answers table
    // What do we do about the quiz_node_results_answers table? It assumes strict
    // bivalence (is_correct). I guess we consider any essay with over 50% to be correct?
    $max = db_result(db_query('SELECT maximum_score FROM {quiz_long_answer_node_properties} WHERE vid = %d', $vid));
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score * 100 / $max > 50 ? 1 : 0;
      $points_awarded = $score;
    }
    $sql = 'UPDATE {quiz_node_results_answers}
      SET points_awarded = %d, is_correct = %d
      WHERE question_vid = %d AND result_id = %d';
    db_query($sql, $points_awarded, $is_correct, $vid, $rid);

    // Third, we update the main quiz results table
    quiz_update_total_score($quiz, $rid);
  }
  return $changed;
}