You are here

class ClozeQuestion in Cloze 6

Same name and namespace in other branches
  1. 7 cloze.classes.inc \ClozeQuestion

Extension of QuizQuestion.

This could have extended long answer, except that that would have entailed adding long answer as a dependency.

Hierarchy

Expanded class hierarchy of ClozeQuestion

1 string reference to 'ClozeQuestion'
cloze_quiz_question_info in ./cloze.module
Implementation of hook_quiz_question_info().

File

./cloze.classes.inc, line 22
The main classes for the short answer question type.

View source
class ClozeQuestion extends QuizQuestion {

  /**
   * Implementation of saveNodeProperties
   *
   * @see QuizQuestion#saveNodeProperties($is_new)
   */
  public function saveNodeProperties($is_new = FALSE) {

    // no special property for cloze question node
  }

  /**
   * Implementation of validateNode
   *
   * @see QuizQuestion#validateNode($form)
   */
  public function validateNode(array &$form) {
    if (substr_count($this->node->body, '[') !== substr_count($this->node->body, ']')) {
      form_set_error('body', t('Please check the question format.'));
    }
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestion#delete($only_this_version)
   */
  public function delete($only_this_version = FALSE) {
    parent::delete($only_this_version);
    if ($only_this_version) {
      db_query('DELETE FROM {quiz_cloze_user_answers} WHERE question_nid = %d AND question_vid = %d', $this->node->nid, $this->node->vid);
    }
    else {
      db_query('DELETE FROM {quiz_cloze_user_answers} WHERE question_nid = %d', $this->node->nid);
    }
  }

  /**
   * Implementation of getNodeProperties
   *
   * @see QuizQuestion#getNodeProperties()
   */
  public function getNodeProperties() {
    if (isset($this->nodeProperties)) {
      return $this->nodeProperties;
    }
    $props = parent::getNodeProperties();
    $this->nodeProperties = $props;
    return $props;
  }

  /**
   * Implementation of getNodeView
   *
   * @see QuizQuestion#getNodeView()
   */
  public function getNodeView() {
    $content = parent::getNodeView();
    drupal_add_css(drupal_get_path('module', 'cloze') . '/theme/cloze.css');
    $chunks = _cloze_get_question_chunks($this->node->body);
    if ($this
      ->viewCanRevealCorrect() && !empty($chunks)) {
      $solution = $this->node->body;
      foreach ($chunks as $position => $chunk) {
        if (strpos($chunk, '[') === FALSE) {
          continue;
        }
        $chunk = str_replace(array(
          '[',
          ']',
        ), '', $chunk);
        $choices = explode(',', $chunk);
        $replace = '<span class="correct answer user-answer">' . $choices[0] . '</span>';
        $solution = str_replace($chunks[$position], $replace, $solution);
      }
      $content['answers'] = array(
        '#type' => 'markup',
        '#value' => '<div class="quiz-solution cloze-question">' . check_markup($solution, variable_get('filter_default_format', 1)) . '</div>',
      );
    }
    else {
      $content['answers'] = array(
        '#type' => 'markup',
        '#value' => '<div class="quiz-answer-hidden">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);
    $form['#theme'] = 'cloze_answering_form';
    drupal_add_css(drupal_get_path('module', 'cloze') . '/theme/cloze.css');
    $form['open_wrapper'] = array(
      '#type' => 'markup',
      '#value' => '<div class="cloze-question">',
    );
    foreach (_cloze_get_question_chunks($this->node->body) as $position => $chunk) {
      if (strpos($chunk, '[') === FALSE) {

        // this "tries[foobar]" hack is needed becaues question handler engine checks for input field
        // with name tries
        $form['tries[' . $position . ']'] = array(
          '#type' => 'markup',
          '#value' => str_replace("\n", "<br/>", $chunk),
          '#prefix' => '<div class="form-item">',
          '#suffix' => '</div>',
        );
      }
      else {
        $chunk = str_replace(array(
          '[',
          ']',
        ), '', $chunk);
        $choices = explode(',', $chunk);
        if (count($choices) > 1) {
          $form['tries[' . $position . ']'] = array(
            '#type' => 'select',
            '#title' => '',
            '#options' => _cloze_shuffle_choices(drupal_map_assoc($choices)),
            '#required' => FALSE,
          );
        }
        else {
          $form['tries[' . $position . ']'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#size' => 32,
            '#required' => FALSE,
            '#attributes' => array(
              'autocomplete' => 'off',
            ),
          );
        }
      }
    }
    $form['close_wrapper'] = array(
      '#type' => 'markup',
      '#value' => '</div>',
    );
    if (isset($rid)) {
      $cloze_esponse = new ClozeResponse($rid, $this->node);
      $response = $cloze_esponse
        ->getResponse();
      if (is_array($response)) {
        foreach ($response as $key => $value) {
          $form["tries[{$key}]"]['#default_value'] = $value;
        }
      }
    }
    return $form;
  }

  /**
   * Implementation of getCreationForm
   *
   * @see QuizQuestion#getCreationForm($form_state)
   */
  public function getCreationForm(array $form_state = NULL) {
    drupal_add_css(drupal_get_path('module', 'cloze') . '/theme/cloze.css');
    $form['instructions'] = array(
      '#type' => 'markup',
      '#value' => '<div class="cloze-instruction">' . t('For free text cloze, mention the correct ansewr inside the square bracket. For multichoice cloze, provide the options separated by commas with correct answer as first. <br/>Example question: [The] Sun raises in the [east, west, north, south]. <br/>Answer: <span class="answer correct correct-answer">The</span> sun raises the <span class="answer correct correct-answer">east</span>.') . '</div>',
      '#weight' => -10,
    );
    return $form;
  }

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

  /**
   * Evaluate the correctness of an answer based on the correct answer and evaluation method.
   */
  public function evaluateAnswer($user_answer) {
    $correct_answer = _cloze_get_correct_answer_chunks($this->node->body);
    $total_answer = count($correct_answer);
    $correct_answer_count = 0;
    if ($total_answer == 0) {
      return $this
        ->getMaximumScore();
    }
    foreach ($correct_answer as $key => $value) {
      if (drupal_strtolower($correct_answer[$key]) == drupal_strtolower($user_answer[$key])) {
        $correct_answer_count++;
      }
    }
    $score = $correct_answer_count / $total_answer * $this
      ->getMaximumScore();
    return round($score);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClozeQuestion::delete public function Implementation of delete
ClozeQuestion::evaluateAnswer public function Evaluate the correctness of an answer based on the correct answer and evaluation method.
ClozeQuestion::getAnsweringForm public function Implementation of getAnsweringForm
ClozeQuestion::getCreationForm public function Implementation of getCreationForm
ClozeQuestion::getMaximumScore public function Implementation of getMaximumScore
ClozeQuestion::getNodeProperties public function Implementation of getNodeProperties
ClozeQuestion::getNodeView public function Implementation of getNodeView
ClozeQuestion::saveNodeProperties public function Implementation of saveNodeProperties
ClozeQuestion::validateNode public function Implementation of validateNode