ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBuddySystemRelationRepository.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 {
10  const TYPE_APPROVED = 'app';
11  const TYPE_REQUESTED = 'req';
12  const TYPE_IGNORED = 'ign';
13 
15  protected $db;
16 
18  protected $usrId;
19 
24  public function __construct(int $usrId)
25  {
26  global $DIC;
27 
28  $this->db = $DIC['ilDB'];
29  $this->usrId = $usrId;
30  }
31 
36  public function getAll() : array
37  {
38  $relations = [];
39 
40  $res = $this->db->queryF(
41  "
42  SELECT
43  buddylist.usr_id, buddylist.buddy_usr_id, buddylist.ts, %s rel_type
44  FROM buddylist
45  INNER JOIN usr_data ud
46  ON ud.usr_id = buddylist.usr_id
47  WHERE buddylist.usr_id = %s
48  UNION
49  SELECT
50  buddylist_requests.usr_id, buddylist_requests.buddy_usr_id, buddylist_requests.ts, (CASE WHEN ignored = 1 THEN %s ELSE %s END) rel_type
51  FROM buddylist_requests
52  INNER JOIN usr_data ud ON ud.usr_id = buddylist_requests.usr_id
53  INNER JOIN usr_data udbuddy ON udbuddy.usr_id = buddylist_requests.buddy_usr_id
54  WHERE buddylist_requests.usr_id = %s OR buddylist_requests.buddy_usr_id = %s
55  ",
56  [
57  'text',
58  'integer',
59  'text',
60  'text',
61  'integer',
62  'integer'
63  ],
64  [
65  self::TYPE_APPROVED,
66  $this->usrId,
67  self::TYPE_IGNORED,
68  self::TYPE_REQUESTED,
69  $this->usrId,
70  $this->usrId
71  ]
72  );
73 
74  while ($row = $this->db->fetchAssoc($res)) {
75  $relation = $this->getRelationByDatabaseRecord($row);
76  $relation->setUsrId((int) $row['usr_id']);
77  $relation->setBuddyUsrId((int) $row['buddy_usr_id']);
78  $relation->setTimestamp((int) $row['ts']);
79  $relation->setIsOwnedByActor($relation->getUsrId() === $this->usrId);
80  $key = $this->usrId === $relation->getUsrId() ? $relation->getBuddyUsrId() : $relation->getUsrId();
81  $relations[$key] = $relation;
82  }
83 
84  return $relations;
85  }
86 
91  private function getRelationByDatabaseRecord($row)
92  {
93  if (self::TYPE_APPROVED === $row['rel_type']) {
95  } elseif (self::TYPE_IGNORED === $row['rel_type']) {
97  }
98 
100  }
101 
105  public function destroy()
106  {
107  $this->db->queryF(
108  "DELETE FROM buddylist WHERE usr_id = %s OR buddy_usr_id = %s",
109  ['integer', 'integer'],
110  [$this->usrId, $this->usrId]
111  );
112 
113  $this->db->queryF(
114  "DELETE FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s",
115  ['integer', 'integer'],
116  [$this->usrId, $this->usrId]
117  );
118  }
119 
123  private function addToApprovedBuddies(ilBuddySystemRelation $relation)
124  {
125  $this->db->replace(
126  'buddylist',
127  [
128  'usr_id' => ['integer', $relation->getUsrId()],
129  'buddy_usr_id' => ['integer', $relation->getBuddyUsrId()]
130  ],
131  [
132  'ts' => ['integer', $relation->getTimestamp()]
133  ]
134  );
135 
136  $this->db->replace(
137  'buddylist',
138  [
139  'usr_id' => ['integer', $relation->getBuddyUsrId()],
140  'buddy_usr_id' => ['integer', $relation->getUsrId()]
141  ],
142  [
143  'ts' => ['integer', $relation->getTimestamp()]
144  ]
145  );
146  }
147 
152  {
153  $this->db->manipulateF(
154  "DELETE FROM buddylist WHERE usr_id = %s AND buddy_usr_id = %s",
155  ['integer', 'integer'],
156  [$relation->getUsrId(), $relation->getBuddyUsrId()]
157  );
158 
159  $this->db->manipulateF(
160  "DELETE FROM buddylist WHERE buddy_usr_id = %s AND usr_id = %s",
161  ['integer', 'integer'],
162  [$relation->getUsrId(), $relation->getBuddyUsrId()]
163  );
164  }
165 
170  private function addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
171  {
172  $this->db->replace(
173  'buddylist_requests',
174  [
175  'usr_id' => ['integer', $relation->getUsrId()],
176  'buddy_usr_id' => ['integer', $relation->getBuddyUsrId()]
177  ],
178  [
179  'ts' => ['integer', $relation->getTimestamp()],
180  'ignored' => ['integer', (int) $ignored]
181  ]
182  );
183  }
184 
189  {
190  $this->db->manipulateF(
191  "DELETE FROM buddylist_requests WHERE usr_id = %s AND buddy_usr_id = %s",
192  ['integer', 'integer'],
193  [$relation->getUsrId(), $relation->getBuddyUsrId()]
194  );
195 
196  $this->db->manipulateF(
197  "DELETE FROM buddylist_requests WHERE buddy_usr_id = %s AND usr_id = %s",
198  ['integer', 'integer'],
199  [$relation->getUsrId(), $relation->getBuddyUsrId()]
200  );
201  }
202 
206  public function save(ilBuddySystemRelation $relation) : void
207  {
208  $ilAtomQuery = $this->db->buildAtomQuery();
209  $ilAtomQuery->addTableLock('buddylist_requests');
210  $ilAtomQuery->addTableLock('buddylist');
211 
212  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($relation) {
213  if ($relation->isLinked()) {
214  $this->addToApprovedBuddies($relation);
215  } elseif ($relation->wasLinked()) {
216  $this->removeFromApprovedBuddies($relation);
217  }
218 
219  if ($relation->isRequested()) {
220  $this->addToRequestedBuddies($relation, false);
221  } elseif ($relation->isIgnored()) {
222  $this->addToRequestedBuddies($relation, true);
223  } elseif ($relation->wasRequested() || $relation->wasIgnored()) {
224  $this->removeFromRequestedBuddies($relation);
225  }
226  });
227 
228  $ilAtomQuery->run();
229  }
230 }
removeFromRequestedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelationRepository.
Interface ilDBInterface.
foreach($_POST as $key=> $value) $res
Class ilBuddySystemRequestedRelationState.
removeFromApprovedBuddies(ilBuddySystemRelation $relation)
addToApprovedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelation.
global $ilDB
$DIC
Definition: xapitoken.php:46
addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
__construct(int $usrId)
ilBuddySystemRelationRepository constructor.