You are here

class ChoiceProcessor in Opigno module 3.x

Same name and namespace in other branches
  1. 8 ActivityTypes/opigno_h5p/src/TypeProcessors/ChoiceProcessor.php \Drupal\opigno_h5p\TypeProcessors\ChoiceProcessor

Class FillInProcessor.

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

Hierarchy

Expanded class hierarchy of ChoiceProcessor

File

ActivityTypes/opigno_h5p/src/TypeProcessors/ChoiceProcessor.php, line 12

Namespace

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

  /**
   * Determines options for interaction, generates a human readable HTML report.
   *
   * @inheritdoc
   */
  public function generateHTML($description, $crp, $response, $extras = NULL, $scoreSettings = NULL) {
    if ($this
      ->isLongChoice($extras)) {
      return H5PReport::getInstance()
        ->generateReport($this->xapiData, 'long-choice', $this->disableScoring);
    }

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

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

  /**
   * Generate description element.
   */
  private function generateDescription($description) {
    return '<p class="h5p-reporting-description h5p-choices-task-description">' . $description . '</p>';
  }

  /**
   * Generate HTML table of choices.
   */
  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>';
  }

  /**
   * Determine if choice is a long choice interaction type.
   */
  private function isLongChoice($extras) {
    $extensions = isset($extras->extensions) ? $extras->extensions : (object) [];

    // Determine if line-breaks extension exists.
    return isset($extensions->{'https://h5p.org/x-api/line-breaks'});
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChoiceProcessor::generateDescription private function Generate description element.
ChoiceProcessor::generateHeader private function Generate header element.
ChoiceProcessor::generateHTML public function Determines options for interaction, generates a human readable HTML report. Overrides TypeProcessor::generateHTML
ChoiceProcessor::generateTable private function Generate HTML table of choices.
ChoiceProcessor::isLongChoice private function Determine if choice is a long choice interaction type.
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.