You are here

class ClozeResponse in Cloze 7

Same name and namespace in other branches
  1. 6 cloze.classes.inc \ClozeResponse

Extension of QuizQuestionResponse

Hierarchy

Expanded class hierarchy of ClozeResponse

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

File

./cloze.classes.inc, line 267
The main classes for the cloze question type. These inherit or implement code found in quiz_question.classes.inc.

View source
class ClozeResponse extends QuizQuestionResponse {

  /**
   * ID of the answer.
   */
  protected $answer_id = 0;

  /**
   * Constructor
   */
  public function __construct($result_id, stdClass $question_node, $answer = NULL) {
    parent::__construct($result_id, $question_node, $answer);
    if (!isset($answer)) {
      $r = db_query("SELECT answer_id, answer, score, question_vid, question_nid, result_id FROM {quiz_cloze_user_answers} WHERE question_nid = :question_nid AND question_vid = :question_vid AND result_id = :result_id", array(
        ':question_nid' => $question_node->nid,
        ':question_vid' => $question_node->vid,
        ':result_id' => $result_id,
      ))
        ->fetch();
      if (!empty($r)) {
        $this->answer = unserialize($r->answer);
        $this->score = $r->score;
        $this->answer_id = $r->answer_id;
      }
    }
    else {
      $this->answer = $answer;
    }
  }

  /**
   * Implementation of isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    return TRUE;
  }

  /**
   * Implementation of save
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    $this->answer_id = db_insert('quiz_cloze_user_answers')
      ->fields(array(
      'answer' => serialize($this->answer),
      'question_nid' => $this->question->nid,
      'question_vid' => $this->question->vid,
      'result_id' => $this->rid,
      'score' => $this
        ->getScore(FALSE),
    ))
      ->execute();
  }

  /**
   * Implementation of delete()
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    db_delete('quiz_cloze_user_answers')
      ->condition('question_nid', $this->question->nid)
      ->condition('question_vid', $this->question->vid)
      ->condition('result_id', $this->rid)
      ->execute();
  }

  /**
   * Implementation of score()
   *
   * @see QuizQuestionResponse#score()
   */
  public function score() {
    $shortAnswer = new ClozeQuestion($this->question);
    $score = $shortAnswer
      ->evaluateAnswer($this->answer);
    return $score;
  }

  /**
   * Implementation of getResponse()
   *
   * @see QuizQuestionResponse#getResponse()
   */
  public function getResponse() {
    return $this->answer;
  }

  /**
   * Implementation of getReportForm()
   *
   * @see QuizQuestionResponse#getReportForm($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportForm($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $form = parent::getReportForm($showpoints, $showfeedback, $allow_scoring);
    $question = strip_tags($form['question']['#markup']);
    $question_form['open_wrapper'] = array(
      '#markup' => '<div class="cloze-question">',
    );
    foreach (_cloze_get_question_chunks($question) as $position => $chunk) {
      if (strpos($chunk, '[') === FALSE) {

        // this "tries[foobar]" hack is needed becaues response handler engine checks for input field
        // with name tries
        $question_form['tries[' . $position . ']'] = array(
          '#markup' => 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) {
          $question_form['tries[' . $position . ']'] = array(
            '#type' => 'select',
            '#title' => '',
            '#options' => _cloze_shuffle_choices(drupal_map_assoc($choices)),
            '#required' => FALSE,
          );
        }
        else {
          $question_form['tries[' . $position . ']'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#size' => 32,
            '#required' => FALSE,
            '#attributes' => array(
              'autocomplete' => 'off',
            ),
          );
        }
      }
    }
    $question_form['close_wrapper'] = array(
      '#markup' => '</div>',
    );
    $form['question']['#markup'] = drupal_render($question_form);
    return $form;
  }

  /**
   * Implementation of getReportFormResponse()
   *
   * @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $form = array();
    $form['#theme'] = 'cloze_response_form';
    $form['#attached']['css'] = array(
      drupal_get_path('module', 'cloze') . '/theme/cloze.css',
    );
    if ($this->question && !empty($this->question->answers)) {
      $answer = (object) current($this->question->answers);
    }
    else {
      return $form;
    }
    $this->question = node_load($this->question->nid);
    $question = $this->question->body[LANGUAGE_NONE][0]['value'];
    $correct_answer = _cloze_get_correct_answer($question);
    $user_answer = _cloze_get_user_answer($question, $this->answer);
    $form['answer'] = array(
      '#markup' => theme('cloze_user_answer', array(
        'answer' => $user_answer,
        'correct' => $correct_answer,
      )),
    );
    return $form;
  }

  /**
   * Implementation of getReportFormScore()
   *
   * @see QuizQuestionResponse#getReportFormScore($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormScore($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
    return array(
      '#markup' => $this
        ->getScore(),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClozeResponse::$answer_id protected property ID of the answer.
ClozeResponse::delete public function Implementation of delete()
ClozeResponse::getReportForm public function Implementation of getReportForm()
ClozeResponse::getReportFormResponse public function Implementation of getReportFormResponse()
ClozeResponse::getReportFormScore public function Implementation of getReportFormScore()
ClozeResponse::getResponse public function Implementation of getResponse()
ClozeResponse::isValid public function Implementation of isValid
ClozeResponse::save public function Implementation of save
ClozeResponse::score public function Implementation of score()
ClozeResponse::__construct public function Constructor