ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.SkillProfileUserDBRepository.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 get(int $profile_id): array
41  {
42  $ilDB = $this->db;
43 
44  $set = $ilDB->query(
45  "SELECT * FROM skl_profile_user " .
46  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
47  );
48  $users = [];
49  while ($rec = $ilDB->fetchAssoc($set)) {
50  $users[] = $rec;
51  }
52 
53  return $users;
54  }
55 
56  public function getFromRecord(array $rec): SkillProfileUserAssignment
57  {
58  return $this->factory_service->profile()->profileUserAssignment(
59  $rec["name"],
60  $rec["id"]
61  );
62  }
63 
64  public function addUserToProfile(int $profile_id, int $user_id): void
65  {
66  $ilDB = $this->db;
67 
68  $ilDB->replace(
69  "skl_profile_user",
70  array("profile_id" => array("integer", $profile_id),
71  "user_id" => array("integer", $user_id),
72  ),
73  []
74  );
75  }
76 
77  public function removeUserFromProfile(int $profile_id, int $user_id): void
78  {
79  $ilDB = $this->db;
80 
81  $ilDB->manipulate(
82  "DELETE FROM skl_profile_user WHERE " .
83  " profile_id = " . $ilDB->quote($profile_id, "integer") .
84  " AND user_id = " . $ilDB->quote($user_id, "integer")
85  );
86  }
87 
88  public function removeUserFromAllProfiles(int $user_id): void
89  {
90  $ilDB = $this->db;
91 
92  $ilDB->manipulate(
93  "DELETE FROM skl_profile_user WHERE " .
94  " user_id = " . $ilDB->quote($user_id, "integer")
95  );
96  }
97 
98  public function deleteProfileUsers(int $profile_id): void
99  {
100  $ilDB = $this->db;
101 
102  $ilDB->manipulate(
103  "DELETE FROM skl_profile_user WHERE " .
104  " profile_id = " . $ilDB->quote($profile_id, "integer")
105  );
106  }
107 
111  public function getProfilesOfUser(int $user_id): array
112  {
113  $ilDB = $this->db;
114 
115  $user_profiles = [];
116  $set = $ilDB->query(
117  "SELECT p.id, p.title, p.description, p.ref_id, p.skill_tree_id, p.image_id " .
118  " FROM skl_profile_user u JOIN skl_profile p ON (u.profile_id = p.id) " .
119  " WHERE u.user_id = " . $ilDB->quote($user_id, "integer") .
120  " ORDER BY p.title ASC"
121  );
122  while ($rec = $ilDB->fetchAssoc($set)) {
123  $user_profiles[] = $this->getProfileFromRecord($rec);
124  }
125 
126  return $user_profiles;
127  }
128 
129  protected function getProfileFromRecord(array $rec): SkillProfile
130  {
131  $rec["id"] = (int) $rec["id"];
132  $rec["title"] = (string) $rec["title"];
133  $rec["description"] = (string) $rec["description"];
134  $rec["skill_tree_id"] = (int) $rec["skill_tree_id"];
135  $rec["image_id"] = (string) $rec["image_id"];
136  $rec["ref_id"] = (int) $rec["ref_id"];
137 
138  return $this->factory_service->profile()->profile(
139  $rec["id"],
140  $rec["title"],
141  $rec["description"],
142  $rec["skill_tree_id"],
143  $rec["image_id"],
144  $rec["ref_id"]
145  );
146  }
147 
148  public function countUsers(int $profile_id): int
149  {
150  $ilDB = $this->db;
151 
152  $set = $ilDB->query(
153  "SELECT count(*) ucnt FROM skl_profile_user " .
154  " WHERE profile_id = " . $ilDB->quote($profile_id, "integer")
155  );
156  $rec = $ilDB->fetchAssoc($set);
157  return (int) $rec["ucnt"];
158  }
159 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(?\ilDBInterface $db=null, ?Service\SkillInternalFactoryService $factory_service=null)
global $DIC
Definition: shib_login.php:26