ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.WidgetManager.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\Awareness;
20
26use ilArrayUtil;
27
34{
36 protected \ilSetting $settings;
38 protected \ilLanguage $lng;
39
40 protected int $user_id;
41 protected int $ref_id = 0;
43 protected \ilUserActionCollector $action_collector;
44 protected array $user_collections;
45 protected ?array $data = null;
46 protected ?array $online_user_data = null;
47 protected static array $instances = array();
48
49 public function __construct(
50 int $a_user_id,
51 int $ref_id,
53 InternalRepoService $repo_service,
54 InternalDomainService $domain_service,
55 \ilUserActionProviderFactory $user_action_provider_factory,
56 \ilUserActionAdmin $user_action_admin
57 ) {
58 $this->lng = $domain_service->lng();
59 $this->user_id = $a_user_id;
60 $this->ref_id = $ref_id;
61 $this->session_repo = $repo_service->awarenessSession();
62 $this->settings = $domain_service->awarenessSettings();
63 $this->data_service = $data_service;
64 $this->user_collector = $domain_service->userCollector($a_user_id, $ref_id);
65 $this->action_collector = new \ilUserActionCollector(
66 $a_user_id,
68 $user_action_provider_factory,
69 $user_action_admin
70 );
71 }
72
73 public function isWidgetVisible(): bool
74 {
75 $awrn_set = $this->settings;
76 if (!$awrn_set->get("awrn_enabled", "0") ||
77 ANONYMOUS_USER_ID == $this->user_id ||
78 $this->user_id == 0) {
79 return false;
80 }
81 return true;
82 }
83
84 public function processMetaBar(): Counter
85 {
86 $cache_period = (int) $this->settings->get("caching_period");
87 $last_update = $this->session_repo->getLastUpdate();
88 $now = time();
89
90 if ($last_update == "" || ($now - $last_update) >= $cache_period) {
91 $counter = $this->getUserCounter();
92 $hcnt = $counter->getHighlightCount();
93 $cnt = $counter->getCount();
94 $this->session_repo->setLastUpdate($now);
95 $this->session_repo->setCount($cnt);
96 $this->session_repo->setHighlightCount($hcnt);
97 } else {
98 $cnt = $this->session_repo->getCount();
99 $hcnt = $this->session_repo->getHighlightCount();
100 }
101 return $this->data_service->counter($cnt, $hcnt);
102 }
103
109 public function getUserCollections(bool $a_online_only = false): array
110 {
111 if (!isset($this->user_collections[(int) $a_online_only])) {
112 $this->user_collections[(int) $a_online_only] = $this->user_collector->collectUsers($a_online_only);
113 }
114 return $this->user_collections[(int) $a_online_only];
115 }
116
117 public function getUserCounter(): Counter
118 {
119 $all_user_ids = array();
120 $hall_user_ids = array();
121
123
124 foreach ($user_collections as $uc) {
125 $user_collection = $uc["collection"];
126 $user_ids = $user_collection->getUsers();
127
128 foreach ($user_ids as $uid) {
129 if (!in_array($uid, $all_user_ids)) {
130 if ($uc["highlighted"]) {
131 $hall_user_ids[] = $uid;
132 } else {
133 $all_user_ids[] = $uid;
134 }
135 }
136 }
137 }
138
139 return $this->data_service->counter(
140 count($all_user_ids),
141 count($hall_user_ids)
142 );
143 }
144
151 public function getListData(string $filter = ""): array
152 {
153 if ($this->user_id == ANONYMOUS_USER_ID) {
154 return [
155 "data" => [],
156 "cnt" => "0:0"
157 ];
158 }
159 $awrn_set = $this->settings;
160 $max = $awrn_set->get("max_nr_entries");
161
162 $all_user_ids = array();
163 $hall_user_ids = array();
164
165 if ($this->data == null) {
166 $online_users = $this->user_collector->getOnlineUsers();
167
169
170 $this->data = array();
171
172 foreach ($user_collections as $uc) {
173 // limit part 1
174 if (count($this->data) >= $max) {
175 continue;
176 }
177
178 $user_collection = $uc["collection"];
179 $user_ids = $user_collection->getUsers();
180
181 foreach ($user_ids as $uid) {
182 if (!in_array($uid, $all_user_ids)) {
183 if ($uc["highlighted"]) {
184 $hall_user_ids[] = $uid;
185 } else {
186 $all_user_ids[] = $uid;
187 }
188 }
189 }
190
192 $user_ids,
193 true,
194 false,
195 "",
196 false,
197 false,
198 true,
199 true
200 );
201
202 // sort and add online information
203 foreach ($names as $k => $n) {
204 if (isset($online_users[$n["id"]])) {
205 $names[$k]["online"] = true;
206 $names[$k]["last_login"] = $online_users[$n["id"]]["last_login"];
207 $sort_str = "1";
208 } else {
209 $names[$k]["online"] = false;
210 $names[$k]["last_login"] = "";
211 $sort_str = "2";
212 }
213 if ($n["public_profile"]) {
214 $sort_str .= $n["lastname"] . " " . $n["firstname"];
215 } else {
216 $sort_str .= $n["login"];
217 }
218 $names[$k]["sort_str"] = $sort_str;
219 }
220
221 $names = ilArrayUtil::sortArray($names, "sort_str", "asc", false, true);
222
223 foreach ($names as $n) {
224 // limit part 2
225 if (count($this->data) >= $max) {
226 continue;
227 }
228
229 // filter
230 $filter = trim($filter);
231 if ($filter != "" &&
232 !is_int(stripos($n["login"], $filter)) &&
233 (
234 !$n["public_profile"] || (
235 !is_int(stripos($n["firstname"], $filter)) &&
236 !is_int(stripos($n["lastname"], $filter))
237 )
238 )
239 ) {
240 continue;
241 }
242
243 $obj = new \stdClass();
244 $obj->lastname = $n["lastname"];
245 $obj->firstname = $n["firstname"];
246 $obj->login = $n["login"];
247 $obj->id = $n["id"];
248 $obj->collector = $uc["uc_title"];
249 $obj->highlighted = $uc["highlighted"];
250
251 //$obj->img = $n["img"];
252 $obj->img = \ilObjUser::_getPersonalPicturePath($n["id"], "xsmall");
253 $obj->public_profile = $n["public_profile"];
254
255 $obj->online = $n["online"];
256 $obj->last_login = $n["last_login"];
257
258 // get actions
259 $action_collection = $this->action_collector->getActionsForTargetUser($n["id"]);
260 $obj->actions = array();
261 foreach ($action_collection->getActions() as $action) {
262 $f = new \stdClass();
263 $f->text = $action->getText();
264 $f->href = $action->getHref();
265 $f->data = $action->getData();
266 $obj->actions[] = $f;
267 }
268
269 $this->data[] = $obj;
270 }
271 }
272 }
273
274 // update counter
275 $this->updateCounter(
276 count($all_user_ids),
277 count($hall_user_ids)
278 );
279
280 return array("data" => $this->data, "cnt" => count($all_user_ids) . ":" . count($hall_user_ids));
281 }
282
283 protected function updateCounter(
284 int $cnt,
285 int $hcnt
286 ): void {
287 // update counter
288 $now = time();
289 $this->session_repo->setLastUpdate($now);
290 $this->session_repo->setCount($cnt);
291 $this->session_repo->setHighlightCount($hcnt);
292 }
293}
Counter DTO class.
Collects users from all providers.
High level business class, interface to front ends.
AwarenessSessionRepository $session_repo
__construct(int $a_user_id, int $ref_id, InternalDataService $data_service, InternalRepoService $repo_service, InternalDomainService $domain_service, \ilUserActionProviderFactory $user_action_provider_factory, \ilUserActionAdmin $user_action_admin)
getListData(string $filter="")
Get data.
getUserCollections(bool $a_online_only=false)
Get user collections.
ilUserActionCollector $action_collector
description of a localized parameter this information is used locate translations while processing no...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getPersonalPicturePath(int $a_usr_id, string $a_size='small', bool $a_force_pic=false)
get(string $a_keyword, ?string $a_default_value=null)
get setting
User action administration.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
const ANONYMOUS_USER_ID
Definition: constants.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$counter