You are here

private function AddExternalPackageForm::validateZipPackage in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/AddExternalPackageForm.php \Drupal\opigno_module\Form\AddExternalPackageForm::validateZipPackage()
1 call to AddExternalPackageForm::validateZipPackage()
AddExternalPackageForm::validateForm in src/Form/AddExternalPackageForm.php
Form validation handler.

File

src/Form/AddExternalPackageForm.php, line 164

Class

AddExternalPackageForm
Add External package form.

Namespace

Drupal\opigno_module\Form

Code

private function validateZipPackage($form, FormStateInterface $form_state, File $file) {

  /* @var \Drupal\opigno_scorm\OpignoScorm $scorm_controller */
  $scorm_controller = \Drupal::service('opigno_scorm.scorm');
  $file_extension = substr(strrchr($file
    ->getFilename(), '.'), 1);
  if ($file_extension == 'zip') {
    $scorm_controller
      ->unzipPackage($file);
    $extract_dir = 'public://opigno_scorm_extracted/scorm_' . $file
      ->id();

    // 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) && !file_exists($tincan_file)) {
      $validation = FALSE;
      $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());
              }
            }
          }
          $validation = TRUE;
        }
      }
      if ($validation == FALSE) {
        $form_state
          ->setError($form['package'], $this
          ->t('Your file is not recognized as a valid SCORM, TinCan, or H5P package.'));
      }
    }
  }
}