You are here

public function OpignoScormPlayer::toRendarableArray in Opigno SCORM 8

Same name and namespace in other branches
  1. 3.x src/OpignoScormPlayer.php \Drupal\opigno_scorm\OpignoScormPlayer::toRendarableArray()

Build rendarable array for scorm package output.

File

src/OpignoScormPlayer.php, line 27

Class

OpignoScormPlayer
Class OpignoScormPlayer.

Namespace

Drupal\opigno_scorm

Code

public function toRendarableArray($scorm) {
  $account = \Drupal::currentUser();

  // Get SCORM API version.
  $metadata = unserialize($scorm->metadata);
  if (strpos($metadata['schemaversion'], '1.2') !== FALSE) {
    $scorm_version = '1.2';
  }
  else {
    $scorm_version = '2004';
  }

  // Get the SCO tree.
  $tree = $this
    ->opignoScormPlayerScormTree($scorm);
  $flat_tree = $this
    ->opignoScormPlayerFlattenTree($tree);

  // Get the start SCO.
  $start_sco = $this
    ->opignoScormPlayerStartSco($flat_tree);

  /* @todo Replace with custom event subscriber implementation. */

  // Get implemented CMI paths.
  $paths = opigno_scorm_add_cmi_paths($scorm_version);

  // Get CMI data for each SCO.
  $data = opigno_scorm_add_cmi_data($scorm, $flat_tree, $scorm_version);
  $sco_identifiers = [];
  $scos_suspend_data = [];
  foreach ($flat_tree as $sco) {
    if ($sco->scorm_type == 'sco') {
      $sco_identifiers[$sco->identifier] = $sco->id;
      $scos_suspend_data[$sco->id] = opigno_scorm_scorm_cmi_get($account
        ->id(), $scorm->id, 'cmi.suspend_data.' . $sco->id, '');
    }
  }
  $last_user_sco = opigno_scorm_scorm_cmi_get($account
    ->id(), $scorm->id, 'user.sco', '');
  if ($last_user_sco != '') {
    foreach ($flat_tree as $sco) {
      if ($last_user_sco == $sco->id && !empty($sco->launch)) {
        $start_sco = $sco;
      }
    }
  }

  // Add base path for player link.
  global $base_path;
  $start_sco->base_path = $base_path;
  return [
    '#theme' => 'opigno_scorm__player',
    '#scorm_id' => $scorm->id,
    '#tree' => count($flat_tree) == 2 ? NULL : $tree,
    '#start_sco' => $start_sco,
    '#attached' => [
      'library' => [
        'opigno_scorm/opigno-scorm-player',
      ],
      'drupalSettings' => [
        'opignoScormUIPlayer' => [
          'cmiPaths' => $paths,
          'cmiData' => $data,
          'scoIdentifiers' => $sco_identifiers,
          'cmiSuspendItems' => $scos_suspend_data,
        ],
        'scormVersion' => $scorm_version,
      ],
    ],
    '#cache' => [
      'max-age' => 0,
    ],
  ];
}