ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSkillLevelDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 {
27  protected ilDBInterface $db;
29 
30  public function __construct(ilSkillTreeRepository $tree_repo, ilDBInterface $db = null)
31  {
32  global $DIC;
33 
34  $this->tree_repo = $tree_repo;
35  $this->db = ($db)
36  ?: $DIC->database();
37  }
38 
39  public function deleteLevelsOfSkill(int $skill_id): void
40  {
41  $ilDB = $this->db;
42 
43  $ilDB->manipulate(
44  "DELETE FROM skl_level WHERE "
45  . " skill_id = " . $ilDB->quote($skill_id, "integer")
46  );
47  }
48 
49  public function addLevel(int $skill_id, string $a_title, string $a_description, string $a_import_id = ""): void
50  {
51  $ilDB = $this->db;
52 
53  $nr = $this->getMaxLevelNr($skill_id);
54  $nid = $ilDB->nextId("skl_level");
55  $ilDB->insert("skl_level", array(
56  "id" => array("integer", $nid),
57  "skill_id" => array("integer", $skill_id),
58  "nr" => array("integer", $nr + 1),
59  "title" => array("text", $a_title),
60  "description" => array("clob", $a_description),
61  "import_id" => array("text", $a_import_id),
62  "creation_date" => array("timestamp", ilUtil::now())
63  ));
64  }
65 
66  protected function getMaxLevelNr(int $skill_id): int
67  {
68  $ilDB = $this->db;
69 
70  $set = $ilDB->query(
71  "SELECT MAX(nr) mnr FROM skl_level WHERE " .
72  " skill_id = " . $ilDB->quote($skill_id, "integer")
73  );
74  $rec = $ilDB->fetchAssoc($set);
75  return (int) ($rec["mnr"] ?? 0);
76  }
77 
81  public function getLevelData(int $skill_id, int $a_id = 0): array
82  {
83  $ilDB = $this->db;
84 
85  $and = "";
86  if ($a_id > 0) {
87  $and = " AND id = " . $ilDB->quote($a_id, "integer");
88  }
89 
90  $set = $ilDB->query(
91  "SELECT * FROM skl_level WHERE " .
92  " skill_id = " . $ilDB->quote($skill_id, "integer") .
93  $and .
94  " ORDER BY nr"
95  );
96  $levels = [];
97  while ($rec = $ilDB->fetchAssoc($set)) {
98  if ($a_id > 0) {
99  return $rec ?? [];
100  }
101  $levels[] = $rec;
102  }
103  return $levels;
104  }
105 
106  protected function lookupLevelProperty(int $a_id, string $a_prop): ?string
107  {
108  $ilDB = $this->db;
109 
110  $set = $ilDB->query(
111  "SELECT $a_prop FROM skl_level WHERE " .
112  " id = " . $ilDB->quote($a_id, "integer")
113  );
114  $rec = $ilDB->fetchAssoc($set);
115 
116  return isset($rec[$a_prop]) ? (string) $rec[$a_prop] : null;
117  }
118 
119  public function lookupLevelTitle(int $a_id): string
120  {
121  return $this->lookupLevelProperty($a_id, "title") ?? "";
122  }
123 
124  public function lookupLevelDescription(int $a_id): string
125  {
126  return $this->lookupLevelProperty($a_id, "description") ?? "";
127  }
128 
129  public function lookupLevelSkillId(int $a_id): int
130  {
131  return (int) ($this->lookupLevelProperty($a_id, "skill_id") ?? 0);
132  }
133 
134  public function lookupLevelNumber(int $a_id): int
135  {
136  return (int) ($this->lookupLevelProperty($a_id, "nr") ?? 0);
137  }
138 
139  protected function writeLevelProperty(int $a_id, string $a_prop, ?string $a_value, string $a_type): void
140  {
141  $ilDB = $this->db;
142 
143  $ilDB->update("skl_level", array(
144  $a_prop => array($a_type, $a_value),
145  ), array(
146  "id" => array("integer", $a_id),
147  ));
148  }
149 
150  public function writeLevelTitle(int $a_id, string $a_title): void
151  {
152  $this->writeLevelProperty($a_id, "title", $a_title, "text");
153  }
154 
155  public function writeLevelDescription(int $a_id, string $a_description): void
156  {
157  $this->writeLevelProperty($a_id, "description", $a_description, "clob");
158  }
159 
160  public function updateLevelOrder(array $order): void
161  {
162  $ilDB = $this->db;
163 
164  $cnt = 1;
165  foreach ($order as $id => $o) {
166  $ilDB->manipulate(
167  "UPDATE skl_level SET " .
168  " nr = " . $ilDB->quote($cnt, "integer") .
169  " WHERE id = " . $ilDB->quote($id, "integer")
170  );
171  $cnt++;
172  }
173  }
174 
175  public function deleteLevel(int $a_id): void
176  {
177  $ilDB = $this->db;
178 
179  $ilDB->manipulate(
180  "DELETE FROM skl_level WHERE "
181  . " id = " . $ilDB->quote($a_id, "integer")
182  );
183  }
184 
185  public function fixLevelNumbering(int $skill_id): void
186  {
187  $ilDB = $this->db;
188 
189  $set = $ilDB->query(
190  "SELECT id, nr FROM skl_level WHERE " .
191  " skill_id = " . $ilDB->quote($skill_id, "integer") .
192  " ORDER BY nr ASC"
193  );
194  $cnt = 1;
195  while ($rec = $ilDB->fetchAssoc($set)) {
196  $ilDB->manipulate(
197  "UPDATE skl_level SET " .
198  " nr = " . $ilDB->quote($cnt, "integer") .
199  " WHERE id = " . $ilDB->quote($rec["id"], "integer")
200  );
201  $cnt++;
202  }
203  }
204 
205  public function getSkillForLevelId(int $a_level_id): ?ilBasicSkill
206  {
207  $ilDB = $this->db;
208 
209  $set = $ilDB->query(
210  "SELECT * FROM skl_level WHERE " .
211  " id = " . $ilDB->quote($a_level_id, "integer")
212  );
213  $skill = null;
214  if ($rec = $ilDB->fetchAssoc($set)) {
215  if ($this->tree_repo->isInAnyTree((int) $rec["skill_id"])) {
216  $skill = new ilBasicSkill((int) $rec["skill_id"]);
217  }
218  }
219  return $skill;
220  }
221 }
writeLevelDescription(int $a_id, string $a_description)
writeLevelProperty(int $a_id, string $a_prop, ?string $a_value, string $a_type)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static now()
Return current timestamp in Y-m-d H:i:s format.
writeLevelTitle(int $a_id, string $a_title)
__construct(ilSkillTreeRepository $tree_repo, ilDBInterface $db=null)
global $DIC
Definition: feed.php:28
addLevel(int $skill_id, string $a_title, string $a_description, string $a_import_id="")
lookupLevelProperty(int $a_id, string $a_prop)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Basic Skill.
getLevelData(int $skill_id, int $a_id=0)
Returns multiple rows when $a_id is 0 else one or [].