class LongAnswerResponse in OG Quiz 7
Extension of QuizQuestionResponse
Hierarchy
- class \QuizQuestionResponse- class \LongAnswerResponse
 
Expanded class hierarchy of LongAnswerResponse
File
- includes/og_long_answer.php, line 177 
- Long answer classes.
View source
class LongAnswerResponse extends QuizQuestionResponse {
  /**
   * Get all scores that have not yet been evaluated.
   *
   * @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;
    $query = db_select('quiz_long_answer_user_answers', 'a');
    $query
      ->fields('a', array(
      'result_id',
      'question_nid',
      'question_vid',
      'answer_feedback',
    ));
    $query
      ->fields('r', array(
      'title',
    ));
    $query
      ->fields('qnr', array(
      'time_end',
      'time_start',
      'uid',
    ));
    $query
      ->join('node_revision', 'r', 'a.question_vid = r.vid');
    $query
      ->join('quiz_node_results', 'qnr', 'a.result_id = qnr.result_id');
    $query
      ->join('node', 'n', 'qnr.nid = n.nid');
    $query
      ->condition('a.is_evaluated', 0);
    if (user_access('score own quiz') && user_access('score taken quiz answer')) {
      $query
        ->condition(db_or()
        ->condition('n.uid', $user->uid)
        ->condition('qnr.uid', $user->uid));
    }
    else {
      if (user_access('score own quiz')) {
        $query
          ->condition('n.uid', $user->uid);
      }
      else {
        if (user_access('score taken quiz answer')) {
          $query
            ->condition('qnr.uid', $user->uid);
        }
      }
    }
    $results = $query
      ->execute();
    $unscored = array();
    foreach ($results as $row) {
      $unscored[] = $row;
    }
    return $unscored;
  }
  /**
   * Given a quiz, return a list of all of the unscored answers.
   *
   * @param $nid
   *  Node ID for the quiz to check.
   * @param $vid
   *  Version ID for the quiz 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('SELECT result_id FROM {quiz_long_answer_user_answers}
      WHERE is_evaluated = :is_evaluated
      AND question_nid = :question_nid
      AND question_vid = :question_vid', array(
      ':is_evaluated' => 0,
      ':question_nid' => $nid,
      ':question_vid' => $vid,
    ));
    $unscored = array();
    foreach ($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)) {
      // Question has been answered allready. We fetch the answer data from the database.
      $r = db_query('SELECT answer_id, answer, is_evaluated, score, question_vid, question_nid, result_id, answer_feedback, answer_feedback_format
        FROM {quiz_long_answer_user_answers}
        WHERE question_nid = :qnid AND question_vid = :qvid AND result_id = :rid', array(
        ':qnid' => $question_node->nid,
        ':qvid' => $question_node->vid,
        ':rid' => $result_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 {
      $this->answer = $answer;
      $this->evaluated = FALSE;
    }
  }
  /**
   * 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() {
    $this->answer_id = db_insert('quiz_long_answer_user_answers')
      ->fields(array(
      'answer' => $this->answer,
      'question_nid' => $this->question->nid,
      'question_vid' => $this->question->vid,
      'result_id' => $this->rid,
    ))
      ->execute();
  }
  /**
   * Implementation of delete
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    db_delete('quiz_long_answer_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() {
    return (int) db_query('SELECT score FROM {quiz_long_answer_user_answers}
      WHERE result_id = :result_id AND question_vid = :question_vid', array(
      ':result_id' => $this->rid,
      ':question_vid' => $this->question->vid,
    ))
      ->fetchField();
  }
  /**
   * 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'] = 'long_answer_response_form';
    if ($this->question && !empty($this->question->answers)) {
      $answer = (object) current($this->question->answers);
    }
    else {
      return $form;
    }
    $form['answer'] = array(
      '#markup' => check_markup($answer->answer),
    );
    if ($answer->is_evaluated == 1) {
      // Show feedback, if any.
      if ($showfeedback && !empty($answer->feedback)) {
        $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) {
      $form['rubric'] = array(
        '#type' => 'item',
        '#title' => t('Rubric'),
        '#markup' => check_markup($this->question->rubric),
      );
    }
    if (!$allow_scoring && !empty($this->answer_feedback)) {
      $form['answer_feedback'] = array(
        '#title' => t('Feedback'),
        '#type' => 'item',
        '#markup' => '<span class="quiz_answer_feedback">' . $this->answer_feedback . '</span>',
      );
    }
    if (!empty($feedback)) {
      $form['feedback'] = array(
        '#markup' => '<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) {
    // The score will be shown as a questionmark if the question haven't been scored already
    $score = $this
      ->isEvaluated() ? $this
      ->getScore() : '?';
    // We show a textfield if the quiz shall be scored. Markup otherwise
    if ($this
      ->quizContextAccessToScore() && $allow_scoring) {
      return array(
        '#type' => 'textfield',
        '#default_value' => $score,
        '#size' => 3,
        '#maxlength' => 3,
        '#attributes' => array(
          'class' => array(
            'quiz-report-score',
          ),
        ),
      );
    }
    else {
      return array(
        '#markup' => $score,
      );
    }
  }
  public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    if ($this
      ->quizContextAccessToScore() && $allow_scoring) {
      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 : NULL,
        '#attributes' => array(
          'class' => array(
            '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) {
    return $allow_scoring ? 'long_answer_report_submit' : FALSE;
  }
  /**
   * Implementation of getReportFormValidate
   *
   * @see QuizQuestionResponse#getReportFormValidate($showfeedback, $showpoints, $allow_scoring)
   */
  public function getReportFormValidate($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
    return $allow_scoring ? 'long_answer_report_validate' : FALSE;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| LongAnswerResponse:: | protected | property | ID of the answer. | |
| LongAnswerResponse:: | public | function | Implementation of delete Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public static | function | Get all scores that have not yet been evaluated. | |
| LongAnswerResponse:: | public static | function | Given a quiz, return a list of all of the unscored answers. | |
| LongAnswerResponse:: | public | function | Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of getReportFormResponse Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of getReportFormScore Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of getReportFormSubmit Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of getReportFormValidate Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of getResponse Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of isValid Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of save Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Implementation of score Overrides QuizQuestionResponse:: | |
| LongAnswerResponse:: | public | function | Constructor Overrides QuizQuestionResponse:: | |
| QuizQuestionResponse:: | protected | property | ||
| QuizQuestionResponse:: | protected | property | ||
| QuizQuestionResponse:: | protected | property | ||
| QuizQuestionResponse:: | public | property | ||
| QuizQuestionResponse:: | public | property | ||
| QuizQuestionResponse:: | public | property | ||
| QuizQuestionResponse:: | protected | property | ||
| QuizQuestionResponse:: | protected | property | 1 | |
| QuizQuestionResponse:: | protected | function | Utility function that returns the format of the node body | |
| QuizQuestionResponse:: | public | function | Returns stored max score if it exists, if not the max score is calculated and returned. | |
| QuizQuestionResponse:: | public | function | Fetch the parent quiz. | |
| QuizQuestionResponse:: | public | function | Get data suitable for reporting a user's score on the question. This expects an object with the following attributes: | |
| QuizQuestionResponse:: | public | function | Creates the report form for the admin pages, and for when a user gets feedback after answering questions. | |
| QuizQuestionResponse:: | public | function | get the question part of the reportForm | |
| QuizQuestionResponse:: | public | function | Get the theme key for the reportForm | |
| QuizQuestionResponse:: | function | Returns stored score if it exists, if not the score is calculated and returned. | ||
| QuizQuestionResponse:: | function | Check to see if the answer is marked as correct. | ||
| QuizQuestionResponse:: | 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:: | public | function | Helper function to determine if a user has access to score a quiz. | |
| QuizQuestionResponse:: | public | function | Used to refresh this instances question node in case drupal has changed it. | |
| QuizQuestionResponse:: | public | function | Saves the quiz result. This is not used when a question is skipped! | |
| QuizQuestionResponse:: | function | Represent the response as a stdClass object. | 
