You are here

class MatchingResponse in Quiz 6.4

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.5 question_types/matching/matching.classes.inc \MatchingResponse
  4. 7.6 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
Implementation of hook_quiz_question_info().

File

question_types/matching/matching.classes.inc, line 345
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)) {
      $sql = '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 = %d AND n.vid = %d AND ua.result_id = %d';
      $res = db_query($sql, $question_node->nid, $question_node->vid, $result_id);
      $this->answer = array();
      while ($obj = db_fetch_object($res)) {
        $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() {
    $user_answers = isset($this->answer) ? $this->answer : array();

    // to prevent warning : Invalid argument supplied for foreach()
    foreach ((array) $user_answers as $key => $value) {
      $score = $key == $value ? 1 : 0;
      $sql = 'INSERT INTO {quiz_matching_user_answers} (match_id, result_id, answer, score) VALUES (%d, %d, %d, %d)';
      db_query($sql, $key, $this->rid, (int) $value, $score);
    }
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    $sql = 'DELETE FROM {quiz_matching_user_answers}
            WHERE match_id IN (
              SELECT match_id
              FROM {quiz_matching_node}
              WHERE nid = %d AND vid = %d
            )
            AND result_id = %d';
    db_query($sql, $this->question->nid, $this->question->vid, $this->rid);
  }

  /**
   * 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');
    if ($showfeedback) {
      $metadata[] = t('Feedback');
    }
    $MatchingQuestion = new MatchingQuestion($this->question);
    $correct_answers = $MatchingQuestion
      ->getCorrectAnswer();
    $user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
    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'] = check_markup($correct_answer['question'], $this->question->format, FALSE);
      if ($showpoints) {
        $answer_data['correct_answer'] = check_markup($correct_answer['answer'], $this->question->format, FALSE);
      }
      $answer_data['user_answer'] = isset($correct_answers[$id]) ? check_markup($correct_answers[$id]['answer'], $this->question->format, FALSE) : NULL;
      if ($showfeedback) {
        $answer_data['feedback'] = check_markup($correct_answer['feedback'], $this->question->format, FALSE);
        $answer_data['is_correct'] = $correct;
      }
      $data[] = $answer_data;
    }
    return array(
      '#type' => 'markup',
      '#value' => theme('matching_response', $metadata, $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::getReportFormAnswerFeedback public function 2
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.