ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.Collector.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Awareness\User;
20 
21 use ILIAS\Awareness;
25 
30 class Collector
31 {
32  protected static ?array $online_users = null;
34  protected static array $online_user_ids = array();
35  protected \ilRbacReview $rbacreview;
40  protected array $collections;
41  protected int $user_id;
42  protected int $ref_id;
43  protected \ilSetting $settings;
45 
46  public function __construct(
47  int $user_id,
48  int $ref_id,
49  InternalDataService $data_service,
50  InternalRepoService $repo_service,
51  InternalDomainService $domain_service
52  ) {
53  $this->user_id = $user_id;
54  $this->settings = $domain_service->settings();
55  $this->provider_factory = $domain_service
56  ->userProvider();
57  $this->data_service = $data_service;
58  $this->admin_manager = $domain_service
59  ->admin($ref_id);
60  $this->rbacreview = $domain_service->rbac()->review();
61  $this->repo_service = $repo_service;
62  }
63 
64  public static function getOnlineUsers(): array
65  {
66  if (self::$online_users === null) {
67  self::$online_user_ids = array();
68  self::$online_users = array();
69  foreach (\ilObjUser::_getUsersOnline() as $u) {
70  // ask context $u["context"] if it supports pushMessages
71  if ($u["context"] &&
72  \ilContext::directCall($u["context"], "supportsPushMessages")) {
73  self::$online_users[$u["user_id"]] = $u;
74  self::$online_user_ids[] = $u["user_id"];
75  }
76  }
77  }
78  return self::$online_users;
79  }
80 
81 
85  public function collectUsers(bool $a_online_only = false): array
86  {
87  $rbacreview = $this->rbacreview;
88 
89  $this->collections = array();
90 
91  $awrn_logger = \ilLoggerFactory::getLogger('awrn');
92 
93  $awrn_logger->debug("Start, Online Only: " . $a_online_only . ", Current User: " . $this->user_id);
94 
95  self::getOnlineUsers();
96  $all_users = array();
97  foreach ($this->provider_factory->getAllProviders() as $prov) {
98  $awrn_logger->debug("Provider: " . $prov->getProviderId() . ", Activation Mode: " . $this->admin_manager->getActivationMode($prov->getProviderId()) . ", Current User: " . $this->user_id);
99 
100  // overall collection of users
101  $collection = $this->data_service->userCollection();
102 
103  $provider_active = $this->admin_manager->isProviderActivated($prov->getProviderId());
104  $provider_includes_offline = $this->admin_manager->includesProviderOfflineUsers($prov->getProviderId());
105 
106  if ($provider_active) {
107  $online_users = null;
108  if (!$provider_includes_offline || $a_online_only) {
109  $awrn_logger->debug("Provider: " . $prov->getProviderId() . ", Online Filter Users: " . count(self::$online_user_ids) . ", Current User: " . $this->user_id);
110  $online_users = self::$online_user_ids;
111  }
112 
113  $coll = $this->collectUsersFromProvider($prov, $online_users);
114  $awrn_logger->debug("Provider: " . $prov->getProviderId() . ", Collected Users: " . count($coll) . ", Current User: " . $this->user_id);
115 
116  foreach ($coll->getUsers() as $user_id) {
117  // filter out the anonymous user
118  if ($user_id == ANONYMOUS_USER_ID) {
119  continue;
120  }
121  // filter out current user
122  if ($user_id == $this->user_id) {
123  continue;
124  }
125 
126  $awrn_logger->debug("Current User: " . $this->user_id . ", " .
127  "Provider: " . $prov->getProviderId() . ", Collected User: " . $user_id);
128 
129  // cross check online, filter out offline users (if necessary)
130  if ((!$a_online_only && $provider_includes_offline)
131  || in_array($user_id, self::$online_user_ids)) {
132  $collection->addUser($user_id);
133  if (!in_array($user_id, $all_users)) {
134  $all_users[] = $user_id;
135  }
136  }
137  }
138  }
139  $this->collections[] = array(
140  "uc_title" => $prov->getTitle(),
141  "highlighted" => $prov->isHighlighted(),
142  "collection" => $collection
143  );
144  }
145 
146  $remove_users = array();
147 
148  if ($this->settings->get("hide_own_online_status") === "n") {
149  // remove all users with hide_own_online_status "y"
150  foreach (\ilObjUser::getUserSubsetByPreferenceValue($all_users, "hide_own_online_status", "y") as $u) {
151  $remove_users[] = $u;
152  }
153  } else {
154  // remove all, except user with hide_own_online_status "n"
155  $show_users = \ilObjUser::getUserSubsetByPreferenceValue($all_users, "hide_own_online_status", "n");
156  $remove_users = array_filter($all_users, function ($i) use ($show_users) {
157  return !in_array($i, $show_users);
158  });
159  }
160 
161  // remove all users that have not accepted the terms of service yet
163  foreach (\ilObjUser::getUsersAgreed(false, $all_users) as $u) {
164  if ($u != SYSTEM_USER_ID && !$rbacreview->isAssigned($u, SYSTEM_ROLE_ID)) {
165  //if ($u != SYSTEM_USER_ID)
166  $remove_users[] = $u;
167  }
168  }
169  }
170 
171  $this->removeUsersFromCollections($remove_users);
172 
173  return $this->collections;
174  }
175 
176  public function collectUsersFromProvider(Provider $prov, ?array $online_users): Collection
177  {
178  $coll = $this->data_service->userCollection();
179  foreach ($prov->getInitialUserSet($online_users) as $user_id) {
180  if ((is_null($online_users) || in_array($user_id, $online_users))) {
181  $coll->addUser($user_id);
182  }
183  }
184  return $coll;
185  }
186 
191  protected function removeUsersFromCollections(array $a_remove_users): void
192  {
193  foreach ($this->collections as $c) {
194  reset($a_remove_users);
195  foreach ($a_remove_users as $u) {
196  $c["collection"]->removeUser($u);
197  }
198  }
199  }
200 }
Represents a set of collected users.
Collects users from all providers.
$c
Definition: cli.php:38
const ANONYMOUS_USER_ID
Definition: constants.php:27
static getLogger(string $a_component_id)
Get component logger.
collectUsers(bool $a_online_only=false)
Collect users.
InternalRepoService $repo_service
const SYSTEM_ROLE_ID
Definition: constants.php:29
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getUserSubsetByPreferenceValue(array $a_user_ids, string $a_keyword, string $a_val)
For a given set of user IDs return a subset that has a given user preference set. ...
removeUsersFromCollections(array $a_remove_users)
Remove users from collection.
Awareness AdminManager $admin_manager
collectUsersFromProvider(Provider $prov, ?array $online_users)
static getUsersAgreed(bool $a_agreed=true, ?array $a_users=null)
Get users that have or have not agreed to the user agreement.
__construct(int $user_id, int $ref_id, InternalDataService $data_service, InternalRepoService $repo_service, InternalDomainService $domain_service)
static _getUsersOnline(int $a_user_id=0, bool $a_no_anonymous=false)
reads all active sessions from db and returns users that are online OR returns only one active user i...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInitialUserSet(?array $user_ids=null)
Get initial set of users.
static directCall(string $a_type, string $a_method)
Call context method directly without internal handling.
Awareness InternalDataService $data_service
Administrate awareness tool.
$i
Definition: metadata.php:41