You are here

class MatchingResponse in Quiz 7.5

Same name and namespace in other branches
  1. 6.6 question_types/matching/matching.classes.inc \MatchingResponse
  2. 6.3 question_types/matching/matching.classes.inc \MatchingResponse
  3. 6.4 question_types/matching/matching.classes.inc \MatchingResponse
  4. 6.5 question_types/matching/matching.classes.inc \MatchingResponse
  5. 7.6 question_types/matching/matching.classes.inc \MatchingResponse
  6. 7 question_types/matching/matching.classes.inc \MatchingResponse
  7. 7.4 question_types/matching/matching.classes.inc \MatchingResponse

Extension of QuizQuestionResponse

Hierarchy

Expanded class hierarchy of MatchingResponse

1 string reference to 'MatchingResponse'
matching_quiz_question_info in question_types/matching/matching.module
Implements hook_quiz_question_info().

File

question_types/matching/matching.classes.inc, line 420
Matching classes.

View source
class MatchingResponse extends QuizQuestionResponse {

  /**
   * Constructor.
   *
   * @param int $result_id
   *   The result ID for the user's result set. There is one result ID per time
   *   the user takes a quiz.
   * @param stdClass $question_node
   *   The question node.
   * @param mixed $answer
   *   The answer (dependent on question type).
   */
  public function __construct($result_id, stdClass $question_node, $answer = NULL) {
    parent::__construct($result_id, $question_node, $answer);
    if (!isset($answer)) {
      $res = db_query('SELECT ua.answer, score, ua.match_id FROM {quiz_matching_user_answers} ua
              JOIN {quiz_matching_node} n ON n.match_id = ua.match_id
              WHERE n.nid = :nid AND n.vid = :vid AND ua.result_answer_id = :result_answer_id', array(
        ':nid' => $question_node->nid,
        ':vid' => $question_node->vid,
        ':result_answer_id' => $this->result_answer_id,
      ));
      $this->answer = array();
      while ($obj = $res
        ->fetch()) {
        $this->answer[$obj->match_id] = $obj->answer;
      }
    }
    $this->is_correct = $this
      ->isCorrect();
  }

  /**
   * Implementation of save().
   *
   * @see QuizQuestionResponse::save()
   */
  public function save() {
    $this
      ->delete();
    $insert = db_insert('quiz_matching_user_answers')
      ->fields(array(
      'match_id',
      'result_answer_id',
      'answer',
      'score',
    ));
    foreach ($this->answer as $key => $value) {
      $insert
        ->values(array(
        'match_id' => $key,
        'result_answer_id' => $this->result_answer_id,
        'answer' => (int) $value,
        'score' => $key == $value ? 1 : 0,
      ));
    }
    $insert
      ->execute();
  }

  /**
   * Implementation of delete().
   *
   * @see QuizQuestionResponse::delete()
   */
  public function delete() {
    db_delete('quiz_matching_user_answers')
      ->condition('result_answer_id', $this->result_answer_id)
      ->execute();
  }

  /**
   * Implementation of score().
   *
   * @see QuizQuestionResponse::score()
   */
  public function score() {
    $wrong_answer = 0;
    $correct_answer = 0;
    $user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
    $MatchingQuestion = new MatchingQuestion($this->question);
    $correct_answers = $MatchingQuestion
      ->getCorrectAnswer();
    foreach ((array) $user_answers as $key => $value) {
      if ($value != 0 && $correct_answers[$key]['answer'] == $correct_answers[$value]['answer']) {
        $correct_answer++;
      }
      elseif ($value == 0 || $value == 'def') {
      }
      else {
        $wrong_answer++;
      }
    }
    $score = $correct_answer;
    if ($this->question->choice_penalty) {
      $score -= $wrong_answer;
    }
    return $score < 0 ? 0 : $score;
  }

  /**
   * Implementation of getResponse().
   *
   * @see QuizQuestionResponse::getResponse()
   */
  public function getResponse() {
    return $this->answer;
  }

  /**
   * Implementation of getFeedbackValues().
   *
   * @see QuizQuestionResponse::getFeedbackValues()
   */
  public function getFeedbackValues() {
    $data = array();
    $answers = $this->question->answers[0]['answer'];
    $solution = $this->quizQuestion
      ->getSubquestions();
    foreach ($this->question->match as $match) {
      $data[] = array(
        'choice' => $match['question'],
        'attempt' => !empty($answers[$match['match_id']]) ? $solution[1][$answers[$match['match_id']]] : '',
        'correct' => $answers[$match['match_id']] == $match['match_id'] ? theme('quiz_answer_result', array(
          'type' => 'correct',
        )) : theme('quiz_answer_result', array(
          'type' => 'incorrect',
        )),
        'score' => $answers[$match['match_id']] == $match['match_id'] ? 1 : 0,
        'answer_feedback' => $match['feedback'],
        'question_feedback' => 'Question feedback',
        'solution' => $solution[1][$match['match_id']],
        'quiz_feedback' => "Quiz feedback",
      );
    }
    return $data;
  }

  /**
   * Get answers for a question in a result.
   *
   * This static method assists in building views for the mass export of
   * question answers.
   *
   * @see views_handler_field_prerender_list for the expected return value.
   */
  public static function viewsGetAnswers(array $result_answer_ids = array()) {
    $items = array();
    foreach ($result_answer_ids as $result_answer_id) {
      $ra = entity_load_single('quiz_result_answer', $result_answer_id);
      $question = node_load($ra->question_nid, $ra->question_vid);

      /** @var $ra_i QuizQuestionResponse */
      $ra_i = _quiz_question_response_get_instance($ra->result_id, $question);
      $matches = array();
      foreach ($question->match as $match) {
        $matches[$match['match_id']] = $match;
      }
      foreach ($ra_i
        ->getResponse() as $question_id => $answer_id) {
        if (!empty($answer_id)) {
          $items[$ra->result_id][] = array(
            'answer' => $matches[$question_id]['question'] . ': ' . $matches[$answer_id]['answer'],
          );
        }
      }
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatchingResponse::delete public function Implementation of delete(). Overrides QuizQuestionResponse::delete
MatchingResponse::getFeedbackValues public function Implementation of getFeedbackValues(). Overrides QuizQuestionResponse::getFeedbackValues
MatchingResponse::getResponse public function Implementation of getResponse(). Overrides QuizQuestionResponse::getResponse
MatchingResponse::save public function Implementation of save(). Overrides QuizQuestionResponse::save
MatchingResponse::score public function Implementation of score(). Overrides QuizQuestionResponse::score
MatchingResponse::viewsGetAnswers public static function Get answers for a question in a result. Overrides QuizQuestionResponse::viewsGetAnswers
MatchingResponse::__construct public function Constructor. Overrides QuizQuestionResponse::__construct
QuizQuestionResponse::$answer protected property
QuizQuestionResponse::$evaluated protected property
QuizQuestionResponse::$is_correct protected property
QuizQuestionResponse::$is_doubtful public property
QuizQuestionResponse::$is_skipped public property
QuizQuestionResponse::$question public property
QuizQuestionResponse::$quizQuestion public property
QuizQuestionResponse::$result_id protected property
QuizQuestionResponse::$score protected property 9
QuizQuestionResponse::canReview public function Can the quiz taker view the requested review?
QuizQuestionResponse::getFormat protected function Utility function that returns the format of the node body.
QuizQuestionResponse::getMaxScore public function Get the max score of this question response. 1
QuizQuestionResponse::getQuizQuestion public function Get the question of this question response.
QuizQuestionResponse::getReport public function Get data suitable for reporting a user's score on the question. 1
QuizQuestionResponse::getReportForm public function Creates the report form for the admin pages, and for when a user gets feedback after answering questions. 2
QuizQuestionResponse::getReportFormAnswerFeedback public function Get the feedback form for the reportForm. 2
QuizQuestionResponse::getReportFormScore public function Implementation of getReportFormScore().
QuizQuestionResponse::getReportFormSubmit public function Get the submit function for the reportForm. 2
QuizQuestionResponse::getReportFormValidate public function Get the validate function for the reportForm.
QuizQuestionResponse::getScore public function Get the score of this question response.
QuizQuestionResponse::getWeightedRatio public function Get the weighted score ratio.
QuizQuestionResponse::isCorrect public function Check to see if the answer is marked as correct. 2
QuizQuestionResponse::isEvaluated public function Indicate whether the response has been evaluated (scored) yet.
QuizQuestionResponse::refreshQuestionNode public function Used to refresh this instances question node in case drupal has changed it.
QuizQuestionResponse::setResultAnswerId public function Set the target result answer ID for this Question response.
QuizQuestionResponse::toBareObject public function Represent the response as a stdClass object.