class LearningPathController in Opigno module 8
Same name and namespace in other branches
- 3.x src/Controller/LearningPathController.php \Drupal\opigno_module\Controller\LearningPathController
Class LearningPathController.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\opigno_module\Controller\LearningPathController
Expanded class hierarchy of LearningPathController
File
- src/
Controller/ LearningPathController.php, line 20
Namespace
Drupal\opigno_module\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
40 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LearningPathController:: |
public | function | Activities bank. | |
LearningPathController:: |
public | function | Add index. | |
LearningPathController:: |
public | function | Duplicate index. | |
LearningPathController:: |
public | function | Edit index. | |
LearningPathController:: |
public | function | Export activity index. | |
LearningPathController:: |
public | function | Export module index. | |
LearningPathController:: |
public | function | Modules index. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |