Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XAPI: Launch tincan tools - refs BT#16742
- Loading branch information
Showing
4 changed files
with
306 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
/* For licensing terms, see /license.txt */ | ||
|
||
use Chamilo\PluginBundle\Entity\XApi\ToolLaunch; | ||
use Symfony\Component\HttpFoundation\Request as HttpRequest; | ||
use Xabbuh\XApi\Common\Exception\NotFoundException; | ||
use Xabbuh\XApi\Model\Activity; | ||
use Xabbuh\XApi\Model\Agent; | ||
use Xabbuh\XApi\Model\DocumentData; | ||
use Xabbuh\XApi\Model\InverseFunctionalIdentifier; | ||
use Xabbuh\XApi\Model\IRI; | ||
use Xabbuh\XApi\Model\State; | ||
use Xabbuh\XApi\Model\StateDocument; | ||
use Xabbuh\XApi\Serializer\Symfony\Serializer; | ||
|
||
require_once __DIR__.'/../../../main/inc/global.inc.php'; | ||
|
||
api_block_anonymous_users(); | ||
api_protect_course_script(true); | ||
|
||
$request = HttpRequest::createFromGlobals(); | ||
|
||
$user = api_get_user_entity(api_get_user_id()); | ||
|
||
$em = Database::getManager(); | ||
|
||
$attemptId = $request->request->get('attempt_id'); | ||
$toolLaunch = $em->find( | ||
ToolLaunch::class, | ||
$request->request->getInt('id') | ||
); | ||
|
||
if (empty($attemptId) | ||
|| null === $toolLaunch | ||
|| $toolLaunch->getCourse()->getId() !== api_get_course_entity()->getId() | ||
) { | ||
api_not_allowed(true); | ||
} | ||
|
||
$plugin = XApiPlugin::create(); | ||
|
||
$activity = new Activity( | ||
IRI::fromString($toolLaunch->getActivityId()) | ||
); | ||
$actor = new Agent( | ||
InverseFunctionalIdentifier::withMbox( | ||
IRI::fromString('mailto:'.$user->getEmail()) | ||
), | ||
$user->getCompleteName() | ||
); | ||
$state = new State( | ||
$activity, | ||
$actor, | ||
$plugin->generateIri('tool-'.$toolLaunch->getId(), 'state') | ||
); | ||
|
||
$nowDate = api_get_utc_datetime(null, false, true)->format('c'); | ||
|
||
try { | ||
$stateDocument = $plugin->getXApiStateClient()->getDocument($state); | ||
|
||
$data = $stateDocument->getData()->getData(); | ||
|
||
if ($stateDocument->offsetExists($attemptId)) { | ||
$data[$attemptId][XApiPlugin::STATE_LAST_LAUNCH] = $nowDate; | ||
} else { | ||
$data[$attemptId] = [ | ||
XApiPlugin::STATE_FIRST_LAUNCH => $nowDate, | ||
XApiPlugin::STATE_LAST_LAUNCH => $nowDate, | ||
]; | ||
} | ||
|
||
uasort($data, function ($attemptA, $attemptB) { | ||
$timeA = strtotime($attemptA[XApiPlugin::STATE_LAST_LAUNCH]); | ||
$timeB = strtotime($attemptB[XApiPlugin::STATE_LAST_LAUNCH]); | ||
|
||
return $timeB - $timeA; | ||
}); | ||
|
||
$documentData = new DocumentData($data); | ||
} catch (NotFoundException $notFoundException) { | ||
$documentData = new DocumentData( | ||
[ | ||
$attemptId => [ | ||
XApiPlugin::STATE_FIRST_LAUNCH => $nowDate, | ||
XApiPlugin::STATE_LAST_LAUNCH => $nowDate, | ||
], | ||
] | ||
); | ||
} catch (Exception $exception) { | ||
Display::addFlash( | ||
Display::return_message($exception->getMessage(), 'error') | ||
); | ||
|
||
header('Location: '.api_get_course_url()); | ||
exit; | ||
} | ||
|
||
try { | ||
$plugin | ||
->getXApiStateClient() | ||
->createOrReplaceDocument( | ||
new StateDocument($state, $documentData) | ||
); | ||
} catch (Exception $exception) { | ||
Display::addFlash( | ||
Display::return_message($exception->getMessage(), 'error') | ||
); | ||
|
||
header('Location: '.api_get_course_url()); | ||
exit; | ||
} | ||
|
||
$authString = $plugin->get(XApiPlugin::SETTING_LRS_AUTH); | ||
$parts = explode(':', $authString); | ||
|
||
$activityLaunchUrl = $toolLaunch->getLaunchUrl().'?' | ||
.http_build_query( | ||
[ | ||
'endpoint' => $plugin->get(XApiPlugin::SETTING_LRS_URL), | ||
'auth' => 'Basic '.base64_encode("{$parts[1]}:{$parts[2]}"), | ||
'actor' => Serializer::createSerializer()->serialize($actor, 'json'), | ||
'registration' => $attemptId, | ||
'activity_id' => $toolLaunch->getActivityId(), | ||
], | ||
'', | ||
'&', | ||
PHP_QUERY_RFC3986 | ||
); | ||
|
||
header("Location: $activityLaunchUrl"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/* For licensing terms, see /license.txt */ | ||
|
||
use Chamilo\PluginBundle\Entity\XApi\ToolLaunch; | ||
use Symfony\Component\HttpFoundation\Request as HttpRequest; | ||
use Xabbuh\XApi\Common\Exception\NotFoundException; | ||
use Xabbuh\XApi\Model\Activity; | ||
use Xabbuh\XApi\Model\Agent; | ||
use Xabbuh\XApi\Model\InverseFunctionalIdentifier; | ||
use Xabbuh\XApi\Model\IRI; | ||
use Xabbuh\XApi\Model\State; | ||
use Xabbuh\XApi\Model\Uuid; | ||
|
||
require_once __DIR__.'/../../../main/inc/global.inc.php'; | ||
|
||
api_block_anonymous_users(); | ||
api_protect_course_script(true); | ||
|
||
$request = HttpRequest::createFromGlobals(); | ||
|
||
$user = api_get_user_entity(api_get_user_id()); | ||
|
||
$em = Database::getManager(); | ||
|
||
$toolLaunch = $em->find( | ||
ToolLaunch::class, | ||
$request->query->getInt('id') | ||
); | ||
|
||
if (null === $toolLaunch | ||
|| $toolLaunch->getCourse()->getId() !== api_get_course_entity()->getId() | ||
) { | ||
api_not_allowed(true); | ||
} | ||
|
||
$plugin = XApiPlugin::create(); | ||
|
||
$activity = new Activity( | ||
IRI::fromString($toolLaunch->getActivityId()) | ||
); | ||
$actor = new Agent( | ||
InverseFunctionalIdentifier::withMbox( | ||
IRI::fromString('mailto:'.$user->getEmail()) | ||
), | ||
$user->getCompleteName() | ||
); | ||
$state = new State( | ||
$activity, | ||
$actor, | ||
$plugin->generateIri('tool-'.$toolLaunch->getId(), 'state') | ||
); | ||
|
||
$cidReq = api_get_cidreq(); | ||
|
||
try { | ||
$stateDocument = $plugin->getXApiStateClient()->getDocument($state); | ||
} catch (NotFoundException $notFoundException) { | ||
$stateDocument = null; | ||
} catch (Exception $exception) { | ||
Display::addFlash( | ||
Display::return_message($exception->getMessage(), 'error') | ||
); | ||
|
||
header('Location: '.api_get_course_url()); | ||
exit; | ||
} | ||
|
||
$frmNewRegistration = new FormValidator( | ||
'launch_new', | ||
'post', | ||
"launch.php?$cidReq", | ||
'', | ||
['target' => '_blank'], | ||
FormValidator::LAYOUT_INLINE | ||
); | ||
$frmNewRegistration->addHidden('attempt_id', Uuid::uuid4()); | ||
$frmNewRegistration->addHidden('id', $toolLaunch->getId()); | ||
$frmNewRegistration->addButton( | ||
'submit', | ||
$plugin->get_lang('LaunchNewAttempt'), | ||
'external-link fa-fw', | ||
'success' | ||
); | ||
|
||
if ($stateDocument) { | ||
$row = 0; | ||
|
||
$table = new HTML_Table(['class' => 'table table-hover table-striped']); | ||
$table->setHeaderContents($row, 0, $plugin->get_lang('ActivityFirstLaunch')); | ||
$table->setHeaderContents($row, 1, $plugin->get_lang('ActivityLastLaunch')); | ||
$table->setHeaderContents($row, 2, get_lang('Actions')); | ||
|
||
$row++; | ||
|
||
$langActivityLaunch = $plugin->get_lang('ActivityLaunch'); | ||
|
||
foreach ($stateDocument->getData()->getData() as $attemptId => $attempt) { | ||
$firstLaunch = api_convert_and_format_date( | ||
$attempt[XApiPlugin::STATE_FIRST_LAUNCH], | ||
DATE_TIME_FORMAT_LONG | ||
); | ||
$lastLaunch = api_convert_and_format_date( | ||
$attempt[XApiPlugin::STATE_LAST_LAUNCH], | ||
DATE_TIME_FORMAT_LONG | ||
); | ||
|
||
$frmLaunch = new FormValidator( | ||
"launch_$row", | ||
'post', | ||
"launch.php?$cidReq", | ||
'', | ||
['target' => '_blank'], | ||
FormValidator::LAYOUT_INLINE | ||
); | ||
$frmLaunch->addHidden('attempt_id', $attemptId); | ||
$frmLaunch->addHidden('id', $toolLaunch->getId()); | ||
$frmLaunch->addButton( | ||
'submit', | ||
$langActivityLaunch, | ||
'external-link fa-fw', | ||
'default' | ||
); | ||
|
||
$table->setCellContents($row, 0, $firstLaunch); | ||
$table->setCellContents($row, 1, $lastLaunch); | ||
$table->setCellContents($row, 2, $frmLaunch->returnForm()); | ||
|
||
$row++; | ||
} | ||
|
||
$table->setColAttributes(0, ['class' => 'text-center']); | ||
$table->setColAttributes(1, ['class' => 'text-center']); | ||
$table->setColAttributes(2, ['class' => 'text-center']); | ||
} | ||
|
||
$pageTitle = $toolLaunch->getTitle(); | ||
$pageContent = ''; | ||
|
||
if ($toolLaunch->getDescription()) { | ||
$pageContent .= PHP_EOL; | ||
$pageContent .= "<p class='lead'>{$toolLaunch->getDescription()}</p>"; | ||
} | ||
|
||
$pageContent .= Display::div( | ||
$frmNewRegistration->returnForm(), | ||
['class' => 'exercise_overview_options'] | ||
); | ||
|
||
if ($stateDocument) { | ||
$pageContent .= $table->toHtml(); | ||
} | ||
|
||
$view = new Template($pageTitle); | ||
$view->assign('header', $pageTitle); | ||
$view->assign('content', $pageContent); | ||
$view->display_one_col_template(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters