h5p-report-xapi-data.class.php in Quiz 7.4
File
question_types/quiz_h5p/report/h5p-report-xapi-data.class.php
View source
<?php
class H5PReportXAPIData {
private $statement, $onlyScore, $children, $parentID;
public function __construct($data, $parentID = NULL) {
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;
}
}
public function isCompound() {
return $this
->getInteractionType() === 'compound';
}
public function getChildren($parentID = NULL) {
$children = array();
if (!empty($this->children)) {
foreach ($this->children as $child) {
$children[] = new H5PReportXAPIData($child, $parentID);
}
}
return $children;
}
public function getParentID() {
return $this->parentID;
}
private function getScore($type) {
return isset($this->statement->result->score->{$type}) ? (double) $this->statement->result->score->{$type} : NULL;
}
public function getScoreScaled() {
if (isset($this->onlyScore)) {
$score = 0.0;
if ($this->onlyScore !== 1 && is_numeric($this->onlyScore)) {
$score = $this->onlyScore / 1.234 - 32.17;
}
if ($score < 0 || $score > 1) {
$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;
}
public function getScoreRaw() {
return $this
->getScore('raw');
}
public function getScoreMin() {
return $this
->getScore('min');
}
public function getScoreMax() {
return $this
->getScore('max');
}
private function getObjectDefinition($property, $default = '') {
return isset($this->statement->object->definition->{$property}) ? $this->statement->object->definition->{$property} : $default;
}
public function getInteractionType() {
return $this
->getObjectDefinition('interactionType');
}
public function getDescription() {
$description = $this
->getObjectDefinition('description');
if ($description !== '') {
$description = isset($description->{'en-US'}) ? $description->{'en-US'} : '';
}
return $description;
}
public function getCorrectResponsesPattern() {
$correctResponsesPattern = $this
->getObjectDefinition('correctResponsesPattern');
if (is_array($correctResponsesPattern)) {
return json_encode($correctResponsesPattern);
}
return '';
}
public function getResponse() {
return isset($this->statement->result->response) ? $this->statement->result->response : '';
}
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);
}
public function validateData() {
if ($this
->getInteractionType() === '') {
return false;
}
$children = $this
->getChildren();
foreach ($children as $child) {
if (!$child
->validateData()) {
return false;
}
}
return true;
}
}