ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBuddySystemRelationRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  private const TYPE_APPROVED = 'app';
28  private const TYPE_REQUESTED = 'req';
29  private const TYPE_IGNORED = 'ign';
30 
31  protected ilDBInterface $db;
32  protected int $usrId;
33 
34  public function __construct(int $usrId, ilDBInterface $db = null)
35  {
36  global $DIC;
37 
38  $this->db = $db ?? $DIC->database();
39  $this->usrId = $usrId;
40  }
41 
46  public function getAll(): array
47  {
48  $relations = [];
49 
50  $res = $this->db->queryF(
51  '
52  SELECT
53  buddylist.usr_id, buddylist.buddy_usr_id, buddylist.ts, %s rel_type
54  FROM buddylist
55  INNER JOIN usr_data ud
56  ON ud.usr_id = buddylist.usr_id
57  INNER JOIN usr_data udbuddy
58  ON udbuddy.usr_id = buddylist.buddy_usr_id
59  WHERE buddylist.usr_id = %s
60  UNION
61  SELECT
62  buddylist_requests.usr_id, buddylist_requests.buddy_usr_id, buddylist_requests.ts, (CASE WHEN ignored = 1 THEN %s ELSE %s END) rel_type
63  FROM buddylist_requests
64  INNER JOIN usr_data ud ON ud.usr_id = buddylist_requests.usr_id
65  INNER JOIN usr_data udbuddy ON udbuddy.usr_id = buddylist_requests.buddy_usr_id
66  WHERE buddylist_requests.usr_id = %s OR buddylist_requests.buddy_usr_id = %s
67  ',
68  [
69  'text',
70  'integer',
71  'text',
72  'text',
73  'integer',
74  'integer'
75  ],
76  [
77  self::TYPE_APPROVED,
78  $this->usrId,
79  self::TYPE_IGNORED,
80  self::TYPE_REQUESTED,
81  $this->usrId,
82  $this->usrId
83  ]
84  );
85 
86  while ($row = $this->db->fetchAssoc($res)) {
87  $relation = $this->getRelationByDatabaseRecord($row);
88  $key = $this->usrId === $relation->getUsrId() ? $relation->getBuddyUsrId() : $relation->getUsrId();
89  $relations[$key] = $relation;
90  }
91 
92  return $relations;
93  }
94 
95  private function getRelationByDatabaseRecord(array $row): ilBuddySystemRelation
96  {
97  if (self::TYPE_APPROVED === $row['rel_type']) {
98  return new ilBuddySystemRelation(
100  (int) $row['usr_id'],
101  (int) $row['buddy_usr_id'],
102  (int) $row['usr_id'] === $this->usrId,
103  (int) $row['ts']
104  );
105  }
106 
107  if (self::TYPE_IGNORED === $row['rel_type']) {
108  return new ilBuddySystemRelation(
110  (int) $row['usr_id'],
111  (int) $row['buddy_usr_id'],
112  (int) $row['usr_id'] === $this->usrId,
113  (int) $row['ts']
114  );
115  }
116 
117  return new ilBuddySystemRelation(
119  (int) $row['usr_id'],
120  (int) $row['buddy_usr_id'],
121  (int) $row['usr_id'] === $this->usrId,
122  (int) $row['ts']
123  );
124  }
125 
126  public function destroy(): void
127  {
128  $this->db->manipulateF(
129  'DELETE FROM buddylist WHERE usr_id = %s OR buddy_usr_id = %s',
130  ['integer', 'integer'],
131  [$this->usrId, $this->usrId]
132  );
133 
134  $this->db->manipulateF(
135  'DELETE FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s',
136  ['integer', 'integer'],
137  [$this->usrId, $this->usrId]
138  );
139  }
140 
141  private function addToApprovedBuddies(ilBuddySystemRelation $relation): void
142  {
143  $this->db->replace(
144  'buddylist',
145  [
146  'usr_id' => ['integer', $relation->getUsrId()],
147  'buddy_usr_id' => ['integer', $relation->getBuddyUsrId()]
148  ],
149  [
150  'ts' => ['integer', $relation->getTimestamp()]
151  ]
152  );
153 
154  $this->db->replace(
155  'buddylist',
156  [
157  'usr_id' => ['integer', $relation->getBuddyUsrId()],
158  'buddy_usr_id' => ['integer', $relation->getUsrId()]
159  ],
160  [
161  'ts' => ['integer', $relation->getTimestamp()]
162  ]
163  );
164  }
165 
166  private function removeFromApprovedBuddies(ilBuddySystemRelation $relation): void
167  {
168  $this->db->manipulateF(
169  'DELETE FROM buddylist WHERE usr_id = %s AND buddy_usr_id = %s',
170  ['integer', 'integer'],
171  [$relation->getUsrId(), $relation->getBuddyUsrId()]
172  );
173 
174  $this->db->manipulateF(
175  'DELETE FROM buddylist WHERE buddy_usr_id = %s AND usr_id = %s',
176  ['integer', 'integer'],
177  [$relation->getUsrId(), $relation->getBuddyUsrId()]
178  );
179  }
180 
181  private function addToRequestedBuddies(ilBuddySystemRelation $relation, bool $ignored): void
182  {
183  $this->db->replace(
184  'buddylist_requests',
185  [
186  'usr_id' => ['integer', $relation->getUsrId()],
187  'buddy_usr_id' => ['integer', $relation->getBuddyUsrId()]
188  ],
189  [
190  'ts' => ['integer', $relation->getTimestamp()],
191  'ignored' => ['integer', (int) $ignored]
192  ]
193  );
194  }
195 
196  private function removeFromRequestedBuddies(ilBuddySystemRelation $relation): void
197  {
198  $this->db->manipulateF(
199  'DELETE FROM buddylist_requests WHERE usr_id = %s AND buddy_usr_id = %s',
200  ['integer', 'integer'],
201  [$relation->getUsrId(), $relation->getBuddyUsrId()]
202  );
203 
204  $this->db->manipulateF(
205  'DELETE FROM buddylist_requests WHERE buddy_usr_id = %s AND usr_id = %s',
206  ['integer', 'integer'],
207  [$relation->getUsrId(), $relation->getBuddyUsrId()]
208  );
209  }
210 
211  public function save(ilBuddySystemRelation $relation): void
212  {
213  $ilAtomQuery = $this->db->buildAtomQuery();
214  $ilAtomQuery->addTableLock('buddylist_requests');
215  $ilAtomQuery->addTableLock('buddylist');
216 
217  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($relation): void {
218  if ($relation->isLinked()) {
219  $this->addToApprovedBuddies($relation);
220  } elseif ($relation->wasLinked()) {
221  $this->removeFromApprovedBuddies($relation);
222  }
223 
224  if ($relation->isRequested()) {
225  $this->addToRequestedBuddies($relation, false);
226  } elseif ($relation->isIgnored()) {
227  $this->addToRequestedBuddies($relation, true);
228  } elseif ($relation->wasRequested() || $relation->wasIgnored()) {
229  $this->removeFromRequestedBuddies($relation);
230  }
231  });
232 
233  $ilAtomQuery->run();
234  }
235 }
$res
Definition: ltiservices.php:69
removeFromRequestedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelationRepository.
global $DIC
Definition: feed.php:28
addToRequestedBuddies(ilBuddySystemRelation $relation, bool $ignored)
string $key
Consumer key/client ID value.
Definition: System.php:193
removeFromApprovedBuddies(ilBuddySystemRelation $relation)
addToApprovedBuddies(ilBuddySystemRelation $relation)
__construct(int $usrId, ilDBInterface $db=null)