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