You are here

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

Duplicate course.

Parameters

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

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

File

src/Controller/OpignoGroupManagerController.php, line 1001

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function courseDuplicate(Group $group) {
  $duplicate = $group
    ->createDuplicate();
  $current_name = $duplicate
    ->label();
  $duplicate
    ->set('label', $this
    ->t('Duplicate of ') . $current_name);
  $current_time = \Drupal::time()
    ->getCurrentTime();
  $duplicate
    ->setOwnerId(\Drupal::currentUser()
    ->id());
  $duplicate
    ->set('created', $current_time);
  $duplicate
    ->set('changed', $current_time);
  $duplicate
    ->save();
  $duplicate_id = $duplicate
    ->id();
  $course_content = $group
    ->getContentEntities();
  foreach ($course_content as $content) {
    if ($content instanceof OpignoModule) {
      $duplicate
        ->addContent($content, 'opigno_module_group');
      $managed_content_array = OpignoGroupManagedContent::loadByProperties([
        'group_content_type_id' => 'ContentTypeModule',
        '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,
  ]);
}