private function LongChoiceProcessor::generateBody in Opigno module 8
Same name and namespace in other branches
- 3.x ActivityTypes/opigno_h5p/src/TypeProcessors/LongChoiceProcessor.php \Drupal\opigno_h5p\TypeProcessors\LongChoiceProcessor::generateBody()
Generates report body from words.
Parameters
object $extras: Additional information used to render report.
array $correctAnswers: Correct answers.
array $responses: Responses.
Return value
string Body element as a string.
1 call to LongChoiceProcessor::generateBody()
- LongChoiceProcessor::generateHTML in ActivityTypes/
opigno_h5p/ src/ TypeProcessors/ LongChoiceProcessor.php - Options for interaction and generates a human readable HTML report.
File
- ActivityTypes/
opigno_h5p/ src/ TypeProcessors/ LongChoiceProcessor.php, line 74
Class
- LongChoiceProcessor
- Class FillInProcessor.
Namespace
Drupal\opigno_h5p\TypeProcessorsCode
private function generateBody($extras, array $correctAnswers, array $responses) {
$choices = $extras->choices;
$extensions = isset($extras->extensions) ? $extras->extensions : (object) [];
// Determine if line-breaks extension exists.
$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';
}
// Add choices html.
$choicesHTML[] = '<span class="' . $classes . '">' . $choice->description->{'en-US'} . '</span>';
// Add line break if extension found.
if (isset($lineBreaks[$lineBreakIndex]) && $lineBreaks[$lineBreakIndex] === $index) {
$choicesHTML[] = '</br>';
$lineBreakIndex++;
}
}
return '<div class="h5p-long-choice-words">' . implode(' ', $choicesHTML) . '</div>';
}