You are here

class TrueFalseResponse in Quiz 7

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.4 question_types/truefalse/truefalse.classes.inc \TrueFalseResponse
  7. 7.5 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 216
Defines the classes necessary for a True/False quiz.

View source
class TrueFalseResponse extends QuizQuestionResponse {

  /**
   * Constructor
   */
  public function __construct($result_id, stdClass $question_node, $answer = NULL) {
    parent::__construct($result_id, $question_node, $answer);
    if (!isset($answer)) {
      $r = $this
        ->getCorrectAnswer();
      if (!empty($r)) {
        $this->answer = $r->answer;
        $this->score = $r->score;
      }
    }
    else {
      $this->answer = $answer;
    }
  }

  /**
   * Implementation of isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    return $this->answer['answer'] === NULL ? t('You must provide an answer') : TRUE;
  }

  /**
   * Implementation of save
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    db_insert('quiz_truefalse_user_answers')
      ->fields(array(
      'question_nid' => $this->question->nid,
      'question_vid' => $this->question->vid,
      'result_id' => $this->rid,
      '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('question_nid', $this->question->nid)
      ->condition('question_vid', $this->question->vid)
      ->condition('result_id', $this->rid)
      ->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() {
    if (!isset($this->answer)) {
      $correct_answer = $this
        ->getCorrectAnswer();
      $this->answer = isset($correct_answer->answer) ? $correct_answer->answer : '';
    }
    return $this->answer;
  }

  /**
   * Implementation of getCorrectAnswer
   */
  public function getCorrectAnswer() {
    return db_query('SELECT answer, score FROM {quiz_truefalse_user_answers} WHERE question_vid = :qvid AND result_id = :rid', array(
      ':qvid' => $this->question->vid,
      ':rid' => $this->rid,
    ))
      ->fetch();
  }

  /**
   * Implementation of getReportFormResponse
   *
   * @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    if (empty($this->question->answers)) {
      return array(
        '#markup' => t('Missing question.'),
      );
    }
    $metadata = array();
    $data = array();

    // Build the question answers header (add blank space for IE).
    if ($showpoints) {
      $metadata[] = t('Correct Answer');
    }
    $metadata[] = t('User answer');
    if ($showfeedback && !empty($this->question->feedback)) {
      $metadata[] = ' ';
    }
    $answer = $this->question->answers[0];
    $correct_answer = $answer['is_correct'] ? $answer['answer'] : !$answer['answer'];
    $user_answer = $answer['answer'];
    if ($showpoints) {
      $data[0]['correct_answer'] = $correct_answer ? t('True') : t('False');
    }
    $data[0]['user_answer'] = $user_answer === NULL ? '' : ($user_answer ? t('True') : t('False'));
    if ($showfeedback && !empty($this->question->feedback)) {
      $data[0]['feedback'] = $this->question->feedback;
    }

    // Return themed output
    return array(
      '#markup' => theme('truefalse_response', array(
        'metadata' => $metadata,
        'data' => $data,
      )),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TrueFalseResponse::delete public function Implementation of delete Overrides QuizQuestionResponse::delete
TrueFalseResponse::getCorrectAnswer public function Implementation of getCorrectAnswer
TrueFalseResponse::getReportFormResponse public function Implementation of getReportFormResponse Overrides QuizQuestionResponse::getReportFormResponse
TrueFalseResponse::getResponse public function Implementation of getResponse Overrides QuizQuestionResponse::getResponse
TrueFalseResponse::isValid public function Implementation of isValid Overrides QuizQuestionResponse::isValid
TrueFalseResponse::save public function Implementation of save Overrides QuizQuestionResponse::save
TrueFalseResponse::score public function Implementation of score Overrides QuizQuestionResponse::score
TrueFalseResponse::__construct public function Constructor Overrides QuizQuestionResponse::__construct