You are here

function opigno_messaging_get_all_users in Opigno messaging 8

Same name and namespace in other branches
  1. 3.x opigno_messaging.module \opigno_messaging_get_all_users()

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

File

./opigno_messaging.module, line 217
Contains opigno_messaging.module.

Code

function opigno_messaging_get_all_users() {
  $current_user = \Drupal::currentUser();
  if ($current_user
    ->hasPermission('message anyone regardless of groups')) {
    return array_filter(User::loadMultiple(), function ($user) {

      /** @var \Drupal\user\UserInterface $user */
      return $user
        ->id() > 0;
    });
  }
  $users = [];
  $groups = opigno_messaging_get_groups('opigno_class') + opigno_messaging_get_groups('learning_path');

  /** @var \Drupal\group\Entity\GroupInterface $group */
  foreach ($groups as $group_info) {
    $group = Group::load($group_info['entity_id']);
    $members = $group
      ->getMembers();
    foreach ($members as $member) {
      $user = $member
        ->getUser();
      if ($user) {
        $users[$user
          ->id()] = $member
          ->getUser();
      }
    }
  }
  unset($users[0]);
  return array_values($users);
}