ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBuddyList.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
28  protected static array $instances = [];
29 
30  protected int $ownerId;
32  protected bool $relationsRead = false;
35 
41  public static function getInstanceByUserId(int $usrId): self
42  {
43  if (ilObjUser::_isAnonymous($usrId)) {
44  throw new ilBuddySystemException(sprintf(
45  'You cannot create an instance for the anonymous user (id: %s)',
46  $usrId
47  ));
48  }
49 
50  if (isset(self::$instances[$usrId])) {
51  return self::$instances[$usrId];
52  }
53 
54  self::$instances[$usrId] = new self($usrId);
55  return self::$instances[$usrId];
56  }
57 
62  public static function getInstanceByGlobalUser(): self
63  {
64  global $DIC;
65 
66  return self::getInstanceByUserId($DIC->user()->getId());
67  }
68 
69  protected function __construct(int $ownerId)
70  {
71  global $DIC;
72 
73  $this->setOwnerId($ownerId);
75 
76  $this->eventHandler = $DIC['ilAppEventHandler'];
77  }
78 
82  public function reset(): void
83  {
84  $this->relationsRead = false;
85  $this->relations = null;
86  unset(self::$instances[$this->getOwnerId()]);
87  }
88 
90  {
91  return $this->repository;
92  }
93 
94  public function setRepository(ilBuddySystemRelationRepository $repository): void
95  {
96  $this->repository = $repository;
97  }
98 
99  public function readFromRepository(): void
100  {
101  $this->setRelations(new ilBuddySystemRelationCollection($this->getRepository()->getAll()));
102  }
103 
104  protected function performLazyLoading(): void
105  {
106  if (!$this->relationsRead) {
107  $this->readFromRepository();
108  $this->relationsRead = true;
109  }
110  }
111 
113  {
114  if (null === $this->relations) {
115  $this->performLazyLoading();
116  }
117 
118  return $this->relations;
119  }
120 
121  public function setRelations(ilBuddySystemRelationCollection $relations): void
122  {
123  $this->relations = $relations;
124  }
125 
130  public function getOwnerId(): int
131  {
132  return $this->ownerId;
133  }
134 
140  {
141  return $this->getRelations()->filter(static function (ilBuddySystemRelation $relation): bool {
142  return $relation->isLinked();
143  });
144  }
145 
151  {
152  return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation): bool {
153  return $relation->getBuddyUsrId() === $this->getOwnerId();
154  });
155  }
156 
162  {
163  return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation): bool {
164  return $relation->getUsrId() === $this->getOwnerId();
165  });
166  }
167 
173  {
174  return $this->getRelations()->filter(static function (ilBuddySystemRelation $relation): bool {
175  return $relation->isRequested();
176  });
177  }
178 
184  {
185  return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation): bool {
186  return $relation->getBuddyUsrId() === $this->getOwnerId();
187  });
188  }
189 
195  {
196  return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation): bool {
197  return $relation->getUsrId() === $this->getOwnerId();
198  });
199  }
200 
206  {
207  return $this->getRelations()->filter(static function (ilBuddySystemRelation $relation): bool {
208  return $relation->isIgnored();
209  });
210  }
211 
216  public function getRelationUserIds(): array
217  {
218  return $this->getRelations()->getKeys();
219  }
220 
221  public function setOwnerId(int $ownerId): void
222  {
223  $this->ownerId = $ownerId;
224  }
225 
226  protected function getRelationTargetUserId(ilBuddySystemRelation $relation): int
227  {
228  return ($relation->getUsrId() === $this->getOwnerId() ? $relation->getBuddyUsrId() : $relation->getUsrId());
229  }
230 
231  public function getRelationByUserId(int $usrId): ilBuddySystemRelation
232  {
233  if ($this->getRelations()->containsKey($usrId)) {
234  return $this->getRelations()->get($usrId);
235  }
236 
237  $relation = new ilBuddySystemRelation(
239  $this->getOwnerId(),
240  $usrId,
241  true,
242  time()
243  );
244 
245  $this->getRelations()->set($usrId, $relation);
246 
247  return $relation;
248  }
249 
255  public function link(ilBuddySystemRelation $relation): self
256  {
257  if ($relation->isLinked()) {
258  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
259  }
260 
261  if ($this->getOwnerId() === $relation->getUsrId()) {
262  throw new ilBuddySystemException('You can only accept a request when you are not the initiator');
263  }
264 
265  $relation->link();
266 
267  $this->getRepository()->save($relation);
268  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
269 
270  $this->eventHandler->raise(
271  'Services/Contact',
272  'relationLinked',
273  [
274  'relation' => $relation
275  ]
276  );
277 
278  return $this;
279  }
280 
286  public function unlink(ilBuddySystemRelation $relation): self
287  {
288  try {
289  $relation->unlink();
290  $this->getRepository()->save($relation);
291  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
292  } catch (ilBuddySystemException $e) {
293  if ($relation->isUnlinked()) {
294  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked');
295  }
296 
297  throw $e;
298  }
299 
300  $this->eventHandler->raise(
301  'Services/Contact',
302  'relationUnlinked',
303  [
304  'relation' => $relation
305  ]
306  );
307 
308  return $this;
309  }
310 
316  public function request(ilBuddySystemRelation $relation): self
317  {
318  if (ilObjUser::_isAnonymous($this->getRelationTargetUserId($relation))) {
319  throw new ilBuddySystemException(sprintf(
320  'You cannot add the anonymous user to the list (id: %s)',
321  $this->getRelationTargetUserId($relation)
322  ));
323  }
324 
325  $login = ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation));
326  if ($login === '') {
327  throw new ilBuddySystemException(sprintf(
328  'You cannot add a non existing user (id: %s)',
329  $this->getRelationTargetUserId($relation)
330  ));
331  }
332 
333  try {
334  $relation->request();
335  $this->getRepository()->save($relation);
336  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
337  } catch (ilBuddySystemException $e) {
338  if ($relation->isRequested()) {
339  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested');
340  }
341 
342  throw $e;
343  }
344 
345  $this->eventHandler->raise(
346  'Services/Contact',
347  'contactRequested',
348  [
349  'usr_id' => $this->getRelationTargetUserId($relation),
350  'relation' => $relation
351  ]
352  );
353 
354  return $this;
355  }
356 
362  public function ignore(ilBuddySystemRelation $relation): self
363  {
364  try {
365  if ($relation->isLinked()) {
366  throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
367  }
368 
369  if ($this->getOwnerId() === $relation->getUsrId()) {
370  throw new ilBuddySystemException('You can only ignore a request when you are not the initiator');
371  }
372 
373  $relation->ignore();
374 
375  $this->getRepository()->save($relation);
376  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
377  } catch (ilBuddySystemException $e) {
378  if ($relation->isIgnored()) {
379  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored');
380  }
381 
382  throw $e;
383  }
384 
385  $this->eventHandler->raise(
386  'Services/Contact',
387  'contactIgnored',
388  [
389  'relation' => $relation
390  ]
391  );
392 
393  return $this;
394  }
395 
400  public function destroy(): self
401  {
402  $this->getRepository()->destroy();
403  $this->getRelations()->clear();
404  return $this;
405  }
406 }
Global event handler.
getRequestRelationsByOwner()
Gets all requested relations the buddy list owner initiated.
ilBuddySystemRelationCollection $relations
static getInstanceByGlobalUser()
setRepository(ilBuddySystemRelationRepository $repository)
getIgnoredRelationsByOwner()
Gets all ignored relations the buddy list owner initiated.
request(ilBuddySystemRelation $relation)
getIgnoredRelationsForOwner()
Gets all ignored relations the buddy list owner has to interact with.
static array $instances
ignore(ilBuddySystemRelation $relation)
Class ilBuddySystemException.
getIgnoredRelations()
Gets all ignored relations: ilBuddySystemRelationCollection.
getRelationUserIds()
Returns an array of all user ids the buddy list owner has a relation with.
ilBuddySystemRelationRepository $repository
Class ilBuddySystemRelationRepository.
unlink(ilBuddySystemRelation $relation)
static getInstanceByUserId(int $usrId)
global $DIC
Definition: feed.php:28
reset()
Remove the singleton instance from static array, used for unit tests.
getRequestedRelations()
Gets all requested relations.
link(ilBuddySystemRelation $relation)
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
getRequestRelationsForOwner()
Gets all requested relations the buddy list owner has to interact with.
getRelationTargetUserId(ilBuddySystemRelation $relation)
getLinkedRelations()
Gets all linked/approved relations.
static _isAnonymous(int $usr_id)
ilAppEventHandler $eventHandler
__construct(int $ownerId)
setOwnerId(int $ownerId)
setRelations(ilBuddySystemRelationCollection $relations)
destroy()
Removes all buddy system references of the user (persistently)
getRelationByUserId(int $usrId)
getOwnerId()
Returns the user id of the buddy list owner.
static _lookupLogin(int $a_user_id)