OpignoScormController.php in Opigno SCORM 3.x
File
src/Controller/OpignoScormController.php
View source
<?php
namespace Drupal\opigno_scorm\Controller;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class OpignoScormController extends ControllerBase {
public function scormIntegrateSco($opigno_scorm_sco) {
$scorm_service = \Drupal::service('opigno_scorm.scorm');
$sco = $scorm_service
->scormLoadSco($opigno_scorm_sco);
if (!empty($sco->launch)) {
$query = [];
$scorm = $scorm_service
->scormLoadById($sco->scorm_id);
if (!empty($sco->attributes['parameters'])) {
$sco->launch .= $sco->attributes['parameters'];
}
$parts = explode('?', $sco->launch);
$launch = array_shift($parts);
if (!empty($parts)) {
$parameters = implode('&', $parts);
}
$sco_location = "{$scorm->extracted_dir}/{$launch}";
$sco_path = file_create_url($sco_location);
if (!empty($parameters)) {
foreach (explode('&', $parameters) as $param) {
list($key, $value) = explode('=', $param);
$query[$key] = !empty($value) ? $value : '';
}
if ($query) {
$query = UrlHelper::buildQuery($query);
$sco_path = $sco_path . '?' . $query;
}
}
return new TrustedRedirectResponse($sco_path);
}
else {
throw new NotFoundHttpException();
}
}
public function scormCommit($opigno_scorm_id, $opigno_scorm_sco_id) {
$data_content = $GLOBALS['request']
->getContent();
if (!empty($_POST['data'])) {
$data = json_decode($_POST['data']);
}
elseif ($data_content) {
$data = json_decode($data_content);
}
if (!empty($data)) {
if (!empty($data->cmi->interactions)) {
$_SESSION['scorm_answer_results'] = [
'opigno_scorm_id' => $opigno_scorm_id,
'opigno_scorm_sco_id' => $opigno_scorm_sco_id,
'data' => $data,
];
}
$scorm_service = \Drupal::service('opigno_scorm.scorm');
$scorm = $scorm_service
->scormLoadById($opigno_scorm_id);
\Drupal::moduleHandler()
->invokeAll('opigno_scorm_commit', [
$scorm,
$opigno_scorm_sco_id,
$data,
]);
return new JsonResponse([
'success' => 1,
]);
}
else {
return new JsonResponse([
'error' => 1,
'message' => 'no data received',
]);
}
}
}