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