ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
33 global $ilDB;
34
35 $this->db = $ilDB;
36 $this->usr_id = $usr_id;
37 }
38
42 public function getDatabaseAdapter()
43 {
44 return $this->db;
45 }
46
50 public function setDatabaseAdapter(ilDB $db)
51 {
52 $this->db = $db;
53 }
54
59 public function getAll()
60 {
61 $relations = array();
62
63 $res = $this->db->queryF(
64 "
65 SELECT usr_id, buddy_usr_id, ts, '" . self::TYPE_APPROVED . "' rel_type FROM buddylist WHERE usr_id = %s
66 UNION
67 SELECT usr_id, buddy_usr_id, ts, IF(ignored = 1, '" . self::TYPE_IGNORED . "', '" . self::TYPE_REQUESTED . "') rel_type FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s
68 ",
69 array('integer', 'integer', 'integer'),
70 array($this->usr_id, $this->usr_id, $this->usr_id)
71 );
72
73 while($row = $this->db->fetchAssoc($res))
74 {
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'])
94 {
96 return $relation;
97 }
98 else
99 {
100 if(self::TYPE_IGNORED == $row['rel_type'])
101 {
103 return $relation;
104 }
105 else
106 {
108 return $relation;
109 }
110 }
111 }
112
116 public function destroy()
117 {
118 $this->db->queryF(
119 "DELETE FROM buddylist WHERE usr_id = %s OR buddy_usr_id = %s",
120 array('integer', 'integer'),
121 array($this->usr_id, $this->usr_id)
122 );
123
124 $this->db->queryF(
125 "DELETE FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s",
126 array('integer', 'integer'),
127 array($this->usr_id, $this->usr_id)
128 );
129 }
130
134 private function addToApprovedBuddies(ilBuddySystemRelation $relation)
135 {
136 $this->db->replace(
137 'buddylist',
138 array(
139 'usr_id' => array('integer', $relation->getUserId()),
140 'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
141 ),
142 array(
143 'ts' => array('integer', $relation->getTimestamp())
144 )
145 );
146
147 $this->db->replace(
148 'buddylist',
149 array(
150 'usr_id' => array('integer', $relation->getBuddyUserId()),
151 'buddy_usr_id' => array('integer', $relation->getUserId())
152 ),
153 array(
154 'ts' => array('integer', $relation->getTimestamp())
155 )
156 );
157 }
158
163 {
164 $this->db->manipulateF(
165 "DELETE FROM buddylist WHERE usr_id = %s AND buddy_usr_id = %s",
166 array('integer', 'integer'),
167 array($relation->getUserId(), $relation->getBuddyUserId())
168 );
169
170 $this->db->manipulateF(
171 "DELETE FROM buddylist WHERE buddy_usr_id = %s AND usr_id = %s",
172 array('integer', 'integer'),
173 array($relation->getUserId(), $relation->getBuddyUserId())
174 );
175 }
176
181 private function addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
182 {
183 $this->db->replace(
184 'buddylist_requests',
185 array(
186 'usr_id' => array('integer', $relation->getUserId()),
187 'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
188 ),
189 array(
190 'ts' => array('integer', $relation->getTimestamp()),
191 'ignored' => array('integer', (int)$ignored)
192 )
193 );
194 }
195
200 {
201 $this->db->manipulateF(
202 "DELETE FROM buddylist_requests WHERE usr_id = %s AND buddy_usr_id = %s",
203 array('integer', 'integer'),
204 array($relation->getUserId(), $relation->getBuddyUserId())
205 );
206
207 $this->db->manipulateF(
208 "DELETE FROM buddylist_requests WHERE buddy_usr_id = %s AND usr_id = %s",
209 array('integer', 'integer'),
210 array($relation->getUserId(), $relation->getBuddyUserId())
211 );
212 }
213
217 public function save(ilBuddySystemRelation $relation)
218 {
219 $this->db->beginTransaction();
220
221 if($relation->isLinked())
222 {
223 $this->addToApprovedBuddies($relation);
224 }
225 else if($relation->wasLinked())
226 {
227 $this->removeFromApprovedBuddies($relation);
228 }
229
230 if($relation->isRequested())
231 {
232 $this->addToRequestedBuddies($relation, false);
233 }
234 else if($relation->isIgnored())
235 {
236 $this->addToRequestedBuddies($relation, true);
237 }
238 else if($relation->wasRequested() || $relation->wasIgnored())
239 {
240 $this->removeFromRequestedBuddies($relation);
241 }
242
243 $this->db->commit();
244 }
245}
Class ilBuddySystemRelationRepository.
removeFromRequestedBuddies(ilBuddySystemRelation $relation)
removeFromApprovedBuddies(ilBuddySystemRelation $relation)
addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
addToApprovedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelation.
Database Wrapper.
Definition: class.ilDB.php:29
global $ilDB