You are here

class LongAnswerResponse in Quiz 7.5

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

Extension of QuizQuestionResponse.

Hierarchy

Expanded class hierarchy of LongAnswerResponse

1 string reference to 'LongAnswerResponse'
long_answer_quiz_question_info in question_types/long_answer/long_answer.module
Implements hook_quiz_question_info().

File

question_types/long_answer/long_answer.classes.inc, line 201
Long answer classes.

View source
class LongAnswerResponse 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)) {

      // Question has been answered already. We fetch the answer data from the
      // database.
      $r = db_query('SELECT *
        FROM {quiz_long_answer_user_answers}
        WHERE result_answer_id = :raid', array(
        ':raid' => $this->result_answer_id,
      ))
        ->fetch();
      if (!empty($r)) {
        $this->answer = $r->answer;
        $this->answer_format = $r->answer_format;
        $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 {
      $this->answer = $answer;
      $this->evaluated = FALSE;
    }
  }

  /**
   * Implementation of save().
   *
   * @see QuizQuestionResponse::save()
   */
  public function save() {
    if (!$this->quizQuestion->node->answer_text_processing) {
      $answer = $this->answer;
      $this->answer = array();
      $this->answer['value'] = $answer;
      $this->answer['format'] = 'plain_text';
    }
    db_merge('quiz_long_answer_user_answers')
      ->key(array(
      'result_answer_id' => $this->result_answer_id,
    ))
      ->fields(array(
      'answer' => $this->answer['value'],
      'answer_format' => $this->answer['format'],
      'result_answer_id' => $this->result_answer_id,
    ))
      ->execute();
  }

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

  /**
   * Implementation of score().
   *
   * @see QuizQuestionResponse::score()
   */
  public function score() {
    return (int) db_query('SELECT score FROM {quiz_long_answer_user_answers}
      WHERE result_answer_id = :raid', array(
      ':raid' => $this->result_answer_id,
    ))
      ->fetchField();
  }

  /**
   * 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 long answer as there are no
      // choices.
      'choice' => NULL,
      'attempt' => check_markup($this->answer, $this->answer_format),
      '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' => check_markup($this->question->rubric['value'], $this->question->rubric['format']),
    );
    return $data;
  }

  /**
   * Implementation of getReportFormAnswerFeedback().
   *
   * @see QuizQuestionResponse::getReportFormAnswerFeedback()
   */
  public function getReportFormAnswerFeedback() {
    return array(
      '#title' => t('Enter feedback'),
      '#type' => 'text_format',
      '#default_value' => isset($this->answer_feedback) ? check_markup($this->answer_feedback, $this->answer_feedback_format) : '',
      '#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 'long_answer_report_submit';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LongAnswerResponse::$answer_feedback private property
LongAnswerResponse::$answer_feedback_format private property
LongAnswerResponse::$answer_id protected property ID of the answer.
LongAnswerResponse::delete public function Implementation of delete(). Overrides QuizQuestionResponse::delete
LongAnswerResponse::getFeedbackValues public function Implementation of getFeedbackValues(). Overrides QuizQuestionResponse::getFeedbackValues
LongAnswerResponse::getReportFormAnswerFeedback public function Implementation of getReportFormAnswerFeedback(). Overrides QuizQuestionResponse::getReportFormAnswerFeedback
LongAnswerResponse::getReportFormSubmit public function Implementation of getReportFormSubmit(). Overrides QuizQuestionResponse::getReportFormSubmit
LongAnswerResponse::getResponse public function Implementation of getResponse(). Overrides QuizQuestionResponse::getResponse
LongAnswerResponse::save public function Implementation of save(). Overrides QuizQuestionResponse::save
LongAnswerResponse::score public function Implementation of score(). Overrides QuizQuestionResponse::score
LongAnswerResponse::__construct public function Constructor. Overrides QuizQuestionResponse::__construct
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