class LongChoiceProcessor 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
Class FillInProcessor.
Processes and generates HTML report for 'fill-in' interaction type.
Hierarchy
- class \Drupal\opigno_h5p\TypeProcessors\TypeProcessor
- class \Drupal\opigno_h5p\TypeProcessors\LongChoiceProcessor
Expanded class hierarchy of LongChoiceProcessor
File
- ActivityTypes/
opigno_h5p/ src/ TypeProcessors/ LongChoiceProcessor.php, line 10
Namespace
Drupal\opigno_h5p\TypeProcessorsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LongChoiceProcessor:: |
private | function | Generates report body from words. | |
LongChoiceProcessor:: |
private | function | Generates description element. | |
LongChoiceProcessor:: |
private | function | Generate footer. | |
LongChoiceProcessor:: |
private | function | Generate header element. | |
LongChoiceProcessor:: |
public | function |
Options for interaction and generates a human readable HTML report. Overrides TypeProcessor:: |
|
TypeProcessor:: |
protected | property | ||
TypeProcessor:: |
private | property | ||
TypeProcessor:: |
protected | property | ||
TypeProcessor:: |
public | function | Generate HTML for report. | |
TypeProcessor:: |
protected | function | Generate score html. | |
TypeProcessor:: |
protected | function | Decode and retrieve Correct Responses Pattern from xAPI data. | |
TypeProcessor:: |
protected | function | Decode and retrieve 'en-US' description from xAPI data. | |
TypeProcessor:: |
protected | function | Decode extras from xAPI data. | |
TypeProcessor:: |
protected | function | Decode and retrieve user response from xAPI data. | |
TypeProcessor:: |
protected | function | Get score settings. | |
TypeProcessor:: |
public | function | Get style used by processor if used. | |
TypeProcessor:: |
protected | function | Set style used by the processor. |