ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilBuddyList.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Contact/BuddySystem/classes/class.ilBuddySystemRelationRepository.php';
5require_once 'Services/Contact/BuddySystem/classes/class.ilBuddySystemRelationCollection.php';
6require_once 'Services/Contact/BuddySystem/classes/class.ilBuddySystemRelation.php';
7require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemException.php';
8require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
9require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemLinkedRelationState.php';
10require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemUnlinkedRelationState.php';
11require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRequestedRelationState.php';
12require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemIgnoredRequestRelationState.php';
13
19{
23 protected $owner_id;
24
28 protected $relations;
29
33 protected $repository;
34
38 protected static $instances = array();
39
43 protected $relations_read = false;
44
48 protected $event_handler;
49
55 public static function getInstanceByUserId($usr_id)
56 {
57 if (ilObjUser::_isAnonymous($usr_id)) {
58 throw new ilBuddySystemException(sprintf("You cannot create an instance for the anonymous user (id: %s)", $usr_id));
59 }
60
61 if (isset(self::$instances[$usr_id])) {
62 return self::$instances[$usr_id];
63 }
64
65 self::$instances[$usr_id] = new self($usr_id);
66 return self::$instances[$usr_id];
67 }
68
72 public static function getInstanceByGlobalUser()
73 {
74 global $DIC;
75
76 return self::getInstanceByUserId($DIC->user()->getId());
77 }
78
82 protected function __construct($owner_id)
83 {
84 global $DIC;
85
86 $this->setOwnerId($owner_id);
88
89 $this->event_handler = $DIC['ilAppEventHandler'];
90 }
91
95 public function reset()
96 {
97 $this->relations_read = false;
98 $this->relations = null;
99 unset(self::$instances[$this->getOwnerId()]);
100 }
101
105 public function getRepository()
106 {
107 return $this->repository;
108 }
109
113 public function setRepository($repository)
114 {
115 $this->repository = $repository;
116 }
117
121 public function readFromRepository()
122 {
123 $this->setRelations(new ilBuddySystemRelationCollection((array) $this->getRepository()->getAll()));
124 }
125
129 protected function performLazyLoading()
130 {
131 if (!$this->relations_read) {
132 $this->readFromRepository();
133 $this->relations_read = true;
134 }
135 }
136
140 public function getRelations()
141 {
142 if (null === $this->relations) {
143 $this->performLazyLoading();
144 }
145
146 return $this->relations;
147 }
148
153 {
154 $this->relations = $relations;
155 }
156
161 public function getOwnerId()
162 {
163 return $this->owner_id;
164 }
165
170 public function getLinkedRelations()
171 {
172 return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
173 return $relation->isLinked();
174 });
175 }
176
182 {
183 $owner = $this->getOwnerId();
184 return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation) use ($owner) {
185 return $relation->getBuddyUserId() == $owner;
186 });
187 }
188
194 {
195 $owner = $this->getOwnerId();
196 return $this->getRequestedRelations()->filter(function (ilBuddySystemRelation $relation) use ($owner) {
197 return $relation->getUserId() == $owner;
198 });
199 }
200
205 public function getRequestedRelations()
206 {
207 return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
208 return $relation->isRequested();
209 });
210 }
211
217 {
218 $owner = $this->getOwnerId();
219 return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation) use ($owner) {
220 return $relation->getBuddyUserId() == $owner;
221 });
222 }
223
229 {
230 $owner = $this->getOwnerId();
231 return $this->getIgnoredRelations()->filter(function (ilBuddySystemRelation $relation) use ($owner) {
232 return $relation->getUserId() == $owner;
233 });
234 }
235
240 public function getIgnoredRelations()
241 {
242 return $this->getRelations()->filter(function (ilBuddySystemRelation $relation) {
243 return $relation->isIgnored();
244 });
245 }
246
251 public function getRelationUserIds()
252 {
253 return $this->getRelations()->getKeys();
254 }
255
260 public function setOwnerId($owner_id)
261 {
262 if (!is_numeric($owner_id)) {
263 throw new InvalidArgumentException(sprintf("Please pass a numeric owner id, given: %s", var_export($owner_id, 1)));
264 }
265
266 $this->owner_id = $owner_id;
267 }
268
273 protected function getRelationTargetUserId(ilBuddySystemRelation $relation)
274 {
275 return ($relation->getUserId() == $this->getOwnerId() ? $relation->getBuddyUserId() : $relation->getUserId());
276 }
277
283 public function getRelationByUserId($usr_id)
284 {
285 if (!is_numeric($usr_id)) {
286 throw new InvalidArgumentException(sprintf("Please pass a numeric owner id, given: %s", var_export($usr_id, 1)));
287 }
288
289 if ($this->getRelations()->containsKey($usr_id)) {
290 return $this->getRelations()->get($usr_id);
291 }
292
293 require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
294 $relation = new ilBuddySystemRelation(ilBuddySystemRelationStateFactory::getInstance()->getInitialState());
295 $relation->setIsOwnedByRequest(true);
296 $relation->setUserId($this->getOwnerId());
297 $relation->setBuddyUserId($usr_id);
298 $relation->setTimestamp(time());
299
300 $this->getRelations()->set($usr_id, $relation);
301
302 return $relation;
303 }
304
310 public function link(ilBuddySystemRelation $relation)
311 {
312 try {
313 if ($relation->isLinked()) {
314 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
315 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
316 }
317
318 if ($this->getOwnerId() == $relation->getUserId()) {
319 throw new ilBuddySystemException("You can only accept a request when you are not the initiator");
320 }
321
322 $relation->link();
323
324 $this->getRepository()->save($relation);
325 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
327 throw $e;
328 }
329
330 return $this;
331 }
332
338 public function unlink(ilBuddySystemRelation $relation)
339 {
340 try {
341 $relation->unlink();
342 $this->getRepository()->save($relation);
343 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
344 } catch (ilBuddySystemException $e) {
345 if ($relation->isUnlinked()) {
346 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
347 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked');
348 }
349
350 throw $e;
351 }
352
353 return $this;
354 }
355
361 public function request(ilBuddySystemRelation $relation)
362 {
363 if (ilObjUser::_isAnonymous($this->getRelationTargetUserId($relation))) {
364 throw new ilBuddySystemException(sprintf("You cannot add the anonymous user to the list (id: %s)", $this->getRelationTargetUserId($relation)));
365 }
366
367 if (!strlen(ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation)))) {
368 throw new ilBuddySystemException(sprintf("You cannot add a non existing user (id: %s)", $this->getRelationTargetUserId($relation)));
369 }
370
371 try {
372 $relation->request();
373 $this->getRepository()->save($relation);
374 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
375 } catch (ilBuddySystemException $e) {
376 if ($relation->isRequested()) {
377 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
378 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested');
379 }
380
381 throw $e;
382 }
383
384 $this->event_handler->raise(
385 'Services/Contact',
386 'contactRequested',
387 array(
388 'usr_id' => $this->getRelationTargetUserId($relation)
389 )
390 );
391
392 return $this;
393 }
394
400 public function ignore(ilBuddySystemRelation $relation)
401 {
402 try {
403 if ($relation->isLinked()) {
404 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateTransitionException.php';
405 throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
406 }
407
408 if ($this->getOwnerId() == $relation->getUserId()) {
409 throw new ilBuddySystemException("You can only ignore a request when you are not the initiator");
410 }
411
412 $relation->ignore();
413
414 $this->getRepository()->save($relation);
415 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
416 } catch (ilBuddySystemException $e) {
417 if ($relation->isIgnored()) {
418 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
419 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored');
420 }
421
422 throw $e;
423 }
424
425 return $this;
426 }
427
432 public function destroy()
433 {
434 $this->getRepository()->destroy();
435 $this->getRelations()->clear();
436 return $this;
437 }
438}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Class ilBuddyList.
unlink(ilBuddySystemRelation $relation)
getIgnoredRelations()
Gets all ignored relations.
link(ilBuddySystemRelation $relation)
setRelations(ilBuddySystemRelationCollection $relations)
ignore(ilBuddySystemRelation $relation)
getOwnerId()
Returns the user id of the buddy list owner.
getIgnoredRelationsForOwner()
Gets all ignored relations the buddy list owner has to interact with.
getRelationTargetUserId(ilBuddySystemRelation $relation)
setRepository($repository)
getLinkedRelations()
Gets all linked/approved relations.
getRelationByUserId($usr_id)
getRelationUserIds()
Returns an array of all user ids the buddy list owner has a relation with.
static getInstanceByUserId($usr_id)
getIgnoredRelationsByOwner()
Gets all ignored relations the buddy list owner initiated.
getRequestRelationsForOwner()
Gets all requested relations the buddy list owner has to interact with.
setOwnerId($owner_id)
static getInstanceByGlobalUser()
request(ilBuddySystemRelation $relation)
reset()
Remove the singleton instance from static array, used for unit tests.
getRequestedRelations()
Gets all requested relations.
destroy()
Removes all buddy system references of the user (persistently)
__construct($owner_id)
getRequestRelationsByOwner()
Gets all requested relations the buddy list owner initiated.
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list.
Class ilBuddySystemRelationRepository.
Class ilBuddySystemRelation.
static _lookupLogin($a_user_id)
lookup login
static _isAnonymous($usr_id)
global $DIC
Definition: saml.php:7