You are here

function opigno_learning_path_entity_delete in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_entity_delete()

Implements hook_entity_delete().

File

./opigno_learning_path.module, line 2086
Contains opigno_learning_path.module.

Code

function opigno_learning_path_entity_delete(EntityInterface $entity) {
  if (in_array($entity
    ->bundle(), [
    'learning_path-group_membership',
    'opigno_course-group_membership',
  ])) {
    LearningPathAccess::deleteUserStatus($entity);
    if ($entity
      ->bundle() == 'learning_path-group_membership') {
      LearningPathAccess::setLearningPathCourseMember($entity, 'delete');
    }
  }
  if ($entity
    ->bundle() == 'group_content_type_27efa0097d858') {

    // Get Class members.
    $members = FALSE;
    if ($entity && $entity
      ->getEntity()) {
      $members = $entity
        ->getEntity()
        ->getMembers();
    }

    // Get class group id.
    $gid = $entity
      ->getGroup()
      ->id();
    $classes_members = [];
    $classes_ids = LearningPathContent::getGroupMembershipIdsByType($gid, [
      'group_content_type_27efa0097d858',
    ]);
    if ($classes_ids) {
      foreach ($classes_ids as $id) {
        $class_members = LearningPathContent::getGroupMembershipIdsByType($id, [
          'opigno_class-group_membership',
        ]);
        if ($class_members) {
          $classes_members = array_merge($classes_members, $class_members);
        }
      }
      if ($classes_members) {
        $classes_members = array_unique($classes_members);
      }
    }
    if ($members && !empty($gid) && is_numeric($gid)) {
      try {
        $group = Group::load($gid);

        // Get class parent groups.
        foreach ($members as $member) {
          $user = $member
            ->getGroupContent()
            ->getEntity();
          $uid = $user
            ->id();
          $class_id = $entity
            ->getEntity()
            ->id();
          $created_group_membership = LearningPathContent::getGroupMembershipTimestamp($gid, $uid);
          $created_class_membership = LearningPathContent::getGroupMembershipTimestamp($class_id, $uid);
          if ($group && $user) {

            // If user is the member only in this class of training all classes.
            $unique_member = $classes_members ? !in_array($uid, $classes_members) : TRUE;

            // Get group and class membership timestamps.
            if ($unique_member && $created_group_membership && $created_class_membership && $created_group_membership >= $created_class_membership) {

              // Remove class parent group membership.
              $group
                ->removeMember($user);
            }
          }
        }
      } catch (\Exception $e) {
        \Drupal::logger('opigno_learning_path')
          ->error($e
          ->getMessage());
        \Drupal::messenger()
          ->addMessage($e
          ->getMessage(), 'error');
      }
    }
  }
  if ($entity
    ->bundle() == 'opigno_class-group_membership') {

    // Get class group id.
    $gid = $entity
      ->getGroup()
      ->id();
    if (!empty($gid) && is_numeric($gid)) {
      try {

        // Get class parent groups.
        $parent_groups = LearningPathContent::getClassGroups($gid);
        if ($parent_groups) {
          $user = $entity
            ->getEntity();
          foreach ($parent_groups as $parent) {
            $group = Group::load($parent->gid);
            if ($group && $user) {

              // Check if user is a member.
              if ($group
                ->getMember($user) !== FALSE) {

                // Get group and class membership timestamps.
                $created_group_membership = LearningPathContent::getGroupMembershipTimestamp($parent->gid, $user
                  ->id());
                $created_class_membership = $entity
                  ->getCreatedTime();
                if ($created_group_membership && $created_class_membership && $created_group_membership >= $created_class_membership) {

                  // Remove class parent group membership.
                  $group
                    ->removeMember($user);
                }
              }
            }
          }
        }
      } catch (\Exception $e) {
        \Drupal::logger('opigno_learning_path')
          ->error($e
          ->getMessage());
        \Drupal::messenger()
          ->addMessage($e
          ->getMessage(), 'error');
      }
    }
  }
  if ($entity
    ->bundle() == 'learning_path') {
    LPStatus::removeCertificateExpiration($entity
      ->id());
  }
}