You are here

class LongChoiceProcessor in Opigno module 8

Same name and namespace in other branches
  1. 3.x ActivityTypes/opigno_h5p/src/TypeProcessors/LongChoiceProcessor.php \Drupal\opigno_h5p\TypeProcessors\LongChoiceProcessor

Class FillInProcessor.

Processes and generates HTML report for 'fill-in' interaction type.

Hierarchy

Expanded class hierarchy of LongChoiceProcessor

File

ActivityTypes/opigno_h5p/src/TypeProcessors/LongChoiceProcessor.php, line 10

Namespace

Drupal\opigno_h5p\TypeProcessors
View source
class LongChoiceProcessor extends TypeProcessor {

  /**
   * Options for interaction and generates a human readable HTML report.
   *
   * {@inheritdoc}
   */
  public function generateHTML($description, $crp, $response, $extras = NULL, $scoreSettings = NULL) {

    // We need some style for our report.
    $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;
  }

  /**
   * Generate header element.
   */
  private function generateHeader($description, $scoreSettings) {
    $descriptionHtml = $this
      ->generateDescription($description);
    $scoreHtml = $this
      ->generateScoreHtml($scoreSettings);
    return "<div class='h5p-long-choice-header'>" . $descriptionHtml . $scoreHtml . "</div>";
  }

  /**
   * Generates description element.
   */
  private function generateDescription($description) {
    if (!$description) {
      return '';
    }
    return '<div class="h5p-reporting-description h5p-long-choice-task-description">' . $description . '</div>';
  }

  /**
   * Generates report body from words.
   *
   * @param object $extras
   *   Additional information used to render report.
   * @param array $correctAnswers
   *   Correct answers.
   * @param array $responses
   *   Responses.
   *
   * @return string
   *   Body element as a string.
   */
  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>';
  }

  /**
   * Generate footer.
   */
  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>';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LongChoiceProcessor::generateBody private function Generates report body from words.
LongChoiceProcessor::generateDescription private function Generates description element.
LongChoiceProcessor::generateFooter private function Generate footer.
LongChoiceProcessor::generateHeader private function Generate header element.
LongChoiceProcessor::generateHTML public function Options for interaction and generates a human readable HTML report. Overrides TypeProcessor::generateHTML
TypeProcessor::$disableScoring protected property
TypeProcessor::$style private property
TypeProcessor::$xapiData protected property
TypeProcessor::generateReport public function Generate HTML for report.
TypeProcessor::generateScoreHtml protected function Generate score html.
TypeProcessor::getCRP protected function Decode and retrieve Correct Responses Pattern from xAPI data.
TypeProcessor::getDescription protected function Decode and retrieve 'en-US' description from xAPI data.
TypeProcessor::getExtras protected function Decode extras from xAPI data.
TypeProcessor::getResponse protected function Decode and retrieve user response from xAPI data.
TypeProcessor::getScoreSettings protected function Get score settings.
TypeProcessor::getStyle public function Get style used by processor if used.
TypeProcessor::setStyle protected function Set style used by the processor.