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