You are here

class ChoiceResponse in Quiz 6.6

The short answer question response class.

Hierarchy

Expanded class hierarchy of ChoiceResponse

1 string reference to 'ChoiceResponse'
choice_quiz_question_info in question_types/choice/choice.module
Implementation of hook_quiz_question_info().

File

question_types/choice/choice.classes.inc, line 564
The main classes for the choice question type.

View source
class ChoiceResponse extends AbstractQuizQuestionResponse {

  /**
   * ID of the answers.
   */
  protected $user_answer_ids;
  protected $choice_order;

  /**
   * Constructor
   *
   * @param $rid - response_id
   * @param $question - as an object
   * @param $answer - answer_id
   */
  public function __construct($rid, $question, $tries = NULL) {
    $this->rid = $rid;
    $this->question = $question;
    $this->user_answer_ids = array();
    $this->choice_order = $tries['choice_order'];
    unset($tries['choice_order']);
    if (is_array($tries['answer'])) {
      foreach ($tries['answer'] as $answer_id) {
        $this->user_answer_ids[] = $answer_id;
      }
    }
    elseif (isset($tries['answer'])) {
      $this->user_answer_ids[] = $tries['answer'];
    }
    else {
      $sql = 'SELECT answer_id
              FROM {quiz_choice_user_answer_multi} uam
              JOIN {quiz_choice_user_answers} ua ON(uam.user_answer_id = ua.id)
              WHERE ua.result_id = %d AND ua.nid = %d AND ua.vid = %d';
      $res = db_query($sql, $rid, $this->question->nid, $this->question->vid);
      while ($res_o = db_fetch_object($res)) {
        $this->user_answer_ids[] = $res_o->answer_id;
      }
    }
  }

  /**
   * Implementation of save
   */
  public function save() {
    $sql = "INSERT INTO {quiz_choice_user_answers}\n            (result_id, vid, nid, choice_order)\n            VALUES (%d, %d, %d, '%s')";
    db_query($sql, $this->rid, $this->question->vid, $this->question->nid, $this->choice_order);
    $user_answer_id = db_last_insert_id('{quiz_choice_user_answers}', 'id');
    for ($i = 0; $i < count($this->user_answer_ids); $i++) {
      $sql = 'INSERT INTO {quiz_choice_user_answer_multi}
              (user_answer_id, answer_id)
              VALUES(%d, %d)';
      db_query($sql, $user_answer_id, $this->user_answer_ids[$i]);
    }
  }

  /**
   * Implementation of delete
   */
  public function delete() {
    $sql = 'DELETE FROM {quiz_choice_user_answer_multi}
            WHERE user_answer_id IN(
              SELECT id FROM {quiz_choice_user_answers}
              WHERE nid = %d AND vid = %d AND result_id = %d
            )';
    db_query($sql, $this->nid, $this->question->vid, $this->question->rid);
    $sql = 'DELETE FROM {quiz_choice_user_answers}
    		WHERE result_id = %d AND nid = %d AND vid = %d';
    db_query($sql, $this->rid, $this->question->nid, $this->question->vid);
  }

  /**
   * Implementation of score
   *
   * @return uint
   */
  public function score() {
    if ($this->question->choice_boolean) {
      $score = 1;
      foreach ($this->question->alternatives as $key => $alt) {
        if (in_array($alt['id'], $this->user_answer_ids)) {
          if ($alt['score_if_chosen'] < $alt['score_if_not_chosen']) {
            $score = 0;
          }
        }
        else {
          if ($alt['score_if_chosen'] > $alt['score_if_not_chosen']) {
            $score = 0;
          }
        }
      }
    }
    else {
      $score = 0;
      foreach ($this->question->alternatives as $key => $alt) {
        if (in_array($alt['id'], $this->user_answer_ids)) {
          $score += $alt['score_if_chosen'];
        }
        else {
          $score += $alt['score_if_not_chosen'];
        }
      }
    }
    return $score;
  }

  /**
   * Implementation of getResponse
   *
   * @return answer
   */
  public function getResponse() {
    return $this->user_answer_ids;
  }

  /**
   * Implementation of format report.
   *
   * @param $showpoints
   * @param $showfeedback
   * @return report as html
   */
  public function formatReport($showpoints = TRUE, $showfeedback = TRUE) {
    $slug = '<div class="quiz_summary_question"><span class="quiz_question_bullet">Q:</span> ' . check_markup($this->question->body, $this->question->filter, FALSE) . '</div>';
    $result = '<div class="quiz_answer_feedback">';
    $result .= '<H4>' . t('Alternatives') . ':</H4>';
    $i = 0;
    $this
      ->orderAlternatives($this->question->alternatives);
    while (is_array($this->question->alternatives[$i])) {
      $short = $this->question->alternatives[$i];
      if (drupal_strlen(strip_tags($short['answer'])) > 0) {
        $row = array();
        $the = $this->question->choice_multi == 1 ? t('a') : t('the');
        $p = drupal_get_path('module', 'choice');
        $rowspan = 1;
        $not = '';
        if ($short['score_if_chosen'] > $short['score_if_not_chosen']) {
          if (in_array($short['id'], $this->user_answer_ids)) {
            if (drupal_strlen(strip_tags($short['feedback_if_chosen'])) > 0) {
              $rowspan = 2;
            }
            $row[] = array(
              'data' => theme_image("{$p}./theme/images/correct.png", t('Correct'), t('You chose !the correct answer', array(
                '!the' => $the,
              )), NULL, FALSE),
              'width' => 35,
              'rowspan' => $rowspan,
            );
          }
          else {
            if (drupal_strlen(strip_tags($short['feedback_if_not_chosen'])) > 0) {
              $rowspan = 2;
              $not = '_not';
            }
            $row[] = array(
              'data' => theme_image("{$p}./theme/images/should.png", t('Should have chosen'), t('This is !the correct answer, but you didn\'t choose it', array(
                '!the' => $the,
              )), NULL, FALSE),
              'width' => 35,
              'rowspan' => $rowspan,
            );
          }
        }
        else {
          if (in_array($short['id'], $this->user_answer_ids)) {
            if (drupal_strlen(strip_tags($short['feedback_if_chosen'])) > 0) {
              $rowspan = 2;
            }
            $row[] = array(
              'data' => theme_image("{$p}./theme/images/wrong.png", t('Wrong'), t('You chose !the wrong answer', array(
                '!the' => $the,
              )), NULL, FALSE),
              'width' => 35,
              'rowspan' => $rowspan,
            );
          }
          else {
            if (drupal_strlen(strip_tags($short['feedback_if_not_chosen'])) > 0) {
              $rowspan = 2;
              $not = '_not';
            }
            $row[] = array(
              'data' => '',
              'width' => 35,
              'rowspan' => $rowspan,
            );
          }
        }
        $row[] = check_markup($short['answer'], $short['answer_format'], FALSE);
        $rows[] = $row;
        if ($rowspan > 1) {
          $feedback = check_markup($short['feedback_if' . $not . '_chosen'], $short['feedback_if' . $not . '_chosen_format'], FALSE);
          $rows[] = array(
            "<H5>Feedback:</H5>{$feedback}",
          );
        }
      }
      $i++;
    }
    $result .= theme('table', NULL, $rows);
    $result .= '</div>';
    return $slug . $result;
  }
  private function orderAlternatives(&$alternatives) {
    if (!$this->question->choice_random) {
      return;
    }
    $sql = "SELECT choice_order\n            FROM {quiz_choice_user_answers}\n            WHERE result_id = %d AND nid = %d AND vid = %d";
    $res = db_query($sql, $this->rid, $this->question->nid, $this->question->vid);
    $order = explode(',', db_result($res));
    $newAlternatives = array();
    foreach ($order as $value) {
      foreach ($alternatives as $alternative) {
        if ($alternative['id'] == $value) {
          $newAlternatives[] = $alternative;
          break;
        }
      }
    }
    $alternatives = $newAlternatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractQuizQuestionResponse::$answer protected property
AbstractQuizQuestionResponse::$evaluated protected property
AbstractQuizQuestionResponse::$is_correct public property
AbstractQuizQuestionResponse::$question protected property
AbstractQuizQuestionResponse::$rid public property
AbstractQuizQuestionResponse::$score protected property
AbstractQuizQuestionResponse::getReport public function
AbstractQuizQuestionResponse::getScore function
AbstractQuizQuestionResponse::isCorrect function Check to see if the answer is marked as correct. 4
AbstractQuizQuestionResponse::isEvaluated public function
AbstractQuizQuestionResponse::toBareObject function
ChoiceResponse::$choice_order protected property
ChoiceResponse::$user_answer_ids protected property ID of the answers.
ChoiceResponse::delete public function Implementation of delete
ChoiceResponse::formatReport public function Implementation of format report.
ChoiceResponse::getResponse public function Implementation of getResponse
ChoiceResponse::orderAlternatives private function
ChoiceResponse::save public function Implementation of save
ChoiceResponse::score public function Implementation of score
ChoiceResponse::__construct public function Constructor