public function LearningPathController::exportModule in Opigno module 3.x
Same name and namespace in other branches
- 8 src/Controller/LearningPathController.php \Drupal\opigno_module\Controller\LearningPathController::exportModule()
Export module index.
1 string reference to 'LearningPathController::exportModule'
File
- src/
Controller/ LearningPathController.php, line 220
Class
- LearningPathController
- Class LearningPathController.
Namespace
Drupal\opigno_module\ControllerCode
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);
}