You are here

class MatchingResponse in Quiz 7.6

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 question_types/matching/matching.classes.inc \MatchingResponse
  6. 7.4 question_types/matching/matching.classes.inc \MatchingResponse
  7. 7.5 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 431
matching.classes

View source
class MatchingResponse extends QuizQuestionResponse {

  /**
   * Constructor
   */
  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_id = :result_id', array(
        ':nid' => $question_node->nid,
        ':vid' => $question_node->vid,
        ':result_id' => $result_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() {
    if (!isset($this->answer) || !is_array($this->answer)) {
      return;
    }
    $insert = db_insert('quiz_matching_user_answers')
      ->fields(array(
      'match_id',
      'result_id',
      'answer',
      'score',
    ));
    foreach ($this->answer as $key => $value) {
      $insert
        ->values(array(
        'match_id' => $key,
        'result_id' => $this->result_id,
        'answer' => (int) $value,
        'score' => $key == $value ? 1 : 0,
      ));
    }
    $insert
      ->execute();
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    $match_id = db_query('SELECT match_id FROM {quiz_matching_node} WHERE nid = :nid AND vid = :vid', array(
      ':nid' => $this->question->nid,
      ':vid' => $this->question->vid,
    ))
      ->fetchCol();
    db_delete('quiz_matching_user_answers')
      ->condition('match_id', is_array($match_id) ? $match_id : array(
      0,
    ), 'IN')
      ->condition('result_id', $this->result_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 getReportFormResponse
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatchingResponse::delete public function Implementation of delete Overrides QuizQuestionResponse::delete
MatchingResponse::getFeedbackValues public function Implementation of getReportFormResponse 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::__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 8
QuizQuestionResponse::canReview public function Can the quiz taker view the requested review?
QuizQuestionResponse::getFeedback public function Returns a renderable array of question feedback.
QuizQuestionResponse::getFormat protected function Utility function that returns the format of the node body
QuizQuestionResponse::getMaxScore public function Returns stored max score if it exists, if not the max score is calculated and returned.
QuizQuestionResponse::getQuizQuestion function
QuizQuestionResponse::getReport public function Get data suitable for reporting a user's score on the question. This expects an object with the following attributes:
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 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 2
QuizQuestionResponse::getScore function Returns stored score if it exists, if not the score is calculated and returned.
QuizQuestionResponse::isCorrect 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. Questions that require human scoring (e.g. essays) may need to manually toggle this.
QuizQuestionResponse::isValid public function Validates response from a quiz taker. If the response isn't valid the quiz taker won't be allowed to proceed.
QuizQuestionResponse::refreshQuestionNode public function Used to refresh this instances question node in case drupal has changed it.
QuizQuestionResponse::setResultId public function Set the target result ID for this Question response.
QuizQuestionResponse::toBareObject function Represent the response as a stdClass object.