ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBuddyList.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
11  protected $ownerId;
12 
14  protected $relations;
15 
17  protected $repository;
18 
20  protected static $instances = [];
21 
23  protected $relationsRead = false;
24 
26  protected $eventHandler;
27 
33  public static function getInstanceByUserId(int $usrId) : self
34  {
35  if (ilObjUser::_isAnonymous($usrId)) {
36  throw new ilBuddySystemException(sprintf(
37  "You cannot create an instance for the anonymous user (id: %s)",
38  $usrId
39  ));
40  }
41 
42  if (isset(self::$instances[$usrId])) {
43  return self::$instances[$usrId];
44  }
45 
46  self::$instances[$usrId] = new self($usrId);
47  return self::$instances[$usrId];
48  }
49 
54  public static function getInstanceByGlobalUser() : self
55  {
56  global $DIC;
57 
58  return self::getInstanceByUserId((int) $DIC->user()->getId());
59  }
60 
65  protected function __construct(int $ownerId)
66  {
67  global $DIC;
68 
69  $this->setOwnerId($ownerId);
71 
72  $this->eventHandler = $DIC['ilAppEventHandler'];
73  }
74 
78  public function reset() : void
79  {
80  $this->relationsRead = false;
81  $this->relations = null;
82  unset(self::$instances[$this->getOwnerId()]);
83  }
84 
89  {
90  return $this->repository;
91  }
92 
97  {
98  $this->repository = $repository;
99  }
100 
104  public function readFromRepository() : void
105  {
106  $this->setRelations(new ilBuddySystemRelationCollection((array) $this->getRepository()->getAll()));
107  }
108 
112  protected function performLazyLoading() : void
113  {
114  if (!$this->relationsRead) {
115  $this->readFromRepository();
116  $this->relationsRead = true;
117  }
118  }
119 
124  {
125  if (null === $this->relations) {
126  $this->performLazyLoading();
127  }
128 
129  return $this->relations;
130  }
131 
136  {
137  $this->relations = $relations;
138  }
139 
144  public function getOwnerId() : int
145  {
146  return $this->ownerId;
147  }
148 
154  {
155  return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
156  return $relation->isLinked();
157  });
158  }
159 
165  {
166  return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation) {
167  return $relation->getBuddyUsrId() === $this->getOwnerId();
168  });
169  }
170 
176  {
177  return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation) {
178  return $relation->getUsrId() === $this->getOwnerId();
179  });
180  }
181 
187  {
188  return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
189  return $relation->isRequested();
190  });
191  }
192 
198  {
199  return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation) {
200  return $relation->getBuddyUsrId() === $this->getOwnerId();
201  });
202  }
203 
209  {
210  return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation) {
211  return $relation->getUsrId() === $this->getOwnerId();
212  });
213  }
214 
220  {
221  return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
222  return $relation->isIgnored();
223  });
224  }
225 
230  public function getRelationUserIds() : array
231  {
232  return $this->getRelations()->getKeys();
233  }
234 
238  public function setOwnerId(int $ownerId) : void
239  {
240  $this->ownerId = $ownerId;
241  }
242 
247  protected function getRelationTargetUserId(ilBuddySystemRelation $relation) : int
248  {
249  return ($relation->getUsrId() === $this->getOwnerId() ? $relation->getBuddyUsrId() : $relation->getUsrId());
250  }
251 
256  public function getRelationByUserId(int $usrId) : ilBuddySystemRelation
257  {
258  if ($this->getRelations()->containsKey($usrId)) {
259  return $this->getRelations()->get($usrId);
260  }
261 
262  $relation = new ilBuddySystemRelation(ilBuddySystemRelationStateFactory::getInstance()->getInitialState());
263  $relation->setIsOwnedByActor(true);
264  $relation->setUsrId($this->getOwnerId());
265  $relation->setBuddyUsrId($usrId);
266  $relation->setTimestamp(time());
267 
268  $this->getRelations()->set($usrId, $relation);
269 
270  return $relation;
271  }
272 
278  public function link(ilBuddySystemRelation $relation) : self
279  {
280  try {
281  if ($relation->isLinked()) {
282  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
283  }
284 
285  if ($this->getOwnerId() == $relation->getUsrId()) {
286  throw new ilBuddySystemException("You can only accept a request when you are not the initiator");
287  }
288 
289  $relation->link();
290 
291  $this->getRepository()->save($relation);
292  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
294  throw $e;
295  }
296 
297  return $this;
298  }
299 
305  public function unlink(ilBuddySystemRelation $relation) : self
306  {
307  try {
308  $relation->unlink();
309  $this->getRepository()->save($relation);
310  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
311  } catch (ilBuddySystemException $e) {
312  if ($relation->isUnlinked()) {
313  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked');
314  }
315 
316  throw $e;
317  }
318 
319  return $this;
320  }
321 
327  public function request(ilBuddySystemRelation $relation) : self
328  {
329  if (ilObjUser::_isAnonymous($this->getRelationTargetUserId($relation))) {
330  throw new ilBuddySystemException(sprintf(
331  "You cannot add the anonymous user to the list (id: %s)",
332  $this->getRelationTargetUserId($relation)
333  ));
334  }
335 
336  if (!strlen((string) ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation)))) {
337  throw new ilBuddySystemException(sprintf(
338  "You cannot add a non existing user (id: %s)",
339  $this->getRelationTargetUserId($relation)
340  ));
341  }
342 
343  try {
344  $relation->request();
345  $this->getRepository()->save($relation);
346  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
347  } catch (ilBuddySystemException $e) {
348  if ($relation->isRequested()) {
349  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested');
350  }
351 
352  throw $e;
353  }
354 
355  $this->eventHandler->raise(
356  'Services/Contact',
357  'contactRequested',
358  [
359  'usr_id' => $this->getRelationTargetUserId($relation)
360  ]
361  );
362 
363  return $this;
364  }
365 
371  public function ignore(ilBuddySystemRelation $relation) : self
372  {
373  try {
374  if ($relation->isLinked()) {
375  throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
376  }
377 
378  if ($this->getOwnerId() == $relation->getUsrId()) {
379  throw new ilBuddySystemException("You can only ignore a request when you are not the initiator");
380  }
381 
382  $relation->ignore();
383 
384  $this->getRepository()->save($relation);
385  $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
386  } catch (ilBuddySystemException $e) {
387  if ($relation->isIgnored()) {
388  throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored');
389  }
390 
391  throw $e;
392  }
393 
394  return $this;
395  }
396 
401  public function destroy() : self
402  {
403  $this->getRepository()->destroy();
404  $this->getRelations()->clear();
405  return $this;
406  }
407 }
static _lookupLogin($a_user_id)
lookup login
getRequestRelationsByOwner()
Gets all requested relations the buddy list owner initiated.
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.
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.
Class ilBuddySystemRelationStateException.
Class ilBuddySystemRelationRepository.
unlink(ilBuddySystemRelation $relation)
static getInstanceByUserId(int $usrId)
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.
repository()
Definition: repository.php:5
getRelationTargetUserId(ilBuddySystemRelation $relation)
global $DIC
Definition: goto.php:24
getLinkedRelations()
Gets all linked/approved relations.
static _isAnonymous($usr_id)
Class ilBuddySystemRelation.
__construct(int $ownerId)
ilBuddyList constructor.
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.