ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBasicSkillUserLevelDBRepository.php
Go to the documentation of this file.
1 <?php
2 
4 {
8  protected $db;
9 
10  public function __construct(ilDBInterface $db = null)
11  {
12  global $DIC;
13 
14  $this->db = ($db)
15  ? $db
16  : $DIC->database();
17  }
18 
22  public function deleteUserLevelsOfSkill(int $skill_id)
23  {
24  $ilDB = $this->db;
25 
26  $ilDB->manipulate("DELETE FROM skl_user_has_level WHERE "
27  . " skill_id = " . $ilDB->quote($skill_id, "integer")
28  );
29  }
30 
34  public function resetUserSkillLevelStatus(
35  bool $update,
36  int $trigger_obj_id,
37  $status_date,
38  int $a_user_id,
39  int $a_skill_id,
40  int $a_tref_id = 0,
41  int $a_trigger_ref_id = 0,
42  bool $a_self_eval = false
43  ) {
44  $ilDB = $this->db;
45 
46  if ($update) {
47  // this will only be set in self eval case, means this will always have a $rec
48  $now = ilUtil::now();
49  $ilDB->manipulate("UPDATE skl_user_skill_level SET " .
50  " level_id = " . $ilDB->quote(0, "integer") . "," .
51  " next_level_fulfilment = " . $ilDB->quote(0.0, "float") . "," .
52  " status_date = " . $ilDB->quote($now, "timestamp") .
53  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
54  " AND status_date = " . $ilDB->quote($status_date, "timestamp") .
55  " AND skill_id = " . $ilDB->quote($a_skill_id, "integer") .
56  " AND status = " . $ilDB->quote(ilBasicSkill::ACHIEVED, "integer") .
57  " AND trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer") .
58  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
59  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
60  );
61  } else {
62  $now = ilUtil::now();
63  $ilDB->manipulate("INSERT INTO skl_user_skill_level " .
64  "(level_id, user_id, tref_id, status_date, skill_id, status, valid, trigger_ref_id," .
65  "trigger_obj_id, trigger_obj_type, trigger_title, self_eval, unique_identifier," .
66  "next_level_fulfilment) VALUES (" .
67  $ilDB->quote(0, "integer") . "," .
68  $ilDB->quote($a_user_id, "integer") . "," .
69  $ilDB->quote((int) $a_tref_id, "integer") . "," .
70  $ilDB->quote($now, "timestamp") . "," .
71  $ilDB->quote($a_skill_id, "integer") . "," .
72  $ilDB->quote(ilBasicSkill::ACHIEVED, "integer") . "," .
73  $ilDB->quote(1, "integer") . "," .
74  $ilDB->quote($a_trigger_ref_id, "integer") . "," .
75  $ilDB->quote($trigger_obj_id, "integer") . "," .
76  $ilDB->quote("", "text") . "," .
77  $ilDB->quote("", "text") . "," .
78  $ilDB->quote($a_self_eval, "integer") . "," .
79  $ilDB->quote("", "text") . "," .
80  $ilDB->quote(0.0, "float") .
81  ")");
82  }
83 
84  $ilDB->manipulate("DELETE FROM skl_user_has_level WHERE "
85  . " user_id = " . $ilDB->quote($a_user_id, "integer")
86  . " AND skill_id = " . $ilDB->quote($a_skill_id, "integer")
87  . " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer")
88  . " AND trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer")
89  . " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
90  );
91  }
92 
96  public function hasRecentSelfEvaluation(
97  int $trigger_obj_id,
98  int $a_user_id,
99  int $a_skill_id,
100  int $a_tref_id = 0,
101  int $a_trigger_ref_id = 0
102  ) {
103  $ilDB = $this->db;
104 
105  $recent = "";
106 
107  $ilDB->setLimit(1);
108  $set = $ilDB->query("SELECT * FROM skl_user_skill_level WHERE " .
109  "skill_id = " . $ilDB->quote($a_skill_id, "integer") . " AND " .
110  "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
111  "tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") . " AND " .
112  "trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer") . " AND " .
113  "self_eval = " . $ilDB->quote(1, "integer") .
114  " ORDER BY status_date DESC"
115  );
116  if ($rec = $ilDB->fetchAssoc($set)) {
117  $status_day = substr($rec["status_date"], 0, 10);
118  $today = substr(ilUtil::now(), 0, 10);
119  if ($rec["valid"] && $rec["status"] == ilBasicSkill::ACHIEVED && $status_day == $today) {
120  $recent = $rec["status_date"];
121  }
122  }
123 
124  return $recent;
125  }
126 
130  public function getNewAchievementsPerUser(
131  string $a_timestamp,
132  string $a_timestamp_to = null,
133  int $a_user_id = 0,
134  int $a_self_eval = 0
135  ) : array {
136  $ilDB = $this->db;
137 
138  $to = (!is_null($a_timestamp_to))
139  ? " AND status_date <= " . $ilDB->quote($a_timestamp_to, "timestamp")
140  : "";
141 
142  $user = ($a_user_id > 0)
143  ? " AND user_id = " . $ilDB->quote($a_user_id, "integer")
144  : "";
145 
146  $set = $ilDB->query("SELECT * FROM skl_user_skill_level " .
147  " WHERE status_date >= " . $ilDB->quote($a_timestamp, "timestamp") .
148  " AND valid = " . $ilDB->quote(1, "integer") .
149  " AND status = " . $ilDB->quote(ilBasicSkill::ACHIEVED, "integer") .
150  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer") .
151  $to .
152  $user .
153  " ORDER BY user_id, status_date ASC ");
154  $achievments = array();
155  while ($rec = $ilDB->fetchAssoc($set)) {
156  $achievments[$rec["user_id"]][] = $rec;
157  }
158 
159  return $achievments;
160  }
161 
165  public function writeUserSkillLevelStatus(
166  int $skill_id,
167  int $trigger_ref_id,
168  int $trigger_obj_id,
169  ?string $trigger_title,
170  ?string $trigger_type,
171  bool $update,
172  $status_date,
173  int $a_level_id,
174  int $a_user_id,
175  int $a_tref_id = 0,
176  bool $a_self_eval = false,
177  string $a_unique_identifier = "",
178  float $a_next_level_fulfilment = 0.0
179  ) {
180  $ilDB = $this->db;
181  $a_status = ilBasicSkill::ACHIEVED;
182 
183  if ($update) {
184  // this will only be set in self eval case, means this will always have a $rec
185  $now = ilUtil::now();
186  $ilDB->manipulate("UPDATE skl_user_skill_level SET " .
187  " level_id = " . $ilDB->quote($a_level_id, "integer") . "," .
188  " status_date = " . $ilDB->quote($now, "timestamp") . "," .
189  " next_level_fulfilment = " . $ilDB->quote((float) $a_next_level_fulfilment, "float") .
190  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
191  " AND status_date = " . $ilDB->quote($status_date, "timestamp") .
192  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
193  " AND status = " . $ilDB->quote($a_status, "integer") .
194  " AND trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer") .
195  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
196  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
197  );
198  } else {
199  if ($a_unique_identifier != "") {
200  $ilDB->manipulate("DELETE FROM skl_user_skill_level WHERE " .
201  " user_id = " . $ilDB->quote($a_user_id, "integer") .
202  " AND tref_id = " . $ilDB->quote($a_tref_id, "integer") .
203  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
204  " AND trigger_ref_id = " . $ilDB->quote($trigger_ref_id, "integer") .
205  " AND trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer") .
206  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer") .
207  " AND unique_identifier = " . $ilDB->quote($a_unique_identifier, "text")
208  );
209  }
210 
211  $now = ilUtil::now();
212  $ilDB->manipulate("INSERT INTO skl_user_skill_level " .
213  "(level_id, user_id, tref_id, status_date, skill_id, status, valid, trigger_ref_id," .
214  "trigger_obj_id, trigger_obj_type, trigger_title, self_eval, unique_identifier," .
215  "next_level_fulfilment) VALUES (" .
216  $ilDB->quote($a_level_id, "integer") . "," .
217  $ilDB->quote($a_user_id, "integer") . "," .
218  $ilDB->quote((int) $a_tref_id, "integer") . "," .
219  $ilDB->quote($now, "timestamp") . "," .
220  $ilDB->quote($skill_id, "integer") . "," .
221  $ilDB->quote($a_status, "integer") . "," .
222  $ilDB->quote(1, "integer") . "," .
223  $ilDB->quote($trigger_ref_id, "integer") . "," .
224  $ilDB->quote($trigger_obj_id, "integer") . "," .
225  $ilDB->quote($trigger_type, "text") . "," .
226  $ilDB->quote($trigger_title, "text") . "," .
227  $ilDB->quote($a_self_eval, "integer") . "," .
228  $ilDB->quote($a_unique_identifier, "text") . "," .
229  $ilDB->quote((float) $a_next_level_fulfilment, "float") .
230  ")");
231  }
232 
233  // fix (removed level_id and added skill id, since table should hold only
234  // one entry per skill)
235  $ilDB->manipulate("DELETE FROM skl_user_has_level WHERE "
236  . " user_id = " . $ilDB->quote($a_user_id, "integer")
237  . " AND skill_id = " . $ilDB->quote($skill_id, "integer")
238  . " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer")
239  . " AND trigger_obj_id = " . $ilDB->quote($trigger_obj_id, "integer")
240  . " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
241  );
242 
243  if ($a_status == ilBasicSkill::ACHIEVED) {
244  $ilDB->manipulate("INSERT INTO skl_user_has_level " .
245  "(level_id, user_id, tref_id, status_date, skill_id, trigger_ref_id, trigger_obj_id, trigger_obj_type," .
246  "trigger_title, self_eval, next_level_fulfilment) VALUES (" .
247  $ilDB->quote($a_level_id, "integer") . "," .
248  $ilDB->quote($a_user_id, "integer") . "," .
249  $ilDB->quote($a_tref_id, "integer") . "," .
250  $ilDB->quote($now, "timestamp") . "," .
251  $ilDB->quote($skill_id, "integer") . "," .
252  $ilDB->quote($trigger_ref_id, "integer") . "," .
253  $ilDB->quote($trigger_obj_id, "integer") . "," .
254  $ilDB->quote($trigger_type, "text") . "," .
255  $ilDB->quote($trigger_title, "text") . "," .
256  $ilDB->quote($a_self_eval, "integer") . "," .
257  $ilDB->quote((float) $a_next_level_fulfilment, "float") .
258  ")");
259  }
260  }
261 
266  int $a_user_id,
267  int $a_trigger_obj_id,
268  bool $a_self_eval = false,
269  string $a_unique_identifier = ""
270  ) : bool {
271  $ilDB = $this->db;
272 
273  $changed = false;
274 
275  $aff_rows = $ilDB->manipulate("DELETE FROM skl_user_skill_level WHERE "
276  . " user_id = " . $ilDB->quote($a_user_id, "integer")
277  . " AND trigger_obj_id = " . $ilDB->quote($a_trigger_obj_id, "integer")
278  . " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
279  . " AND unique_identifier = " . $ilDB->quote($a_unique_identifier, "text")
280  );
281  if ($aff_rows > 0) {
282  $changed = true;
283  }
284 
285  $aff_rows = $ilDB->manipulate("DELETE FROM skl_user_has_level WHERE "
286  . " user_id = " . $ilDB->quote($a_user_id, "integer")
287  . " AND trigger_obj_id = " . $ilDB->quote($a_trigger_obj_id, "integer")
288  . " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
289  );
290  if ($aff_rows > 0) {
291  $changed = true;
292  }
293  return $changed;
294  }
295 
299  public function removeAllUserData(int $a_user_id)
300  {
301  $ilDB = $this->db;
302 
303  $ilDB->manipulate("DELETE FROM skl_user_skill_level WHERE "
304  . " user_id = " . $ilDB->quote($a_user_id, "integer")
305  );
306  $ilDB->manipulate("DELETE FROM skl_user_has_level WHERE "
307  . " user_id = " . $ilDB->quote($a_user_id, "integer")
308  );
309  }
310 
314  public function getMaxLevelPerType(
315  int $skill_id,
316  array $levels,
317  int $a_tref_id,
318  string $a_type,
319  int $a_user_id = 0,
320  int $a_self_eval = 0
321  ) : int {
322  $ilDB = $this->db;
323 
324  $set = $ilDB->query($q = "SELECT level_id FROM skl_user_has_level " .
325  " WHERE trigger_obj_type = " . $ilDB->quote($a_type, "text") .
326  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
327  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
328  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
329  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
330  );
331 
332  $has_level = array();
333  while ($rec = $ilDB->fetchAssoc($set)) {
334  $has_level[$rec["level_id"]] = true;
335  }
336  $max_level = 0;
337  foreach ($levels as $l) {
338  if (isset($has_level[$l["id"]])) {
339  $max_level = $l["id"];
340  }
341  }
342  return $max_level;
343  }
344 
348  public function getAllLevelEntriesOfUser(
349  int $skill_id,
350  int $a_tref_id,
351  int $a_user_id = 0,
352  int $a_self_eval = 0
353  ) : array {
354  $ilDB = $this->db;
355 
356  $set = $ilDB->query($q = "SELECT * FROM skl_user_has_level " .
357  " WHERE skill_id = " . $ilDB->quote($skill_id, "integer") .
358  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
359  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
360  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer") .
361  " ORDER BY status_date DESC"
362  );
363 
364  $levels = array();
365  while ($rec = $ilDB->fetchAssoc($set)) {
366  $levels[] = $rec;
367  }
368  return $levels;
369  }
370 
375  int $skill_id,
376  int $a_tref_id,
377  int $a_user_id = 0,
378  int $a_eval_by = 0
379  ) : array {
380  $ilDB = $this->db;
381 
382  $by = ($a_eval_by != ilBasicSkill::EVAL_BY_ALL)
383  ? " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
384  : "";
385 
386  $set = $ilDB->query($q = "SELECT * FROM skl_user_skill_level " .
387  " WHERE skill_id = " . $ilDB->quote($skill_id, "integer") .
388  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
389  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
390  $by .
391  " ORDER BY status_date DESC"
392  );
393  $levels = array();
394  while ($rec = $ilDB->fetchAssoc($set)) {
395  $levels[] = $rec;
396  }
397  return $levels;
398  }
399 
403  public function getMaxLevelPerObject(
404  int $skill_id,
405  array $levels,
406  int $a_tref_id,
407  int $a_object_id,
408  int $a_user_id = 0,
409  int $a_self_eval = 0
410  ) : int {
411  $ilDB = $this->db;
412 
413  $set = $ilDB->query($q = "SELECT level_id FROM skl_user_has_level " .
414  " WHERE trigger_obj_id = " . $ilDB->quote($a_object_id, "integer") .
415  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
416  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
417  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
418  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
419  );
420 
421  $has_level = array();
422  while ($rec = $ilDB->fetchAssoc($set)) {
423  $has_level[$rec["level_id"]] = true;
424  }
425  $max_level = 0;
426  foreach ($levels as $l) {
427  if (isset($has_level[$l["id"]])) {
428  $max_level = $l["id"];
429  }
430  }
431  return $max_level;
432  }
433 
437  public function getMaxLevel(
438  int $skill_id,
439  array $levels,
440  int $a_tref_id,
441  int $a_user_id = 0,
442  int $a_self_eval = 0
443  ) : int {
444  $ilDB = $this->db;
445 
446  $set = $ilDB->query($q = "SELECT level_id FROM skl_user_has_level " .
447  " WHERE skill_id = " . $ilDB->quote($skill_id, "integer") .
448  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
449  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
450  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer")
451  );
452 
453  $has_level = array();
454  while ($rec = $ilDB->fetchAssoc($set)) {
455  $has_level[$rec["level_id"]] = true;
456  }
457  $max_level = 0;
458  foreach ($levels as $l) {
459  if (isset($has_level[$l["id"]])) {
460  $max_level = $l["id"];
461  }
462  }
463  return $max_level;
464  }
465 
469  public function hasSelfEvaluated(int $a_user_id, int $a_skill_id, int $a_tref_id) : bool
470  {
471  $ilDB = $this->db;
472 
473  $set = $ilDB->query($q = "SELECT level_id FROM skl_user_has_level " .
474  " WHERE skill_id = " . $ilDB->quote((int) $a_skill_id, "integer") .
475  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
476  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
477  " AND self_eval = " . $ilDB->quote(1, "integer")
478  );
479 
480  if ($rec = $ilDB->fetchAssoc($set)) {
481  return true;
482  }
483  return false;
484  }
485 
489  public function getLastLevelPerObject(
490  int $skill_id,
491  int $a_tref_id,
492  int $a_object_id,
493  int $a_user_id = 0,
494  int $a_self_eval = 0
495  ) : ?int {
496  $ilDB = $this->db;
497 
498  $ilDB->setLimit(1);
499  $set = $ilDB->query($q = "SELECT level_id FROM skl_user_has_level " .
500  " WHERE trigger_obj_id = " . $ilDB->quote($a_object_id, "integer") .
501  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
502  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
503  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
504  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer") .
505  " ORDER BY status_date DESC"
506  );
507 
508  $rec = $ilDB->fetchAssoc($set);
509 
510  return $rec["level_id"];
511  }
512 
516  public function getLastUpdatePerObject(
517  int $skill_id,
518  int $a_tref_id,
519  int $a_object_id,
520  int $a_user_id = 0,
521  int $a_self_eval = 0
522  ) : ?string {
523  $ilDB = $this->db;
524 
525  $ilDB->setLimit(1);
526  $set = $ilDB->query($q = "SELECT status_date FROM skl_user_has_level " .
527  " WHERE trigger_obj_id = " . $ilDB->quote($a_object_id, "integer") .
528  " AND skill_id = " . $ilDB->quote($skill_id, "integer") .
529  " AND tref_id = " . $ilDB->quote((int) $a_tref_id, "integer") .
530  " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
531  " AND self_eval = " . $ilDB->quote($a_self_eval, "integer") .
532  " ORDER BY status_date DESC"
533  );
534 
535  $rec = $ilDB->fetchAssoc($set);
536 
537  return $rec["status_date"];
538  }
539 }
getLastLevelPerObject(int $skill_id, int $a_tref_id, int $a_object_id, int $a_user_id=0, int $a_self_eval=0)
hasSelfEvaluated(int $a_user_id, int $a_skill_id, int $a_tref_id)
getMaxLevelPerObject(int $skill_id, array $levels, int $a_tref_id, int $a_object_id, int $a_user_id=0, int $a_self_eval=0)
resetUserSkillLevelStatus(bool $update, int $trigger_obj_id, $status_date, int $a_user_id, int $a_skill_id, int $a_tref_id=0, int $a_trigger_ref_id=0, bool $a_self_eval=false)
static now()
Return current timestamp in Y-m-d H:i:s format.
getAllLevelEntriesOfUser(int $skill_id, int $a_tref_id, int $a_user_id=0, int $a_self_eval=0)
hasRecentSelfEvaluation(int $trigger_obj_id, int $a_user_id, int $a_skill_id, int $a_tref_id=0, int $a_trigger_ref_id=0)
getLastUpdatePerObject(int $skill_id, int $a_tref_id, int $a_object_id, int $a_user_id=0, int $a_self_eval=0)
global $DIC
Definition: goto.php:24
getNewAchievementsPerUser(string $a_timestamp, string $a_timestamp_to=null, int $a_user_id=0, int $a_self_eval=0)
getMaxLevelPerType(int $skill_id, array $levels, int $a_tref_id, string $a_type, int $a_user_id=0, int $a_self_eval=0)
getMaxLevel(int $skill_id, array $levels, int $a_tref_id, int $a_user_id=0, int $a_self_eval=0)
Interface ilBasicSkillUserLevelRepository.
global $ilDB
getAllHistoricLevelEntriesOfUser(int $skill_id, int $a_tref_id, int $a_user_id=0, int $a_eval_by=0)
removeAllUserSkillLevelStatusOfObject(int $a_user_id, int $a_trigger_obj_id, bool $a_self_eval=false, string $a_unique_identifier="")
writeUserSkillLevelStatus(int $skill_id, int $trigger_ref_id, int $trigger_obj_id, ?string $trigger_title, ?string $trigger_type, bool $update, $status_date, int $a_level_id, int $a_user_id, int $a_tref_id=0, bool $a_self_eval=false, string $a_unique_identifier="", float $a_next_level_fulfilment=0.0)