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