ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBuddySystemRelationRepository.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
10 {
11  const TYPE_APPROVED = 'app';
12  const TYPE_REQUESTED = 'req';
13  const TYPE_IGNORED = 'ign';
14 
18  protected $db;
19 
23  protected $usr_id;
24 
28  public function __construct($usr_id)
29  {
30  global $DIC;
31 
32  $this->db = $DIC['ilDB'];
33  $this->usr_id = $usr_id;
34  }
35 
39  public function getDatabaseAdapter()
40  {
41  return $this->db;
42  }
43 
48  {
49  $this->db = $db;
50  }
51 
56  public function getAll()
57  {
58  $relations = array();
59 
60  $res = $this->db->queryF(
61  "
62  SELECT usr_id, buddy_usr_id, ts, %s rel_type FROM buddylist WHERE usr_id = %s
63  UNION
64  SELECT usr_id, buddy_usr_id, ts, (CASE WHEN ignored = 1 THEN %s ELSE %s END) rel_type FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s
65  ",
66  array(
67  'text', 'integer', 'text', 'text', 'integer', 'integer'
68  ),
69  array(
70  self::TYPE_APPROVED, $this->usr_id, self::TYPE_IGNORED, self::TYPE_REQUESTED, $this->usr_id, $this->usr_id
71  )
72  );
73 
74  while ($row = $this->db->fetchAssoc($res)) {
75  $relation = $this->getRelationByDatabaseRecord($row);
76  $relation->setUserId($row['usr_id']);
77  $relation->setBuddyUserId($row['buddy_usr_id']);
78  $relation->setTimestamp($row['ts']);
79  $relation->setIsOwnedByRequest($relation->getUserId() == $this->usr_id);
80  $key = $this->usr_id == $relation->getUserId() ? $relation->getBuddyUserId() : $relation->getUserId();
81  $relations[$key] = $relation;
82  }
83 
84  return $relations;
85  }
86 
92  {
93  if (self::TYPE_APPROVED == $row['rel_type']) {
95  return $relation;
96  } else {
97  if (self::TYPE_IGNORED == $row['rel_type']) {
99  return $relation;
100  } else {
102  return $relation;
103  }
104  }
105  }
106 
110  public function destroy()
111  {
112  $this->db->queryF(
113  "DELETE FROM buddylist WHERE usr_id = %s OR buddy_usr_id = %s",
114  array('integer', 'integer'),
115  array($this->usr_id, $this->usr_id)
116  );
117 
118  $this->db->queryF(
119  "DELETE FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s",
120  array('integer', 'integer'),
121  array($this->usr_id, $this->usr_id)
122  );
123  }
124 
128  private function addToApprovedBuddies(ilBuddySystemRelation $relation)
129  {
130  $this->db->replace(
131  'buddylist',
132  array(
133  'usr_id' => array('integer', $relation->getUserId()),
134  'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
135  ),
136  array(
137  'ts' => array('integer', $relation->getTimestamp())
138  )
139  );
140 
141  $this->db->replace(
142  'buddylist',
143  array(
144  'usr_id' => array('integer', $relation->getBuddyUserId()),
145  'buddy_usr_id' => array('integer', $relation->getUserId())
146  ),
147  array(
148  'ts' => array('integer', $relation->getTimestamp())
149  )
150  );
151  }
152 
157  {
158  $this->db->manipulateF(
159  "DELETE FROM buddylist WHERE usr_id = %s AND buddy_usr_id = %s",
160  array('integer', 'integer'),
161  array($relation->getUserId(), $relation->getBuddyUserId())
162  );
163 
164  $this->db->manipulateF(
165  "DELETE FROM buddylist WHERE buddy_usr_id = %s AND usr_id = %s",
166  array('integer', 'integer'),
167  array($relation->getUserId(), $relation->getBuddyUserId())
168  );
169  }
170 
175  private function addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
176  {
177  $this->db->replace(
178  'buddylist_requests',
179  array(
180  'usr_id' => array('integer', $relation->getUserId()),
181  'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
182  ),
183  array(
184  'ts' => array('integer', $relation->getTimestamp()),
185  'ignored' => array('integer', (int) $ignored)
186  )
187  );
188  }
189 
194  {
195  $this->db->manipulateF(
196  "DELETE FROM buddylist_requests WHERE usr_id = %s AND buddy_usr_id = %s",
197  array('integer', 'integer'),
198  array($relation->getUserId(), $relation->getBuddyUserId())
199  );
200 
201  $this->db->manipulateF(
202  "DELETE FROM buddylist_requests WHERE buddy_usr_id = %s AND usr_id = %s",
203  array('integer', 'integer'),
204  array($relation->getUserId(), $relation->getBuddyUserId())
205  );
206  }
207 
211  public function save(ilBuddySystemRelation $relation)
212  {
213  $ilAtomQuery = $this->db->buildAtomQuery();
214  $ilAtomQuery->addTableLock('buddylist_requests');
215  $ilAtomQuery->addTableLock('buddylist');
216 
217  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($relation) {
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 }
global $DIC
Definition: saml.php:7
removeFromRequestedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelationRepository.
foreach($_POST as $key=> $value) $res
removeFromApprovedBuddies(ilBuddySystemRelation $relation)
addToApprovedBuddies(ilBuddySystemRelation $relation)
$row
Class ilBuddySystemRelation.
global $ilDB
addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
$key
Definition: croninfo.php:18