You are here

function short_answer_score_an_answer in Quiz 7.5

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

array $values: The FAPI $form_state['values'].

bool $update_total: Shall the total score be updated?

1 call to short_answer_score_an_answer()
short_answer_report_submit in question_types/short_answer/short_answer.module
Submit function for the report form.

File

question_types/short_answer/short_answer.module, line 110
Short_answer question type for the Quiz module.

Code

function short_answer_score_an_answer($values, $update_total = TRUE) {
  $nid = $values['nid'];
  $vid = $values['vid'];
  $result_id = $values['result_id'];
  $score = $values['score'];
  $answer_feedback = $values['answer_feedback'];
  $quiz = $values['quiz'];
  $question_node = node_load($nid, $vid);
  $quiz_question_response = _quiz_question_response_get_instance($result_id, $question_node);
  $ratio = $quiz_question_response
    ->getWeightedRatio();
  db_merge('quiz_short_answer_user_answers')
    ->fields(array(
    'score' => !empty($score) && !empty($ratio) ? $score / $ratio : 0,
    'is_evaluated' => 1,
    'answer_feedback' => empty($answer_feedback['value']) ? '' : $answer_feedback['value'],
    'answer_feedback_format' => empty($answer_feedback['format']) ? '' : $answer_feedback['format'],
  ))
    ->key(array(
    'result_answer_id' => $quiz_question_response->result_answer_id,
  ))
    ->execute();

  // Now the user data has been updated. We also need to update the data in the
  // quiz tables.
  $quiz_result_answer = entity_load_single('quiz_result_answer', $quiz_question_response->result_answer_id);
  $quiz_result_answer->points_awarded = $score;
  $quiz_result_answer->is_correct = $quiz_question_response
    ->isCorrect();
  $quiz_result_answer
    ->save();

  // Third, we update the main quiz results table.
  if ($update_total) {
    quiz_update_total_score($quiz, $result_id);
  }
}