You are here

class MultichoiceResponse in Quiz 6.4

Same name and namespace in other branches
  1. 7.6 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
  2. 7 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
  3. 7.4 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
  4. 7.5 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse

Extension of QuizQuestionResponse

Hierarchy

Expanded class hierarchy of MultichoiceResponse

1 string reference to 'MultichoiceResponse'
multichoice_quiz_question_info in question_types/multichoice/multichoice.module
Implementation of hook_quiz_question_info().

File

question_types/multichoice/multichoice.classes.inc, line 693
The main classes for the multichoice question type.

View source
class MultichoiceResponse extends QuizQuestionResponse {

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

  /**
   * Constructor
   */
  public function __construct($result_id, stdClass $question_node, $tries = NULL) {
    parent::__construct($result_id, $question_node, $tries);
    $this->user_answer_ids = array();

    // tries is the tries part of the post data
    if (is_array($tries)) {
      if (isset($tries['choice_order'])) {
        $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;
          $this->answer = $this->user_answer_ids;

          //@todo: Stop using user_answer_ids and only use answer instead...
        }
      }
      elseif (isset($tries['answer'])) {
        $this->user_answer_ids[] = $tries['answer'];
      }
    }
    else {
      $sql = 'SELECT answer_id
              FROM {quiz_multichoice_user_answers} ua
              LEFT OUTER JOIN {quiz_multichoice_user_answer_multi} uam ON(uam.user_answer_id = ua.id)
              WHERE ua.result_id = %d AND ua.question_nid = %d AND ua.question_vid = %d';
      $res = db_query($sql, $result_id, $this->question->nid, $this->question->vid);
      while ($res_o = db_fetch_object($res)) {
        $this->user_answer_ids[] = $res_o->answer_id;
      }
    }
  }

  /**
   * Implementation of isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    if ($this->question->choice_multi) {
      return TRUE;
    }
    if (empty($this->user_answer_ids)) {
      return t('You must provide an answer');
    }

    // Perform extra check since FAPI isn't beeing used:
    if (count($this->user_answer_ids) > 1) {
      return t('You are only allowed to select one answer');
    }
    return TRUE;
  }

  /**
   * Implementation of save
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    $sql = "INSERT INTO {quiz_multichoice_user_answers}\n            (result_id, question_vid, question_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_multichoice_user_answers}', 'id');
    for ($i = 0; $i < count($this->user_answer_ids); $i++) {
      $sql = 'INSERT INTO {quiz_multichoice_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
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    $sql = 'DELETE FROM {quiz_multichoice_user_answer_multi}
            USING {quiz_multichoice_user_answer_multi}
            INNER JOIN {quiz_multichoice_user_answers}
            ON {quiz_multichoice_user_answer_multi}.user_answer_id = {quiz_multichoice_user_answers}.id
            WHERE {quiz_multichoice_user_answers}.question_nid = %d
            AND {quiz_multichoice_user_answers}.question_vid = %d
            AND {quiz_multichoice_user_answers}.result_id = %d';
    db_query($sql, $this->question->nid, $this->question->vid, $this->rid);
    $sql = 'DELETE FROM {quiz_multichoice_user_answers}
            WHERE result_id = %d AND question_nid = %d AND question_vid = %d';
    db_query($sql, $this->rid, $this->question->nid, $this->question->vid);
  }

  /**
   * Implementation of score
   *
   * @return uint
   *
   * @see QuizQuestionResponse#score()
   */
  public function score() {
    if ($this->question->choice_boolean || $this
      ->isAllWrong()) {
      $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;
  }

  /**
   * If all answers in a question is wrong
   *
   * @return boolean
   *  TRUE if all answers are wrong. False otherwise.
   */
  public function isAllWrong() {
    foreach ($this->question->alternatives as $key => $alt) {
      if ($alt['score_if_chosen'] > 0 || $alt['score_if_not_chosen'] > 0) {
        return FALSE;
      }
    }
    return TRUE;
  }

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

  /**
   * Implementation of getReportFormResponse
   *
   * @see getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $i = 0;
    $this
      ->orderAlternatives($this->question->alternatives);

    // Find the alternative with the highest score
    if ($this->question->choice_multi == 0) {
      $max_score_if_chosen = -999;

      // TODO: Is is_array needed here?
      while (isset($this->question->alternatives[$i]) && is_array($this->question->alternatives[$i])) {
        $short = $this->question->alternatives[$i];
        if ($short['score_if_chosen'] > $max_score_if_chosen) {
          $max_score_if_chosen = $short['score_if_chosen'];
        }
        $i++;
      }
      $i = 0;
    }

    // Fetch all data for the report
    $data = array();
    while (isset($this->question->alternatives[$i])) {
      $short = $this->question->alternatives[$i];
      if (drupal_strlen($this
        ->checkMarkup($short['answer'], $short['answer_format'])) > 0) {
        $alternative = array();

        // Did the user choose the alternative?
        $alternative['is_chosen'] = in_array($short['id'], $this->user_answer_ids);

        // Questions where multiple answers isn't allowed are scored differently...
        if ($this->question->choice_multi == 0) {
          if ($this->question->choice_boolean == 0) {
            if ($short['score_if_chosen'] > $short['score_if_not_chosen']) {
              $alternative['is_correct'] = $short['score_if_chosen'] < $max_score_if_chosen ? 1 : 2;
            }
            else {
              $alternative['is_correct'] = 0;
            }
          }
          else {
            $alternative['is_correct'] = $short['score_if_chosen'] > $short['score_if_not_chosen'] ? 2 : 0;
          }
        }
        else {
          $alternative['is_correct'] = $short['score_if_chosen'] > $short['score_if_not_chosen'] ? 2 : 0;
        }
        $alternative['answer'] = $this
          ->checkMarkup($short['answer'], $short['answer_format'], FALSE);
        $not = $alternative['is_chosen'] ? '' : '_not';
        $alternative['feedback'] = $this
          ->checkMarkup($short['feedback_if' . $not . '_chosen'], $short['feedback_if' . $not . '_chosen_format'], FALSE);
        $data[] = $alternative;
      }
      $i++;
    }

    // Return themed report
    return array(
      '#type' => 'markup',
      '#value' => theme('multichoice_response', $data),
    );
  }

  /**
   * Order the alternatives according to the choice order stored in the database
   *
   * @param array $alternatives
   *  The alternatives to be ordered
   */
  private function orderAlternatives(array &$alternatives) {
    if (!$this->question->choice_random) {
      return;
    }
    $sql = "SELECT choice_order\n            FROM {quiz_multichoice_user_answers}\n            WHERE result_id = %d AND question_nid = %d AND question_vid = %d";
    $result = db_result(db_query($sql, $this->rid, $this->question->nid, $this->question->vid));
    if (!$result) {
      return;
    }
    $order = explode(',', $result);
    $newAlternatives = array();
    foreach ($order as $value) {
      foreach ($alternatives as $alternative) {
        if ($alternative['id'] == $value) {
          $newAlternatives[] = $alternative;
          break;
        }
      }
    }
    $alternatives = $newAlternatives;
  }

  /**
   * Run check_markup() on the field of the specified choice alternative
   *
   * @param $alternative
   *  String to be checked
   * @param $format
   *  The input format to be used
   * @param $check_user_access
   *  Whether or not we are to check the users access to the chosen format
   * @return HTML markup
   */
  private function checkMarkup($alternative, $format, $check_user_access = FALSE) {

    // If the string is empty we don't run it through input filters(They might add empty tags).
    if (drupal_strlen($alternative) == 0) {
      return '';
    }
    return check_markup($alternative, $format, $check_user_access);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultichoiceResponse::$choice_order protected property
MultichoiceResponse::$user_answer_ids protected property ID of the answers.
MultichoiceResponse::checkMarkup private function Run check_markup() on the field of the specified choice alternative
MultichoiceResponse::delete public function Implementation of delete Overrides QuizQuestionResponse::delete
MultichoiceResponse::getReportFormResponse public function Implementation of getReportFormResponse Overrides QuizQuestionResponse::getReportFormResponse
MultichoiceResponse::getResponse public function Implementation of getResponse Overrides QuizQuestionResponse::getResponse
MultichoiceResponse::isAllWrong public function If all answers in a question is wrong
MultichoiceResponse::isValid public function Implementation of isValid Overrides QuizQuestionResponse::isValid
MultichoiceResponse::orderAlternatives private function Order the alternatives according to the choice order stored in the database
MultichoiceResponse::save public function Implementation of save Overrides QuizQuestionResponse::save
MultichoiceResponse::score public function Implementation of score Overrides QuizQuestionResponse::score
MultichoiceResponse::__construct public function Constructor Overrides QuizQuestionResponse::__construct
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::getReportFormAnswerFeedback public function 2
QuizQuestionResponse::getReportFormQuestion public function get the question part of the reportForm
QuizQuestionResponse::getReportFormScore public function Get the score part of the report form 2
QuizQuestionResponse::getReportFormSubmit public function Get the submit function for the reportForm 2
QuizQuestionResponse::getReportFormTheme public function Get the theme key for the reportForm
QuizQuestionResponse::getReportFormValidate public function Get the validate function for the reportForm 2
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.