ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.SkillProfileRoleDBRepository.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 deleteProfileRoles(int $profile_id): void
42  {
43  $ilDB = $this->db;
44 
45  $ilDB->manipulate(
46  "DELETE FROM skl_profile_role WHERE " .
47  " profile_id = " . $ilDB->quote($profile_id, "integer")
48  );
49  }
50 
51  public function get(int $profile_id): array
52  {
53  $ilDB = $this->db;
54 
55  $set = $ilDB->query(
56  "SELECT * FROM skl_profile_role " .
57  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
58  );
59  $roles = [];
60  while ($rec = $ilDB->fetchAssoc($set)) {
61  $roles[] = $rec;
62  }
63 
64  return $roles;
65  }
66 
68  {
69  return $this->factory_service->profile()->profileRoleAssignment(
70  $rec["name"],
71  $rec["id"],
72  $rec["object_title"],
73  $rec["object_type"],
74  $rec["object_id"]
75  );
76  }
77 
78  public function addRoleToProfile(int $profile_id, int $role_id): void
79  {
80  $ilDB = $this->db;
81 
82  $ilDB->replace(
83  "skl_profile_role",
84  array("profile_id" => array("integer", $profile_id),
85  "role_id" => array("integer", $role_id),
86  ),
87  []
88  );
89  }
90 
91  public function removeRoleFromProfile(int $profile_id, int $role_id): void
92  {
93  $ilDB = $this->db;
94 
95  $ilDB->manipulate(
96  "DELETE FROM skl_profile_role WHERE " .
97  " profile_id = " . $ilDB->quote($profile_id, "integer") .
98  " AND role_id = " . $ilDB->quote($role_id, "integer")
99  );
100  }
101 
102  public function removeRoleFromAllProfiles(int $role_id): void
103  {
104  $ilDB = $this->db;
105 
106  $ilDB->manipulate(
107  "DELETE FROM skl_profile_role WHERE " .
108  " role_id = " . $ilDB->quote($role_id, "integer")
109  );
110  }
111 
115  public function getAllProfilesOfRole(int $role_id): array
116  {
117  $ilDB = $this->db;
118 
119  $profiles = [];
120  $set = $ilDB->query(
121  "SELECT spr.profile_id, spr.role_id, sp.title, sp.description, sp.ref_id, sp.skill_tree_id, sp.image_id " .
122  " FROM skl_profile_role spr INNER JOIN skl_profile sp ON (spr.profile_id = sp.id) " .
123  " WHERE spr.role_id = " . $ilDB->quote($role_id, "integer") .
124  " ORDER BY sp.title ASC"
125  );
126  while ($rec = $ilDB->fetchAssoc($set)) {
127  $profiles[(int) $rec["profile_id"]] = $this->getRoleProfileFromRecord($rec);
128  }
129  return $profiles;
130  }
131 
135  public function getGlobalProfilesOfRole(int $role_id): array
136  {
137  $ilDB = $this->db;
138 
139  $profiles = [];
140  $set = $ilDB->query(
141  "SELECT spr.profile_id, spr.role_id, sp.title, sp.description, sp.ref_id, sp.skill_tree_id, sp.image_id " .
142  " FROM skl_profile_role spr INNER JOIN skl_profile sp ON (spr.profile_id = sp.id) " .
143  " WHERE spr.role_id = " . $ilDB->quote($role_id, "integer") .
144  " AND sp.ref_id = 0" .
145  " ORDER BY sp.title ASC"
146  );
147  while ($rec = $ilDB->fetchAssoc($set)) {
148  $profiles[(int) $rec["profile_id"]] = $this->getRoleProfileFromRecord($rec);
149  }
150 
151  return $profiles;
152  }
153 
157  public function getLocalProfilesOfRole(int $role_id): array
158  {
159  $ilDB = $this->db;
160 
161  $profiles = [];
162  $set = $ilDB->query(
163  "SELECT spr.profile_id, spr.role_id, sp.title, sp.description, sp.ref_id, sp.skill_tree_id, sp.image_id " .
164  " FROM skl_profile_role spr INNER JOIN skl_profile sp ON (spr.profile_id = sp.id) " .
165  " WHERE spr.role_id = " . $ilDB->quote($role_id, "integer") .
166  " AND sp.ref_id <> 0" .
167  " ORDER BY sp.title ASC"
168  );
169  while ($rec = $ilDB->fetchAssoc($set)) {
170  $profiles[(int) $rec["profile_id"]] = $this->getRoleProfileFromRecord($rec);
171  }
172  return $profiles;
173  }
174 
175  protected function getProfileFromRecord(array $rec): SkillProfile
176  {
177  $rec["id"] = (int) $rec["id"];
178  $rec["title"] = (string) $rec["title"];
179  $rec["description"] = (string) $rec["description"];
180  $rec["skill_tree_id"] = (int) $rec["skill_tree_id"];
181  $rec["image_id"] = (string) $rec["image_id"];
182  $rec["ref_id"] = (int) $rec["ref_id"];
183 
184  return $this->factory_service->profile()->profile(
185  $rec["id"],
186  $rec["title"],
187  $rec["description"],
188  $rec["skill_tree_id"],
189  $rec["image_id"],
190  $rec["ref_id"]
191  );
192  }
193 
194  protected function getRoleProfileFromRecord(array $rec): SkillRoleProfile
195  {
196  $rec["role_id"] = (int) $rec["role_id"];
197  $rec["profile_id"] = (int) $rec["profile_id"];
198  $rec["title"] = (string) $rec["title"];
199  $rec["description"] = (string) $rec["description"];
200  $rec["skill_tree_id"] = (int) $rec["skill_tree_id"];
201  $rec["image_id"] = (string) $rec["image_id"];
202  $rec["ref_id"] = (int) $rec["ref_id"];
203 
204  return $this->factory_service->profile()->roleProfile(
205  $rec["role_id"],
206  $rec["profile_id"],
207  $rec["title"],
208  $rec["description"],
209  $rec["skill_tree_id"],
210  $rec["image_id"],
211  $rec["ref_id"]
212  );
213  }
214 
215  public function countRoles(int $profile_id): int
216  {
217  $ilDB = $this->db;
218 
219  $set = $ilDB->query(
220  "SELECT count(*) rcnt FROM skl_profile_role " .
221  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
222  );
223  $rec = $ilDB->fetchAssoc($set);
224  return (int) $rec["rcnt"];
225  }
226 }
__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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...