You are here

class TrueFalseQuestion in Quiz 7.5

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

Extension of QuizQuestion.

Hierarchy

Expanded class hierarchy of TrueFalseQuestion

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

File

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

View source
class TrueFalseQuestion extends QuizQuestion {

  /**
   * Implementation of saveNodeProperties().
   *
   * @see QuizQuestion::saveNodeProperties()
   */
  public function saveNodeProperties($is_new = FALSE) {
    db_merge('quiz_truefalse_node')
      ->key(array(
      'nid' => $this->node->nid,
      'vid' => $this->node->vid,
    ))
      ->fields(array(
      'nid' => $this->node->nid,
      'vid' => $this->node->vid,
      'correct_answer' => $this->node->correct_answer,
    ))
      ->execute();
  }

  /**
   * Implementation of validateNode().
   *
   * @see QuizQuestion::validateNode()
   */
  public function validateNode(array &$form) {

    // This space intentionally left blank. :)
  }

  /**
   * Implementation of delete().
   *
   * @see QuizQuestion::delete()
   */
  public function delete($only_this_version = FALSE) {
    $delete_node = db_delete('quiz_truefalse_node');
    $delete_node
      ->condition('nid', $this->node->nid);
    if ($only_this_version) {
      $delete_node
        ->condition('vid', $this->node->vid);
    }
    $delete_node
      ->execute();
    parent::delete($only_this_version);
  }

  /**
   * Implementation of getNodeProperties().
   *
   * @see QuizQuestion::getNodeProperties()
   */
  public function getNodeProperties() {
    if (isset($this->nodeProperties)) {
      return $this->nodeProperties;
    }
    $props = parent::getNodeProperties();
    $res_a = db_query('SELECT correct_answer FROM {quiz_truefalse_node} WHERE nid = :nid AND vid = :vid', array(
      ':nid' => $this->node->nid,
      ':vid' => $this->node->vid,
    ))
      ->fetchAssoc();
    if (is_array($res_a)) {
      $props = array_merge($props, $res_a);
    }
    $this->nodeProperties = $props;
    return $props;
  }

  /**
   * Implementation of getNodeView().
   *
   * @see QuizQuestion::getNodeView()
   */
  public function getNodeView() {
    $content = parent::getNodeView();
    if ($this
      ->viewCanRevealCorrect()) {
      $answer = $this->node->correct_answer ? t('True') : t('False');
      $content['answers']['#markup'] = '<div class="quiz-solution">' . $answer . '</div>';
      $content['answers']['#weight'] = 2;
    }
    else {
      $content['answers'] = array(
        '#markup' => '<div class="quiz-answer-hidden">' . t('Answer hidden') . '</div>',
        '#weight' => 2,
      );
    }
    return $content;
  }

  /**
   * Implementation of getAnsweringForm().
   *
   * @see QuizQuestion::getAnsweringForm()
   */
  public function getAnsweringForm(array $form_state = NULL, $result_id) {
    $element = parent::getAnsweringForm($form_state, $result_id);
    $element += array(
      '#type' => 'radios',
      '#title' => t('Choose one'),
      '#options' => array(
        1 => t('True'),
        0 => t('False'),
      ),
    );
    if (isset($result_id)) {
      $response = new TrueFalseResponse($result_id, $this->node);
      $default = $response
        ->getResponse();
      $element['#default_value'] = is_null($default) ? NULL : $default;
    }
    return $element;
  }

  /**
   * Question response validator.
   */
  public function getAnsweringFormValidate(array &$element, &$value) {
    if (is_null($value)) {
      form_error($element, t('You must provide an answer.'));
    }
  }

  /**
   * Implementation of getBodyFieldTitle().
   *
   * @see QuizQuestion::getBodyFieldTitle()
   */
  public function getBodyFieldTitle() {
    return t('True/false statement');
  }

  /**
   * Implementation of getCreationForm().
   *
   * @see QuizQuestion::getCreationForm()
   */
  public function getCreationForm(array &$form_state = NULL) {
    $form['correct_answer'] = array(
      '#type' => 'radios',
      '#title' => t('Correct answer'),
      '#options' => array(
        1 => t('True'),
        0 => t('False'),
      ),
      '#default_value' => isset($this->node->correct_answer) ? $this->node->correct_answer : 1,
      '#required' => TRUE,
      '#weight' => -4,
      '#description' => t('Choose if the correct answer for this question is "true" or "false".'),
    );
    return $form;
  }

  /**
   * Implementation of getMaximumScore().
   *
   * @see QuizQuestion::getMaximumScore()
   */
  public function getMaximumScore() {
    return 1;
  }

  /**
   * Get the correct answer to this question.
   *
   * This is a utility function. It is not defined in the interface.
   *
   * @return bool
   *   Boolean indicating if the correct answer is TRUE or FALSE
   */
  public function getCorrectAnswer() {
    return db_query('SELECT correct_answer FROM {quiz_truefalse_node} WHERE nid = :nid AND vid = :vid', array(
      ':nid' => $this->node->nid,
      ':vid' => $this->node->vid,
    ))
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuizQuestion::$node public property The current node for this question.
QuizQuestion::$nodeProperties public property
QuizQuestion::autoUpdateMaxScore protected function This may be overridden in subclasses. If it returns true, it means the max_score is updated for all occurrences of this question in quizzes.
QuizQuestion::getFormat protected function Utility function that returns the format of the node body.
QuizQuestion::getNodeForm public function Returns a node form to quiz_question_form.
QuizQuestion::hasBeenAnswered public function Finds out if a question has been answered or not.
QuizQuestion::hasFeedback public function Does this question type give feedback? 3
QuizQuestion::isGraded public function Is this question graded? 2
QuizQuestion::isQuestion public function Is this "question" an actual question? 2
QuizQuestion::save public function Responsible for handling insert/update of question-specific data.
QuizQuestion::saveRelationships public function Save this Question to the specified Quiz.
QuizQuestion::viewCanRevealCorrect public function Determines if the user can view the correct answers.
QuizQuestion::__construct public function QuizQuestion constructor stores the node object. 1
TrueFalseQuestion::delete public function Implementation of delete(). Overrides QuizQuestion::delete
TrueFalseQuestion::getAnsweringForm public function Implementation of getAnsweringForm(). Overrides QuizQuestion::getAnsweringForm
TrueFalseQuestion::getAnsweringFormValidate public function Question response validator. Overrides QuizQuestion::getAnsweringFormValidate
TrueFalseQuestion::getBodyFieldTitle public function Implementation of getBodyFieldTitle(). Overrides QuizQuestion::getBodyFieldTitle
TrueFalseQuestion::getCorrectAnswer public function Get the correct answer to this question.
TrueFalseQuestion::getCreationForm public function Implementation of getCreationForm(). Overrides QuizQuestion::getCreationForm
TrueFalseQuestion::getMaximumScore public function Implementation of getMaximumScore(). Overrides QuizQuestion::getMaximumScore
TrueFalseQuestion::getNodeProperties public function Implementation of getNodeProperties(). Overrides QuizQuestion::getNodeProperties
TrueFalseQuestion::getNodeView public function Implementation of getNodeView(). Overrides QuizQuestion::getNodeView
TrueFalseQuestion::saveNodeProperties public function Implementation of saveNodeProperties(). Overrides QuizQuestion::saveNodeProperties
TrueFalseQuestion::validateNode public function Implementation of validateNode(). Overrides QuizQuestion::validateNode