ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.SkillUsageManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 namespace ILIAS\Skill\Usage;
23 
28 
47 {
48  public const TYPE_GENERAL = "gen";
49  public const USER_ASSIGNED = "user";
50  public const PERSONAL_SKILL = "pers";
51  public const USER_MATERIAL = "mat";
52  public const SELF_EVAL = "seval";
53  public const PROFILE = "prof";
54  public const RESOURCE = "res";
55 
59  protected array $classes = [\ilBasicSkill::class, AssignedMaterialManager::class, SkillProfileManager::class,
60  SkillResourcesManager::class, SkillUsageManager::class];
61 
64  protected \ilSkillTreeRepository $tree_repo;
66  protected \ilLanguage $lng;
67 
68  public function __construct(
69  SkillUsageDBRepository $usage_repo = null,
70  SkillTreeFactory $tree_factory = null,
71  \ilSkillTreeRepository $tree_repo = null,
72  SkillProfileManager $profile_manager = null
73  ) {
74  global $DIC;
75 
76  $this->usage_repo = ($usage_repo) ?: $DIC->skills()->internal()->repo()->getUsageRepo();
77  $this->tree_factory = ($tree_factory) ?: $DIC->skills()->internal()->factory()->tree();
78  $this->tree_repo = ($tree_repo) ?: $DIC->skills()->internal()->repo()->getTreeRepo();
79  $this->profile_manager = ($profile_manager) ?: $DIC->skills()->internal()->manager()->getProfileManager();
80  $this->lng = $DIC->language();
81  }
82 
83  public function addUsage(int $obj_id, int $skill_id, int $tref_id): void
84  {
85  $this->usage_repo->add($obj_id, $skill_id, $tref_id);
86  }
87 
88  public function removeUsage(int $obj_id, int $skill_id, int $tref_id): void
89  {
90  $this->usage_repo->remove($obj_id, $skill_id, $tref_id);
91  }
92 
93  public function removeUsagesFromObject(int $obj_id): void
94  {
95  $this->usage_repo->removeFromObject($obj_id);
96  }
97 
98  public function removeUsagesForSkill(int $node_id, bool $is_referenece = false): void
99  {
100  $this->usage_repo->removeForSkill($node_id, $is_referenece);
101  }
102 
106  public function getUsages(int $skill_id, int $tref_id): array
107  {
108  $obj_ids = $this->usage_repo->getUsages($skill_id, $tref_id);
109 
110  return $obj_ids;
111  }
112 
116  public static function getUsageInfo(array $a_cskill_ids): array
117  {
118  $class = new self();
119  return $class->getUsageInfoGeneric(
120  $a_cskill_ids,
121  self::TYPE_GENERAL,
122  "skl_usage",
123  "obj_id"
124  );
125  }
126 
133  public function getUsageInfoGeneric(
134  array $cskill_ids,
135  string $usage_type,
136  string $table,
137  string $key_field,
138  string $skill_field = "skill_id",
139  string $tref_field = "tref_id"
140  ): array {
141  if (count($cskill_ids) === 0) {
142  return [];
143  }
144 
145  $usages = $this->usage_repo->getUsageInfoGeneric(
146  $cskill_ids,
147  $usage_type,
148  $table,
149  $key_field,
150  $skill_field,
151  $tref_field
152  );
153 
154  return $usages;
155  }
156 
161  public function getAllUsagesInfo(array $cskill_ids): array
162  {
163  $classes = $this->classes;
164 
165  $usages = [];
166  foreach ($classes as $class) {
167  $usages = array_merge_recursive($usages, $class::getUsageInfo($cskill_ids));
168  }
169  return $usages;
170  }
171 
176  public function getAllUsagesInfoOfTrees(array $tree_ids): array
177  {
178  // get nodes
179 
180  $allnodes = [];
181  foreach ($tree_ids as $t) {
182  $vtree = $this->tree_factory->getGlobalVirtualTree();
183  $nodes = $vtree->getSubTreeForTreeId((string) $t);
184  foreach ($nodes as $n) {
185  $allnodes[] = $n;
186  }
187  }
188 
189  return $this->getAllUsagesInfo($allnodes);
190  }
191 
195  public function getAllUsagesInfoOfSubtree(int $skill_id, int $tref_id = 0): array
196  {
197  // get nodes
198  $vtree = $this->tree_repo->getVirtualTreeForNodeId($skill_id);
199  $nodes = $vtree->getSubTreeForCSkillId($skill_id . ":" . $tref_id);
200 
201  return $this->getAllUsagesInfo($nodes);
202  }
203 
208  public function getAllUsagesInfoOfSubtrees(array $cskill_ids): array
209  {
210  // get nodes
211  $allnodes = [];
212  foreach ($cskill_ids as $s) {
213  $vtree = $this->tree_repo->getVirtualTreeForNodeId($s["skill_id"]);
214  $nodes = $vtree->getSubTreeForCSkillId($s["skill_id"] . ":" . $s["tref_id"]);
215  foreach ($nodes as $n) {
216  $allnodes[] = $n;
217  }
218  }
219 
220  return $this->getAllUsagesInfo($allnodes);
221  }
222 
226  public function getAllUsagesOfTemplate(int $template_id): array
227  {
228  $skill_logger = \ilLoggerFactory::getLogger("skll");
229  $skill_logger->debug("SkillUsageManager: getAllUsagesOfTemplate(" . $template_id . ")");
230 
231  // get all trefs for template id
233 
234  // get all usages of subtrees of template_id:tref
235  $cskill_ids = [];
236  foreach ($trefs as $tref) {
237  $cskill_ids[] = array("skill_id" => $template_id, "tref_id" => $tref);
238  $skill_logger->debug("SkillUsageManager: ... skill_id: " . $template_id . ", tref_id: " . $tref . ".");
239  }
240 
241  $skill_logger->debug("SkillUsageManager: ... count cskill_ids: " . count($cskill_ids) . ".");
242 
243  return $this->getAllUsagesInfoOfSubtrees($cskill_ids);
244  }
245 
246  public function getTypeInfoString(string $type): string
247  {
248  return $this->lng->txt("skmg_usage_type_info_" . $type);
249  }
250 
251  public function getObjTypeString(string $type): string
252  {
253  switch ($type) {
254  case self::TYPE_GENERAL:
255  case self::RESOURCE:
256  return $this->lng->txt("skmg_usage_obj_objects");
257 
258  case self::USER_ASSIGNED:
259  case self::PERSONAL_SKILL:
260  case self::USER_MATERIAL:
261  case self::SELF_EVAL:
262  return $this->lng->txt("skmg_usage_obj_users");
263 
264  case self::PROFILE:
265  return $this->lng->txt("skmg_usage_obj_profiles");
266 
267  default:
268  return $this->lng->txt("skmg_usage_type_info_" . $type);
269  }
270  }
271 
275  public function getAssignedObjectsForSkill(int $skill_id, int $tref_id): array
276  {
277  //$objects = $this->getAllUsagesInfoOfSubtree($a_skill_id, $a_tref_id);
278  $objects = $this->getUsages($skill_id, $tref_id);
279 
280  return $objects;
281  }
282 
286  public function getAssignedObjectsForSkillTemplate(int $template_id): array
287  {
288  $usages = $this->getAllUsagesOfTemplate($template_id);
289  $obj_usages = array_column($usages, "gen");
290 
291  return array_column(current(array_reverse($obj_usages)) ?: [], "key");
292  }
293 
297  public function getAssignedObjectsForSkillProfile(int $profile_id): array
298  {
299  $skills = $this->profile_manager->getSkillLevels($profile_id);
300  $objects = [];
301 
302  // usages for skills within skill profile
303  foreach ($skills as $skill) {
304  $obj_usages = $this->getUsages($skill->getBaseSkillId(), $skill->getTrefId());
305  foreach ($obj_usages as $id) {
306  if (!in_array($id, $objects) && \ilObject::_hasUntrashedReference($id)) {
307  $objects[] = $id;
308  }
309  }
310  }
311 
312  // courses and groups which are using skill profile
313  $roles = $this->profile_manager->getRoleAssignments($profile_id);
314  foreach ($roles as $role) {
315  if (($role->getObjType() == "crs" || $role->getObjType() == "grp")
316  && !in_array($role->getObjId(), $objects)) {
317  $obj_ref_id = \ilObject::_getAllReferences($role->getObjId());
318  $obj_ref_id = end($obj_ref_id);
319  if ($role->getId() === \ilParticipants::getDefaultMemberRole($obj_ref_id)) {
320  $objects[] = $role->getObjId();
321  }
322  }
323  }
324 
325  return $objects;
326  }
327 }
static getLogger(string $a_component_id)
Get component logger.
addUsage(int $obj_id, int $skill_id, int $tref_id)
static _getAllReferences(int $id)
get all reference ids for object ID
static _hasUntrashedReference(int $obj_id)
checks whether an object has at least one reference that is not in trash
static getDefaultMemberRole(int $a_ref_id)
getUsageInfoGeneric(array $cskill_ids, string $usage_type, string $table, string $key_field, string $skill_field="skill_id", string $tref_field="tref_id")
Get standard usage query.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
removeUsagesForSkill(int $node_id, bool $is_referenece=false)
global $DIC
Definition: shib_login.php:25
getAssignedObjectsForSkill(int $skill_id, int $tref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(SkillUsageDBRepository $usage_repo=null, SkillTreeFactory $tree_factory=null, \ilSkillTreeRepository $tree_repo=null, SkillProfileManager $profile_manager=null)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
static getUsageInfo(array $a_cskill_ids)
Get title of an assigned item.int, tref_id: int}[] $a_cskill_ids array of common skill idsarray<strin...
getAllUsagesInfoOfSubtree(int $skill_id, int $tref_id=0)
removeUsage(int $obj_id, int $skill_id, int $tref_id)
Get info on usages of skills.