You are here

function quizfileupload_score_an_answer in Quiz File Upload 7.4

Same name and namespace in other branches
  1. 7.5 quizfileupload.module \quizfileupload_score_an_answer()
1 call to quizfileupload_score_an_answer()
quizfileupload_report_submit in ./quizfileupload.module
Submit the result report for short answer

File

./quizfileupload.module, line 101
The main file for quizfileupload.

Code

function quizfileupload_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_fileupload_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;
}