ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
50 public static function getInstanceByUserId($usr_id)
51 {
52 if(ilObjUser::_isAnonymous($usr_id))
53 {
54 throw new ilBuddySystemException(sprintf("You cannot create an instance for the anonymous user (id: %s)", $usr_id));
55 }
56
57 if(isset(self::$instances[$usr_id]))
58 {
59 return self::$instances[$usr_id];
60 }
61
62 self::$instances[$usr_id] = new self($usr_id);
63 return self::$instances[$usr_id];
64 }
65
69 public static function getInstanceByGlobalUser()
70 {
74 global $ilUser;
75
76 return self::getInstanceByUserId($ilUser->getId());
77 }
78
82 protected function __construct($owner_id)
83 {
84 $this->setOwnerId($owner_id);
86 }
87
91 public function reset()
92 {
93 $this->relations_read = false;
94 $this->relations = null;
95 unset(self::$instances[$this->getOwnerId()]);
96 }
97
101 public function getRepository()
102 {
103 return $this->repository;
104 }
105
109 public function setRepository($repository)
110 {
111 $this->repository = $repository;
112 }
113
117 public function readFromRepository()
118 {
119 $this->setRelations(new ilBuddySystemRelationCollection((array)$this->getRepository()->getAll()));
120 }
121
125 protected function performLazyLoading()
126 {
127 if(!$this->relations_read)
128 {
129 $this->readFromRepository();
130 $this->relations_read = true;
131 }
132 }
133
137 public function getRelations()
138 {
139 if(null === $this->relations)
140 {
141 $this->performLazyLoading();
142 }
143
144 return $this->relations;
145 }
146
151 {
152 $this->relations = $relations;
153 }
154
159 public function getOwnerId()
160 {
161 return $this->owner_id;
162 }
163
168 public function getLinkedRelations()
169 {
170 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
171 return $relation->isLinked();
172 });
173 }
174
180 {
181 $owner = $this->getOwnerId();
182 return $this->getRequestedRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
183 return $relation->getBuddyUserId() == $owner;
184 });
185 }
186
192 {
193 $owner = $this->getOwnerId();
194 return $this->getRequestedRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
195 return $relation->getUserId() == $owner;
196 });
197 }
198
203 public function getRequestedRelations()
204 {
205 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
206 return $relation->isRequested();
207 });
208 }
209
215 {
216 $owner = $this->getOwnerId();
217 return $this->getIgnoredRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
218 return $relation->getBuddyUserId() == $owner;
219 });
220 }
221
227 {
228 $owner = $this->getOwnerId();
229 return $this->getIgnoredRelations()->filter(function(ilBuddySystemRelation $relation) use ($owner) {
230 return $relation->getUserId() == $owner;
231 });
232 }
233
238 public function getIgnoredRelations()
239 {
240 return $this->getRelations()->filter(function(ilBuddySystemRelation $relation) {
241 return $relation->isIgnored();
242 });
243 }
244
249 public function getRelationUserIds()
250 {
251 return $this->getRelations()->getKeys();
252 }
253
258 public function setOwnerId($owner_id)
259 {
260 if(!is_numeric($owner_id))
261 {
262 throw new InvalidArgumentException(sprintf("Please pass a numeric owner id, given: %s", var_export($owner_id, 1)));
263 }
264
265 $this->owner_id = $owner_id;
266 }
267
272 protected function getRelationTargetUserId(ilBuddySystemRelation $relation)
273 {
274 return ($relation->getUserId() == $this->getOwnerId() ? $relation->getBuddyUserId() : $relation->getUserId());
275 }
276
282 public function getRelationByUserId($usr_id)
283 {
284 if(!is_numeric($usr_id))
285 {
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 {
291 return $this->getRelations()->get($usr_id);
292 }
293
294 require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
295 $relation = new ilBuddySystemRelation(ilBuddySystemRelationStateFactory::getInstance()->getInitialState());
296 $relation->setIsOwnedByRequest(true);
297 $relation->setUserId($this->getOwnerId());
298 $relation->setBuddyUserId($usr_id);
299 $relation->setTimestamp(time());
300
301 $this->getRelations()->set($usr_id, $relation);
302
303 return $relation;
304 }
305
311 public function link(ilBuddySystemRelation $relation)
312 {
313 try
314 {
315 if($relation->isLinked())
316 {
317 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
318 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_linked');
319 }
320
321 if($this->getOwnerId() == $relation->getUserId())
322 {
323 throw new ilBuddySystemException("You can only accept a request when you are not the initiator");
324 }
325
326 $relation->link();
327
328 $this->getRepository()->save($relation);
329 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
330 }
332 {
333 throw $e;
334 }
335
336 return $this;
337 }
338
344 public function unlink(ilBuddySystemRelation $relation)
345 {
346 try
347 {
348 $relation->unlink();
349 $this->getRepository()->save($relation);
350 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
351 }
352 catch(ilBuddySystemException $e)
353 {
354 if($relation->isUnlinked())
355 {
356 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
357 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_unlinked');
358 }
359
360 throw $e;
361 }
362
363 return $this;
364 }
365
371 public function request(ilBuddySystemRelation $relation)
372 {
374 {
375 throw new ilBuddySystemException(sprintf("You cannot add the anonymous user to the list (id: %s)", $this->getRelationTargetUserId($relation)));
376 }
377
378 if(!strlen(ilObjUser::_lookupLogin($this->getRelationTargetUserId($relation))))
379 {
380 throw new ilBuddySystemException(sprintf("You cannot add a non existing user (id: %s)", $this->getRelationTargetUserId($relation)));
381 }
382
383 try
384 {
385 $relation->request();
386 $this->getRepository()->save($relation);
387 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
388 }
389 catch(ilBuddySystemException $e)
390 {
391 if($relation->isRequested())
392 {
393 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
394 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_requested');
395 }
396
397 throw $e;
398 }
399
400 $GLOBALS['ilAppEventHandler']->raise(
401 'Services/Contact',
402 'contactRequested',
403 array(
404 'usr_id' => $this->getRelationTargetUserId($relation)
405 )
406 );
407
408 return $this;
409 }
410
416 public function ignore(ilBuddySystemRelation $relation)
417 {
418 try
419 {
420 if($relation->isLinked())
421 {
422 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateTransitionException.php';
423 throw new ilBuddySystemRelationStateTransitionException('buddy_bs_action_already_linked');
424 }
425
426 if($this->getOwnerId() == $relation->getUserId())
427 {
428 throw new ilBuddySystemException("You can only ignore a request when you are not the initiator");
429 }
430
431 $relation->ignore();
432
433 $this->getRepository()->save($relation);
434 $this->getRelations()->set($this->getRelationTargetUserId($relation), $relation);
435 }
436 catch(ilBuddySystemException $e)
437 {
438 if($relation->isIgnored())
439 {
440 require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemRelationStateAlreadyGivenException.php';
441 throw new ilBuddySystemRelationStateAlreadyGivenException('buddy_bs_action_already_ignored');
442 }
443
444 throw $e;
445 }
446
447 return $this;
448 }
449
454 public function destroy()
455 {
456 $this->getRepository()->destroy();
457 $this->getRelations()->clear();
458 return $this;
459 }
460}
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)
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)
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
global $ilUser
Definition: imgupload.php:15