ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
59 throw new ilBuddySystemException(sprintf("You cannot create an instance for the anonymous user (id: %s)", $usr_id));
60 }
61
62 if(isset(self::$instances[$usr_id]))
63 {
64 return self::$instances[$usr_id];
65 }
66
67 self::$instances[$usr_id] = new self($usr_id);
68 return self::$instances[$usr_id];
69 }
70
74 public static function getInstanceByGlobalUser()
75 {
76 global $DIC;
77
78 return self::getInstanceByUserId($DIC->user()->getId());
79 }
80
84 protected function __construct($owner_id)
85 {
86 global $DIC;
87
88 $this->setOwnerId($owner_id);
90
91 $this->event_handler = $DIC['ilAppEventHandler'];
92 }
93
97 public function reset()
98 {
99 $this->relations_read = false;
100 $this->relations = null;
101 unset(self::$instances[$this->getOwnerId()]);
102 }
103
107 public function getRepository()
108 {
109 return $this->repository;
110 }
111
115 public function setRepository($repository)
116 {
117 $this->repository = $repository;
118 }
119
123 public function readFromRepository()
124 {
125 $this->setRelations(new ilBuddySystemRelationCollection((array)$this->getRepository()->getAll()));
126 }
127
131 protected function performLazyLoading()
132 {
133 if(!$this->relations_read)
134 {
135 $this->readFromRepository();
136 $this->relations_read = true;
137 }
138 }
139
143 public function getRelations()
144 {
145 if(null === $this->relations)
146 {
147 $this->performLazyLoading();
148 }
149
150 return $this->relations;
151 }
152
157 {
158 $this->relations = $relations;
159 }
160
165 public function getOwnerId()
166 {
167 return $this->owner_id;
168 }
169
174 public function getLinkedRelations()
175 {
176 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
177 return $relation->isLinked();
178 });
179 }
180
186 {
187 $owner = $this->getOwnerId();
188 return $this->getRequestedRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
189 return $relation->getBuddyUserId() == $owner;
190 });
191 }
192
198 {
199 $owner = $this->getOwnerId();
200 return $this->getRequestedRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
201 return $relation->getUserId() == $owner;
202 });
203 }
204
209 public function getRequestedRelations()
210 {
211 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
212 return $relation->isRequested();
213 });
214 }
215
221 {
222 $owner = $this->getOwnerId();
223 return $this->getIgnoredRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
224 return $relation->getBuddyUserId() == $owner;
225 });
226 }
227
233 {
234 $owner = $this->getOwnerId();
235 return $this->getIgnoredRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
236 return $relation->getUserId() == $owner;
237 });
238 }
239
244 public function getIgnoredRelations()
245 {
246 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
247 return $relation->isIgnored();
248 });
249 }
250
255 public function getRelationUserIds()
256 {
257 return $this->getRelations()->getKeys();
258 }
259
264 public function setOwnerId($owner_id)
265 {
266 if(!is_numeric($owner_id))
267 {
268 throw new InvalidArgumentException(sprintf("Please pass a numeric owner id, given: %s", var_export($owner_id, 1)));
269 }
270
271 $this->owner_id = $owner_id;
272 }
273
278 protected function getRelationTargetUserId(ilBuddySystemRelation $relation)
279 {
280 return ($relation->getUserId() == $this->getOwnerId() ? $relation->getBuddyUserId() : $relation->getUserId());
281 }
282
288 public function getRelationByUserId($usr_id)
289 {
290 if(!is_numeric($usr_id))
291 {
292 throw new InvalidArgumentException(sprintf("Please pass a numeric owner id, given: %s", var_export($usr_id, 1)));
293 }
294
295 if($this->getRelations()->containsKey($usr_id))
296 {
297 return $this->getRelations()->get($usr_id);
298 }
299
300 require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
301 $relation = new ilBuddySystemRelation(ilBuddySystemRelationStateFactory::getInstance()->getInitialState());
302 $relation->setIsOwnedByRequest(true);
303 $relation->setUserId($this->getOwnerId());
304 $relation->setBuddyUserId($usr_id);
305 $relation->setTimestamp(time());
306
307 $this->getRelations()->set($usr_id, $relation);
308
309 return $relation;
310 }
311
317 public function link(ilBuddySystemRelation $relation)
318 {
319 try
320 {
321 if($relation->isLinked())
322 {
323 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
324 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
325 }
326
327 if($this->getOwnerId() == $relation->getUserId())
328 {
329 throw new ilBuddySystemException("You can only accept a request when you are not the initiator");
330 }
331
332 $relation->link();
333
334 $this->getRepository()->save($relation);
335 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
336 }
338 {
339 throw $e;
340 }
341
342 return $this;
343 }
344
350 public function unlink(ilBuddySystemRelation $relation)
351 {
352 try
353 {
354 $relation->unlink();
355 $this->getRepository()->save($relation);
356 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
357 }
358 catch(ilBuddySystemException $e)
359 {
360 if($relation->isUnlinked())
361 {
362 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
363 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked');
364 }
365
366 throw $e;
367 }
368
369 return $this;
370 }
371
377 public function request(ilBuddySystemRelation $relation)
378 {
380 {
381 throw new ilBuddySystemException(sprintf("You cannot add the anonymous user to the list (id: %s)", $this->getRelationTargetUserId($relation)));
382 }
383
384 if(!strlen(ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation))))
385 {
386 throw new ilBuddySystemException(sprintf("You cannot add a non existing user (id: %s)", $this->getRelationTargetUserId($relation)));
387 }
388
389 try
390 {
391 $relation->request();
392 $this->getRepository()->save($relation);
393 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
394 }
395 catch(ilBuddySystemException $e)
396 {
397 if($relation->isRequested())
398 {
399 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
400 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested');
401 }
402
403 throw $e;
404 }
405
406 $this->event_handler->raise(
407 'Services/Contact',
408 'contactRequested',
409 array(
410 'usr_id' => $this->getRelationTargetUserId($relation)
411 )
412 );
413
414 return $this;
415 }
416
422 public function ignore(ilBuddySystemRelation $relation)
423 {
424 try
425 {
426 if($relation->isLinked())
427 {
428 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateTransitionException.php';
429 throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
430 }
431
432 if($this->getOwnerId() == $relation->getUserId())
433 {
434 throw new ilBuddySystemException("You can only ignore a request when you are not the initiator");
435 }
436
437 $relation->ignore();
438
439 $this->getRepository()->save($relation);
440 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
441 }
442 catch(ilBuddySystemException $e)
443 {
444 if($relation->isIgnored())
445 {
446 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
447 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored');
448 }
449
450 throw $e;
451 }
452
453 return $this;
454 }
455
460 public function destroy()
461 {
462 $this->getRepository()->destroy();
463 $this->getRelations()->clear();
464 return $this;
465 }
466}
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