You are here

function long_answer_score_an_answer in Quiz 7

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. 6.5 question_types/long_answer/long_answer.module \long_answer_score_an_answer()
  6. 7.6 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.


$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.

$update_total: Shall the total score be updated?

Return value

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

3 calls to long_answer_score_an_answer()
LongAnswerUnitTest::unitTestScoreAnswer in question_types/long_answer/long_answer.test
Test modifying (scoring) an answer.
long_answer_report_submit in question_types/long_answer/long_answer.module
Submit function for the report form
long_answer_score_form_submit in question_types/long_answer/
Submit handler for the long answer score form


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


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

  // 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 moduleretains a limited amount of scoring information.
  // 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
  $question_max_score = db_query('SELECT max_score FROM {quiz_question_properties} WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $nid,
    ':vid' => $vid,
  $quiz_max_score = db_query('SELECT max_score FROM {quiz_node_relationship} WHERE parent_vid = :pvid AND child_vid = :cvid', array(
    ':pvid' => $quiz->vid,
    ':cvid' => $vid,
  $changed = db_update('quiz_long_answer_user_answers')
    'score' => $question_max_score / $quiz_max_score,
    'is_evaluated' => 1,
    ->condition('question_nid', $nid)
    ->condition('question_vid', $vid)
    ->condition('result_id', $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_query('SELECT max_score FROM {quiz_question_properties} WHERE vid = :vid', array(
      ':vid' => $vid,
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    else {
      $is_correct = $score == $max ? 1 : 0;
      $points_awarded = $score;
      'points_awarded' => $points_awarded,
      'is_correct' => $is_correct,
      ->condition('question_vid', $vid)
      ->condition('result_id', $rid)

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