public function ImportTrainingForm::submitForm in Opigno module 8
Same name and namespace in other branches
- 3.x src/Form/ImportTrainingForm.php \Drupal\opigno_module\Form\ImportTrainingForm::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ ImportTrainingForm.php, line 125
Class
- ImportTrainingForm
- Import Course form.
Namespace
Drupal\opigno_module\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->prepareTemporary();
$files = $this
->getImportFiles();
if (in_array('list_of_files.json', $files)) {
$file_path = $this->tmp . '/list_of_files.json';
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$format = 'json';
$ids = [];
$serializer = \Drupal::service('serializer');
$content = $serializer
->decode($file, $format);
$prev_id = 0;
$file_path = $this->tmp . '/' . $content['training'];
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$training_content_array = $serializer
->decode($file, $format);
$training_content = reset($training_content_array);
$new_training = $this
->importTraining($training_content);
$ids['training'][$training_content['id'][0]['value']] = $new_training
->id();
if (!empty($content['courses'])) {
foreach ($content['courses'] as $course_name => $course_path) {
$file_path = $this->tmp . '/' . $course_path;
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$course_content_array = $serializer
->decode($file, $format);
$course_content = reset($course_content_array);
$new_course = $this
->importCourse($course_content);
$ids['course'][$course_content['id'][0]['value']] = $new_course
->id();
$managed_content = $course_content['managed_content'];
$new_training
->addContent($new_course, 'subgroup:opigno_course');
$new_content = OpignoGroupManagedContent::createWithValues($new_training
->id(), 'ContentTypeCourse', $new_course
->id(), $course_content['managed_content']['success_score_min'][0]['value'], $course_content['managed_content']['is_mandatory'][0]['value'], $course_content['managed_content']['coordinate_x'][0]['value'], $course_content['managed_content']['coordinate_y'][0]['value']);
$new_content
->save();
$ids['link'][$managed_content['id'][0]['value']] = $new_content
->id();
$ids['link_child'][$course_content['id'][0]['value']] = $new_content
->id();
}
}
if (!empty($content['modules'])) {
foreach ($content['modules'] as $module_name => $module_path) {
$file_path = $this->tmp . '/' . $module_path;
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$module_content_array = $serializer
->decode($file, $format);
$module_content = reset($module_content_array);
$new_module = $this
->importModule($module_content);
$ids['module'][$module_content['id'][0]['value']] = $new_module
->id();
$managed_content = $module_content['managed_content'];
$parent_links = $module_content['parent_links'];
$parent_group_id = $new_training
->id();
if (isset($module_content['course_rel'])) {
$parent_group_id = $ids['course'][$module_content['course_rel']];
$course = Group::load($parent_group_id);
$course
->addContent($new_module, 'opigno_module_group');
}
else {
$new_training
->addContent($new_module, 'opigno_module_group');
}
$new_content = OpignoGroupManagedContent::createWithValues($parent_group_id, $managed_content['group_content_type_id'][0]['value'], $new_module
->id(), $managed_content['success_score_min'][0]['value'], $managed_content['is_mandatory'][0]['value'], $managed_content['coordinate_x'][0]['value'], $managed_content['coordinate_y'][0]['value']);
$new_content
->save();
$ids['link'][$managed_content['id'][0]['value']] = $new_content
->id();
foreach ($content['activities'][$module_name] as $activity_file_name) {
$file_path = $this->tmp . '/' . $activity_file_name;
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$activity_content_array = $serializer
->decode($file, $format);
$activity_content = reset($activity_content_array);
$new_activity = $this
->importActivity($activity_content);
$ids['activities'][$activity_content['id'][0]['value']] = $new_activity
->id();
$opigno_module_obj = \Drupal::service('opigno_module.opigno_module');
$max_score = isset($activity_content['max_score']) ? $activity_content['max_score'] : 10;
$opigno_module_obj
->activitiesToModule([
$new_activity,
], $new_module, NULL, $max_score);
}
foreach ($parent_links as $link) {
if ($link['required_activities']) {
foreach ($link['required_activities'] as $key_req => $require_string) {
$require = explode('-', $require_string);
$link['required_activities'][$key_req] = str_replace($require[0], $ids['activities'][$require[0]], $link['required_activities'][$key_req]);
}
$link['required_activities'] = serialize($link['required_activities']);
}
else {
$link['required_activities'] = NULL;
}
$new_content_id = $new_content
->id();
$new_parent_id = $ids['link'][$link['parent_content_id']];
if ($new_content_id === $ids['link'][$link['parent_content_id']] && !empty($prev_id)) {
$new_parent_id = $prev_id;
}
if (!empty($ids['link'][$link['parent_content_id']])) {
OpignoGroupManagedLink::createWithValues($parent_group_id, $new_parent_id, $new_content_id, $link['required_score'], $link['required_activities'])
->save();
$prev_id = $new_content_id;
}
}
}
}
// Set links for courses.
if (!empty($content['courses'])) {
foreach ($content['courses'] as $course_name => $course_path) {
$file_path = $this->tmp . '/' . $course_path;
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$course_content_array = $serializer
->decode($file, $format);
$course_content = reset($course_content_array);
$parent_links = $course_content['parent_links'];
foreach ($parent_links as $link) {
if ($link['required_activities']) {
foreach ($link['required_activities'] as $key_req => $require_string) {
$require = explode('-', $require_string);
$link['required_activities'][$key_req] = str_replace($require[0], $ids['activities'][$require[0]], $link['required_activities'][$key_req]);
}
}
OpignoGroupManagedLink::createWithValues($new_training
->id(), $ids['link'][$link['parent_content_id']], $ids['link_child'][$link['child_content_id']], $link['required_score'], serialize($link['required_activities']))
->save();
}
}
}
// Import documents library.
$tids_relationships = [];
$main_tid = $new_training
->get('field_learning_path_folder')
->getString();
$tids_relationships[$training_content['field_learning_path_folder'][0]['target_id']] = $main_tid;
if (!empty($content['terms'])) {
foreach ($content['terms'] as $term_file_name) {
$file_path = $this->tmp . '/library/' . $term_file_name;
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$term_content = $serializer
->decode($file, $format);
$parent_id = $tids_relationships[$term_content['parent'][0]['target_id']];
$new_term = Term::create([
'name' => $term_content['name'][0]['value'],
'langcode' => $term_content['langcode'][0]['value'],
'vid' => 'tft_tree',
'parent' => $parent_id,
]);
$new_term
->save();
$tids_relationships[$term_content['tid'][0]['value']] = $new_term
->id();
}
}
if (!empty($content['files'])) {
foreach ($content['files'] as $document) {
$file_path = $this->tmp . '/library/' . $document['file'];
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$file_content = $serializer
->decode($file, $format);
$current_timestamp = \Drupal::time()
->getCurrentTime();
$date = date('Y-m', $current_timestamp);
$file_source = $this->tmp . '/library/' . $file_content['fid'][0]['value'] . '-' . $file_content['filename'][0]['value'];
$dest_folder = 'public://' . $date;
$destination = $dest_folder . '/' . $file_content['filename'][0]['value'];
$uri = $this
->copyFile($file_source, $destination, $dest_folder);
if (!empty($uri)) {
$file = File::Create([
'uri' => $uri,
'uid' => $this
->currentUser()
->id(),
'status' => $file_content['status'][0]['value'],
]);
$file
->save();
$fid = $file
->id();
$file_path = $this->tmp . '/library/' . $document['media'];
$real_path = $this->fileSystem
->realpath($file_path);
$file = file_get_contents($real_path);
$file_content = $serializer
->decode($file, $format);
}
if (!empty($file_content['file_name'])) {
$media = Media::create([
'bundle' => $file_content['bundle'],
'name' => $file_content['file_name'],
'tft_file' => [
'target_id' => $fid,
],
'tft_folder' => [
'target_id' => $tids_relationships[$file_content['tft_folder'][0]['target_id']],
],
]);
$media
->save();
}
}
}
$route_parameters = [
'group' => $new_training
->id(),
];
\Drupal::messenger()
->addMessage(t('Imported training %training', [
'%training' => Link::createFromRoute($new_training
->label(), 'entity.group.canonical', $route_parameters)
->toString(),
]));
$form_state
->setRedirect('entity.group.collection');
}
}