You are here

function opigno_moxtra_group_content_insert in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x opigno_moxtra.module \opigno_moxtra_group_content_insert()

Implements hook_ENTITY_TYPE_insert().

File

./opigno_moxtra.module, line 477
Contains opigno_moxtra.module.

Code

function opigno_moxtra_group_content_insert(EntityInterface $entity) {
  if (!_opigno_moxtra_is_active()) {
    return;
  }

  /** @var \Drupal\group\Entity\GroupContentInterface $entity */
  $bundle = $entity
    ->bundle();
  $type = $entity
    ->getEntity()
    ->getEntityTypeId();
  if ($bundle === 'learning_path-group_membership') {

    // Adds user to the collaborative workspace of the training.
    $group = $entity
      ->getGroup();
    $workspace = _opigno_moxtra_get_workspace($group);
    if ($workspace !== NULL) {
      $binder_id = $workspace
        ->getBinderId();
      if (!empty($binder_id)) {
        $moxtra_api = _opigno_moxtra_get_moxtra_api();
        $owner_id = $workspace
          ->getOwnerId();

        /** @var \Drupal\user\Entity\User $user */
        $user = $entity
          ->getEntity();
        $user_id = $user
          ->id();
        $auto_register = $workspace
          ->getAutoRegister();
        if ($auto_register == 1) {
          if (_opigno_moxtra_is_user_enabled($user)) {

            // Add user to the collaborative workspace.
            $workspace
              ->addMember($user_id);
            $workspace
              ->save();

            // Add user to the binder in the Moxtra.
            $moxtra_api
              ->addUsersToWorkspace($owner_id, $binder_id, [
              $user_id,
            ]);
          }
        }
      }
    }
  }
  elseif ($type === 'opigno_moxtra_meeting') {

    // Updates the related training field in the meeting.

    /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
    $meeting = $entity
      ->getEntity();
    $training = $entity
      ->getGroup();
    $meeting
      ->setTraining($training);
    $meeting
      ->save();
  }
}