You are here

public function OpignoGroupManagerController::trainingDuplicate 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::trainingDuplicate()

Duplicate training.

Parameters

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

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

File

src/Controller/OpignoGroupManagerController.php, line 1731

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function trainingDuplicate(Group $group) {
  $duplicate = $group
    ->createDuplicate();
  $current_name = $duplicate
    ->label();
  $new_name = $this
    ->t('Duplicate of ') . $current_name;
  $duplicate
    ->set('label', $new_name);
  $current_time = \Drupal::time()
    ->getCurrentTime();
  $user = \Drupal::currentUser();
  $user_id = $user
    ->id();
  $duplicate
    ->setOwnerId($user_id);
  $duplicate
    ->set('created', $current_time);
  $duplicate
    ->set('changed', $current_time);
  $duplicate
    ->set('field_learning_path_enable_forum', 0);
  $duplicate
    ->set('field_learning_path_forum', NULL);
  $duplicate
    ->set('field_learning_path_folder', NULL);
  $duplicate
    ->save();
  if ($this
    ->moduleHandler()
    ->moduleExists('opigno_moxtra')) {

    // Create workspace.
    $moxtra_api = _opigno_moxtra_get_moxtra_api();
    $response = $moxtra_api
      ->createWorkspace($user_id, $new_name);
    $binder_id = $response['data']['id'];
    $workspace = Workspace::create();
    $workspace
      ->setName($new_name);
    $workspace
      ->setBinderId($binder_id);
    $workspace
      ->save();

    // Attach workspace to the training.
    $duplicate
      ->set('field_workspace', [
      'target_id' => $workspace
        ->id(),
    ]);
  }

  // Duplicate documents library.
  $tid = $group
    ->get('field_learning_path_folder')
    ->getString();
  $items_list = $this
    ->documentsLibraryList($tid);
  $main_tid = $duplicate
    ->get('field_learning_path_folder')
    ->getString();
  $tids_relashionships = [];
  $tids_relashionships[$tid] = $main_tid;
  foreach ($items_list as $item) {
    if ($item['type'] == 'file') {
      $media = Media::load($item['id']);
      $media_copy = $media
        ->createDuplicate();
      $file_id = $media
        ->get('tft_file')
        ->getValue()[0]['target_id'];
      $old_tid = $media
        ->get('tft_folder')
        ->getValue()[0]['target_id'];
      $file = File::load($file_id);
      if ($file) {
        $file_copy = $file
          ->createDuplicate();
        $new_name = $file
          ->id() . '-' . $file
          ->label();
        $new_name = substr($new_name, 0, 200);
        $new_uri = str_replace($file
          ->label(), $new_name, $file
          ->getFileUri());
        $file_copy
          ->setFilename($new_name);
        $file_copy
          ->setFileUri($new_uri);
        $file_copy
          ->save();
        $file_copy_id = $file_copy
          ->id();
        if (isset($tids_relashionships[$old_tid])) {
          $new_tid = $tids_relashionships[$old_tid];
        }
        else {
          $new_tid = $main_tid;
        }
        $media_copy
          ->set('tft_file', $file_copy_id);
        $media_copy
          ->set('tft_folder', $new_tid);
        copy($file
          ->getFileUri(), $new_uri);
        $media_copy
          ->save();
      }
    }
    elseif ($item['type'] == 'term') {
      $old_term = Term::load($item['id']);
      if ($old_term) {
        $legacy_parent_id = $old_term
          ->get('parent')
          ->getValue()[0]['target_id'];
        if ($tids_relashionships[$legacy_parent_id]) {
          $parent = $tids_relashionships[$legacy_parent_id];
        }
        else {
          $parent = $main_tid;
        }
        $new_term = Term::create([
          'vid' => 'tft_tree',
          'name' => $item['name'],
          'parent' => $parent,
        ]);
        $new_term
          ->save();
        $new_term_id = $new_term
          ->id();
        $tids_relashionships[$item['id']] = $new_term_id;
      }
    }
  }
  $duplicate_id = $duplicate
    ->id();
  $group_content = $group
    ->getContentEntities();
  foreach ($group_content as $content) {
    if ($content instanceof OpignoModule || $content instanceof Group) {
      if ($content instanceof OpignoModule) {
        $duplicate
          ->addContent($content, 'opigno_module_group');
        $group_content_type_id = 'ContentTypeModule';
      }
      else {
        $duplicate
          ->addContent($content, 'subgroup:opigno_course');
        $group_content_type_id = 'ContentTypeCourse';
      }
      $managed_content_array = OpignoGroupManagedContent::loadByProperties([
        'group_content_type_id' => $group_content_type_id,
        'entity_id' => $content
          ->id(),
        'group_id' => $group
          ->id(),
      ]);
      $managed_content = reset($managed_content_array);
      $parent_links = $managed_content
        ->getParentsLinks();
      $new_content = OpignoGroupManagedContent::createWithValues($duplicate
        ->id(), $managed_content
        ->getGroupContentTypeId(), $content
        ->id(), $managed_content
        ->getSuccessScoreMin(), $managed_content
        ->isMandatory(), $managed_content
        ->getCoordinateX(), $managed_content
        ->getCoordinateY());
      $new_content
        ->save();
      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' => $duplicate_id,
        ]);
        $parent_new_content = reset($parent_new_content_array);
        if ($parent_new_content) {
          OpignoGroupManagedLink::createWithValues($duplicate_id, $parent_new_content
            ->id(), $new_content
            ->id(), $link
            ->getRequiredScore(), serialize($link
            ->getRequiredActivities()))
            ->save();
        }
      }
    }
  }
  $duplicate
    ->save();
  return $this
    ->redirect('entity.group.edit_form', [
    'group' => $duplicate_id,
  ]);
}