ILIAS  release_8 Revision v8.24
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 ) {
56 $this->lng = $domain_service->lng();
57 $this->user_id = $a_user_id;
58 $this->ref_id = $ref_id;
59 $this->session_repo = $repo_service->awarenessSession();
60 $this->settings = $domain_service->awarenessSettings();
61 $this->data_service = $data_service;
62 $this->user_collector = $domain_service->userCollector($a_user_id, $ref_id);
63 $this->action_collector = \ilUserActionCollector::getInstance($a_user_id, new \ilAwarenessUserActionContext());
64 }
65
66 public function isWidgetVisible(): bool
67 {
68 $awrn_set = $this->settings;
69 if (!$awrn_set->get("awrn_enabled", "0") ||
70 ANONYMOUS_USER_ID == $this->user_id ||
71 $this->user_id == 0) {
72 return false;
73 }
74 return true;
75 }
76
77 public function processMetaBar(): Counter
78 {
79 $cache_period = (int) $this->settings->get("caching_period");
80 $last_update = $this->session_repo->getLastUpdate();
81 $now = time();
82
83 if ($last_update == "" || ($now - $last_update) >= $cache_period) {
84 $counter = $this->getUserCounter();
85 $hcnt = $counter->getHighlightCount();
86 $cnt = $counter->getCount();
87 $this->session_repo->setLastUpdate($now);
88 $this->session_repo->setCount($cnt);
89 $this->session_repo->setHighlightCount($hcnt);
90 } else {
91 $cnt = $this->session_repo->getCount();
92 $hcnt = $this->session_repo->getHighlightCount();
93 }
94 return $this->data_service->counter($cnt, $hcnt);
95 }
96
102 public function getUserCollections(bool $a_online_only = false): array
103 {
104 if (!isset($this->user_collections[(int) $a_online_only])) {
105 $this->user_collections[(int) $a_online_only] = $this->user_collector->collectUsers($a_online_only);
106 }
107 return $this->user_collections[(int) $a_online_only];
108 }
109
110 public function getUserCounter(): Counter
111 {
112 $all_user_ids = array();
113 $hall_user_ids = array();
114
116
117 foreach ($user_collections as $uc) {
118 $user_collection = $uc["collection"];
119 $user_ids = $user_collection->getUsers();
120
121 foreach ($user_ids as $uid) {
122 if (!in_array($uid, $all_user_ids)) {
123 if ($uc["highlighted"]) {
124 $hall_user_ids[] = $uid;
125 } else {
126 $all_user_ids[] = $uid;
127 }
128 }
129 }
130 }
131
132 return $this->data_service->counter(
133 count($all_user_ids),
134 count($hall_user_ids)
135 );
136 }
137
144 public function getListData(string $filter = ""): array
145 {
146 if ($this->user_id == ANONYMOUS_USER_ID) {
147 return [
148 "data" => [],
149 "cnt" => "0:0"
150 ];
151 }
152 $awrn_set = $this->settings;
153 $max = $awrn_set->get("max_nr_entries");
154
155 $all_user_ids = array();
156 $hall_user_ids = array();
157
158 if ($this->data == null) {
159 $online_users = $this->user_collector->getOnlineUsers();
160
162
163 $this->data = array();
164
165 foreach ($user_collections as $uc) {
166
167 // limit part 1
168 if (count($this->data) >= $max) {
169 continue;
170 }
171
172 $user_collection = $uc["collection"];
173 $user_ids = $user_collection->getUsers();
174
175 foreach ($user_ids as $uid) {
176 if (!in_array($uid, $all_user_ids)) {
177 if ($uc["highlighted"]) {
178 $hall_user_ids[] = $uid;
179 } else {
180 $all_user_ids[] = $uid;
181 }
182 }
183 }
184
186 $user_ids,
187 true,
188 false,
189 "",
190 false,
191 false,
192 true,
193 true
194 );
195
196 // sort and add online information
197 foreach ($names as $k => $n) {
198 if (isset($online_users[$n["id"]])) {
199 $names[$k]["online"] = true;
200 $names[$k]["last_login"] = $online_users[$n["id"]]["last_login"];
201 $sort_str = "1";
202 } else {
203 $names[$k]["online"] = false;
204 $names[$k]["last_login"] = "";
205 $sort_str = "2";
206 }
207 if ($n["public_profile"]) {
208 $sort_str .= $n["lastname"] . " " . $n["firstname"];
209 } else {
210 $sort_str .= $n["login"];
211 }
212 $names[$k]["sort_str"] = $sort_str;
213 }
214
215 $names = ilArrayUtil::sortArray($names, "sort_str", "asc", false, true);
216
217 foreach ($names as $n) {
218 // limit part 2
219 if (count($this->data) >= $max) {
220 continue;
221 }
222
223 // filter
224 $filter = trim($filter);
225 if ($filter != "" &&
226 !is_int(stripos($n["login"], $filter)) &&
227 (
228 !$n["public_profile"] || (
229 !is_int(stripos($n["firstname"], $filter)) &&
230 !is_int(stripos($n["lastname"], $filter))
231 )
232 )
233 ) {
234 continue;
235 }
236
237 $obj = new \stdClass();
238 $obj->lastname = $n["lastname"];
239 $obj->firstname = $n["firstname"];
240 $obj->login = $n["login"];
241 $obj->id = $n["id"];
242 $obj->collector = $uc["uc_title"];
243 $obj->highlighted = $uc["highlighted"];
244
245 //$obj->img = $n["img"];
246 $obj->img = \ilObjUser::_getPersonalPicturePath($n["id"], "xsmall");
247 $obj->public_profile = $n["public_profile"];
248
249 $obj->online = $n["online"];
250 $obj->last_login = $n["last_login"];
251
252 // get actions
253 $action_collection = $this->action_collector->getActionsForTargetUser($n["id"]);
254 $obj->actions = array();
255 foreach ($action_collection->getActions() as $action) {
256 $f = new \stdClass();
257 $f->text = $action->getText();
258 $f->href = $action->getHref();
259 $f->data = $action->getData();
260 $obj->actions[] = $f;
261 }
262
263 $this->data[] = $obj;
264 }
265 }
266 }
267
268 // update counter
269 $this->updateCounter(
270 count($all_user_ids),
271 count($hall_user_ids)
272 );
273
274 return array("data" => $this->data, "cnt" => count($all_user_ids) . ":" . count($hall_user_ids));
275 }
276
277 protected function updateCounter(
278 int $cnt,
279 int $hcnt
280 ): void {
281 // update counter
282 $now = time();
283 $this->session_repo->setLastUpdate($now);
284 $this->session_repo->setCount($cnt);
285 $this->session_repo->setHighlightCount($hcnt);
286 }
287}
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)
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, bool $a_prevent_no_photo_image=false, bool $html_export=false)
get(string $a_keyword, ?string $a_default_value=null)
get setting
static getInstance(int $a_user_id, ilUserActionContext $a_context)
Get instance (for a user)
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="ilpublicuserprofilegui")
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...