You are here

function short_answer_score_an_answer in Quiz 7.4

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

Set a score for a short answer question.

This stores a score for a short 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.

$update_total: Shall the total score for a quiz be updated?

Return value

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

2 calls to short_answer_score_an_answer()
short_answer_report_submit in question_types/short_answer/short_answer.module
Submit the result report for short answer
short_answer_score_form_submit in question_types/short_answer/short_answer.admin.inc
Submit the score form

File

question_types/short_answer/short_answer.module, line 152
The main file for short_answer.

Code

function short_answer_score_an_answer($values, $update_total = TRUE) {
  extract($values);

  // When we set the score we make sure that the max score in the quiz the question belongs to is considered
  $question_max_score = db_query('SELECT max_score FROM {quiz_question_properties} WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $nid,
    ':vid' => $vid,
  ))
    ->FetchField();
  $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,
  ))
    ->fetchField();
  $db_update_fields = array(
    'score' => $score * $question_max_score / $quiz_max_score,
    'is_evaluated' => 1,
  );
  if (isset($answer_feedback)) {
    $db_update_fields['answer_feedback'] = empty($answer_feedback['value']) ? '' : $answer_feedback['value'];
    $db_update_fields['answer_feedback_format'] = $answer_feedback['format'];
  }
  $changed = db_update('quiz_short_answer_user_answers')
    ->fields($db_update_fields)
    ->condition('question_nid', $nid)
    ->condition('question_vid', $vid)
    ->condition('result_id', $rid)
    ->execute();

  // Now the short answer user data has been updated. We also need to update the data in the quiz tables
  if ($changed > 0) {
    $max = db_query('SELECT max_score FROM {quiz_question_properties} WHERE vid = :vid', array(
      ':vid' => $vid,
    ))
      ->fetchField();
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score / $max > 0.5 ? 1 : 0;
      $points_awarded = $score;
    }
    db_update('quiz_node_results_answers')
      ->fields(array(
      'points_awarded' => $points_awarded,
      'is_correct' => $is_correct,
    ))
      ->condition('question_vid', $vid)
      ->condition('result_id', $rid)
      ->execute();

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