You are here

private function LongChoiceProcessor::generateBody in Opigno module 3.x

Same name and namespace in other branches
  1. 8 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\TypeProcessors

Code

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>';
}