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