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