You are here

class QuizQuestionH5PResponse in Quiz 7.4

Hierarchy

Expanded class hierarchy of QuizQuestionH5PResponse

1 string reference to 'QuizQuestionH5PResponse'
quiz_h5p_quiz_question_info in question_types/quiz_h5p/quiz_h5p.module
Implements hook_quiz_question_info().

File

question_types/quiz_h5p/QuizQuestionH5PResponse.class.inc, line 3

View source
class QuizQuestionH5PResponse extends QuizQuestionResponse {

  /**
   * Delete result row for this question.
   */
  public function delete() {
    db_delete('quiz_h5p_user_results')
      ->condition('question_nid', $this->question->nid)
      ->condition('question_vid', $this->question->vid)
      ->condition('result_id', $this->rid)
      ->execute();
  }

  /**
   * Needs to be implemented, not used.
   */
  public function getResponse() {
  }

  /**
   * Save current score and xAPI response in db.
   */
  public function save() {
    global $user;

    // Decode xAPI answer data
    $data = new H5PReportXAPIData(json_decode($this->answer));

    // Parse data
    $this
      ->saveXAPIData($data);
  }

  /**
   * Recusive save of xAPI data
   *
   * @param \H5P\XAPIData $data
   */
  private function saveXAPIData($data) {

    // Save statement data
    $dataID = db_insert('quiz_h5p_user_results')
      ->fields(array(
      'parent_id' => $data
        ->getParentID(),
      'question_nid' => $this->question->nid,
      'question_vid' => $this->question->vid,
      'result_id' => $this->rid,
      'score_scaled' => $data
        ->getScoreScaled(),
      'score_raw' => $data
        ->getScoreRaw(),
      'score_min' => $data
        ->getScoreMin(),
      'score_max' => $data
        ->getScoreMax(),
      'interaction_type' => $data
        ->getInteractionType(),
      'description' => $data
        ->getDescription(),
      'correct_responses_pattern' => $data
        ->getCorrectResponsesPattern(),
      'response' => $data
        ->getResponse(),
      'additionals' => $data
        ->getAdditionals(),
    ))
      ->execute();

    // Save sub content statements data
    if ($data
      ->isCompound()) {
      foreach ($data
        ->getChildren($dataID) as $subData) {
        $this
          ->saveXAPIData($subData);
      }
    }
  }

  /**
   * Get scaled score for question
   *
   * @return int
   */
  public function score() {
    global $user;
    $percentScore = db_query("SELECT score_scaled\n         FROM {quiz_h5p_user_results}\n        WHERE question_nid = :question_nid\n          AND question_vid = :question_vid\n          AND result_id = :result_id", array(
      'question_nid' => $this->question->nid,
      'question_vid' => $this->question->vid,
      'result_id' => $this->rid,
    ))
      ->fetchField();
    return round($percentScore * $this
      ->getMaxScore());
  }

  /**
   * Implementation of getReportFormQuestion
   *
   * @see QuizQuestionResponse#getReportFormQuestion($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormQuestion($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(
      ':nid' => $this->question->nid,
    ))
      ->fetchField();
    return array(
      '#markup' => check_plain($title),
    );
  }

  /**
   * Implements getReportFormResponse of QuizQuestionResponse interface
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $result = db_select('quiz_h5p_user_results', 'qhur')
      ->fields('qhur')
      ->condition('question_nid', $this->question->nid)
      ->condition('question_vid', $this->question->vid)
      ->condition('result_id', $this->rid)
      ->orderBy('id', 'ASC')
      ->execute();

    // Make it easy to map questions by id
    $questionsById = array();
    foreach ($result as $record) {
      $questionsById[$record->id] = $record;
    }

    // Assemble our question tree
    foreach ($questionsById as $question) {
      if ($question->parent_id === NULL) {

        // This is the root of our tree
        $baseQuestion = $question;
      }
      elseif (isset($questionsById[$question->parent_id])) {

        // Add to parent
        $questionsById[$question->parent_id]->children[] = $question;
      }
    }

    // Process question tree and create markup for report
    if (isset($baseQuestion)) {
      $report = theme('quiz_h5p_response', array(
        'question' => $baseQuestion,
      ));
      if (!empty($report)) {
        return array(
          '#markup' => $report,
        );
      }
    }

    // No report available
    return array(
      '#no-response' => TRUE,
    );
  }

  /**
   * Needs to be implemented. Is not used.
   */
  public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuizQuestionH5PResponse::delete public function Delete result row for this question. Overrides QuizQuestionResponse::delete
QuizQuestionH5PResponse::getReportFormAnswerFeedback public function Needs to be implemented. Is not used. Overrides QuizQuestionResponse::getReportFormAnswerFeedback
QuizQuestionH5PResponse::getReportFormQuestion public function Implementation of getReportFormQuestion Overrides QuizQuestionResponse::getReportFormQuestion
QuizQuestionH5PResponse::getReportFormResponse public function Implements getReportFormResponse of QuizQuestionResponse interface Overrides QuizQuestionResponse::getReportFormResponse
QuizQuestionH5PResponse::getResponse public function Needs to be implemented, not used. Overrides QuizQuestionResponse::getResponse
QuizQuestionH5PResponse::save public function Save current score and xAPI response in db. Overrides QuizQuestionResponse::save
QuizQuestionH5PResponse::saveXAPIData private function Recusive save of xAPI data
QuizQuestionH5PResponse::score public function Get scaled score for question Overrides QuizQuestionResponse::score
QuizQuestionResponse::$answer protected property
QuizQuestionResponse::$evaluated protected property
QuizQuestionResponse::$is_correct protected property
QuizQuestionResponse::$is_doubtful public property
QuizQuestionResponse::$is_skipped public property
QuizQuestionResponse::$question public property
QuizQuestionResponse::$rid protected property
QuizQuestionResponse::$score protected property 9
QuizQuestionResponse::getFormat protected function Utility function that returns the format of the node body
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::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::isValid public function Validates response from a quiz taker. If the response isn't valid the quiz taker won't be allowed to proceed. 6
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.
QuizQuestionResponse::__construct public function Create a new user response. 7