You are here

class H5PReportXAPIData in Quiz 7.4

Hierarchy

Expanded class hierarchy of H5PReportXAPIData

File

question_types/quiz_h5p/report/h5p-report-xapi-data.class.php, line 3

View source
class H5PReportXAPIData {
  private $statement, $onlyScore, $children, $parentID;

  /**
   * @param object $data Containing 'statement' and 'children'
   * @param int $parentID Optional parent identifier
   */
  public function __construct($data, $parentID = NULL) {

    // Keep track of statement and children
    if (isset($data->statement)) {
      $this->statement = $data->statement;
    }
    else {
      if (isset($data->onlyScore)) {
        $this->onlyScore = $data->onlyScore;
      }
    }
    $this->parentID = $parentID;
    if (!empty($data->children)) {
      $this->children = $data->children;
    }
  }

  /**
   * Check if the interaction has sub interactions with scoring.
   *
   * @return boolean
   */
  public function isCompound() {
    return $this
      ->getInteractionType() === 'compound';
  }

  /**
   * Get list of children with given parentID
   *
   * @param int $parentID
   * @return array
   */
  public function getChildren($parentID = NULL) {
    $children = array();

    // Parse children data
    if (!empty($this->children)) {
      foreach ($this->children as $child) {
        $children[] = new H5PReportXAPIData($child, $parentID);
      }
    }
    return $children;
  }

  /**
   * Get the ID of the parent statement.
   * Only works for statements part of a compound interaction.
   *
   * @return int
   */
  public function getParentID() {
    return $this->parentID;
  }

  /**
   * Get score of given type from statement result
   *
   * @param string $type
   * @return float
   */
  private function getScore($type) {
    return isset($this->statement->result->score->{$type}) ? (double) $this->statement->result->score->{$type} : NULL;
  }

  /**
   * Get the optional scaled score.
   * Must be between -1 and 1.
   *
   * @return float
   */
  public function getScoreScaled() {
    if (isset($this->onlyScore)) {

      // Special case if we only have the scaled score.
      $score = 0.0;
      if ($this->onlyScore !== 1 && is_numeric($this->onlyScore)) {

        // Let's "decrypt" it…
        $score = $this->onlyScore / 1.234 - 32.17;
      }
      if ($score < 0 || $score > 1) {

        // Invalid score
        $score = 0.0;
      }
      return $score;
    }
    $score = $this
      ->getScore('scaled');
    if ($score !== NULL) {
      if ($score < -1) {
        $score = -1.0;
      }
      elseif ($score > 1) {
        $score = 1.0;
      }
    }
    return $score;
  }

  /**
   * Get the required raw score for the interaction.
   * Can be anything between min and max.
   *
   * @return float
   */
  public function getScoreRaw() {
    return $this
      ->getScore('raw');
  }

  /**
   * Get the optional min. score
   *
   * @return float
   */
  public function getScoreMin() {
    return $this
      ->getScore('min');
  }

  /**
   * Get the optional max. score
   *
   * @return float
   */
  public function getScoreMax() {
    return $this
      ->getScore('max');
  }

  /**
   * Get object definition property or default value if not set.
   *
   * @param string $property
   * @param mixed $default If not set. Default default is blank string.
   * @return mixed
   */
  private function getObjectDefinition($property, $default = '') {
    return isset($this->statement->object->definition->{$property}) ? $this->statement->object->definition->{$property} : $default;
  }

  /**
   * Get the type of interaction.
   *
   * @return string
   */
  public function getInteractionType() {

    // Can be any string
    return $this
      ->getObjectDefinition('interactionType');
  }

  /**
   * Get the description of the interaction.
   *
   * @return string
   */
  public function getDescription() {
    $description = $this
      ->getObjectDefinition('description');
    if ($description !== '') {
      $description = isset($description->{'en-US'}) ? $description->{'en-US'} : '';
    }
    return $description;
  }

  /**
   * Get the correct reponse patterns.
   *
   * @return string
   */
  public function getCorrectResponsesPattern() {
    $correctResponsesPattern = $this
      ->getObjectDefinition('correctResponsesPattern');
    if (is_array($correctResponsesPattern)) {
      return json_encode($correctResponsesPattern);
    }
    return '';
  }

  /**
   * Get the user reponse.
   *
   * @return string
   */
  public function getResponse() {
    return isset($this->statement->result->response) ? $this->statement->result->response : '';
  }

  /**
   * Get additonal data for some interaction types.
   *
   * @return string JSON
   */
  public function getAdditionals() {
    $additionals = array();
    switch ($this
      ->getInteractionType()) {
      case 'choice':
        $additionals['choices'] = $this
          ->getObjectDefinition('choices', array());
        $additionals['extensions'] = $this
          ->getObjectDefinition('extensions', (object) array());
        break;
      case 'long-choice':
        $additionals['choices'] = $this
          ->getObjectDefinition('choices', array());
        $additionals['extensions'] = $this
          ->getObjectDefinition('extensions', (object) array());
        break;
      case 'matching':
        $additionals['source'] = $this
          ->getObjectDefinition('source', array());
        $additionals['target'] = $this
          ->getObjectDefinition('target', array());
        break;
    }
    return empty($additionals) ? '' : json_encode($additionals);
  }

  /**
   * Checks if data is valid
   *
   * @return bool True if valid data
   */
  public function validateData() {
    if ($this
      ->getInteractionType() === '') {
      return false;
    }

    // Validate children
    $children = $this
      ->getChildren();
    foreach ($children as $child) {
      if (!$child
        ->validateData()) {
        return false;
      }
    }
    return true;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
H5PReportXAPIData::$statement private property
H5PReportXAPIData::getAdditionals public function Get additonal data for some interaction types.
H5PReportXAPIData::getChildren public function Get list of children with given parentID
H5PReportXAPIData::getCorrectResponsesPattern public function Get the correct reponse patterns.
H5PReportXAPIData::getDescription public function Get the description of the interaction.
H5PReportXAPIData::getInteractionType public function Get the type of interaction.
H5PReportXAPIData::getObjectDefinition private function Get object definition property or default value if not set.
H5PReportXAPIData::getParentID public function Get the ID of the parent statement. Only works for statements part of a compound interaction.
H5PReportXAPIData::getResponse public function Get the user reponse.
H5PReportXAPIData::getScore private function Get score of given type from statement result
H5PReportXAPIData::getScoreMax public function Get the optional max. score
H5PReportXAPIData::getScoreMin public function Get the optional min. score
H5PReportXAPIData::getScoreRaw public function Get the required raw score for the interaction. Can be anything between min and max.
H5PReportXAPIData::getScoreScaled public function Get the optional scaled score. Must be between -1 and 1.
H5PReportXAPIData::isCompound public function Check if the interaction has sub interactions with scoring.
H5PReportXAPIData::validateData public function Checks if data is valid
H5PReportXAPIData::__construct public function