ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.PersonalSkillDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 namespace ILIAS\Skill\Personal;
23 
26 
31 {
32  protected \ilSkillTreeRepository $tree_repo;
33  protected \ilDBInterface $db;
35 
36  public function __construct(
37  \ilSkillTreeRepository $tree_repo,
38  \ilDBInterface $db = null,
39  Service\SkillInternalFactoryService $factory_service = null
40  ) {
41  global $DIC;
42 
43  $this->tree_repo = $tree_repo;
44  $this->db = ($db) ?: $DIC->database();
45  $this->factory_service = ($factory_service) ?: $DIC->skills()->internal()->factory();
46  }
47 
51  public function get(int $user_id): array
52  {
53  $ilDB = $this->db;
54 
55  $set = $ilDB->query(
56  "SELECT * FROM skl_personal_skill " .
57  " WHERE user_id = " . $ilDB->quote($user_id, "integer")
58  );
59  $pskills = [];
60  while ($rec = $ilDB->fetchAssoc($set)) {
61  $skill_node_id = (int) $rec["skill_node_id"];
62  if ($this->tree_repo->isInAnyTree($skill_node_id)) {
63  $pskills[$skill_node_id] = $this->getFromRecord($rec);
64  }
65  }
66  return $pskills;
67  }
68 
69  protected function getFromRecord(array $rec): SelectedUserSkill
70  {
71  $skill_node_id = (int) $rec["skill_node_id"];
72 
73  return $this->factory_service->personal()->selectedUserSkill(
74  $skill_node_id,
75  \ilSkillTreeNode::_lookupTitle($skill_node_id)
76  );
77  }
78 
79  public function add(int $user_id, int $skill_node_id): void
80  {
81  $ilDB = $this->db;
82 
83  $set = $ilDB->query(
84  "SELECT * FROM skl_personal_skill " .
85  " WHERE user_id = " . $ilDB->quote($user_id, "integer") .
86  " AND skill_node_id = " . $ilDB->quote($skill_node_id, "integer")
87  );
88  if (!$ilDB->fetchAssoc($set)) {
89  $ilDB->manipulate("INSERT INTO skl_personal_skill " .
90  "(user_id, skill_node_id) VALUES (" .
91  $ilDB->quote($user_id, "integer") . "," .
92  $ilDB->quote($skill_node_id, "integer") .
93  ")");
94  }
95  }
96 
97  public function remove(int $user_id, int $skill_node_id): void
98  {
99  $ilDB = $this->db;
100 
101  $ilDB->manipulate(
102  "DELETE FROM skl_personal_skill WHERE " .
103  " user_id = " . $ilDB->quote($user_id, "integer") .
104  " AND skill_node_id = " . $ilDB->quote($skill_node_id, "integer")
105  );
106  }
107 
108  public function removeAllForUser(int $user_id): void
109  {
110  $ilDB = $this->db;
111 
112  $ilDB->manipulate(
113  "DELETE FROM skl_personal_skill WHERE " .
114  " user_id = " . $ilDB->quote($user_id, "integer")
115  );
116  }
117 
118  public function removeAllForSkill(int $skill_node_id): void
119  {
120  $ilDB = $this->db;
121 
122  $ilDB->manipulate(
123  "DELETE FROM skl_personal_skill WHERE " .
124  " skill_node_id = " . $ilDB->quote($skill_node_id, "integer")
125  );
126  }
127 
135  public function getUsages(array $usages, array $pskill_ids, array $tref_ids): array
136  {
137  $ilDB = $this->db;
138 
139  $set = $ilDB->query(
140  "SELECT skill_node_id, user_id FROM skl_personal_skill " .
141  " WHERE " . $ilDB->in("skill_node_id", $pskill_ids, false, "integer") .
142  " GROUP BY skill_node_id, user_id"
143  );
144  while ($rec = $ilDB->fetchAssoc($set)) {
145  if (isset($tref_ids[(int) $rec["skill_node_id"]])) {
146  $usages[$tref_ids[$rec["skill_node_id"]] . ":" . $rec["skill_node_id"]][SkillUsageManager::PERSONAL_SKILL][] =
147  array("key" => $rec["user_id"]);
148  } else {
149  $usages[$rec["skill_node_id"] . ":0"][SkillUsageManager::PERSONAL_SKILL][] =
150  array("key" => $rec["user_id"]);
151  }
152  }
153 
154  return $usages;
155  }
156 }
static _lookupTitle(int $a_obj_id, int $a_tref_id=0)
getUsages(array $usages, array $pskill_ids, array $tref_ids)
global $DIC
Definition: shib_login.php:25
__construct(\ilSkillTreeRepository $tree_repo, \ilDBInterface $db=null, Service\SkillInternalFactoryService $factory_service=null)
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...