You are here

public function OpignoGroupManagerController::courseExport in Opigno group manager 8

Same name and namespace in other branches
  1. 3.x src/Controller/OpignoGroupManagerController.php \Drupal\opigno_group_manager\Controller\OpignoGroupManagerController::courseExport()

Export course.

Parameters

\Drupal\group\Entity\Group $group: Group object.

1 string reference to 'OpignoGroupManagerController::courseExport'
opigno_group_manager.routing.yml in ./opigno_group_manager.routing.yml
opigno_group_manager.routing.yml

File

src/Controller/OpignoGroupManagerController.php, line 1079

Class

OpignoGroupManagerController
Controller for all the actions of the Opigno group manager app.

Namespace

Drupal\opigno_group_manager\Controller

Code

public function courseExport(Group $group) {
  $course_content = $group
    ->getContentEntities();
  $course_fields = $group
    ->getFields();
  $files_to_export = [];
  foreach ($course_fields as $field_key => $field) {
    $data_structure[$group
      ->id()][$field_key] = $field
      ->getValue();
  }
  $course_name = $data_structure[$group
    ->id()]['label'][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);
  $serializer = \Drupal::service('serializer');
  $content = $serializer
    ->serialize($data_structure, $format);
  $filename = "export-course_{$course_name}_{$group->id()}.{$format}";
  $filename_path = "{$dir}/{$filename}";
  $files_to_export['course'] = $filename;
  $context['results']['file'] = \Drupal::service('file_system')
    ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
  $new_filename = "opigno-course_{$course_name}_{$group->id()}.opi";
  $zip = new \ZipArchive();
  $zip
    ->open($dir . '/' . $new_filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  $zip
    ->addFile($filename_path, $filename);
  foreach ($course_content as $entity) {
    if ($entity instanceof OpignoModule) {
      $activities = $entity
        ->getModuleActivities();
      $module_fields = $entity
        ->getFields();
      $data_structure = [];
      foreach ($module_fields as $field_key => $field) {
        $data_structure[$entity
          ->id()][$field_key] = $field
          ->getValue();
      }
      $managed_content_array = OpignoGroupManagedContent::loadByProperties([
        'group_content_type_id' => 'ContentTypeModule',
        'entity_id' => $entity
          ->id(),
        'group_id' => $group
          ->id(),
      ]);
      $managed_content = reset($managed_content_array);
      $parent_links = $managed_content
        ->getParentsLinks();
      foreach ($parent_links as $link) {
        $parent_old_content = OpignoGroupManagedContent::load($link
          ->getParentContentId());
        $parent_module_id = $parent_old_content
          ->getEntityId();
        $parent_new_content_array = OpignoGroupManagedContent::loadByProperties([
          'group_content_type_id' => 'ContentTypeModule',
          'entity_id' => $parent_module_id,
          'group_id' => $group
            ->id(),
        ]);
        $parent_new_content = reset($parent_new_content_array);
        if ($parent_new_content) {
          $link = [
            'group_id' => $group
              ->id(),
            'parent_content_id' => $parent_new_content
              ->id(),
            'child_content_id' => $entity
              ->id(),
            'required_score' => $link
              ->getRequiredScore(),
            'required_activities' => $link
              ->getRequiredActivities(),
          ];
          $data_structure[$entity
            ->id()]['parent_links'][] = $link;
        }
      }
      $data_structure[$entity
        ->id()]['managed_content'] = $managed_content;
      $module_name = $data_structure[$entity
        ->id()]['name'][0]['value'];
      $content = $serializer
        ->serialize($data_structure, $format);
      $filename = "export-module_{$module_name}_{$entity->id()}.{$format}";
      $filename_path = "{$dir}/{$filename}";
      $files_to_export['modules'][$module_name . '_' . $entity
        ->id()] = $filename;
      $context['results']['file'] = \Drupal::service('file_system')
        ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
      $zip
        ->addFile($filename_path, $filename);
      foreach ($activities as $activity) {
        $opigno_activity = OpignoActivity::load($activity->id);
        $fields = $opigno_activity
          ->getFields();
        $data_structure = [];
        foreach ($fields as $field_key => $field) {
          $data_structure[$opigno_activity
            ->id()][$field_key] = $field
            ->getValue();
        }
        $activity_name = $data_structure[$opigno_activity
          ->id()]['name'][0]['value'];
        $filename = "export-activity_{$activity_name}_{$opigno_activity->id()}.{$format}";
        $filename_path = "{$dir}/{$filename}";
        $files_to_export['activities'][$module_name . '_' . $entity
          ->id()][] = $filename;
        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;
        }
        if ($opigno_activity
          ->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);
        }
        $content = $serializer
          ->serialize($data_structure, $format);
        $context['results']['file'] = \Drupal::service('file_system')
          ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
        $zip
          ->addFile($filename_path, $filename);
      }
    }
  }
  $content = $serializer
    ->serialize($files_to_export, $format);
  $filename = "list_of_files.{$format}";
  $filename_path = "{$dir}/{$filename}";
  $files_to_export['activities'][] = $filename;
  $context['results']['file'] = \Drupal::service('file_system')
    ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
  $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);
}