Skip to content
Snippets Groups Projects

Added evaluation display; fixed double slash issue; restructured code a bit.

Files

+ 62
7
@@ -9,7 +9,7 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\lms\Attribute\ActivityAnswer;
use Drupal\lms\Entity\Answer;
use Drupal\lms\Plugin\ActivityAnswerBase;
use Drupal\lms_xapi\TincanService;
use Drupal\lms_xapi\XapiService;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -22,35 +22,90 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class Xapi extends ActivityAnswerBase {
/**
* The TinCan service.
* The Xapi service.
*/
protected TinCanService $lrsService;
protected XapiService $xapi;
/**
* Static score storage.
*/
private ?float $score = NULL;
/**
* {@inheritdoc}
*/
public function injectServices(ContainerInterface $container): void {
parent::injectServices($container);
$this->lrsService = $container->get('lms_xapi.tincan');
$this->xapi = $container->get(XapiService::class);
}
/**
* {@inheritdoc}
*/
public function getScore(Answer $answer): float {
if ($this->score !== NULL) {
return $this->score;
}
$account = $answer->getOwner();
$activity = $answer->getActivity();
$cacheable_metadata = new CacheableMetadata();
$lms_id = $this->lrsService->getLmsId($activity, $account, $cacheable_metadata);
$score = $this->lrsService->getScoreFromLrs($lms_id);
return $score ?? 0;
$lms_id = $this->xapi->getLmsId($activity, $account, $cacheable_metadata);
$score = $this->xapi->getScoreFromLrs($lms_id);
$this->score = $score ?? 0;
return $this->score;
}
/**
* {@inheritdoc}
*/
public function evaluatedOnSave(Answer $answer): bool {
if ($this->getScore($answer) === 0.0) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function evaluationDisplay(Answer $answer): array {
$activity = $answer->getActivity();
if (!$activity->hasField('field_xapi_package')) {
throw new \Exception('Xapi field missing on activity.');
}
$items = $activity->get('field_xapi_package');
if ($items->isEmpty()) {
return [];
}
/** @var \Drupal\lms_xapi\Plugin\Field\FieldType\XapiItem */
$xapi_item = $items->first();
$cacheable_metadata = new CacheableMetadata();
$launch_url = $this->xapi->getLaunchUrl(
$xapi_item->getPackagePath(),
$activity,
$answer->get('user_id')->entity,
$cacheable_metadata
);
if ($launch_url === NULL) {
return [];
}
// Display the entire package with student data loaded from LRS.
Please register or sign in to reply
$build = [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'src' => $launch_url,
'style' => 'width: 100%; min-height: 800px; border: 0;',
],
'#value' => '',
];
$cacheable_metadata->applyTo($build);
return $build;
}
}
Loading