You are here

class ShortAnswerResponse in Quiz 7.5

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

Extension of QuizQuestionResponse.

Hierarchy

Expanded class hierarchy of ShortAnswerResponse

1 string reference to 'ShortAnswerResponse'
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 270
Short answer classes.

View source
class ShortAnswerResponse extends QuizQuestionResponse {

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

  /**
   * Constructor.
   *
   * @param int $result_id
   *   The result ID for the user's result set. There is one result ID per time
   *   the user takes a quiz.
   * @param stdClass $question_node
   *   The question node.
   * @param mixed $answer
   *   The answer (dependent on question type).
   */
  public function __construct($result_id, stdClass $question_node, $answer = NULL) {
    parent::__construct($result_id, $question_node, $answer);
    if (!isset($answer)) {
      $r = db_query('SELECT *
        FROM {quiz_short_answer_user_answers}
        WHERE result_answer_id = :raid', array(
        ':raid' => $this->result_answer_id,
      ))
        ->fetch();
      if (!empty($r)) {
        $this->answer = $r->answer;
        $this->score = $r->score;
        $this->evaluated = $r->is_evaluated;
        $this->answer_id = $r->answer_id;
        $this->answer_feedback = $r->answer_feedback;
        $this->answer_feedback_format = $r->answer_feedback_format;
      }
    }
    else {
      if (is_array($answer)) {
        $this->answer = $answer['answer'];
      }
      else {
        $this->answer = $answer;
        $this->evaluated = $this->question->correct_answer_evaluation != ShortAnswerQuestion::ANSWER_MANUAL;
      }
    }
  }

  /**
   * Implementation of save().
   *
   * @see QuizQuestionResponse::save()
   */
  public function save() {

    // We need to set is_evaluated depending on whether the type requires
    // evaluation.
    $this->is_evaluated = (int) ($this->question->correct_answer_evaluation != ShortAnswerQuestion::ANSWER_MANUAL);
    db_merge('quiz_short_answer_user_answers')
      ->key(array(
      'result_answer_id' => $this->result_answer_id,
    ))
      ->fields(array(
      'answer' => $this->answer,
      'result_answer_id' => $this->result_answer_id,
      'score' => $this
        ->getScore(FALSE),
      'is_evaluated' => $this->is_evaluated,
    ))
      ->execute();
  }

  /**
   * Implementation of delete().
   *
   * @see QuizQuestionResponse::delete()
   */
  public function delete() {
    db_delete('quiz_short_answer_user_answers')
      ->condition('result_answer_id', $this->result_answer_id)
      ->execute();
  }

  /**
   * Implementation of score().
   *
   * @see QuizQuestionResponse::score()
   */
  public function score() {

    // Manual scoring means we go with what is in the DB.
    if ($this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
      $score = db_query('SELECT score FROM {quiz_short_answer_user_answers} WHERE result_answer_id = :raid', array(
        ':raid' => $this->result_answer_id,
      ))
        ->fetchField();
      if (!$score) {
        $score = 0;
      }
    }
    else {
      $shortAnswer = new ShortAnswerQuestion($this->question);
      $score = $shortAnswer
        ->evaluateAnswer($this
        ->getResponse());
    }
    return $score;
  }

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

  /**
   * Implementation of getFeedbackValues().
   *
   * @see QuizQuestionResponse::getFeedbackValues()
   */
  public function getFeedbackValues() {
    $data = array();
    $score = $this
      ->score();
    $max = $this
      ->getMaxScore(FALSE);
    if ($this->evaluated) {

      // Question has been graded.
      if ($score == 0) {
        $icon = quiz_icon('incorrect');
      }
      if ($score > 0) {
        $icon = quiz_icon('almost');
      }
      if ($score == $max) {
        $icon = quiz_icon('correct');
      }
    }
    else {
      $icon = quiz_icon('unknown');
    }
    $data[] = array(
      // Hide this column. Does not make sense for short answer as there are no
      // choices.
      'choice' => NULL,
      'attempt' => $this->answer,
      'correct' => $icon,
      'score' => !$this->evaluated ? t('This answer has not yet been scored.') : $this
        ->getScore(),
      'answer_feedback' => check_markup($this->answer_feedback, $this->answer_feedback_format),
      'solution' => $this->question->correct_answer,
    );
    return $data;
  }

  /**
   * Implementation of getReportFormAnswerFeedback().
   *
   * @see QuizQuestionResponse::getReportFormAnswerFeedback()
   */
  public function getReportFormAnswerFeedback() {
    return array(
      '#title' => t('Enter feedback'),
      '#type' => 'text_format',
      '#default_value' => $this->answer_feedback,
      '#format' => isset($this->answer_feedback_format) ? $this->answer_feedback_format : filter_default_format(),
      '#attributes' => array(
        'class' => array(
          'quiz-report-score',
        ),
      ),
    );
  }

  /**
   * Implementation of getReportFormSubmit().
   *
   * @see QuizQuestionResponse::getReportFormSubmit()
   */
  public function getReportFormSubmit() {
    return 'short_answer_report_submit';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuizQuestionResponse::$answer protected property
QuizQuestionResponse::$evaluated protected property
QuizQuestionResponse::$is_correct protected property
QuizQuestionResponse::$is_doubtful public property
QuizQuestionResponse::$is_skipped public property
QuizQuestionResponse::$question public property
QuizQuestionResponse::$quizQuestion public property
QuizQuestionResponse::$result_id protected property
QuizQuestionResponse::$score protected property 9
QuizQuestionResponse::canReview public function Can the quiz taker view the requested review?
QuizQuestionResponse::getFormat protected function Utility function that returns the format of the node body.
QuizQuestionResponse::getMaxScore public function Get the max score of this question response. 1
QuizQuestionResponse::getQuizQuestion public function Get the question of this question response.
QuizQuestionResponse::getReport public function Get data suitable for reporting a user's score on the question. 1
QuizQuestionResponse::getReportForm public function Creates the report form for the admin pages, and for when a user gets feedback after answering questions. 2
QuizQuestionResponse::getReportFormScore public function Implementation of getReportFormScore().
QuizQuestionResponse::getReportFormValidate public function Get the validate function for the reportForm.
QuizQuestionResponse::getScore public function Get the score of this question response.
QuizQuestionResponse::getWeightedRatio public function Get the weighted score ratio.
QuizQuestionResponse::isCorrect public function Check to see if the answer is marked as correct. 2
QuizQuestionResponse::isEvaluated public function Indicate whether the response has been evaluated (scored) yet.
QuizQuestionResponse::refreshQuestionNode public function Used to refresh this instances question node in case drupal has changed it.
QuizQuestionResponse::setResultAnswerId public function Set the target result answer ID for this Question response.
QuizQuestionResponse::toBareObject public function Represent the response as a stdClass object.
QuizQuestionResponse::viewsGetAnswers public static function Get answers for a question in a result. 3
ShortAnswerResponse::$answer_feedback private property
ShortAnswerResponse::$answer_feedback_format private property
ShortAnswerResponse::$answer_id protected property ID of the answer.
ShortAnswerResponse::delete public function Implementation of delete(). Overrides QuizQuestionResponse::delete
ShortAnswerResponse::getFeedbackValues public function Implementation of getFeedbackValues(). Overrides QuizQuestionResponse::getFeedbackValues
ShortAnswerResponse::getReportFormAnswerFeedback public function Implementation of getReportFormAnswerFeedback(). Overrides QuizQuestionResponse::getReportFormAnswerFeedback
ShortAnswerResponse::getReportFormSubmit public function Implementation of getReportFormSubmit(). Overrides QuizQuestionResponse::getReportFormSubmit
ShortAnswerResponse::getResponse public function Implementation of getResponse(). Overrides QuizQuestionResponse::getResponse
ShortAnswerResponse::save public function Implementation of save(). Overrides QuizQuestionResponse::save
ShortAnswerResponse::score public function Implementation of score(). Overrides QuizQuestionResponse::score
ShortAnswerResponse::__construct public function Constructor. Overrides QuizQuestionResponse::__construct