You are here

class LearningPathController in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Controller/LearningPathController.php \Drupal\opigno_module\Controller\LearningPathController

Class LearningPathController.

Hierarchy

Expanded class hierarchy of LearningPathController

File

src/Controller/LearningPathController.php, line 20

Namespace

Drupal\opigno_module\Controller
View source
class LearningPathController extends ControllerBase {

  /**
   * Add index.
   */
  public function addIndex() {
    $opigno_module = OpignoModule::create();
    $form = \Drupal::service('entity.form_builder')
      ->getForm($opigno_module);
    return $form;
  }

  /**
   * Edit index.
   */
  public function editIndex($opigno_module) {
    return \Drupal::service('entity.form_builder')
      ->getForm($opigno_module);
  }

  /**
   * Duplicate index.
   */
  public function duplicateModule($opigno_module) {
    $duplicate = $opigno_module
      ->createDuplicate();
    $current_name = $duplicate
      ->label();
    $duplicate
      ->setName($this
      ->t('Duplicate of ') . $current_name);
    $activities = $opigno_module
      ->getModuleActivities();
    $current_time = \Drupal::time()
      ->getCurrentTime();
    $add_activities = [];
    foreach ($activities as $activity) {
      $add_activities[] = OpignoActivity::load($activity->id);
    }
    $duplicate
      ->setOwnerId(\Drupal::currentUser()
      ->id());
    $duplicate
      ->set('created', $current_time);
    $duplicate
      ->set('changed', $current_time);
    $duplicate
      ->save();
    $duplicate_id = $duplicate
      ->id();
    $opigno_module_obj = \Drupal::service('opigno_module.opigno_module');
    $opigno_module_obj
      ->activitiesToModule($add_activities, $duplicate);
    return $this
      ->redirect('opigno_module.edit', [
      'opigno_module' => $duplicate_id,
    ]);
  }

  /**
   * Export activity index.
   */
  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);
  }

  /**
   * Export module index.
   */
  public function exportModule($opigno_module) {
    $activities = $opigno_module
      ->getModuleActivities();
    $module_fields = $opigno_module
      ->getFields();
    $files_to_export = [];
    foreach ($module_fields as $field_key => $field) {
      $data_structure[$opigno_module
        ->id()][$field_key] = $field
        ->getValue();
    }
    $module_name = $data_structure[$opigno_module
      ->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);
    $serializer = \Drupal::service('serializer');
    $content = $serializer
      ->serialize($data_structure, $format);
    $filename = "export-module_{$module_name}.{$format}";
    $filename_path = "{$dir}/{$filename}";
    $files_to_export['module'] = $filename;
    $context['results']['file'] = \Drupal::service('file_system')
      ->saveData($content, $filename_path, FileSystemInterface::EXISTS_REPLACE);
    $new_filename = "opigno-module_{$module_name}.opi";
    $zip = new \ZipArchive();
    $zip
      ->open($dir . '/' . $new_filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
    $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'][] = $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);
  }

  /**
   * Modules index.
   */
  public function modulesIndex($opigno_module, Request $request) {
    return [
      '#theme' => 'opigno_learning_path_modules',
      '#attached' => [
        'library' => [
          'opigno_group_manager/manage_app',
        ],
      ],
      '#base_path' => $request
        ->getBasePath(),
      '#base_href' => $request
        ->getPathInfo(),
      '#learning_path_id' => $opigno_module
        ->id(),
      '#module_context' => 'true',
    ];
  }

  /**
   * Activities bank.
   */
  public function activitiesBank($opigno_module) {

    // Output activities bank view.
    $activities_bank['activities_bank'] = views_embed_view('opigno_activities_bank_lp_interface');
    $build[] = $activities_bank;
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 46
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LearningPathController::activitiesBank public function Activities bank.
LearningPathController::addIndex public function Add index.
LearningPathController::duplicateModule public function Duplicate index.
LearningPathController::editIndex public function Edit index.
LearningPathController::exportActivity public function Export activity index.
LearningPathController::exportModule public function Export module index.
LearningPathController::modulesIndex public function Modules index.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.