ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.SkillProfileLevelsDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 namespace ILIAS\Skill\Profile;
23 
25 
27 {
28  protected \ilDBInterface $db;
30 
31  public function __construct(
32  \ilDBInterface $db = null,
33  Service\SkillInternalFactoryService $factory_service = null
34  ) {
35  global $DIC;
36 
37  $this->db = ($db) ?: $DIC->database();
38  $this->factory_service = ($factory_service) ?: $DIC->skills()->internal()->factory();
39  }
40 
44  public function getAll(int $profile_id): array
45  {
46  $ilDB = $this->db;
47 
48  $set = $ilDB->query(
49  "SELECT * FROM skl_profile_level " .
50  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
51  );
52 
53  $levels = [];
54  while ($rec = $ilDB->fetchAssoc($set)) {
55  $levels[] = $this->getFromRecord($rec);
56  }
57 
58  return $levels;
59  }
60 
61  public function get(int $profile_id, int $base_skill_id, int $tref_id): SkillProfileLevel
62  {
63  $ilDB = $this->db;
64 
65  $set = $ilDB->query(
66  "SELECT * FROM skl_profile_level " .
67  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer") .
68  " AND base_skill_id = " . $ilDB->quote($base_skill_id, "integer") .
69  " AND tref_id = " . $ilDB->quote($tref_id, "integer")
70  );
71 
72  $level = [];
73  if ($rec = $ilDB->fetchAssoc($set)) {
74  $level = $this->getFromRecord($rec);
75  }
76 
77  return $level;
78  }
79 
80  protected function getFromRecord(array $rec): SkillProfileLevel
81  {
82  $rec["profile_id"] = (int) $rec["profile_id"];
83  $rec["base_skill_id"] = (int) $rec["base_skill_id"];
84  $rec["tref_id"] = (int) $rec["tref_id"];
85  $rec["level_id"] = (int) $rec["level_id"];
86  $rec["order_nr"] = (int) $rec["order_nr"];
87 
88  return $this->factory_service->profile()->profileLevel(
89  $rec["profile_id"],
90  $rec["base_skill_id"],
91  $rec["tref_id"],
92  $rec["level_id"],
93  $rec["order_nr"]
94  );
95  }
96 
97  public function createOrUpdate(SkillProfileLevel $skill_level_obj): void
98  {
99  $ilDB = $this->db;
100 
101  $ilDB->replace(
102  "skl_profile_level",
103  array("profile_id" => array("integer", $skill_level_obj->getProfileId()),
104  "tref_id" => array("integer", $skill_level_obj->getTrefId()),
105  "base_skill_id" => array("integer", $skill_level_obj->getBaseSkillId())
106  ),
107  array("order_nr" => array("integer", $skill_level_obj->getOrderNr()),
108  "level_id" => array("integer", $skill_level_obj->getLevelId())
109  )
110  );
111  }
112 
113  public function delete(SkillProfileLevel $skill_level_obj): void
114  {
115  $ilDB = $this->db;
116 
117  $ilDB->manipulate(
118  "DELETE FROM skl_profile_level WHERE " .
119  " base_skill_id = " . $ilDB->quote($skill_level_obj->getBaseSkillId(), "integer") .
120  " AND tref_id = " . $ilDB->quote($skill_level_obj->getTrefId(), "integer") .
121  " AND level_id = " . $ilDB->quote($skill_level_obj->getLevelId(), "integer") .
122  " AND order_nr = " . $ilDB->quote($skill_level_obj->getOrderNr(), "integer")
123  );
124  }
125 
126  public function deleteAllForProfile(int $profile_id): void
127  {
128  $ilDB = $this->db;
129 
130  $ilDB->manipulate(
131  "DELETE FROM skl_profile_level WHERE " .
132  " profile_id = " . $ilDB->quote($profile_id, "integer")
133  );
134  }
135 
136  public function deleteAllForSkill(int $skill_node_id, bool $is_reference): void
137  {
138  $ilDB = $this->db;
139 
140  if (!$is_reference) {
141  $ilDB->manipulate(
142  "DELETE FROM skl_profile_level WHERE " .
143  " base_skill_id = " . $ilDB->quote($skill_node_id, "integer")
144  );
145  } else {
146  $ilDB->manipulate(
147  "DELETE FROM skl_profile_level WHERE " .
148  " tref_id = " . $ilDB->quote($skill_node_id, "integer")
149  );
150  }
151  }
152 
153  public function updateSkillOrder(int $profile_id, array $order): void
154  {
155  $ilDB = $this->db;
156 
157  $cnt = 1;
158  foreach ($order as $id => $o) {
159  $id_arr = explode("_", $id);
160  $ilDB->manipulate(
161  "UPDATE skl_profile_level SET " .
162  " order_nr = " . $ilDB->quote(($cnt * 10), "integer") .
163  " WHERE base_skill_id = " . $ilDB->quote($id_arr[0], "integer") .
164  " AND tref_id = " . $ilDB->quote($id_arr[1], "integer") .
165  " AND profile_id = " . $ilDB->quote($profile_id, "integer")
166  );
167  $cnt++;
168  }
169  }
170 
171  public function fixSkillOrderNumbering(int $profile_id): void
172  {
173  $ilDB = $this->db;
174 
175  $set = $ilDB->query(
176  "SELECT profile_id, base_skill_id, tref_id, order_nr FROM skl_profile_level WHERE " .
177  " profile_id = " . $ilDB->quote($profile_id, "integer") .
178  " ORDER BY order_nr ASC"
179  );
180  $cnt = 1;
181  while ($rec = $ilDB->fetchAssoc($set)) {
182  $ilDB->manipulate(
183  "UPDATE skl_profile_level SET " .
184  " order_nr = " . $ilDB->quote(($cnt * 10), "integer") .
185  " WHERE profile_id = " . $ilDB->quote($rec["profile_id"], "integer") .
186  " AND base_skill_id = " . $ilDB->quote($rec["base_skill_id"], "integer") .
187  " AND tref_id = " . $ilDB->quote($rec["tref_id"], "integer")
188  );
189  $cnt++;
190  }
191  }
192 
193  public function getMaxOrderNr(int $profile_id): int
194  {
195  $ilDB = $this->db;
196 
197  $set = $ilDB->query(
198  "SELECT MAX(order_nr) mnr FROM skl_profile_level WHERE " .
199  " profile_id = " . $ilDB->quote($profile_id, "integer")
200  );
201  $rec = $ilDB->fetchAssoc($set);
202  return (int) $rec["mnr"];
203  }
204 }
__construct(\ilDBInterface $db=null, Service\SkillInternalFactoryService $factory_service=null)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$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...