You are here

class ShortAnswerQuestion in Quiz 7.5

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

Extension of QuizQuestion.

Hierarchy

Expanded class hierarchy of ShortAnswerQuestion

1 string reference to 'ShortAnswerQuestion'
short_answer_quiz_question_info in question_types/short_answer/short_answer.module
Implements hook_quiz_question_info().

File

question_types/short_answer/short_answer.classes.inc, line 21
Short answer classes.

View source
class ShortAnswerQuestion extends QuizQuestion {

  // Constants for answer matching options.
  const ANSWER_MATCH = 0;
  const ANSWER_INSENSITIVE_MATCH = 1;
  const ANSWER_REGEX = 2;
  const ANSWER_MANUAL = 3;

  /**
   * Implementation of saveNodeProperties().
   *
   * @see QuizQuestion::saveNodeProperties()
   */
  public function saveNodeProperties($is_new = FALSE) {
    db_merge('quiz_short_answer_node_properties')
      ->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,
      'correct_answer_evaluation' => $this->node->correct_answer_evaluation,
    ))
      ->execute();
  }

  /**
   * Implementation of validateNode().
   *
   * @see QuizQuestion::validateNode()
   */
  public function validateNode(array &$form) {
    if ($this->node->correct_answer_evaluation != self::ANSWER_MANUAL && empty($this->node->correct_answer)) {
      form_set_error('correct_answer', t('An answer must be specified for any evaluation type other than manual scoring.'));
    }
  }

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

  /**
   * 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, correct_answer_evaluation FROM {quiz_short_answer_node_properties}
      WHERE nid = :nid AND vid = :vid', array(
      ':nid' => $this->node->nid,
      ':vid' => $this->node->vid,
    ))
      ->fetchAssoc();
    $this->nodeProperties = is_array($res_a) ? array_merge($props, $res_a) : $props;
    return $this->nodeProperties;
  }

  /**
   * Implementation of getNodeView().
   *
   * @see QuizQuestion::getNodeView()
   */
  public function getNodeView() {
    $content = parent::getNodeView();
    if ($this
      ->viewCanRevealCorrect()) {
      $content['answers'] = array(
        '#markup' => '<div class="quiz-solution">' . check_plain($this->node->correct_answer) . '</div>',
        '#weight' => 2,
      );
    }
    else {
      $content['answers'] = array(
        '#markup' => '<div class="quiz-answer-hidden">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);

    //$form['#theme'] = 'short_answer_answering_form';
    $element += array(
      '#type' => 'textfield',
      '#title' => t('Answer'),
      '#description' => t('Enter your answer here'),
      '#default_value' => '',
      '#size' => 60,
      '#maxlength' => 256,
      '#required' => FALSE,
      '#attributes' => array(
        'autocomplete' => 'off',
      ),
    );
    if (isset($result_id)) {
      $response = new ShortAnswerResponse($result_id, $this->node);
      $element['#default_value'] = $response
        ->getResponse();
    }
    return $element;
  }

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

  /**
   * Implementation of getCreationForm().
   *
   * @see QuizQuestion::getCreationForm()
   */
  public function getCreationForm(array &$form_state = NULL) {
    $form['answer'] = array(
      '#type' => 'fieldset',
      '#title' => t('Answer'),
      '#description' => t('Provide the answer and the method by which the answer will be evaluated.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => -4,
    );
    $options = array(
      self::ANSWER_MATCH => t('Automatic and case sensitive'),
      self::ANSWER_INSENSITIVE_MATCH => t('Automatic. Not case sensitive'),
    );
    $access_regex = user_access('use regex for short answer');
    if ($access_regex) {
      $options[self::ANSWER_REGEX] = t('Match against a regular expression (answer must match the supplied regular expression)');
    }
    $options[self::ANSWER_MANUAL] = t('Manual');
    $form['answer']['correct_answer_evaluation'] = array(
      '#type' => 'radios',
      '#title' => t('Pick an evaluation method'),
      '#description' => t('Choose how the answer shall be evaluated.'),
      '#options' => $options,
      '#default_value' => isset($this->node->correct_answer_evaluation) ? $this->node->correct_answer_evaluation : self::ANSWER_INSENSITIVE_MATCH,
      '#required' => FALSE,
    );
    if ($access_regex) {
      $form['answer']['regex_box'] = array(
        '#type' => 'fieldset',
        '#title' => t('About regular expressions'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['answer']['regex_box']['regex_help'] = array(
        '#markup' => '<p>' . t('Regular expressions are an advanced syntax for pattern matching. They allow you to create a concise set of rules that must be met before a value can be considered a match.') . '</p><p>' . t('For more on regular expression syntax, visit !url.', array(
          '!url' => l(t('the PHP regular expressions documentation'), 'http://www.php.net/manual/en/book.pcre.php'),
        )) . '</p>',
      );
    }
    $form['answer']['correct_answer'] = array(
      '#type' => 'textfield',
      '#title' => t('Correct answer'),
      '#description' => t('Specify the answer. If this question is manually scored, no answer needs to be supplied.'),
      '#default_value' => isset($this->node->correct_answer) ? $this->node->correct_answer : '',
      '#size' => 60,
      '#maxlength' => 256,
      '#required' => FALSE,
    );
    return $form;
  }

  /**
   * Implementation of getMaximumScore().
   *
   * @see QuizQuestion::getMaximumScore()
   */
  public function getMaximumScore() {
    return variable_get('short_answer_default_max_score', 5);
  }

  /**
   * Evaluate the correctness of an answer.
   *
   * The way we decide how to evaluate the correctness depends on the
   * evaluation method of the question.
   *
   * @param string $user_answer
   *   The answer given by the user.
   *
   * @return int
   *   The given score for the answer.
   */
  public function evaluateAnswer($user_answer) {
    $score = 0;

    // Ignore white spaces for correct answer and user's answer.
    $user_answer = trim($user_answer);
    $this->node->correct_answer = trim($this->node->correct_answer);
    switch ($this->node->correct_answer_evaluation) {
      case self::ANSWER_MATCH:
        if ($user_answer == $this->node->correct_answer) {
          $score = $this->node->max_score;
        }
        break;
      case self::ANSWER_INSENSITIVE_MATCH:
        if (drupal_strtolower($user_answer) == drupal_strtolower($this->node->correct_answer)) {
          $score = $this->node->max_score;
        }
        break;
      case self::ANSWER_REGEX:
        if (preg_match($this->node->correct_answer, $user_answer) > 0) {
          $score = $this->node->max_score;
        }
        break;
    }
    return $score;
  }

}

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::getBodyFieldTitle public function Allow question types to override the body field title. 4
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
ShortAnswerQuestion::ANSWER_INSENSITIVE_MATCH constant
ShortAnswerQuestion::ANSWER_MANUAL constant
ShortAnswerQuestion::ANSWER_MATCH constant
ShortAnswerQuestion::ANSWER_REGEX constant
ShortAnswerQuestion::delete public function Implementation of delete(). Overrides QuizQuestion::delete
ShortAnswerQuestion::evaluateAnswer public function Evaluate the correctness of an answer.
ShortAnswerQuestion::getAnsweringForm public function Implementation of getAnsweringForm(). Overrides QuizQuestion::getAnsweringForm
ShortAnswerQuestion::getAnsweringFormValidate public function Question response validator. Overrides QuizQuestion::getAnsweringFormValidate
ShortAnswerQuestion::getCreationForm public function Implementation of getCreationForm(). Overrides QuizQuestion::getCreationForm
ShortAnswerQuestion::getMaximumScore public function Implementation of getMaximumScore(). Overrides QuizQuestion::getMaximumScore
ShortAnswerQuestion::getNodeProperties public function Implementation of getNodeProperties(). Overrides QuizQuestion::getNodeProperties
ShortAnswerQuestion::getNodeView public function Implementation of getNodeView(). Overrides QuizQuestion::getNodeView
ShortAnswerQuestion::saveNodeProperties public function Implementation of saveNodeProperties(). Overrides QuizQuestion::saveNodeProperties
ShortAnswerQuestion::validateNode public function Implementation of validateNode(). Overrides QuizQuestion::validateNode