You are here

public function LearningPathController::exportActivity in Opigno module 8

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

Export activity index.

1 string reference to 'LearningPathController::exportActivity'
opigno_module.routing.yml in ./opigno_module.routing.yml
opigno_module.routing.yml

File

src/Controller/LearningPathController.php, line 69

Class

LearningPathController
Class LearningPathController.

Namespace

Drupal\opigno_module\Controller

Code

public function exportActivity($opigno_activity) {
  $bundle = $opigno_activity
    ->bundle();
  $fields = $opigno_activity
    ->getFields();
  $ignore_fields = [
    'id',
    'uuid',
    'vid',
    'revision_created',
    'revision_user',
    'evision_log_message',
    'uid',
    'revision_default',
    'revision_translation_affected',
  ];
  $data_structure[$opigno_activity
    ->id()]['entity_type'] = (string) $opigno_activity
    ->getEntityTypeId();
  $data_structure[$opigno_activity
    ->id()]['bundle'] = (string) $opigno_activity
    ->bundle();
  foreach ($fields as $field_key => $field) {
    if (!isset($opigno_activity->{$field_key}) || in_array($field_key, $ignore_fields)) {
      continue;
    }
    $data_structure[$opigno_activity
      ->id()][$field_key] = $field
      ->getValue();
  }
  $activity_name = $data_structure[$opigno_activity
    ->id()]['name'][0]['value'];
  $format = 'json';
  $dir = 'sites/default/files/opigno-export';
  \Drupal::service('file_system')
    ->deleteRecursive($dir);
  \Drupal::service('file_system')
    ->prepareDirectory($dir, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY);
  $filename = "export-{$opigno_activity->getEntityTypeId()}.{$format}";
  $filename_path = "{$dir}/{$filename}";
  $new_filename = "opigno-activity_{$activity_name}.opi";
  $zip = new \ZipArchive();
  $zip
    ->open($dir . '/' . $new_filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  switch ($opigno_activity
    ->bundle()) {
    case 'opigno_scorm':
      if (isset($opigno_activity
        ->get('opigno_scorm_package')->target_id)) {
        $file = File::load($opigno_activity
          ->get('opigno_scorm_package')->target_id);
        if ($file) {
          $file_uri = $file
            ->getFileUri();
          $file_path = \Drupal::service('file_system')
            ->realpath($file_uri);
          $scorm_filename = $file
            ->id() . '-' . $file
            ->getFilename();
          $data_structure[$opigno_activity
            ->id()]['files'][$scorm_filename] = [
            'file_name' => $file
              ->getFilename(),
            'filemime' => $file
              ->getMimeType(),
            'status' => $file
              ->get('status')
              ->getValue()[0]['value'],
          ];
          $zip
            ->addFile($file_path, $scorm_filename);
        }
      }
      break;
    case 'opigno_tincan':
      if (isset($opigno_activity
        ->get('opigno_tincan_package')->target_id)) {
        $file = File::load($opigno_activity
          ->get('opigno_tincan_package')->target_id);
        if ($file) {
          $file_uri = $file
            ->getFileUri();
          $file_path = \Drupal::service('file_system')
            ->realpath($file_uri);
          $tincan_filename = $file
            ->id() . '-' . $file
            ->getFilename();
          $data_structure[$opigno_activity
            ->id()]['files'][$tincan_filename] = [
            'file_name' => $file
              ->getFilename(),
            'filemime' => $file
              ->getMimeType(),
            'status' => $file
              ->get('status')
              ->getValue()[0]['value'],
          ];
          $zip
            ->addFile($file_path, $tincan_filename);
        }
      }
      break;
    case 'opigno_slide':
      if (isset($opigno_activity
        ->get('opigno_slide_pdf')->target_id)) {
        $media = Media::load($opigno_activity
          ->get('opigno_slide_pdf')->target_id);
        $file_id = $media
          ->get('field_media_file')
          ->getValue()[0]['target_id'];
        $file = File::load($file_id);
        if ($file) {
          $file_uri = $file
            ->getFileUri();
          $file_path = \Drupal::service('file_system')
            ->realpath($file_uri);
          $pdf_filename = $file
            ->id() . '-' . $file
            ->getFilename();
          $data_structure[$opigno_activity
            ->id()]['files'][$pdf_filename] = [
            'file_name' => $file
              ->getFilename(),
            'filemime' => $file
              ->getMimeType(),
            'status' => $file
              ->get('status')
              ->getValue()[0]['value'],
            'bundle' => $media
              ->bundle(),
          ];
          $zip
            ->addFile($file_path, $pdf_filename);
        }
      }
      break;
    case 'opigno_video':
      if (isset($opigno_activity
        ->get('field_video')->target_id)) {
        $file = File::load($opigno_activity
          ->get('field_video')->target_id);
        if ($file) {
          $file_uri = $file
            ->getFileUri();
          $file_path = \Drupal::service('file_system')
            ->realpath($file_uri);
          $video_filename = $file
            ->id() . '-' . $file
            ->getFilename();
          $data_structure[$opigno_activity
            ->id()]['files'][$video_filename] = [
            'file_name' => $file
              ->getFilename(),
            'filemime' => $file
              ->getMimeType(),
            'status' => $file
              ->get('status')
              ->getValue()[0]['value'],
          ];
          $zip
            ->addFile($file_path, $video_filename);
        }
      }
      break;
  }
  $serializer = \Drupal::service('serializer');
  $content = $serializer
    ->serialize($data_structure, $format);
  $context['results']['file'] = \Drupal::service('file_system')
    ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
  if ($bundle == 'opigno_h5p') {
    $hp5_id = $data_structure[$opigno_activity
      ->id()]['opigno_h5p'][0]['h5p_content_id'];
    $h5p_content = H5PContent::load($hp5_id);
    $h5p_content
      ->getFilteredParameters();
    $hp5_archive = "interactive-content-{$hp5_id}.h5p";
    $zip
      ->addFile('sites/default/files/h5p/exports/' . $hp5_archive, $hp5_archive);
  }
  $zip
    ->addFile($filename_path, $filename);
  $zip
    ->close();
  $headers = [
    'Content-Type' => 'application/opi',
    'Content-Disposition' => 'attachment; filename="' . $new_filename . '"',
  ];
  if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
    $headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0';
    $headers['Pragma'] = 'public';
  }
  else {
    $headers['Pragma'] = 'no-cache';
  }
  return new BinaryFileResponse($dir . '/' . $new_filename, 200, $headers);
}