ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.SkillProfileDBRepository.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 
41  public function get(int $profile_id): SkillProfile
42  {
43  $ilDB = $this->db;
44 
45  $set = $ilDB->query(
46  "SELECT * FROM skl_profile " .
47  " WHERE id = " . $ilDB->quote($profile_id, "integer")
48  );
49 
50  if ($rec = $ilDB->fetchAssoc($set)) {
51  return $this->getFromRecord($rec);
52  }
53  throw new \ilSkillProfileNotFoundException("Profile with ID $profile_id not found.");
54  }
55 
56  protected function getFromRecord(array $rec): SkillProfile
57  {
58  $rec["id"] = (int) $rec["id"];
59  $rec["title"] = (string) $rec["title"];
60  $rec["description"] = (string) $rec["description"];
61  $rec["skill_tree_id"] = (int) $rec["skill_tree_id"];
62  $rec["image_id"] = (string) $rec["image_id"];
63  $rec["ref_id"] = (int) $rec["ref_id"];
64 
65  return $this->factory_service->profile()->profile(
66  $rec["id"],
67  $rec["title"],
68  $rec["description"],
69  $rec["skill_tree_id"],
70  $rec["image_id"],
71  $rec["ref_id"]
72  );
73  }
74 
75  public function getNextId(): int
76  {
77  $ilDB = $this->db;
78 
79  $next_id = $ilDB->nextId("skl_profile");
80  return $next_id;
81  }
82 
83  public function createProfile(
84  SkillProfile $profile
85  ): SkillProfile {
86  $ilDB = $this->db;
87 
88  $new_profile_id = $this->getNextId();
89  $ilDB->manipulate("INSERT INTO skl_profile " .
90  "(id, title, description, skill_tree_id, image_id, ref_id) VALUES (" .
91  $ilDB->quote($new_profile_id, "integer") . "," .
92  $ilDB->quote($profile->getTitle(), "text") . "," .
93  $ilDB->quote($profile->getDescription(), "text") . "," .
94  $ilDB->quote($profile->getSkillTreeId(), "integer") . "," .
95  $ilDB->quote($profile->getImageId(), "text") . "," .
96  $ilDB->quote($profile->getRefId(), "integer") .
97  ")");
98 
99  return $this->get($new_profile_id);
100  }
101 
102  public function updateProfile(
103  SkillProfile $profile
104  ): SkillProfile {
105  $ilDB = $this->db;
106 
107  // profile
108  $ilDB->manipulate(
109  "UPDATE skl_profile SET " .
110  " title = " . $ilDB->quote($profile->getTitle(), "text") . "," .
111  " description = " . $ilDB->quote($profile->getDescription(), "text") . "," .
112  " image_id = " . $ilDB->quote($profile->getImageId(), "text") .
113  " WHERE id = " . $ilDB->quote($profile->getId(), "integer") .
114  " AND ref_id = " . $ilDB->quote($profile->getRefId(), "integer")
115  );
116 
117  return $this->get($profile->getId());
118  }
119 
120  public function deleteProfile(int $profile_id): void
121  {
122  $ilDB = $this->db;
123 
124  $ilDB->manipulate(
125  "DELETE FROM skl_profile WHERE " .
126  " id = " . $ilDB->quote($profile_id, "integer")
127  );
128  }
129 
130  public function deleteProfilesFromObject(int $ref_id): void
131  {
132  $ilDB = $this->db;
133 
134  $ilDB->manipulate(
135  "DELETE FROM skl_profile WHERE " .
136  " ref_id = " . $ilDB->quote($ref_id, "integer")
137  );
138  }
139 
143  public function getProfilesForAllSkillTrees(): array
144  {
145  $ilDB = $this->db;
146 
147  $set = $ilDB->query(
148  "SELECT * FROM skl_profile " .
149  " ORDER BY title "
150  );
151  $profiles = [];
152  while ($rec = $ilDB->fetchAssoc($set)) {
153  $profiles[] = $this->getFromRecord($rec);
154  }
155 
156  return $profiles;
157  }
158 
162  public function getProfilesForSkillTree(int $skill_tree_id): array
163  {
164  $ilDB = $this->db;
165 
166  $set = $ilDB->query(
167  "SELECT * FROM skl_profile " .
168  " WHERE skill_tree_id = " . $ilDB->quote($skill_tree_id, "integer") .
169  " ORDER BY title "
170  );
171  $profiles = [];
172  while ($rec = $ilDB->fetchAssoc($set)) {
173  $profiles[] = $this->getFromRecord($rec);
174  }
175 
176  return $profiles;
177  }
178 
182  public function getAllGlobalProfiles(): array
183  {
184  $ilDB = $this->db;
185 
186  $set = $ilDB->query(
187  "SELECT * FROM skl_profile " .
188  " WHERE ref_id = 0 " .
189  " ORDER BY title "
190  );
191  $profiles = [];
192  while ($rec = $ilDB->fetchAssoc($set)) {
193  $profiles[] = $this->getFromRecord($rec);
194  }
195 
196  return $profiles;
197  }
198 
202  public function getLocalProfilesForObject(int $ref_id): array
203  {
204  $ilDB = $this->db;
205 
206  $set = $ilDB->query(
207  "SELECT * FROM skl_profile " .
208  " WHERE ref_id = " . $ref_id .
209  " ORDER BY title "
210  );
211  $profiles = [];
212  while ($rec = $ilDB->fetchAssoc($set)) {
213  $profiles[] = $this->getFromRecord($rec);
214  }
215 
216  return $profiles;
217  }
218 
219  public function lookup(int $id, string $field): string
220  {
221  $ilDB = $this->db;
222 
223  $set = $ilDB->query(
224  "SELECT " . $field . " FROM skl_profile " .
225  " WHERE id = " . $ilDB->quote($id, "integer")
226  );
227  $rec = $ilDB->fetchAssoc($set);
228 
229  return (string) ($rec[$field] ?? "");
230  }
231 
232  public function updateRefIdAfterImport(int $profile_id, int $new_ref_id): void
233  {
234  $ilDB = $this->db;
235 
236  $ilDB->update(
237  "skl_profile",
238  array(
239  "ref_id" => array("integer", $new_ref_id)
240  ),
241  array(
242  "id" => array("integer", $profile_id)
243  )
244  );
245  }
246 
247  public function getTreeId(int $profile_id): int
248  {
249  $db = $this->db;
250 
251  $set = $db->queryF(
252  "SELECT * FROM skl_profile " .
253  " WHERE id = %s ",
254  ["integer"],
255  [$profile_id]
256  );
257  $rec = $db->fetchAssoc($set);
258  return (int) ($rec["skill_tree_id"] ?? 0);
259  }
260 }
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)