You are here

public function OpignoScorm::unzipPackage in Opigno SCORM 8

Same name and namespace in other branches
  1. 3.x src/OpignoScorm.php \Drupal\opigno_scorm\OpignoScorm::unzipPackage()
1 call to OpignoScorm::unzipPackage()
OpignoScorm::scormExtract in src/OpignoScorm.php
Extract and save Scorm data from Scorm package.

File

src/OpignoScorm.php, line 22

Class

OpignoScorm
Class OpignoScorm.

Namespace

Drupal\opigno_scorm

Code

public function unzipPackage(File $file) {
  $path = \Drupal::service('file_system')
    ->realpath($file
    ->getFileUri());
  $zip = new \ZipArchive();
  $result = $zip
    ->open($path);
  if ($result === TRUE) {
    $extract_dir = 'public://opigno_scorm_extracted/scorm_' . $file
      ->id();
    $zip
      ->extractTo($extract_dir);
    $zip
      ->close();
    return TRUE;
  }
  else {
    $error = 'none';
    switch ($result) {
      case \ZipArchive::ER_EXISTS:
        $error = 'ER_EXISTS';
        break;
      case \ZipArchive::ER_INCONS:
        $error = 'ER_INCONS';
        break;
      case \ZipArchive::ER_INVAL:
        $error = 'ER_INVAL';
        break;
      case \ZipArchive::ER_NOENT:
        $error = 'ER_NOENT';
        break;
      case \ZipArchive::ER_NOZIP:
        $error = 'ER_NOZIP';
        break;
      case \ZipArchive::ER_OPEN:
        $error = 'ER_OPEN';
        break;
      case \ZipArchive::ER_READ:
        $error = 'ER_READ';
        break;
      case \ZipArchive::ER_SEEK:
        $error = 'ER_SEEK';
        break;
    }
    \Drupal::logger('opigno_scorm')
      ->error("An error occured when unziping the SCORM package data. Error: !error", [
      '!error' => $error,
    ]);
  }
  return FALSE;
}