ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilRecommendedContentDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
20 {
24  protected $db;
25 
29  public function __construct(\ilDBInterface $db = null)
30  {
31  global $DIC;
32 
33  $this->db = (is_null($db))
34  ? $DIC->database()
35  : $db;
36  }
37 
38 
44  public function addRoleRecommendation(int $role_id, int $ref_id)
45  {
46  $db = $this->db;
47 
48  $db->replace(
49  "rep_rec_content_role",
50  [ // pk
51  "role_id" => ["integer", $role_id],
52  "ref_id" => ["integer", $ref_id]
53  ],
54  []
55  );
56  }
57 
63  public function removeRoleRecommendation(int $role_id, int $ref_id)
64  {
65  $db = $this->db;
66 
67  $db->manipulateF(
68  "DELETE FROM rep_rec_content_role WHERE " .
69  " role_id = %s AND ref_id = %s",
70  ["integer", "integer"],
71  [$role_id, $ref_id]
72  );
73  }
74 
80  public function addObjectRecommendation(int $user_id, int $ref_id)
81  {
82  $db = $this->db;
83 
84  if (!$this->ifExistsObjectRecommendation($user_id, $ref_id)) {
85  $db->insert("rep_rec_content_obj", [
86  "user_id" => ["integer", $user_id],
87  "ref_id" => ["integer", $ref_id],
88  "declined" => ["integer", false]
89  ]);
90  }
91  }
92 
98  public function removeObjectRecommendation(int $user_id, int $ref_id)
99  {
100  $db = $this->db;
101 
102  $db->manipulateF(
103  "DELETE FROM rep_rec_content_obj WHERE " .
104  " user_id = %s AND ref_id = %s",
105  ["integer", "integer"],
106  [$user_id, $ref_id]
107  );
108  }
109 
115  public function removeRecommendationsOfRefId(int $ref_id)
116  {
117  $db = $this->db;
118 
119  $db->manipulateF(
120  "DELETE FROM rep_rec_content_obj WHERE " .
121  " ref_id = %s",
122  ["integer"],
123  [$ref_id]
124  );
125 
126  $db->manipulateF(
127  "DELETE FROM rep_rec_content_role WHERE " .
128  " ref_id = %s",
129  ["integer"],
130  [$ref_id]
131  );
132  }
133 
139  public function removeRecommendationsOfUser(int $user_id)
140  {
141  $db = $this->db;
142 
143  $db->manipulateF(
144  "DELETE FROM rep_rec_content_obj WHERE " .
145  " user_id = %s",
146  ["integer"],
147  [$user_id]
148  );
149  }
150 
156  public function removeRecommendationsOfRole(int $role_id)
157  {
158  $db = $this->db;
159 
160  $db->manipulateF(
161  "DELETE FROM rep_rec_content_role WHERE " .
162  " role_id = %s",
163  ["integer"],
164  [$role_id]
165  );
166  }
167 
174  protected function ifExistsObjectRecommendation(int $user_id, int $ref_id)
175  {
176  $db = $this->db;
177 
178  $set = $db->queryF(
179  "SELECT * FROM rep_rec_content_obj " .
180  " WHERE user_id = %s AND ref_id = %s",
181  ["integer","integer"],
182  [$user_id, $ref_id]
183  );
184  if ($rec = $db->fetchAssoc($set)) {
185  return true;
186  }
187  return false;
188  }
189 
190 
197  public function declineObjectRecommendation(int $user_id, int $ref_id)
198  {
199  $db = $this->db;
200 
201  if ($this->ifExistsObjectRecommendation($user_id, $ref_id)) {
202  $db->update(
203  "rep_rec_content_obj",
204  [
205  "declined" => ["integer", true]
206  ],
207  [ // where
208  "user_id" => ["integer", $user_id],
209  "ref_id" => ["integer", $ref_id]
210  ]
211  );
212  } else {
213  $db->insert("rep_rec_content_obj", [
214  "user_id" => ["integer", $user_id],
215  "ref_id" => ["integer", $ref_id],
216  "declined" => ["integer", true]
217  ]);
218  }
219  }
220 
227  public function getRecommendationsOfRoles(array $role_ids)
228  {
229  $db = $this->db;
230 
231  $set = $db->query(
232  "SELECT DISTINCT ref_id FROM rep_rec_content_role " .
233  " WHERE " . $db->in("role_id", $role_ids, false, "integer")
234  );
235  return array_column($db->fetchAll($set), "ref_id");
236  }
237 
244  protected function getUserObjectRecommendations(int $user_id) : array
245  {
246  $db = $this->db;
247 
248  $set = $db->queryF(
249  "SELECT ref_id FROM rep_rec_content_obj " .
250  " WHERE user_id = %s AND declined = %s",
251  ["integer", "integer"],
252  [$user_id, false]
253  );
254  return array_column($db->fetchAll($set), "ref_id");
255  }
256 
263  protected function getDeclinedUserObjectRecommendations(int $user_id) : array
264  {
265  $db = $this->db;
266 
267  $set = $db->queryF(
268  "SELECT ref_id FROM rep_rec_content_obj " .
269  " WHERE user_id = %s AND declined = %s",
270  ["integer", "integer"],
271  [$user_id, true]
272  );
273  return array_column($db->fetchAll($set), "ref_id");
274  }
275 
276 
284  public function getOpenRecommendationsOfUser(int $user_id, array $role_ids) : array
285  {
286  // recommendations of role
287  $role_recommendations = $this->getRecommendationsOfRoles($role_ids);
288 
289  // recommendations of user
290  $obj_recommendations = $this->getUserObjectRecommendations($user_id);
291 
292  $recommendations = array_unique($role_recommendations + $obj_recommendations);
293 
294  // filter declined recommendations
295  $declined_recommendations = $this->getDeclinedUserObjectRecommendations($user_id);
296  return array_filter($recommendations, function ($i) use ($declined_recommendations) {
297  return !in_array($i, $declined_recommendations);
298  });
299  }
300 }
addObjectRecommendation(int $user_id, int $ref_id)
Add object recommendation.
addRoleRecommendation(int $role_id, int $ref_id)
Add role recommendation.
removeRecommendationsOfRole(int $role_id)
Remove all recommendations of a role.
getOpenRecommendationsOfUser(int $user_id, array $role_ids)
Open recommendations of user (by role or object, without declined ones)
removeRecommendationsOfRefId(int $ref_id)
Remove all recommendations of a ref id (role and user/object related)
declineObjectRecommendation(int $user_id, int $ref_id)
Decline object recommendation.
ifExistsObjectRecommendation(int $user_id, int $ref_id)
Does object recommendation exist?
Interface ilDBInterface.
__construct(\ilDBInterface $db=null)
Constructor.
getRecommendationsOfRoles(array $role_ids)
Get recommendations of roles.
removeRecommendationsOfUser(int $user_id)
Remove all recommendations of a user.
getDeclinedUserObjectRecommendations(int $user_id)
Get declined user object recommendations.
removeRoleRecommendation(int $role_id, int $ref_id)
Remove role recommendation.
$DIC
Definition: xapitoken.php:46
getUserObjectRecommendations(int $user_id)
Get user object recommendations.
removeObjectRecommendation(int $user_id, int $ref_id)
Remove object recommendation.
$i
Definition: metadata.php:24