ChoiceProcessor.php in Opigno module 3.x
File
ActivityTypes/opigno_h5p/src/TypeProcessors/ChoiceProcessor.php
View source
<?php
namespace Drupal\opigno_h5p\TypeProcessors;
use Drupal\opigno_h5p\H5PReport;
class ChoiceProcessor extends TypeProcessor {
public function generateHTML($description, $crp, $response, $extras = NULL, $scoreSettings = NULL) {
if ($this
->isLongChoice($extras)) {
return H5PReport::getInstance()
->generateReport($this->xapiData, 'long-choice', $this->disableScoring);
}
$this
->setStyle('opigno_h5p/opigno_h5p.choice');
if (!isset($crp[0])) {
$crp[0] = '';
}
$correctAnswers = explode('[,]', $crp[0]);
$responses = explode('[,]', $response);
$headerHtml = $this
->generateHeader($description, $scoreSettings);
$tableHTML = $this
->generateTable($extras, $correctAnswers, $responses);
return '<div class="h5p-reporting-container h5p-choices-container">' . $headerHtml . $tableHTML . '</div>';
}
private function generateHeader($description, $scoreSettings) {
$descriptionHtml = $this
->generateDescription($description);
$scoreHtml = $this
->generateScoreHtml($scoreSettings);
return "<div class='h5p-choices-header'>" . $descriptionHtml . $scoreHtml . "</div>";
}
private function generateDescription($description) {
return '<p class="h5p-reporting-description h5p-choices-task-description">' . $description . '</p>';
}
private function generateTable($extras, $correctAnswers, $responses) {
$choices = $extras->choices;
$tableHeader = '<tr class="h5p-choices-table-heading">' . '<td class="h5p-choices-choice">' . t('Answers') . '</td>' . '<td class="h5p-choices-user-answer">' . t('Your Answer') . '</td>' . '<td class="h5p-choices-crp-answer">' . t('Correct') . '</td></tr>';
$rows = '';
foreach ($choices as $choice) {
$choiceID = $choice->id;
$isCRP = in_array($choiceID, $correctAnswers);
$isAnswered = in_array($choiceID, $responses);
$userClasses = 'h5p-choices-user';
$crpClasses = 'h5p-choices-crp';
if ($isAnswered) {
$userClasses .= ' h5p-choices-answered';
}
if ($isCRP) {
$userClasses .= ' h5p-choices-user-correct';
$crpClasses .= ' h5p-choices-crp-correct';
}
$row = '<td class="h5p-choices-alternative">' . $choice->description->{'en-US'} . '</td><td class="h5p-choices-icon-cell"><span class="' . $userClasses . '"></span></td>' . '<td class="h5p-choices-icon-cell"><span class="' . $crpClasses . '"></span></td>';
$rows .= '<tr>' . $row . '</tr>';
}
$tableContent = '<tbody>' . $tableHeader . $rows . '</tbody>';
return '<table class="h5p-choices-table">' . $tableContent . '</table>';
}
private function isLongChoice($extras) {
$extensions = isset($extras->extensions) ? $extras->extensions : (object) [];
return isset($extensions->{'https://h5p.org/x-api/line-breaks'});
}
}