You are here

public function OpignoGroupManagerController::addItem in Opigno group manager 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoGroupManagerController.php \Drupal\opigno_group_manager\Controller\OpignoGroupManagerController::addItem()

This method adds an item (content) in the learning path.

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

File

src/Controller/OpignoGroupManagerController.php, line 548

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function addItem(Group $group, Request $request) {
  $moduleHandler = \Drupal::service('module_handler');

  // First, check if all parameters are here.
  $datas = json_decode($request
    ->getContent());
  $entity_id = !empty($datas->entityId) ? $datas->entityId : 0;
  $content_type = !empty($datas->contentType) ? $datas->contentType : 0;
  $exist = $this
    ->hasItem($group
    ->id(), $entity_id, $content_type);
  if ($exist) {
    return new JsonResponse(NULL, Response::HTTP_OK);
  }
  if (empty($datas->entityId) || empty($datas->contentType)) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

  // Get the params.
  $entityId = $datas->entityId;
  $contentType = $datas->contentType;
  $parentCid = empty($datas->parentCid) ? NULL : $datas->parentCid;

  // Create the added item as an LP content.
  $new_content = OpignoGroupManagedContent::createWithValues($group
    ->id(), $contentType, $entityId);

  // Check if the content type is a module.
  // If yes then check if the module in the skills system.
  if ($moduleHandler
    ->moduleExists('opigno_skills_system') && $contentType === 'ContentTypeModule') {
    $module = \Drupal::entityTypeManager()
      ->getStorage('opigno_module')
      ->load($entityId);
    $new_content
      ->set('in_skills_system', $module
      ->getSkillsActive());
  }
  $new_content
    ->save();

  // Then, create the links to the parent content.
  if (!empty($parentCid)) {
    OpignoGroupManagedLink::createWithValues($group
      ->id(), $parentCid, $new_content
      ->id())
      ->save();
  }

  // Add created entity as Group content.
  $plugin_definition = $this->content_types_manager
    ->getDefinition($datas->contentType);
  if (!empty($plugin_definition['group_content_plugin_id'])) {

    // Load Course (Group) entity and save as content using specific plugin.
    $added_entity = \Drupal::entityTypeManager()
      ->getStorage($plugin_definition['entity_type'])
      ->load($datas->entityId);
    $group
      ->addContent($added_entity, $plugin_definition['group_content_plugin_id']);
  }
  $response = new JsonResponse([
    'cid' => $new_content
      ->id(),
  ], Response::HTTP_OK);
  sleep(1);
  return $response;
}