You are here

function quizfileupload_score_an_answer in Quiz File Upload 7.5

Same name and namespace in other branches
  1. 7.4 quizfileupload.module \quizfileupload_score_an_answer()

Set a score for a quizfileupload question.

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

1 call to quizfileupload_score_an_answer()
quizfileupload_report_submit in ./quizfileupload.module
Submit the result report for quizfileupload.

File

./quizfileupload.module, line 116
Quizfileupload question type for the Quiz module.

Code

function quizfileupload_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_update('quiz_fileupload_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'],
  ))
    ->condition('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);
  }
}