You are here

class MatchingQuestion in Quiz 6.6

Same name and namespace in other branches
  1. 6.3 question_types/matching/matching.classes.inc \MatchingQuestion
  2. 6.4 question_types/matching/matching.classes.inc \MatchingQuestion
  3. 6.5 question_types/matching/matching.classes.inc \MatchingQuestion
  4. 7.6 question_types/matching/matching.classes.inc \MatchingQuestion
  5. 7 question_types/matching/matching.classes.inc \MatchingQuestion
  6. 7.4 question_types/matching/matching.classes.inc \MatchingQuestion
  7. 7.5 question_types/matching/matching.classes.inc \MatchingQuestion

Implementation of Matching.

Hierarchy

Expanded class hierarchy of MatchingQuestion

1 string reference to 'MatchingQuestion'
matching_quiz_question_info in question_types/matching/matching.module
Implementation of hook_quiz_question_info().

File

question_types/matching/matching.classes.inc, line 16
matching.classes

View source
class MatchingQuestion implements QuizQuestion {

  /**
   * The current node for this question.
   */
  protected $node = NULL;
  public function __construct($node) {
    if (empty($node->match)) {
      $node->match = array();
    }
    $this->node = $node;
  }
  public function save($is_new = FALSE) {
    foreach ($this->node->match as $match) {
      if (empty($match['question']) || empty($match['answer'])) {
        continue;
      }
      $match['feedback'] = isset($match['feedback']) ? $match['feedback'] : '';
      if ($is_new || $this->node->revision == 1) {
        $sql = "INSERT INTO {quiz_matching_node} (nid, vid, question, answer, feedback) VALUES (%d, %d, '%s', '%s', '%s')";
        db_query($sql, $this->node->nid, $this->node->vid, $match['question'], $match['answer'], $match['feedback']);
      }
      else {

        //$match_id = $this->node->{'match_id_' . $i};
        $sql = "UPDATE {quiz_matching_node} SET question = '%s', answer = '%s', feedback = '%s' WHERE match_id = %d";
        db_query($sql, $match['question'], $match['answer'], $match['feedback'], $match['match_id']);
      }
    }
  }
  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_matching_node} WHERE nid = %d AND vid = %d';
      db_query($sql, $this->node->nid, $this->node->vid);
    }
    else {
      $sql = 'DELETE FROM {quiz_matching_node} WHERE nid = %d';
      db_query($sql, $this->node->nid, $this->node->vid);
    }
  }
  public function load() {
    $rows = $result = array();
    $sql = "SELECT match_id, question, answer, feedback FROM {quiz_matching_node} WHERE nid = %d AND vid = %d";
    $results = db_query($sql, $this->node->nid, $this->node->vid);
    while ($result = db_fetch_object($results)) {
      $rows['answer'][] = array(
        'match_id' => $result->match_id,
        'question' => $result->question,
        'answer' => $result->answer,
        'feedback' => $result->feedback,
      );
    }
    return $rows;
  }
  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) {
    list($questions, $select_option) = $this
      ->getQuestion($node);
    foreach ($questions as $question) {
      $title = $question['question'];
      if (!_quiz_is_taking_context()) {
        $title .= " (answer is '{$question['answer']}')";
      }
      $form['tries[' . $question['match_id'] . ']'] = array(
        '#type' => 'select',
        '#title' => $title,
        '#options' => $this
          ->customShuffle($select_option),
      );
    }
    return variable_get('quiz_matching_shuffle_options', TRUE) ? $this
      ->customShuffle($form) : $form;
  }
  public function customShuffle($array = array()) {
    $new_array = array();
    while (count($array)) {
      $element = array_rand($array);
      $new_array[$element] = $array[$element];
      unset($array[$element]);
    }
    return $new_array;
  }
  public function getQuestion($node) {
    $questions = $select_option = array();
    $sql = "SELECT match_id, question, answer, feedback FROM {quiz_matching_node} WHERE nid = %d AND vid = %d";
    $results = db_query($sql, $node->nid, $node->vid);
    while ($result = db_fetch_object($results)) {
      $questions[] = array(
        'match_id' => $result->match_id,
        'question' => $result->question,
        'answer' => $result->answer,
        'feedback' => $result->feedback,
      );
      $select_option[$result->match_id] = $result->answer;
    }
    return array(
      $questions,
      $select_option,
    );
  }
  public function getAdminForm($edit = NULL) {
    $form['matching'] = array(
      '#type' => 'fieldset',
      '#title' => t('Matching Settings'),
      '#description' => t('Matching Questions Settings and Configuration'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['matching']['quiz_matching_form_size'] = array(
      '#type' => 'textfield',
      '#title' => t('Match Question Size'),
      '#description' => t('Number of question allowed to wrap under a match type question.'),
      '#default_value' => variable_get('quiz_matching_form_size', 5),
    );
    $form['matching']['quiz_matching_shuffle_options'] = array(
      '#type' => 'checkbox',
      '#title' => t('Shuffle Matching Options'),
      '#default_value' => variable_get('quiz_matching_shuffle_options', TRUE),
      '#description' => t('If checked matching options will be shuffled'),
    );
    return system_settings_form($form);
  }
  public function getCreationForm($edit) {
    $form['match'] = array(
      '#type' => 'fieldset',
      '#title' => t('Match Question'),
      '#tree' => TRUE,
    );
    for ($i = 1; $i <= variable_get('quiz_matching_form_size', 5); ++$i) {
      $form['match'][$i] = array(
        '#type' => 'fieldset',
        '#title' => t('Question ' . $i),
      );
      $form['match'][$i]['match_id'] = array(
        '#type' => 'hidden',
        '#default_value' => isset($this->node->answer[$i - 1]['match_id']) ? $this->node->answer[$i - 1]['match_id'] : '',
      );
      $form['match'][$i]['question'] = array(
        '#type' => 'textarea',
        '#title' => t('Question'),
        '#rows' => 2,
        '#default_value' => isset($this->node->answer[$i - 1]['question']) ? $this->node->answer[$i - 1]['question'] : '',
      );
      $form['match'][$i]['answer'] = array(
        '#type' => 'textarea',
        '#title' => t('Match'),
        '#rows' => 2,
        '#default_value' => isset($this->node->answer[$i - 1]['answer']) ? $this->node->answer[$i - 1]['answer'] : '',
      );
      $form['match'][$i]['feedback'] = array(
        '#type' => 'textarea',
        '#title' => t('Feedback / Reason'),
        '#rows' => 2,
        '#default_value' => isset($this->node->answer[$i - 1]['feedback']) ? $this->node->answer[$i - 1]['feedback'] : '',
      );
    }
    return $form;
  }
  public function getMaximumScore() {
    return count($this->node->answer);
  }
  public function getCorrectAnswer() {
    $correct_answers = array();

    //match_id  nid   vid   question  answer  feedback # {quiz_matching_node} fields
    $results = db_query("SELECT match_id, question, answer FROM {quiz_matching_node} WHERE nid = %d AND vid = %d", $this->node->nid, $this->node->vid);
    while ($result = db_fetch_object($results)) {
      $correct_answers[$result->match_id] = array(
        'match_id' => $result->match_id,
        'question' => $result->question,
        'answer' => $result->answer,
      );
    }
    return $correct_answers;
  }

}

Members

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