You are here

class MatchingResponse in Quiz 7

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.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 363
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 isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    foreach ($this->answer as $value) {
      if ($value != 'def') {
        return TRUE;
      }
    }
    return t('You need to match at least one of the items.');
  }

  /**
   * 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->rid,
        '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->rid)
      ->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;
    foreach ((array) $user_answers as $key => $value) {
      if ($key == $value) {
        $correct_answer++;
      }
      elseif ($value == 0 || $value == 'def') {
      }
      else {
        $wrong_answer++;
      }
    }
    $score = $correct_answer - $wrong_answer;
    return $score < 0 ? 0 : $score;
  }

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

  /**
   * Implementation of getReportFormResponse
   *
   * @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $data = $metadata = array();

    // Build the question answers header (add blank space for IE).
    $metadata[] = t('Match');
    if ($showpoints) {
      $metadata[] = t('Correct Answer');
    }
    $metadata[] = t('User answer');
    $MatchingQuestion = new MatchingQuestion($this->question);
    $correct_answers = $MatchingQuestion
      ->getCorrectAnswer();
    $user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
    $has_feedback = TRUE;
    foreach ($correct_answers as $correct_answer) {
      $answer_data = array();
      $id = $user_answers[$correct_answer['match_id']];
      $correct = isset($correct_answers[$id]) && $correct_answer['answer'] == $correct_answers[$id]['answer'];
      $answer_data['question'] = $correct_answer['question'];
      if ($showpoints) {
        $answer_data['correct_answer'] = $correct_answer['answer'];
      }
      $answer_data['user_answer'] = isset($correct_answers[$id]) ? $correct_answers[$id]['answer'] : NULL;
      if ($showfeedback && !empty($correct_answer['feedback'])) {
        $answer_data['feedback'] = check_markup($correct_answer['feedback']);
        $answer_data['is_correct'] = $correct;
      }
      else {
        $has_feedback = FALSE;
      }
      $data[] = $answer_data;
    }
    if ($showfeedback && $has_feedback) {
      $metadata[] = t('Feedback');
    }
    return array(
      '#markup' => theme('matching_response', array(
        'metadata' => $metadata,
        'data' => $data,
      )),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatchingResponse::delete public function Implementation of delete Overrides QuizQuestionResponse::delete
MatchingResponse::getReportFormResponse public function Implementation of getReportFormResponse Overrides QuizQuestionResponse::getReportFormResponse
MatchingResponse::getResponse public function Implementation of getResponse Overrides QuizQuestionResponse::getResponse
MatchingResponse::isValid public function Implementation of isValid Overrides QuizQuestionResponse::isValid
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_skipped public property
QuizQuestionResponse::$question public property
QuizQuestionResponse::$rid protected property
QuizQuestionResponse::$score protected property 7
QuizQuestionResponse::getMaxScore public function Returns stored max score if it exists, if not the max score is calculated and returned.
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. 1
QuizQuestionResponse::getReportFormQuestion public function get the question part of the reportForm
QuizQuestionResponse::getReportFormScore public function Get the score part of the report form 2
QuizQuestionResponse::getReportFormSubmit public function Get the submit function for the reportForm 2
QuizQuestionResponse::getReportFormTheme public function Get the theme key for the reportForm
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. 1
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::refreshQuestionNode public function Used to refresh this instances question node in case drupal has changed it.
QuizQuestionResponse::saveResult public function Saves the quiz result. This is not used when a question is skipped!
QuizQuestionResponse::toBareObject function Represent the response as a stdClass object.