You are here

class DragDropResponse in Quiz Drag Drop 7

Extension of QuizQuestionResponse

Hierarchy

Expanded class hierarchy of DragDropResponse

1 string reference to 'DragDropResponse'
quiz_drag_drop_quiz_question_info in ./quiz_drag_drop.module
Implements hook_quiz_question_info().

File

./quiz_drag_drop.classes.inc, line 267
Question type, enabling the creation of drag drop type of questions.

View source
class DragDropResponse extends QuizQuestionResponse {

  /**
   * Constructor.
   */
  public function __construct($result_id, stdClass $question_node, $tries = NULL) {
    parent::__construct($result_id, $question_node, $tries);
  }

  /**
   * Implementation of isValid.
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    $drop_count = $_POST['dropCount'];
    if ($drop_count != count($this->question->field_dragdrop_image[LANGUAGE_NONE])) {
      return t('You must drop all the image into placeholders.');
    }
    return TRUE;
  }

  /**
   * Implementation of save.
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    if (isset($_POST['dropCount']) && !empty($_POST['dropCount'])) {
      $drop_count = $_POST['dropCount'];
    }
    if (isset($drop_count) && $drop_count == count($this->question->field_dragdrop_image[LANGUAGE_NONE])) {
      $this->answer_id = db_insert('quiz_drag_drop_user_answers')
        ->fields(array(
        'question_nid' => $this->question->nid,
        'question_vid' => $this->question->vid,
        'result_id' => $this->rid,
        'score' => (int) $this
          ->getScore(),
      ))
        ->execute();
    }
  }

  /**
   * Implementation of delete.
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    db_delete('quiz_drag_drop_user_answers')
      ->condition('result_id', $this->rid)
      ->condition('question_nid', $this->question->nid)
      ->condition('question_vid', $this->question->vid)
      ->execute();
  }

  /**
   * Implementation of score.
   *
   * @return int
   *   Calculates score for the user.
   *
   * @see QuizQuestionResponse#score()
   */
  public function score() {
    $score = 0;
    if (isset($_POST['answerCount']) && !empty($_POST['answerCount'])) {
      $answer_count = $_POST['answerCount'];
      $score = 0;
      if ($answer_count == count($this->question->field_dragdrop_image[LANGUAGE_NONE])) {
        $score = 1;
      }
    }
    else {
      $result = db_query('SELECT score FROM {quiz_drag_drop_user_answers}
                          WHERE result_id = :result_id AND
                                question_nid = :question_nid AND
                                question_vid = :question_vid', array(
        ':result_id' => $this->rid,
        ':question_nid' => $this->question->nid,
        ':question_vid' => $this->question->vid,
      ))
        ->fetchField();
      if (!$result) {
        return;
      }
      $score = $result;
    }
    return $score;
  }

  /**
   * If all answers in a question is wrong.
   *
   * @return Boolean
   *   TRUE if all answers are wrong. False otherwise.
   */
  public function isAllWrong() {
    return TRUE;
  }

  /**
   * Implementation of getResponse.
   *
   * @see QuizQuestionResponse#getResponse()
   */
  public function getResponse() {

    // TODO.
  }

  /**
   * Implementation of getReportFormResponse.
   *
   * @see getReportFormResponse()
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $score = (int) $this
      ->getScore();
    $data = array(
      'score' => $score,
      'is_skipped' => $this->is_skipped,
    );

    // Return themed report.
    return array(
      '#markup' => theme('quiz_drag_drop_response', array(
        'data' => $data,
      )),
    );
  }

  /**
   * Run check_markup() on the field of the specified choice alternative.
   *
   * @param String $alternative
   *   String to be checked
   * @param String $format
   *   The input format to be used
   * @param Boolean $check_user_access
   *   Whether or not we are to check the users access to the chosen format
   *
   * @return HTML
   *   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, $langcode = '', $check_user_access);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DragDropResponse::checkMarkup private function Run check_markup() on the field of the specified choice alternative.
DragDropResponse::delete public function Implementation of delete.
DragDropResponse::getReportFormResponse public function Implementation of getReportFormResponse.
DragDropResponse::getResponse public function Implementation of getResponse.
DragDropResponse::isAllWrong public function If all answers in a question is wrong.
DragDropResponse::isValid public function Implementation of isValid.
DragDropResponse::save public function Implementation of save.
DragDropResponse::score public function Implementation of score.
DragDropResponse::__construct public function Constructor.