You are here

class TrueFalseResponse in Quiz 7.5

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

Extension of QuizQuestionResponse.

Hierarchy

Expanded class hierarchy of TrueFalseResponse

1 string reference to 'TrueFalseResponse'
truefalse_quiz_question_info in question_types/truefalse/truefalse.module
Implements hook_quiz_question_info().

File

question_types/truefalse/truefalse.classes.inc, line 191
TrueFalse classes.

View source
class TrueFalseResponse 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.
   */
  public function __construct($result_id, stdClass $question_node, $answer = NULL) {
    parent::__construct($result_id, $question_node, $answer);
    if (!isset($answer)) {

      // Get from DB.
      $r = $this
        ->getUserAnswer();
      if (!empty($r)) {
        $this->answer = $r->answer;
        $this->score = $r->score;
      }
    }
    else {

      // Load from input.
      $this->answer = $answer;
    }
  }

  /**
   * Implementation of save().
   *
   * @see QuizQuestionResponse::save()
   */
  public function save() {
    db_merge('quiz_truefalse_user_answers')
      ->key(array(
      'result_answer_id' => $this->result_answer_id,
    ))
      ->fields(array(
      'result_answer_id' => $this->result_answer_id,
      'answer' => (int) $this->answer,
      'score' => (int) $this
        ->getScore(),
    ))
      ->execute();
  }

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

  /**
   * Implementation of score().
   *
   * @see QuizQuestionResponse::score()
   */
  public function score() {
    $tfQuestion = new TrueFalseQuestion($this->question);
    return $this
      ->getResponse() == $tfQuestion
      ->getCorrectAnswer() ? 1 : 0;
  }

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

  /**
   * Get the user's answer from the database.
   *
   * @return array
   *   An array containing both the answer and the score
   */
  public function getUserAnswer() {
    if (isset($this->result_answer_id)) {
      return db_query('SELECT answer, score FROM {quiz_truefalse_user_answers} WHERE result_answer_id = :raid', array(
        ':raid' => $this->result_answer_id,
      ))
        ->fetch();
    }
  }

  /**
   * Implementation of getFeedbackValues().
   *
   * @see QuizQuestionResponse::getFeedbackValues()
   */
  public function getFeedbackValues() {
    $answer = $this->question->answers[0]['answer'];
    if (!is_null($answer)) {
      $answer = intval($answer);
    }
    $correct_answer = intval($this->question->correct_answer);
    $data = array();
    $data[] = array(
      'choice' => t('True'),
      'attempt' => $answer === 1 ? quiz_icon('selected') : '',
      'correct' => $answer === 1 ? quiz_icon($correct_answer ? 'correct' : 'incorrect') : '',
      'score' => intval($correct_answer === 1 && $answer === 1),
      'answer_feedback' => '',
      'solution' => $correct_answer === 1 ? quiz_icon('should') : '',
    );
    $data[] = array(
      'choice' => t('False'),
      'attempt' => $answer === 0 ? quiz_icon('selected') : '',
      'correct' => $answer === 0 ? quiz_icon(!$correct_answer ? 'correct' : 'incorrect') : '',
      'score' => intval($correct_answer === 0 && $answer === 0),
      'answer_feedback' => '',
      'solution' => $correct_answer === 0 ? quiz_icon('should') : '',
    );
    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);
      $items[$ra->result_id][] = array(
        'answer' => $ra_i
          ->getResponse() ? t('True') : t('False'),
      );
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TrueFalseResponse::delete public function Implementation of delete(). Overrides QuizQuestionResponse::delete
TrueFalseResponse::getFeedbackValues public function Implementation of getFeedbackValues(). Overrides QuizQuestionResponse::getFeedbackValues
TrueFalseResponse::getResponse public function Implementation of getResponse(). Overrides QuizQuestionResponse::getResponse
TrueFalseResponse::getUserAnswer public function Get the user's answer from the database.
TrueFalseResponse::save public function Implementation of save(). Overrides QuizQuestionResponse::save
TrueFalseResponse::score public function Implementation of score(). Overrides QuizQuestionResponse::score
TrueFalseResponse::viewsGetAnswers public static function Get answers for a question in a result. Overrides QuizQuestionResponse::viewsGetAnswers
TrueFalseResponse::__construct public function Constructor. Overrides QuizQuestionResponse::__construct