You are here

protected static function ExternalPackageController::checkPackageType in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Controller/ExternalPackageController.php \Drupal\opigno_module\Controller\ExternalPackageController::checkPackageType()

Function for checking what package type was downloaded.

Return value

string|bool Returned 'scorm' or 'tincan' or FALSE.

1 call to ExternalPackageController::checkPackageType()
ExternalPackageController::ajaxFormExternalPackageFormSubmit in src/Controller/ExternalPackageController.php
Custom submit handler added via the function opigno_module_form_alter().

File

src/Controller/ExternalPackageController.php, line 175

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

protected static function checkPackageType($file) {

  // Unzip file.
  $path = \Drupal::service('file_system')
    ->realpath($file
    ->getFileUri());
  $zip = new \ZipArchive();
  $result = $zip
    ->open($path);
  if ($result === TRUE) {
    $extract_dir = 'public://external_package_extracted/package_' . $file
      ->id();
    $zip
      ->extractTo($extract_dir);
    $zip
      ->close();

    // This is a standard: these files must always be here.
    $scorm_file = $extract_dir . '/imsmanifest.xml';
    $tincan_file = $extract_dir . '/tincan.xml';
    if (file_exists($scorm_file)) {
      $package_type = 'scorm';
    }
    elseif (file_exists($tincan_file)) {
      $package_type = 'tincan';
    }
    else {
      $files = scandir($extract_dir);
      $count_files = count($files);
      if ($count_files == 3 && is_dir($extract_dir . '/' . $files[2])) {
        $subfolder_files = scandir($extract_dir . '/' . $files[2]);
        if (in_array('imsmanifest.xml', $subfolder_files)) {
          $source = $extract_dir . '/' . $files[2];
          $i = new \RecursiveDirectoryIterator($source);
          foreach ($i as $f) {
            if ($f
              ->isFile()) {
              rename($f
                ->getPathname(), $extract_dir . '/' . $f
                ->getFilename());
            }
            else {
              if ($f
                ->isDir()) {
                rename($f
                  ->getPathname(), $extract_dir . '/' . $f
                  ->getFilename());
                unlink($f
                  ->getPathname());
              }
            }
          }
          $package_type = 'scorm';
        }
      }
    }

    // Delete extracted archive.
    \Drupal::service('file_system')
      ->deleteRecursive($extract_dir);
    if (isset($package_type)) {
      return $package_type;
    }
  }
  return FALSE;
}