LongChoiceProcessor.php in Opigno module 3.x
File
ActivityTypes/opigno_h5p/src/TypeProcessors/LongChoiceProcessor.php
View source
<?php
namespace Drupal\opigno_h5p\TypeProcessors;
class LongChoiceProcessor extends TypeProcessor {
public function generateHTML($description, $crp, $response, $extras = NULL, $scoreSettings = NULL) {
$this
->setStyle('opigno_h5p/opigno_h5p.long-choice');
$correctAnswers = explode('[,]', $crp[0]);
$responses = !empty($response) ? explode('[,]', $response) : [];
$header = $this
->generateHeader($description, $scoreSettings);
$bodyHTML = $this
->generateBody($extras, $correctAnswers, $responses);
$footer = $this
->generateFooter();
return '<div class="h5p-reporting-container h5p-long-choice-container">' . $header . $bodyHTML . '</div>' . $footer;
}
private function generateHeader($description, $scoreSettings) {
$descriptionHtml = $this
->generateDescription($description);
$scoreHtml = $this
->generateScoreHtml($scoreSettings);
return "<div class='h5p-long-choice-header'>" . $descriptionHtml . $scoreHtml . "</div>";
}
private function generateDescription($description) {
if (!$description) {
return '';
}
return '<div class="h5p-reporting-description h5p-long-choice-task-description">' . $description . '</div>';
}
private function generateBody($extras, array $correctAnswers, array $responses) {
$choices = $extras->choices;
$extensions = isset($extras->extensions) ? $extras->extensions : (object) [];
$lineBreaks = isset($extensions->{'https://h5p.org/x-api/line-breaks'}) ? $extensions->{'https://h5p.org/x-api/line-breaks'} : [];
$lineBreakIndex = 0;
$choicesHTML = [];
foreach ($choices as $index => $choice) {
$choiceID = $choice->id;
$isCRP = in_array($choiceID, $correctAnswers);
$isAnswered = in_array($choiceID, $responses);
$classes = 'h5p-long-choice-word';
if ($isAnswered) {
$classes .= ' h5p-long-choice-answered';
}
if ($isCRP) {
$classes .= ' h5p-long-choice-correct';
}
$choicesHTML[] = '<span class="' . $classes . '">' . $choice->description->{'en-US'} . '</span>';
if (isset($lineBreaks[$lineBreakIndex]) && $lineBreaks[$lineBreakIndex] === $index) {
$choicesHTML[] = '</br>';
$lineBreakIndex++;
}
}
return '<div class="h5p-long-choice-words">' . implode(' ', $choicesHTML) . '</div>';
}
private function generateFooter() {
return '<div class="h5p-long-choice-footer">' . '<span class="h5p-long-choice-word h5p-long-choice-correct">' . t('Correct Answer') . '</span>' . '<span class="h5p-long-choice-word h5p-long-choice-answered h5p-long-choice-correct">' . t('Your correct answer') . '</span>' . '<span class="h5p-long-choice-word h5p-long-choice-answered">' . t('Your incorrect answer') . '</span>' . '</div>';
}
}