ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.SkillProfileDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 namespace ILIAS\Skill\Profile;
21 
23 
25 {
26  protected \ilDBInterface $db;
28 
29  public function __construct(\ilDBInterface $db = null, Service\SkillInternalFactoryService $factory_service = null)
30  {
31  global $DIC;
32 
33  $this->db = ($db) ?: $DIC->database();
34  $this->factory_service = ($factory_service) ?: $DIC->skills()->internal()->factory();
35  }
36 
37  public function getById(int $profile_id): SkillProfile
38  {
39  $ilDB = $this->db;
40 
41  $set = $ilDB->query(
42  "SELECT * FROM skl_profile " .
43  " WHERE id = " . $ilDB->quote($profile_id, "integer")
44  );
45 
46  if ($rec = $ilDB->fetchAssoc($set)) {
47  return $this->getProfileFromRecord($rec);
48  }
49  throw new \ilSkillProfileNotFoundException("Profile with ID $profile_id not found.");
50  }
51 
52  protected function getProfileFromRecord(array $rec): SkillProfile
53  {
54  $rec["id"] = (int) $rec["id"];
55  $rec["title"] = (string) $rec["title"];
56  $rec["description"] = (string) $rec["description"];
57  $rec["skill_tree_id"] = (int) $rec["skill_tree_id"];
58  $rec["image_id"] = (string) $rec["image_id"];
59  $rec["ref_id"] = (int) $rec["ref_id"];
60 
61  return $this->factory_service->profile(
62  $rec["id"],
63  $rec["title"],
64  $rec["description"],
65  $rec["skill_tree_id"],
66  $rec["image_id"],
67  $rec["ref_id"]
68  );
69  }
70 
71  public function getNextId(): int
72  {
73  $ilDB = $this->db;
74 
75  $next_id = $ilDB->nextId("skl_profile");
76  return $next_id;
77  }
78 
79  public function createProfile(
80  SkillProfile $profile
81  ): SkillProfile {
82  $ilDB = $this->db;
83 
84  $new_profile_id = $this->getNextId();
85  $ilDB->manipulate("INSERT INTO skl_profile " .
86  "(id, title, description, skill_tree_id, image_id, ref_id) VALUES (" .
87  $ilDB->quote($new_profile_id, "integer") . "," .
88  $ilDB->quote($profile->getTitle(), "text") . "," .
89  $ilDB->quote($profile->getDescription(), "text") . "," .
90  $ilDB->quote($profile->getSkillTreeId(), "integer") . "," .
91  $ilDB->quote($profile->getImageId(), "text") . "," .
92  $ilDB->quote($profile->getRefId(), "integer") .
93  ")");
94 
95  return $this->getById($new_profile_id);
96  }
97 
98  public function updateProfile(
99  SkillProfile $profile
100  ): SkillProfile {
101  $ilDB = $this->db;
102 
103  // profile
104  $ilDB->manipulate(
105  "UPDATE skl_profile SET " .
106  " title = " . $ilDB->quote($profile->getTitle(), "text") . "," .
107  " description = " . $ilDB->quote($profile->getDescription(), "text") . "," .
108  " image_id = " . $ilDB->quote($profile->getImageId(), "text") .
109  " WHERE id = " . $ilDB->quote($profile->getId(), "integer") .
110  " AND ref_id = " . $ilDB->quote($profile->getRefId(), "integer")
111  );
112 
113  return $this->getById($profile->getId());
114  }
115 
116  public function deleteProfile(int $profile_id): void
117  {
118  $ilDB = $this->db;
119 
120  $ilDB->manipulate(
121  "DELETE FROM skl_profile WHERE " .
122  " id = " . $ilDB->quote($profile_id, "integer")
123  );
124  }
125 
126  public function deleteProfilesFromObject(int $ref_id): void
127  {
128  $ilDB = $this->db;
129 
130  $ilDB->manipulate(
131  "DELETE FROM skl_profile WHERE " .
132  " ref_id = " . $ilDB->quote($ref_id, "integer")
133  );
134  }
135 
136  public function getProfilesForAllSkillTrees(): array
137  {
138  $ilDB = $this->db;
139 
140  $set = $ilDB->query(
141  "SELECT * FROM skl_profile " .
142  " ORDER BY title "
143  );
144  $profiles = [];
145  while ($rec = $ilDB->fetchAssoc($set)) {
146  $profiles[$rec["id"]] = $rec;
147  }
148 
149  return $profiles;
150  }
151 
152  public function getProfilesForSkillTree(int $skill_tree_id): array
153  {
154  $ilDB = $this->db;
155 
156  $set = $ilDB->query(
157  "SELECT * FROM skl_profile " .
158  " WHERE skill_tree_id = " . $ilDB->quote($skill_tree_id, "integer") .
159  " ORDER BY title "
160  );
161  $profiles = [];
162  while ($rec = $ilDB->fetchAssoc($set)) {
163  $profiles[$rec["id"]] = $rec;
164  }
165 
166  return $profiles;
167  }
168 
169  public function getAllGlobalProfiles(): array
170  {
171  $ilDB = $this->db;
172 
173  $set = $ilDB->query(
174  "SELECT * FROM skl_profile " .
175  " WHERE ref_id = 0 " .
176  " ORDER BY title "
177  );
178  $profiles = [];
179  while ($rec = $ilDB->fetchAssoc($set)) {
180  $profiles[$rec["id"]] = $rec;
181  }
182 
183  return $profiles;
184  }
185 
186  public function getLocalProfilesForObject(int $ref_id): array
187  {
188  $ilDB = $this->db;
189 
190  $set = $ilDB->query(
191  "SELECT * FROM skl_profile " .
192  " WHERE ref_id = " . $ref_id .
193  " ORDER BY title "
194  );
195  $profiles = [];
196  while ($rec = $ilDB->fetchAssoc($set)) {
197  $profiles[$rec["id"]] = $rec;
198  }
199 
200  return $profiles;
201  }
202 
203  public function lookup(int $id, string $field): string
204  {
205  $ilDB = $this->db;
206 
207  $set = $ilDB->query(
208  "SELECT " . $field . " FROM skl_profile " .
209  " WHERE id = " . $ilDB->quote($id, "integer")
210  );
211  $rec = $ilDB->fetchAssoc($set);
212 
213  return (string) ($rec[$field] ?? "");
214  }
215 
216  public function updateRefIdAfterImport(int $profile_id, int $new_ref_id): void
217  {
218  $ilDB = $this->db;
219 
220  $ilDB->update(
221  "skl_profile",
222  array(
223  "ref_id" => array("integer", $new_ref_id)
224  ),
225  array(
226  "id" => array("integer", $profile_id)
227  )
228  );
229  }
230 
231  public function getTreeId(int $profile_id): int
232  {
233  $db = $this->db;
234 
235  $set = $db->queryF(
236  "SELECT * FROM skl_profile " .
237  " WHERE id = %s ",
238  ["integer"],
239  [$profile_id]
240  );
241  $rec = $db->fetchAssoc($set);
242  return (int) ($rec["skill_tree_id"] ?? 0);
243  }
244 }
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
queryF(string $query, array $types, array $values)
$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...
__construct(\ilDBInterface $db=null, Service\SkillInternalFactoryService $factory_service=null)