ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.SkillProfileCompletionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 namespace ILIAS\Skill\Profile;
23 
25 
32 {
33  protected \ilDBInterface $db;
35 
36  public function __construct(
37  \ilDBInterface $db = null,
38  Service\SkillInternalFactoryService $factory_service = null
39  ) {
40  global $DIC;
41 
42  $this->db = ($db) ?: $DIC->database();
43  $this->factory_service = ($factory_service) ?: $DIC->skills()->internal()->factory();
44  }
45 
50  public function getEntries(int $user_id, int $profile_id): array
51  {
52  $ilDB = $this->db;
53 
54  $set = $ilDB->query(
55  "SELECT * FROM skl_profile_completion " .
56  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer") .
57  " AND user_id = " . $ilDB->quote($user_id, "integer")
58  );
59  $entries = [];
60  while ($rec = $ilDB->fetchAssoc($set)) {
61  $entries[] = $this->getFromRecord($rec);
62  }
63 
64  return $entries;
65  }
66 
67  protected function getFromRecord(array $rec): SkillProfileCompletion
68  {
69  $rec["profile_id"] = (int) $rec["profile_id"];
70  $rec["user_id"] = (int) $rec["user_id"];
71  $rec["fulfilled"] = (bool) $rec["fulfilled"];
72 
73  return $this->factory_service->profile()->profileCompletion(
74  $rec["profile_id"],
75  $rec["user_id"],
76  $rec["date"],
77  $rec["fulfilled"]
78  );
79  }
80 
84  public function addFulfilmentEntry(int $user_id, int $profile_id): void
85  {
86  $ilDB = $this->db;
87 
88  $set = $ilDB->query(
89  "SELECT * FROM skl_profile_completion " .
90  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer") .
91  " AND user_id = " . $ilDB->quote($user_id, "integer") .
92  " ORDER BY date DESC" .
93  " LIMIT 1"
94  );
95 
96  $entry = null;
97  while ($rec = $ilDB->fetchAssoc($set)) {
98  $entry = $rec["fulfilled"];
99  }
100 
101  if ($entry == 0) {
102  $now = \ilUtil::now();
103  $ilDB->manipulate("INSERT INTO skl_profile_completion " .
104  "(profile_id, user_id, date, fulfilled) VALUES (" .
105  $ilDB->quote($profile_id, "integer") . "," .
106  $ilDB->quote($user_id, "integer") . "," .
107  $ilDB->quote($now, "timestamp") . "," .
108  $ilDB->quote(1, "integer") .
109  ")");
110  }
111  }
112 
116  public function addNonFulfilmentEntry(int $user_id, int $profile_id): void
117  {
118  $ilDB = $this->db;
119 
120  $set = $ilDB->query(
121  "SELECT * FROM skl_profile_completion " .
122  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer") .
123  " AND user_id = " . $ilDB->quote($user_id, "integer") .
124  " ORDER BY date DESC" .
125  " LIMIT 1"
126  );
127 
128  $entry = null;
129  while ($rec = $ilDB->fetchAssoc($set)) {
130  $entry = $rec["fulfilled"];
131  }
132 
133  if (is_null($entry) || $entry == 1) {
134  $now = \ilUtil::now();
135  $ilDB->manipulate("INSERT INTO skl_profile_completion " .
136  "(profile_id, user_id, date, fulfilled) VALUES (" .
137  $ilDB->quote($profile_id, "integer") . "," .
138  $ilDB->quote($user_id, "integer") . "," .
139  $ilDB->quote($now, "timestamp") . "," .
140  $ilDB->quote(0, "integer") .
141  ")");
142  }
143  }
144 
149  public function getFulfilledEntriesForUser(int $user_id): array
150  {
151  $ilDB = $this->db;
152 
153  $set = $ilDB->query(
154  "SELECT * FROM skl_profile_completion " .
155  " WHERE user_id = " . $ilDB->quote($user_id, "integer") .
156  " AND fulfilled = 1"
157  );
158  $entries = [];
159  while ($rec = $ilDB->fetchAssoc($set)) {
160  $entries[] = $this->getFromRecord($rec);
161  }
162 
163  return $entries;
164  }
165 
170  public function getAllEntriesForUser(int $user_id): array
171  {
172  $ilDB = $this->db;
173 
174  $set = $ilDB->query(
175  "SELECT * FROM skl_profile_completion " .
176  " WHERE user_id = " . $ilDB->quote($user_id, "integer")
177  );
178  $entries = [];
179  while ($rec = $ilDB->fetchAssoc($set)) {
180  $entries[] = $this->getFromRecord($rec);
181  }
182 
183  return $entries;
184  }
185 
190  public function getAllEntriesForProfile(int $profile_id): array
191  {
192  $ilDB = $this->db;
193 
194  $set = $ilDB->query(
195  "SELECT * FROM skl_profile_completion " .
196  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
197  );
198  $entries = [];
199  while ($rec = $ilDB->fetchAssoc($set)) {
200  $entries[] = $this->getFromRecord($rec);
201  }
202 
203  return $entries;
204  }
205 
209  public function deleteEntriesForProfile(int $profile_id): void
210  {
211  $ilDB = $this->db;
212 
213  $ilDB->manipulate(
214  "DELETE FROM skl_profile_completion WHERE "
215  . " profile_id = " . $ilDB->quote($profile_id, "integer")
216  );
217  }
218 
222  public function deleteEntriesForUser(int $user_id): void
223  {
224  $ilDB = $this->db;
225 
226  $ilDB->manipulate(
227  "DELETE FROM skl_profile_completion WHERE "
228  . " user_id = " . $ilDB->quote($user_id, "integer")
229  );
230  }
231 }
getFulfilledEntriesForUser(int $user_id)
Get all fulfilled profile completion entries for a user.
deleteEntriesForProfile(int $profile_id)
Delete all profile completion entries for a profile.
getAllEntriesForProfile(int $profile_id)
Get all completion entries for a single profile.
getAllEntriesForUser(int $user_id)
Get all profile completion entries for a user.
static now()
Return current timestamp in Y-m-d H:i:s format.
deleteEntriesForUser(int $user_id)
Delete all profile completion entries for a user.
global $DIC
Definition: feed.php:28
__construct(\ilDBInterface $db=null, Service\SkillInternalFactoryService $factory_service=null)
getEntries(int $user_id, int $profile_id)
Get profile completion entries for given user-profile-combination.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addNonFulfilmentEntry(int $user_id, int $profile_id)
Add profile non-fulfilment entry to given user-profile-combination.
addFulfilmentEntry(int $user_id, int $profile_id)
Add profile fulfilment entry to given user-profile-combination.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...