You are here

class ClozeResponse in Cloze 6

Same name and namespace in other branches
  1. 7 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 220
The main classes for the short answer question type.

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_fetch_object(db_query('SELECT answer_id, answer, score, question_vid, question_nid, result_id FROM {quiz_cloze_user_answers} WHERE question_nid = %d AND question_vid = %d AND result_id = %d', $question_node->nid, $question_node->vid, $result_id));
      if (!empty($r)) {
        $this->answer = unserialize($r->answer);
        $this->score = $r->score;
        $this->answer_id = $r->answer_id;
        $this->answer_feedback = $r->answer_feedback;
      }
    }
    else {
      $this->answer = $answer;
    }
  }

  /**
   * Implementation of isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {

    /*
    if (trim($this->answer) == '') {
      return t('You must provide an answer');
    }
    */
    return TRUE;
  }

  /**
   * Implementation of save
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    $sql = 'INSERT INTO {quiz_cloze_user_answers} (answer, question_nid, question_vid, result_id, score) VALUES ("%s", %d, %d, %d, %d)';
    db_query($sql, serialize($this->answer), $this->question->nid, $this->question->vid, $this->rid, $this
      ->getScore(FALSE));
    $this->answer_id = db_last_insert_id('quiz_cloze_user_answers', 'answer_id');
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    db_query('DELETE FROM {quiz_cloze_user_answers} WHERE question_nid = %d AND question_vid = %d AND result_id = %d', $this->question->nid, $this->question->vid, $this->rid);
  }

  /**
   * 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 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';
    if ($this->question && !empty($this->question->answers)) {
      $answer = (object) current($this->question->answers);
    }
    else {
      return $form;
    }
    $question = $this->question->body;
    $correct_answer = _cloze_get_correct_answer($question);
    $user_answer = _cloze_get_user_answer($question, $this->answer);
    $form['answer'] = array(
      '#type' => 'markup',
      '#value' => theme('cloze_user_answer', $user_answer, $correct_answer),
    );
    return $form;
  }

  /**
   * Implementation of getReportFormScore
   *
   * @see QuizQuestionResponse#getReportFormScore($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormScore($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
    $score = $this
      ->isEvaluated() ? $this
      ->getScore() : '?';
    if (quiz_access_to_score() && $allow_scoring && $this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
      return array(
        '#type' => 'textfield',
        '#default_value' => $score,
        '#size' => 3,
        '#maxlength' => 3,
        '#attributes' => array(
          'class' => 'quiz-report-score',
        ),
      );
    }
    else {
      return array(
        '#type' => 'markup',
        '#value' => $score,
      );
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClozeResponse::$answer_id protected property ID of the answer.
ClozeResponse::delete public function Implementation of delete
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