You are here

function opigno_messaging_get_all_recipients in Opigno messaging 3.x

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

Helper function to get list of all users that current user can message to.

1 call to opigno_messaging_get_all_recipients()
opigno_messaging_get_user_for_group in ./opigno_messaging.module
Helper function to get users for group.

File

./opigno_messaging.module, line 213
Contains opigno_messaging.module.

Code

function opigno_messaging_get_all_recipients($show_all) {
  $current_user = \Drupal::currentUser();
  if ($current_user
    ->isAnonymous()) {
    return [];
  }
  $social_config = \Drupal::config('opigno_class.socialsettings')
    ->get('social');
  if ($social_config == SocialSettingsForm::ALL_USERS || $show_all) {
    return array_filter(User::loadMultiple(), function ($user) {

      /** @var \Drupal\user\UserInterface $user */
      return $user
        ->id() > 0 && $user
        ->isActive();
    });
  }
  else {
    $users = [];

    // Get all users with roles 'user manager' or 'content manager' or 'administrator'.
    $global_managers_roles = [
      'user_manager',
      'content_manager',
      'administrator',
    ];
    $global_managers_ids = \Drupal::entityQuery('user')
      ->condition('status', 1)
      ->condition('roles', $global_managers_roles, 'IN')
      ->execute();
    $users = array_merge($users, $global_managers_ids);

    // Get users with group roles 'student manager' or 'content manager' in which the current user is a member.

    /* @var \Drupal\group\GroupMembershipLoaderInterface $membership_service */
    $membership_service = \Drupal::service('group.membership_loader');

    /* @var \Drupal\group\GroupMembership[] $memberships */
    $memberships = $membership_service
      ->loadByUser($current_user);
    foreach ($memberships as $membership) {
      $group = $membership
        ->getGroup();
      $members = $group
        ->getMembers();
      $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()) {
          $users[] = $member
            ->getUser()
            ->id();
        }
      }
    }

    // Get members from groups and classes where current user is a 'class manager' in a class,
    // 'student manager' or 'content manager' in a group.
    $memberships = $membership_service
      ->loadByUser($current_user, [
      'learning_path-user_manager',
      'opigno_class-class_manager',
    ]);
    foreach ($memberships as $membership) {
      $group = $membership
        ->getGroup();
      $members = $group
        ->getMembers();
      foreach ($members as $member) {
        $users[] = $member
          ->getUser()
          ->id();
      }
    }
  }
  $users = User::loadMultiple($users);
  return array_values($users);
}