TypeProcessor.php in Opigno module 8
File
ActivityTypes/opigno_h5p/src/TypeProcessors/TypeProcessor.php
View source
<?php
namespace Drupal\opigno_h5p\TypeProcessors;
use stdClass;
abstract class TypeProcessor {
private $style;
protected $xapiData;
protected $disableScoring;
public function generateReport($xapiData, $disableScoring = FALSE) {
$this->xapiData = $xapiData;
$this->disableScoring = $disableScoring;
$description = $this
->getDescription($xapiData);
$crp = $this
->getCRP($xapiData);
$extras = $this
->getExtras($xapiData);
$scoreSettings = $this
->getScoreSettings($xapiData);
return $this
->generateHTML($description, $crp, $this
->getResponse($xapiData), $extras, $scoreSettings);
}
protected function getScoreSettings($xapiData) {
$scoreSettings = (object) [];
if (!isset($xapiData->score_raw) || !isset($xapiData->score_max)) {
return $scoreSettings;
}
$scoreSettings->rawScore = $xapiData->score_raw;
$scoreSettings->maxScore = $xapiData->score_max;
$scoreSettings->scoreLabel = 'Score:';
if (isset($xapiData->score_label)) {
$scoreSettings->scoreLabel = $xapiData->score_label;
}
$scoreSettings->scoreDelimiter = 'out of';
if (isset($xapiData->score_delimiter)) {
$scoreSettings->scoreDelimiter = $xapiData->score_delimiter;
}
$scoreSettings->scaledScoreDelimiter = ',';
if (isset($xapiData->scaled_score_delimiter)) {
$scoreSettings->scaledScoreDelimiter = $xapiData->scaled_score_delimiter;
}
if (isset($xapiData->score_scale)) {
$scoreSettings->scoreScale = $xapiData->score_scale;
$scoreSettings->scaledScoreLabel = 'Scaled score:';
if (isset($xapiData->score_label)) {
$scoreSettings->scaledScoreLabel = $xapiData->scaled_score_label;
}
}
return $scoreSettings;
}
protected function generateScoreHtml($scoreSettings) {
$showScores = isset($scoreSettings->rawScore) && isset($scoreSettings->maxScore) && !$this->disableScoring;
if (!$showScores) {
return '';
}
$scoreLabel = $scoreSettings->scoreLabel;
$scoreDelimiter = $scoreSettings->scoreDelimiter;
$scaleDelimiter = '';
$scaledHtml = "";
if (isset($scoreSettings->scoreScale)) {
$scaleDelimiter = $scoreSettings->scaledScoreDelimiter;
$scaledHtml = "<div class='h5p-reporting-scaled-container'>" . "<span class='h5p-reporting-scaled-label'>{$scoreSettings->scaledScoreLabel}</span>" . "<span class='h5p-reporting-scaled-score'>{$scoreSettings->scoreScale}</span>" . "</div>";
}
$scoreHtml = "<div class='h5p-reporting-score-container'>" . "<span class='h5p-reporting-score-label'>{$scoreLabel}</span>" . "<span class='h5p-reporting-score'>" . $scoreSettings->rawScore . " " . $scoreDelimiter . " " . $scoreSettings->maxScore . $scaleDelimiter . "</span></div>";
$html = "<div class='h5p-reporting-score-wrapper'>{$scoreHtml}{$scaledHtml}</div>";
return $html;
}
protected function getExtras($xapiData) {
$extras = $xapiData->additionals === '' ? new stdClass() : json_decode($xapiData->additionals);
if (isset($xapiData->children)) {
$extras->children = $xapiData->children;
}
return $extras;
}
protected function getDescription($xapiData) {
return $xapiData->description;
}
protected function getCRP($xapiData) {
return json_decode($xapiData->correct_responses_pattern, TRUE);
}
protected function getResponse($xapiData) {
return $xapiData->response;
}
abstract function generateHTML($description, $crp, $response, $extras, $scoreSettings);
protected function setStyle($style) {
$this->style = $style;
}
public function getStyle() {
return $this->style;
}
}