You are here

class TrueFalseQuestion in Quiz 6.3

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

Implementation of QuizQuestion.

Hierarchy

Expanded class hierarchy of TrueFalseQuestion

1 string reference to 'TrueFalseQuestion'
quiz_question_quiz_question_info in question_types/quiz_question/quiz_question.module
Implementation of hook_quiz_question_info().

File

question_types/quiz_question/quiz_question.truefalse.inc, line 13
Defines the classes necessary for a True/False quiz.

View source
class TrueFalseQuestion implements QuizQuestion {

  /**
   * The current node for this question.
   */
  protected $node = NULL;
  public function __construct($node) {
    $this->node = $node;
  }
  public function save($is_new = FALSE) {

    //drupal_set_message('Save called', 'status');
    if (!isset($this->node->feedback)) {
      $this->node->feedback = '';
    }
    if ($is_new || $this->node->revision == 1) {
      $sql = "INSERT INTO {quiz_truefalse_node} (nid, vid, correct_answer, feedback) VALUES (%d, %d, %d, '%s')";
      db_query($sql, $this->node->nid, $this->node->vid, (int) $this->node->correct_answer, $this->node->feedback);
    }
    else {
      drupal_set_message('Updating', 'status');
      $sql = "UPDATE {quiz_truefalse_node} SET correct_answer = %d, feedback = '%s' WHERE nid = %d AND vid = %d";
      db_query($sql, (int) $this->node->correct_answer, $this->node->feedback, $this->node->nid, $this->node->vid);
    }
  }
  public function validate($node, &$form) {

    // This space intentionally left blank. :)
  }
  public function delete($only_this_version = FALSE) {

    // Only delete a nid/vid.
    if ($only_this_version) {
      $sql = 'DELETE FROM {quiz_truefalse_node} WHERE nid = %d AND vid = %d';
      db_query($sql, $this->node->nid, $this->node->vid);
    }
    else {
      $sql = 'DELETE FROM {quiz_truefalse_node} WHERE nid = %d';
      db_query($sql, $this->node->nid, $this->node->vid);
    }
  }
  public function load() {
    $sql = 'SELECT correct_answer, feedback FROM {quiz_truefalse_node} WHERE nid = %d AND vid = %d';
    $result = db_fetch_object(db_query($sql, $this->node->nid, $this->node->vid));
    return $result;
  }
  public function view() {
    return $this
      ->getQuestionForm($this->node);
  }

  // This is called whenever a question is rendered, either
  // to an administrator or to a quiz taker.
  public function getQuestionForm($node, $context = NULL) {

    // Question first
    $form['question'] = array(
      '#type' => 'markup',
      '#value' => $node->body,
    );

    // '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' => 1,
      '#required' => FALSE,
    );
    return $form;
  }
  public function getAdminForm($edit = NULL) {
    $form['settings'] = array(
      '#type' => 'markup',
      '#value' => t('There are no settings for this question type.'),
    );
    return $form;
  }
  public function getCreationForm($edit) {
    $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,
    );
    $form['feedback_fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Feedback Settings'),
      '#description' => t('Settings pertaining to feedback given along with results.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['feedback_fields']['feedback'] = array(
      '#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;
  }
  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() {
    $sql = "SELECT correct_answer FROM {quiz_truefalse_node} WHERE nid = %d AND vid = %d";
    return db_result(db_query($sql, $this->node->nid, $this->node->vid));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TrueFalseQuestion::$node protected property The current node for this question.
TrueFalseQuestion::delete public function Deletes a question from the database. Overrides QuizQuestion::delete
TrueFalseQuestion::getAdminForm public function Get the form that contains admin settings for this question type. Overrides QuizQuestion::getAdminForm
TrueFalseQuestion::getCorrectAnswer public function Get the answer to this question.
TrueFalseQuestion::getCreationForm public function Get the form used to create a new question. Overrides QuizQuestion::getCreationForm
TrueFalseQuestion::getMaximumScore public function Get the maximum possible score for this question. Overrides QuizQuestion::getMaximumScore
TrueFalseQuestion::getQuestionForm public function Get the form that will be displayed to the test-taking user. Overrides QuizQuestion::getQuestionForm
TrueFalseQuestion::load public function Retrieve information about the question and add it to the node. Overrides QuizQuestion::load
TrueFalseQuestion::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. This function is only responsible for saving data specific to the implement ation. Overrides QuizQuestion::save
TrueFalseQuestion::validate public function Provides validation for question before it is created. Overrides QuizQuestion::validate
TrueFalseQuestion::view public function Retrieve information relevant for viewing the node. This data is generally added to the node's extra field. Overrides QuizQuestion::view
TrueFalseQuestion::__construct public function Construct a new quiz question. Overrides QuizQuestion::__construct