ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBuddyList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
28 protected static array $instances = [];
29
30 protected int $ownerId;
32 protected bool $relationsRead = false;
35
39 public static function getInstanceByUserId(int $usrId): self
40 {
41 if (ilObjUser::_isAnonymous($usrId)) {
42 throw new ilBuddySystemException(sprintf(
43 'You cannot create an instance for the anonymous user (id: %s)',
44 $usrId
45 ));
46 }
47
48 if (isset(self::$instances[$usrId])) {
49 return self::$instances[$usrId];
50 }
51
52 self::$instances[$usrId] = new self($usrId);
53 return self::$instances[$usrId];
54 }
55
56 public static function getInstanceByGlobalUser(?ilObjUser $user = null): self
57 {
58 global $DIC;
59
60 if (null === $user) {
61 $user = $DIC->user();
62 }
63
64 return self::getInstanceByUserId($user->getId());
65 }
66
67 protected function __construct(int $ownerId, ?ilAppEventHandler $event_handler = null)
68 {
69 global $DIC;
70
71 $this->eventHandler = $event_handler ?? $DIC['ilAppEventHandler'];
72
73 $this->setOwnerId($ownerId);
75 }
76
80 public function reset(): void
81 {
82 $this->relationsRead = false;
83 $this->relations = null;
84 unset(self::$instances[$this->getOwnerId()]);
85 }
86
88 {
89 return $this->repository;
90 }
91
93 {
94 $this->repository = $repository;
95 }
96
97 public function readFromRepository(): void
98 {
99 $this->setRelations(new ilBuddySystemRelationCollection($this->getRepository()->getAll()));
100 }
101
102 protected function performLazyLoading(): void
103 {
104 if (!$this->relationsRead) {
105 $this->readFromRepository();
106 $this->relationsRead = true;
107 }
108 }
109
111 {
112 if (null === $this->relations) {
113 $this->performLazyLoading();
114 }
115
116 return $this->relations;
117 }
118
120 {
121 $this->relations = $relations;
122 }
123
127 public function getOwnerId(): int
128 {
129 return $this->ownerId;
130 }
131
136 {
137 return $this->getRelations()->filter(
138 static fn(ilBuddySystemRelation $relation): bool => $relation->isLinked()
139 );
140 }
141
146 {
147 return $this->getRequestedRelations()->filter(
148 fn(ilBuddySystemRelation $relation): bool => $relation->getBuddyUsrId() === $this->getOwnerId()
149 );
150 }
151
156 {
157 return $this->getRequestedRelations()->filter(
158 fn(ilBuddySystemRelation $relation): bool => $relation->getUsrId() === $this->getOwnerId()
159 );
160 }
161
166 {
167 return $this->getRelations()->filter(
168 static fn(ilBuddySystemRelation $relation): bool => $relation->isRequested()
169 );
170 }
171
176 {
177 return $this->getIgnoredRelations()->filter(
178 fn(ilBuddySystemRelation $relation): bool => $relation->getBuddyUsrId() === $this->getOwnerId()
179 );
180 }
181
186 {
187 return $this->getIgnoredRelations()->filter(
188 fn(ilBuddySystemRelation $relation): bool => $relation->getUsrId() === $this->getOwnerId()
189 );
190 }
191
196 {
197 return $this->getRelations()->filter(
198 static fn(ilBuddySystemRelation $relation): bool => $relation->isIgnored()
199 );
200 }
201
206 public function getRelationUserIds(): array
207 {
208 return $this->getRelations()->getKeys();
209 }
210
211 public function setOwnerId(int $ownerId): void
212 {
213 $this->ownerId = $ownerId;
214 }
215
217 {
218 return ($relation->getUsrId() === $this->getOwnerId() ? $relation->getBuddyUsrId() : $relation->getUsrId());
219 }
220
221 public function getRelationByUserId(int $usrId): ilBuddySystemRelation
222 {
223 if ($this->getRelations()->containsKey($usrId)) {
224 return $this->getRelations()->get($usrId);
225 }
226
229 $this->getOwnerId(),
230 $usrId,
231 true,
232 time()
233 );
234
235 $this->getRelations()->set($usrId, $relation);
236
237 return $relation;
238 }
239
243 public function link(ilBuddySystemRelation $relation): self
244 {
245 if ($relation->isLinked()) {
246 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
247 }
248
249 if ($this->getOwnerId() === $relation->getUsrId()) {
250 throw new ilBuddySystemException('You can only accept a request when you are not the initiator');
251 }
252
253 $relation->link();
254
255 $this->getRepository()->save($relation);
256 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
257
258 $this->eventHandler->raise(
259 'components/ILIAS/Contact',
260 'relationLinked',
261 [
262 'relation' => $relation
263 ]
264 );
265
266 return $this;
267 }
268
273 {
274 try {
275 $relation->unlink();
276 $this->getRepository()->save($relation);
277 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
278 } catch (ilBuddySystemException $e) {
279 if ($relation->isUnlinked()) {
280 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked', $e->getCode(), $e);
281 }
282
283 throw $e;
284 }
285
286 $this->eventHandler->raise(
287 'components/ILIAS/Contact',
288 'relationUnlinked',
289 [
290 'relation' => $relation
291 ]
292 );
293
294 return $this;
295 }
296
301 {
302 if (ilObjUser::_isAnonymous($this->getRelationTargetUserId($relation))) {
303 throw new ilBuddySystemException(sprintf(
304 'You cannot add the anonymous user to the list (id: %s)',
305 $this->getRelationTargetUserId($relation)
306 ));
307 }
308
309 $login = ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation));
310 if ($login === '') {
311 throw new ilBuddySystemException(sprintf(
312 'You cannot add a non existing user (id: %s)',
313 $this->getRelationTargetUserId($relation)
314 ));
315 }
316
317 try {
318 $relation->request();
319 $this->getRepository()->save($relation);
320 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
321 } catch (ilBuddySystemException $e) {
322 if ($relation->isRequested()) {
323 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested', $e->getCode(), $e);
324 }
325
326 throw $e;
327 }
328
329 $this->eventHandler->raise(
330 'components/ILIAS/Contact',
331 'contactRequested',
332 [
333 'usr_id' => $this->getRelationTargetUserId($relation),
334 'relation' => $relation
335 ]
336 );
337
338 return $this;
339 }
340
345 {
346 try {
347 if ($relation->isLinked()) {
348 throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
349 }
350
351 if ($this->getOwnerId() === $relation->getUsrId()) {
352 throw new ilBuddySystemException('You can only ignore a request when you are not the initiator');
353 }
354
355 $relation->ignore();
356
357 $this->getRepository()->save($relation);
358 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
359 } catch (ilBuddySystemException $e) {
360 if ($relation->isIgnored()) {
361 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored', $e->getCode(), $e);
362 }
363
364 throw $e;
365 }
366
367 $this->eventHandler->raise(
368 'components/ILIAS/Contact',
369 'contactIgnored',
370 [
371 'relation' => $relation
372 ]
373 );
374
375 return $this;
376 }
377
381 public function destroy(): self
382 {
383 $this->getRepository()->destroy();
384 $this->getRelations()->clear();
385 return $this;
386 }
387}
$relation
Global event handler.
Class ilBuddyList.
unlink(ilBuddySystemRelation $relation)
getIgnoredRelations()
Gets all ignored relations: ilBuddySystemRelationCollection.
link(ilBuddySystemRelation $relation)
setRelations(ilBuddySystemRelationCollection $relations)
static getInstanceByUserId(int $usrId)
setOwnerId(int $ownerId)
ignore(ilBuddySystemRelation $relation)
ilBuddySystemRelationRepository $repository
getOwnerId()
Returns the user id of the buddy list owner.
getIgnoredRelationsForOwner()
Gets all ignored relations the buddy list owner has to interact with.
ilBuddySystemRelationCollection $relations
getRelationTargetUserId(ilBuddySystemRelation $relation)
getLinkedRelations()
Gets all linked/approved relations.
static getInstanceByGlobalUser(?ilObjUser $user=null)
ilAppEventHandler $eventHandler
getRelationUserIds()
Returns an array of all user ids the buddy list owner has a relation with.
getIgnoredRelationsByOwner()
Gets all ignored relations the buddy list owner initiated.
getRequestRelationsForOwner()
Gets all requested relations the buddy list owner has to interact with.
setRepository(ilBuddySystemRelationRepository $repository)
static array $instances
request(ilBuddySystemRelation $relation)
reset()
Remove the singleton instance from static array, used for unit tests.
__construct(int $ownerId, ?ilAppEventHandler $event_handler=null)
getRequestedRelations()
Gets all requested relations.
destroy()
Removes all buddy system references of the user (persistently)
getRelationByUserId(int $usrId)
getRequestRelationsByOwner()
Gets all requested relations the buddy list owner initiated.
Class ilBuddySystemException.
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list.
Class ilBuddySystemRelationRepository.
Class ilBuddySystemRelation.
User class.
static _isAnonymous(int $usr_id)
static _lookupLogin(int $a_user_id)
global $DIC
Definition: shib_login.php:26