You are here

public function OpignoScormController::scormIntegrateSco in Opigno SCORM 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoScormController.php \Drupal\opigno_scorm\Controller\OpignoScormController::scormIntegrateSco()
1 string reference to 'OpignoScormController::scormIntegrateSco'
opigno_scorm.routing.yml in ./opigno_scorm.routing.yml
opigno_scorm.routing.yml

File

src/Controller/OpignoScormController.php, line 19

Class

OpignoScormController
Class OpignoScormController.

Namespace

Drupal\opigno_scorm\Controller

Code

public function scormIntegrateSco($opigno_scorm_sco) {
  $scorm_service = \Drupal::service('opigno_scorm.scorm');
  $sco = $scorm_service
    ->scormLoadSco($opigno_scorm_sco);

  // Does the SCO have a launch property ?
  if (!empty($sco->launch)) {
    $query = [];

    // Load the SCO data.
    $scorm = $scorm_service
      ->scormLoadById($sco->scorm_id);

    // Remove the URL parameters from the launch URL.
    if (!empty($sco->attributes['parameters'])) {
      $sco->launch .= $sco->attributes['parameters'];
    }
    $parts = explode('?', $sco->launch);
    $launch = array_shift($parts);
    if (!empty($parts)) {

      // Failsafe - in case a launch URL has 2 or more '?'.
      $parameters = implode('&', $parts);
    }

    // Get the SCO location on the filesystem.
    $sco_location = "{$scorm->extracted_dir}/{$launch}";
    $sco_path = file_create_url($sco_location);

    // Where there any parameters ? If so, prepare them for Drupal.
    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();
  }
}