You are here

class TrueFalseQuestion in Quiz 8.4

Extension of QuizQuestion.

Hierarchy

Expanded class hierarchy of TrueFalseQuestion

File

question_types/truefalse/lib/Drupal/truefalse/TrueFalseQuestion.php, line 16
Defines the classes necessary for a True/False quiz.

Namespace

Drupal\truefalse
View source
class TrueFalseQuestion extends QuizQuestion {

  /**
   * Implementation of saveNodeProperties
   *
   * @see QuizQuestion#saveNodeProperties($is_new)
   */
  public function saveNodeProperties($is_new = FALSE) {
    if (!isset($this->node->feedback)) {
      $this->node->feedback = '';
    }
    if ($is_new || $this->node
      ->isNewRevision() == 1) {
      $id = db_insert('quiz_truefalse_node')
        ->fields(array(
        'nid' => $this->node
          ->id(),
        'vid' => $this->node
          ->getRevisionId(),
        'correct_answer' => (int) $this->node->correct_answer,
        'feedback' => $this->node->feedback,
      ))
        ->execute();
    }
    else {
      db_update('quiz_truefalse_node')
        ->fields(array(
        'correct_answer' => (int) $this->node->correct_answer,
        'feedback' => $this->node->feedback,
      ))
        ->condition('nid', $this->node
        ->id())
        ->condition('vid', $this->node
        ->getRevisionId())
        ->execute();
    }
  }

  /**
   * Implementation of validateNode
   *
   * @see QuizQuestion#validateNode($form_state)
   */
  public function validateNode(array &$form_state) {

    // This space intentionally left blank. :)
  }

  /**
   * Implementation of entityBuilder
   */
  public function entityBuilder(&$form_state) {
    $this->node->correct_answer = $form_state['values']['correct_answer'];
    $this->node->feedback = $form_state['values']['feedback'];
    $this->node->add_directly = $form_state['values']['add_directly'];
  }

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

  /**
   * Implementation of getNodeProperties
   *
   * @see QuizQuestion#getNodeProperties()
   */
  public function getNodeProperties() {
    if (isset($this->nodeProperties)) {
      return $this->nodeProperties;
    }
    $props = parent::getNodeProperties();
    if ($this->node instanceof \stdClass) {
      $params = array(
        ':nid' => $this->node->nid,
        ':vid' => $this->node->vid,
      );
    }
    else {
      $params = array(
        ':nid' => $this->node
          ->id(),
        ':vid' => $this->node
          ->getRevisionId(),
      );
    }
    $res_a = db_query('SELECT correct_answer, feedback FROM {quiz_truefalse_node} WHERE nid = :nid AND vid = :vid', $params)
      ->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($form_state, $rid)
   */
  public function getAnsweringForm(array $form_state = NULL, $rid) {
    $form = parent::getAnsweringForm($form_state, $rid);

    // 'tries' is unfortunately required by quiz.module
    $form['tries'] = array(
      '#type' => 'radios',
      '#title' => t('Choose one'),
      '#options' => array(
        1 => t('True'),
        0 => t('False'),
      ),
      '#default_value' => NULL,
    );
    if (isset($rid)) {
      $response = new TrueFalseResponse($rid, $this->node);
      $default = $response
        ->getResponse();
      $form['tries']['#default_value'] = is_null($default) ? NULL : $default;
    }
    return $form;
  }

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

  /**
   * Implementation of getCreationForm
   *
   * @see QuizQuestion#getCreationForm($form_state)
   */
  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".'),
    );
    $form['feedback_fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Feedback Settings'),
      '#description' => t('Settings pertaining to feedback given along with results.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => -3,
    );
    $form['feedback_fields']['feedback'] = array(
      // @todo: Does this make sense?
      '#type' => 'textarea',
      '#title' => t('Feedback Text'),
      '#description' => t('Text to be displayed when the results are displayed'),
      '#rows' => 5,
      '#cols' => 60,
      '#required' => FALSE,
      '#default_value' => isset($this->node->feedback) ? $this->node->feedback : '',
    );
    return $form;
  }

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

  /**
   * Get the answer to this question.
   *
   * This is a utility function. It is not defined in the interface.
   */
  public function getCorrectAnswer() {
    return db_query('SELECT correct_answer FROM {quiz_truefalse_node} WHERE nid = :nid AND vid = :vid', array(
      ':nid' => $this->node
        ->id(),
      ':vid' => $this->node
        ->getRevisionId(),
    ))
      ->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. 1
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::save public function Responsible for handling insert/update of question-specific data. This is typically called from within the Node API, so there is no need to save the node.
QuizQuestion::saveRelationships function Handle the add to quiz part of the quiz_question_form
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::entityBuilder public function Implementation of entityBuilder
TrueFalseQuestion::getAnsweringForm public function Implementation of getAnsweringForm Overrides QuizQuestion::getAnsweringForm
TrueFalseQuestion::getBodyFieldTitle public function Implementation of getBodyFieldTitle Overrides QuizQuestion::getBodyFieldTitle
TrueFalseQuestion::getCorrectAnswer public function Get the 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