You are here

class ShortAnswerResponse in Quiz 6.4

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.5 question_types/short_answer/short_answer.classes.inc \ShortAnswerResponse
  4. 7.6 question_types/short_answer/short_answer.classes.inc \ShortAnswerResponse
  5. 7 question_types/short_answer/short_answer.classes.inc \ShortAnswerResponse
  6. 7.4 question_types/short_answer/short_answer.classes.inc \ShortAnswerResponse
  7. 7.5 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
Implementation of hook_quiz_question_info().

File

question_types/short_answer/short_answer.classes.inc, line 250
The main classes for the short answer question type.

View source
class ShortAnswerResponse extends QuizQuestionResponse {

  /**
   * Get all quiz scores that haven't been evaluated yet.
   *
   * @param $count
   *  Number of items to return (default: 50).
   * @param $offset
   *  Where in the results we should start (default: 0).
   *
   * @return
   *  Array of objects describing unanswered questions. Each object will have result_id, question_nid, and question_vid.
   */
  public static function fetchAllUnscoredAnswers($count = 50, $offset = 0) {
    global $user;
    $sql = 'SELECT a.result_id, a.question_nid, a.question_vid, a.answer_feedback, r.title, qnr.time_end, qnr.time_start, qnr.uid
            FROM {quiz_short_answer_user_answers} AS a
            INNER JOIN {node_revisions} r ON a.question_vid = r.vid
            INNER JOIN {quiz_node_results} qnr ON a.result_id = qnr.result_id
            JOIN {node} n ON qnr.nid = n.nid
            WHERE a.is_evaluated = 0';
    if (!user_access('score any quiz')) {
      $sql .= ' AND n.uid = %d';
    }
    $results = db_query_range(db_rewrite_sql($sql), $user->uid, $offset, $count);
    $unscored = array();
    if ($results) {
      while ($row = db_fetch_object($results)) {
        $unscored[] = $row;
      }
    }
    return $unscored;
  }

  /**
   * Given a question, return a list of all of the unscored answers.
   *
   * @param $nid
   *  Node ID for the question to check.
   * @param $vid
   *  Version ID for the question to check.
   * @param $count
   *  Number of items to return (default: 50).
   * @param $offset
   *  Where in the results we should start (default: 0).
   *
   * @return
   *  Indexed array of result IDs that need to be scored.
   */
  public static function fetchUnscoredAnswersByQuestion($nid, $vid, $count = 50, $offset = 0) {
    $results = db_query_range('SELECT result_id FROM {quiz_short_answer_user_answers} WHERE is_evaluated = 0 AND question_nid = %d AND question_vid = %d', $nid, $vid, $offset, $count);
    $unscored = array();
    foreach (db_fetch_object($results) as $row) {
      $unscored[] = $row->result_id;
    }
    return $unscored;
  }

  /**
   * 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)) {
      $sql = "SELECT answer_id, answer, is_evaluated, score, question_vid, question_nid, result_id, answer_feedback\n        FROM {quiz_short_answer_user_answers}\n        WHERE question_nid = %d AND question_vid = %d AND result_id = %d";
      $r = db_fetch_object(db_query($sql, $question_node->nid, $question_node->vid, $result_id));
      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;
      }
    }
    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 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() {

    // 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);
    $sql = "INSERT INTO {quiz_short_answer_user_answers}\n      (answer, question_nid, question_vid, result_id, score, is_evaluated)\n      VALUES ('%s', %d, %d, %d, %d, %d)";
    db_query($sql, $this->answer, $this->question->nid, $this->question->vid, $this->rid, $this
      ->getScore(FALSE), $this->is_evaluated);
    $this->answer_id = db_last_insert_id('quiz_long_answer_user_answers', 'answer_id');
  }

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

  /**
   * 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) {
      $sql = "SELECT score FROM {quiz_short_answer_user_answers} WHERE result_id = %d AND question_vid = %d";
      $score = db_result(db_query($sql, $this->rid, $this->question->vid));
    }
    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 getReportFormResponse
   *
   * @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $form = array();
    $form['#theme'] = 'short_answer_response_form';
    if ($this->question && !empty($this->question->answers)) {
      $answer = (object) current($this->question->answers);
    }
    else {
      return $form;
    }
    $form['answer'] = array(
      '#type' => 'markup',
      '#value' => theme('short_answer_user_answer', check_plain($answer->answer), check_plain($this->question->correct_answer)),
    );
    if ($answer->is_evaluated == 1) {

      // Show feedback, if any.
      if ($showfeedback && !empty($answer->feedback)) {

        // @todo: Feedback doesn't seem to be in use anymore...
        $feedback = check_markup($answer->feedback);
      }
    }
    else {
      $feedback = t('This answer has not yet been scored.') . '<br/>' . t('Until the answer is scored, the total score will not be correct.');
    }
    if (!$allow_scoring && !empty($this->answer_feedback)) {
      $form['answer_feedback'] = array(
        '#title' => t('Feedback'),
        '#type' => 'item',
        '#value' => '<span class="quiz_answer_feedback">' . $this->answer_feedback . '</span>',
      );
    }
    if (!empty($feedback)) {
      $form['feedback'] = array(
        '#type' => 'markup',
        '#value' => '<span class="quiz_answer_feedback">' . $feedback . '</span>',
      );
    }
    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,
      );
    }
  }
  public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    if (quiz_access_to_score() && $allow_scoring && $this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
      return array(
        '#title' => t('Enter feedback'),
        '#type' => 'textarea',
        '#default_value' => $this->answer_feedback,
        '#attributes' => array(
          'class' => 'quiz-report-score',
        ),
      );
    }
    return FALSE;
  }

  /**
   * Implementation of getReportFormSubmit
   *
   * @see QuizQuestionResponse#getReportFormSubmit($showfeedback, $showpoints, $allow_scoring)
   */
  public function getReportFormSubmit($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
    if (quiz_access_to_score() && $allow_scoring && $this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
      return $allow_scoring ? 'short_answer_report_submit' : FALSE;
    }
    return FALSE;
  }

  /**
   * Implementation of getReportFormValidate
   *
   * @see QuizQuestionResponse#getReportFormValidate($showfeedback, $showpoints, $allow_scoring)
   */
  public function getReportFormValidate($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
    if (quiz_access_to_score() && $allow_scoring && $this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
      return $allow_scoring ? 'short_answer_report_validate' : FALSE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuizQuestionResponse::$answer protected property
QuizQuestionResponse::$evaluated protected property
QuizQuestionResponse::$is_correct protected property
QuizQuestionResponse::$is_skipped public property
QuizQuestionResponse::$question public property
QuizQuestionResponse::$rid protected property
QuizQuestionResponse::$score protected property 7
QuizQuestionResponse::getMaxScore public function Returns stored max score if it exists, if not the max score is calculated and returned.
QuizQuestionResponse::getReport public function Get data suitable for reporting a user's score on the question. This expects an object with the following attributes:
QuizQuestionResponse::getReportForm public function Creates the report form for the admin pages, and for when a user gets feedback after answering questions. 1
QuizQuestionResponse::getReportFormQuestion public function get the question part of the reportForm
QuizQuestionResponse::getReportFormTheme public function Get the theme key for the reportForm
QuizQuestionResponse::getScore function Returns stored score if it exists, if not the score is calculated and returned.
QuizQuestionResponse::isCorrect function Check to see if the answer is marked as correct. 1
QuizQuestionResponse::isEvaluated public function Indicate whether the response has been evaluated (scored) yet. Questions that require human scoring (e.g. essays) may need to manually toggle this.
QuizQuestionResponse::refreshQuestionNode public function Used to refresh this instances question node in case drupal has changed it.
QuizQuestionResponse::saveResult public function Saves the quiz result. This is not used when a question is skipped!
QuizQuestionResponse::toBareObject function Represent the response as a stdClass object.
ShortAnswerResponse::$answer_id protected property ID of the answer.
ShortAnswerResponse::delete public function Implementation of delete Overrides QuizQuestionResponse::delete
ShortAnswerResponse::fetchAllUnscoredAnswers public static function Get all quiz scores that haven't been evaluated yet.
ShortAnswerResponse::fetchUnscoredAnswersByQuestion public static function Given a question, return a list of all of the unscored answers.
ShortAnswerResponse::getReportFormAnswerFeedback public function Overrides QuizQuestionResponse::getReportFormAnswerFeedback
ShortAnswerResponse::getReportFormResponse public function Implementation of getReportFormResponse Overrides QuizQuestionResponse::getReportFormResponse
ShortAnswerResponse::getReportFormScore public function Implementation of getReportFormScore Overrides QuizQuestionResponse::getReportFormScore
ShortAnswerResponse::getReportFormSubmit public function Implementation of getReportFormSubmit Overrides QuizQuestionResponse::getReportFormSubmit
ShortAnswerResponse::getReportFormValidate public function Implementation of getReportFormValidate Overrides QuizQuestionResponse::getReportFormValidate
ShortAnswerResponse::getResponse public function Implementation of getResponse Overrides QuizQuestionResponse::getResponse
ShortAnswerResponse::isValid public function Implementation of isValid Overrides QuizQuestionResponse::isValid
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