ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.SkillUsageDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 
25 {
26  protected \ilDBInterface $db;
27 
28  public function __construct(
29  \ilDBInterface $db = null
30  ) {
31  global $DIC;
32 
33  $this->db = ($db) ?: $DIC->database();
34  }
35 
36  public function add(int $obj_id, int $skill_id, int $tref_id): void
37  {
38  $this->db->replace(
39  "skl_usage",
40  [
41  "obj_id" => ["integer", $obj_id],
42  "skill_id" => ["integer", $skill_id],
43  "tref_id" => ["integer", $tref_id]
44  ],
45  []
46  );
47  }
48 
49  public function remove(int $obj_id, int $skill_id, int $tref_id): void
50  {
51  $this->db->manipulate(
52  "DELETE FROM skl_usage WHERE " .
53  " obj_id = " . $this->db->quote($obj_id, "integer") .
54  " AND skill_id = " . $this->db->quote($skill_id, "integer") .
55  " AND tref_id = " . $this->db->quote($tref_id, "integer")
56  );
57  }
58 
59  public function removeFromObject(int $obj_id): void
60  {
61  $this->db->manipulate(
62  "DELETE FROM skl_usage WHERE " .
63  " obj_id = " . $this->db->quote($obj_id, "integer")
64  );
65  }
66 
67  public function removeForSkill(int $node_id, bool $is_referenece = false): void
68  {
69  if (!$is_referenece) {
70  $this->db->manipulate(
71  "DELETE FROM skl_usage WHERE " .
72  " skill_id = " . $this->db->quote($node_id, "integer")
73  );
74  } else {
75  $this->db->manipulate(
76  "DELETE FROM skl_usage WHERE " .
77  " tref_id = " . $this->db->quote($node_id, "integer")
78  );
79  }
80  }
81 
85  public function getUsages(int $skill_id, int $tref_id): array
86  {
87  $set = $this->db->query(
88  "SELECT obj_id FROM skl_usage " .
89  " WHERE skill_id = " . $this->db->quote($skill_id, "integer") .
90  " AND tref_id = " . $this->db->quote($tref_id, "integer")
91  );
92  $obj_ids = [];
93  while ($rec = $this->db->fetchAssoc($set)) {
94  $obj_ids[] = (int) $rec["obj_id"];
95  }
96 
97  return $obj_ids;
98  }
99 
106  public function getUsageInfoGeneric(
107  array $cskill_ids,
108  string $usage_type,
109  string $table,
110  string $key_field,
111  string $skill_field = "skill_id",
112  string $tref_field = "tref_id"
113  ): array {
114  $usages = [];
115 
116  $w = "WHERE";
117  $q = "SELECT " . $key_field . ", " . $skill_field . ", " . $tref_field . " FROM " . $table . " ";
118  foreach ($cskill_ids as $sk) {
119  $q .= $w . " (" . $skill_field . " = " . $this->db->quote($sk["skill_id"], "integer") .
120  " AND " . $tref_field . " = " . $this->db->quote($sk["tref_id"], "integer") . ") ";
121  $w = "OR";
122  }
123  $q .= " GROUP BY " . $key_field . ", " . $skill_field . ", " . $tref_field;
124 
125  $set = $this->db->query($q);
126  while ($rec = $this->db->fetchAssoc($set)) {
127  $usages[$rec[$skill_field] . ":" . $rec[$tref_field]][$usage_type][] =
128  array("key" => $rec[$key_field]);
129  }
130 
131  return $usages;
132  }
133 }
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
add(int $obj_id, int $skill_id, int $tref_id)
getUsageInfoGeneric(array $cskill_ids, string $usage_type, string $table, string $key_field, string $skill_field="skill_id", string $tref_field="tref_id")
Get standard usage query.
removeForSkill(int $node_id, bool $is_referenece=false)
$q
Definition: shib_logout.php:21