You are here

function _quiz_question_response_get_instance in Quiz 7.6

Same name and namespace in other branches
  1. 8.6 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  2. 8.4 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  3. 8.5 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  4. 6.6 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  5. 6.3 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  6. 6.4 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  7. 6.5 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  8. 7 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  9. 7.4 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()
  10. 7.5 question_types/quiz_question/quiz_question.module \_quiz_question_response_get_instance()

Get an instance of a quiz question responce.

Get information about the class and use it to construct a new object of the appropriate type.

Parameters

$result_id: Result id

$question: The question node(not a QuizQuestion instance)

$answer: Resonce to the answering form.

$nid: Question node id

$vid: Question node version id

Return value

The appropriate QuizQuestionResponce extension instance

9 calls to _quiz_question_response_get_instance()
quiz_clone_quiz_result in ./quiz.module
Clone a result, and its correct answers. Do not finish.
quiz_end_scoring in ./quiz.module
Score a completed quiz.
quiz_question_answering_form_submit in question_types/quiz_question/quiz_question.module
Submit handler for the question answering form.
quiz_question_delete_result in question_types/quiz_question/quiz_question.module
Delete a question response by result and question.
quiz_question_element_validate in question_types/quiz_question/quiz_question.module
Element validator (for repeat until correct).

... See full list

File

question_types/quiz_question/quiz_question.module, line 648
Quiz Question module. This module provides the basic facilities for adding quiz question types to a quiz.

Code

function _quiz_question_response_get_instance($result_id, $question, $answer = NULL, $nid = NULL, $vid = NULL) {

  // We cache responses to improve performance
  static $quiz_responses = array();
  if (is_object($question) && isset($quiz_responses[$result_id][$question->vid])) {

    // We refresh the question node in case it has been changed since we cached the response
    $quiz_responses[$result_id][$question->vid]
      ->refreshQuestionNode($question);
    if ($quiz_responses[$result_id][$question->vid]->is_skipped !== FALSE) {
      return $quiz_responses[$result_id][$question->vid];
    }
  }
  elseif (isset($quiz_responses[$result_id][$vid])) {
    if ($quiz_responses[$result_id][$vid]->is_skipped !== FALSE) {
      return $quiz_responses[$result_id][$vid];
    }
  }
  if (!isset($quiz_responses[$result_id])) {

    // Prepare to cache responses for this result id
    $quiz_responses[$result_id] = array();
  }

  // If the question node isn't set we fetch it from the QuizQuestion instance this responce belongs to
  if (!isset($question)) {
    $question_node = node_load($nid, $vid);
    $question = _quiz_question_get_instance($question_node, TRUE)->node;
  }
  if (!$question) {
    return FALSE;
  }
  $info = quiz_question_get_info();
  $constructor = $info[$question->type]['response provider'];
  $to_return = new $constructor($result_id, $question, $answer);

  // All responce classes must extend QuizQuestionResponse
  if (!$to_return instanceof QuizQuestionResponse) {
    drupal_set_message(t('The question-response isn\'t a QuizQuestionResponse. It needs to extend the QuizQuestionResponse interface, or extend the abstractQuizQuestionResponse class.'), 'error', FALSE);
  }
  $result = $to_return
    ->getReport();
  $to_return->question->answers[$result['answer_id']] = $result;
  $to_return->question->correct = $result['is_correct'];

  // Cache the responce instance
  $quiz_responses[$result_id][$question->vid] = $to_return;
  return $to_return;
}