You are here

function opigno_messaging_get_user_for_group in Opigno messaging 3.x

Same name and namespace in other branches
  1. 8 opigno_messaging.module \opigno_messaging_get_user_for_group()

Helper function to get users for group.

Parameters

int $group_id: Group.

Return value

\Drupal\user\UserInterface[] User ID.

File

./opigno_messaging.module, line 82
Contains opigno_messaging.module.

Code

function opigno_messaging_get_user_for_group($group_id = NULL, $show_all = FALSE) {
  if ($group_id === NULL || $group_id === 0) {
    return opigno_messaging_get_all_recipients($show_all);
  }
  $users = [];
  $group = Group::load($group_id);
  $members = $group
    ->getMembers();
  $current_user = \Drupal::currentUser();
  if ($show_all) {
    if ($members) {
      $users = array_map(function ($member) {

        /** @var \Drupal\group\GroupMembership $member */
        return $member
          ->getUser();
      }, $members);
      $users = array_filter($users, function ($user) {

        /** @var \Drupal\user\UserInterface $user */
        return $user
          ->id() > 0 && $user
          ->isActive();
      });
    }
    return $users;
  }
  $member = $group
    ->getMember($current_user);
  $member_roles = $member
    ->getRoles();
  if (opigno_messaging_user_is_manager($member_roles)) {
    $users = array_map(function ($member) {

      /** @var \Drupal\group\GroupMembership $member */
      return $member
        ->getUser();
    }, $members);
    $users = array_filter($users, function ($user) {

      /** @var \Drupal\user\UserInterface $user */
      return $user
        ->id() > 0 && $user
        ->isActive();
    });
  }
  else {
    $owner = $group
      ->getOwner();
    foreach ($members as $member) {
      $member_roles = $member
        ->getRoles();
      if (opigno_messaging_social_sharing($group) || opigno_messaging_user_is_manager($member_roles) || $member
        ->getUser()
        ->id() == $owner
        ->id()) {
        if ($member
          ->getUser()
          ->isActive()) {
          $users[] = $member
            ->getUser();
        }
      }
    }
  }
  return $users;
}