ILIAS  release_8 Revision v8.24
class.ilSkillUsage.php
Go to the documentation of this file.
1<?php
2
23
42{
43 public const TYPE_GENERAL = "gen";
44 public const USER_ASSIGNED = "user";
45 public const PERSONAL_SKILL = "pers";
46 public const USER_MATERIAL = "mat";
47 public const SELF_EVAL = "seval";
48 public const PROFILE = "prof";
49 public const RESOURCE = "res";
50
54 protected array $classes = [ilBasicSkill::class, ilPersonalSkill::class, SkillProfile::class,
55 ilSkillResources::class, ilSkillUsage::class];
56
60
61 public function __construct()
62 {
63 global $DIC;
64
65 $this->tree_repo = $DIC->skills()->internal()->repo()->getTreeRepo();
66 $this->tree_factory = $DIC->skills()->internal()->factory();
67 $this->profile_manager = $DIC->skills()->internal()->manager()->getProfileManager();
68 }
69
70 public static function setUsage(int $a_obj_id, int $a_skill_id, int $a_tref_id, bool $a_use = true): void
71 {
72 global $DIC;
73
74 $ilDB = $DIC->database();
75
76 if ($a_use) {
77 $ilDB->replace(
78 "skl_usage",
79 array(
80 "obj_id" => array("integer", $a_obj_id),
81 "skill_id" => array("integer", $a_skill_id),
82 "tref_id" => array("integer", $a_tref_id)
83 ),
84 []
85 );
86 } else {
87 $ilDB->manipulate(
88 $q = "DELETE FROM skl_usage WHERE " .
89 " obj_id = " . $ilDB->quote($a_obj_id, "integer") .
90 " AND skill_id = " . $ilDB->quote($a_skill_id, "integer") .
91 " AND tref_id = " . $ilDB->quote($a_tref_id, "integer")
92 );
93 }
94 }
95
96 public static function removeUsagesFromObject(int $a_obj_id): void
97 {
98 global $DIC;
99
100 $ilDB = $DIC->database();
101
102 $ilDB->manipulate(
103 $q = "DELETE FROM skl_usage WHERE " .
104 " obj_id = " . $ilDB->quote($a_obj_id, "integer")
105 );
106 }
107
111 public static function getUsages(int $a_skill_id, int $a_tref_id): array
112 {
113 global $DIC;
114
115 $ilDB = $DIC->database();
116
117 $set = $ilDB->query(
118 "SELECT obj_id FROM skl_usage " .
119 " WHERE skill_id = " . $ilDB->quote($a_skill_id, "integer") .
120 " AND tref_id = " . $ilDB->quote($a_tref_id, "integer")
121 );
122 $obj_ids = [];
123 while ($rec = $ilDB->fetchAssoc($set)) {
124 $obj_ids[] = (int) $rec["obj_id"];
125 }
126
127 return $obj_ids;
128 }
129
135 public static function getUsageInfo(array $a_cskill_ids): array
136 {
138 $a_cskill_ids,
140 "skl_usage",
141 "obj_id"
142 );
143 }
144
151 public static function getUsageInfoGeneric(
152 array $a_cskill_ids,
153 string $a_usage_type,
154 string $a_table,
155 string $a_key_field,
156 string $a_skill_field = "skill_id",
157 string $a_tref_field = "tref_id"
158 ): array {
159 global $DIC;
160
161 $a_usages = [];
162
163 $ilDB = $DIC->database();
164
165 if (count($a_cskill_ids) == 0) {
166 return [];
167 }
168
169 $w = "WHERE";
170 $q = "SELECT " . $a_key_field . ", " . $a_skill_field . ", " . $a_tref_field . " FROM " . $a_table . " ";
171 foreach ($a_cskill_ids as $sk) {
172 $q .= $w . " (" . $a_skill_field . " = " . $ilDB->quote($sk["skill_id"], "integer") .
173 " AND " . $a_tref_field . " = " . $ilDB->quote($sk["tref_id"], "integer") . ") ";
174 $w = "OR";
175 }
176 $q .= " GROUP BY " . $a_key_field . ", " . $a_skill_field . ", " . $a_tref_field;
177
178 $set = $ilDB->query($q);
179 while ($rec = $ilDB->fetchAssoc($set)) {
180 $a_usages[$rec[$a_skill_field] . ":" . $rec[$a_tref_field]][$a_usage_type][] =
181 array("key" => $rec[$a_key_field]);
182 }
183
184 return $a_usages;
185 }
186
191 public function getAllUsagesInfo(array $a_cskill_ids): array
192 {
193 $classes = $this->classes;
194
195 $usages = [];
196 foreach ($classes as $class) {
197 $usages = array_merge_recursive($usages, $class::getUsageInfo($a_cskill_ids));
198 }
199 return $usages;
200 }
201
206 public function getAllUsagesInfoOfTrees(array $a_tree_ids): array
207 {
208 // get nodes
209
210 $allnodes = [];
211 foreach ($a_tree_ids as $t) {
212 $vtree = $this->tree_factory->tree()->getGlobalVirtualTree();
213 $nodes = $vtree->getSubTreeForTreeId($t);
214 foreach ($nodes as $n) {
215 $allnodes[] = $n;
216 }
217 }
218
219 return $this->getAllUsagesInfo($allnodes);
220 }
221
225 public function getAllUsagesInfoOfSubtree(int $a_skill_id, int $a_tref_id = 0): array
226 {
227 // get nodes
228 $vtree = $this->tree_repo->getVirtualTreeForNodeId($a_skill_id);
229 $nodes = $vtree->getSubTreeForCSkillId($a_skill_id . ":" . $a_tref_id);
230
231 return $this->getAllUsagesInfo($nodes);
232 }
233
238 public function getAllUsagesInfoOfSubtrees(array $a_cskill_ids): array
239 {
240 // get nodes
241 $allnodes = [];
242 foreach ($a_cskill_ids as $s) {
243 $vtree = $this->tree_repo->getVirtualTreeForNodeId($s["skill_id"]);
244 $nodes = $vtree->getSubTreeForCSkillId($s["skill_id"] . ":" . $s["tref_id"]);
245 foreach ($nodes as $n) {
246 $allnodes[] = $n;
247 }
248 }
249
250 return $this->getAllUsagesInfo($allnodes);
251 }
252
256 public function getAllUsagesOfTemplate(int $a_template_id): array
257 {
258 $skill_logger = ilLoggerFactory::getLogger('skll');
259 $skill_logger->debug("ilSkillUsage: getAllUsagesOfTemplate(" . $a_template_id . ")");
260
261 // get all trefs for template id
263
264 // get all usages of subtrees of template_id:tref
265 $cskill_ids = [];
266 foreach ($trefs as $tref) {
267 $cskill_ids[] = array("skill_id" => $a_template_id, "tref_id" => $tref);
268 $skill_logger->debug("ilSkillUsage: ... skill_id: " . $a_template_id . ", tref_id: " . $tref . ".");
269 }
270
271 $skill_logger->debug("ilSkillUsage: ... count cskill_ids: " . count($cskill_ids) . ".");
272
273 return $this->getAllUsagesInfoOfSubtrees($cskill_ids);
274 }
275
276 public static function getTypeInfoString(string $a_type): string
277 {
278 global $DIC;
279
280 $lng = $DIC->language();
281
282 return $lng->txt("skmg_usage_type_info_" . $a_type);
283 }
284
285 public static function getObjTypeString(string $a_type): string
286 {
287 global $DIC;
288
289 $lng = $DIC->language();
290
291 switch ($a_type) {
292 case self::TYPE_GENERAL:
293 case self::RESOURCE:
294 return $lng->txt("skmg_usage_obj_objects");
295
296 case self::USER_ASSIGNED:
297 case self::PERSONAL_SKILL:
298 case self::USER_MATERIAL:
299 case self::SELF_EVAL:
300 return $lng->txt("skmg_usage_obj_users");
301
302 case self::PROFILE:
303 return $lng->txt("skmg_usage_obj_profiles");
304
305 default:
306 return $lng->txt("skmg_usage_type_info_" . $a_type);
307 }
308 }
309
313 public function getAssignedObjectsForSkill(int $a_skill_id, int $a_tref_id): array
314 {
315 //$objects = $this->getAllUsagesInfoOfSubtree($a_skill_id, $a_tref_id);
316 $objects = self::getUsages($a_skill_id, $a_tref_id);
317
318 return $objects;
319 }
320
324 public function getAssignedObjectsForSkillTemplate(int $a_template_id): array
325 {
326 $usages = $this->getAllUsagesOfTemplate($a_template_id);
327 $obj_usages = array_column($usages, "gen");
328
329 return array_column(current(array_reverse($obj_usages)) ?: [], 'key');
330 }
331
335 public function getAssignedObjectsForSkillProfile(int $a_profile_id): array
336 {
337 $profile = $this->profile_manager->getById($a_profile_id);
338 $skills = $profile->getSkillLevels();
339 $objects = [];
340
341 // usages for skills within skill profile
342 foreach ($skills as $skill) {
343 $obj_usages = self::getUsages($skill["base_skill_id"], $skill["tref_id"]);
344 foreach ($obj_usages as $id) {
345 if (!in_array($id, $objects)) {
346 $objects[] = $id;
347 }
348 }
349 }
350
351 // courses and groups which are using skill profile
352 $roles = $this->profile_manager->getAssignedRoles($profile->getId());
353 foreach ($roles as $role) {
354 if (($role["object_type"] == "crs" || $role["object_type"] == "grp")
355 && !in_array($role["object_id"], $objects)) {
356 $objects[] = $role["object_id"];
357 }
358 }
359
360 return $objects;
361 }
362}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static getLogger(string $a_component_id)
Get component logger.
static getUsageInfo(array $a_cskill_ids)
getAllUsagesOfTemplate(int $a_template_id)
getAssignedObjectsForSkill(int $a_skill_id, int $a_tref_id)
getAllUsagesInfoOfSubtree(int $a_skill_id, int $a_tref_id=0)
getAssignedObjectsForSkillTemplate(int $a_template_id)
static getUsages(int $a_skill_id, int $a_tref_id)
static getObjTypeString(string $a_type)
SkillInternalFactoryService $tree_factory
getAllUsagesInfoOfSubtrees(array $a_cskill_ids)
static getTypeInfoString(string $a_type)
SkillProfileManager $profile_manager
getAllUsagesInfo(array $a_cskill_ids)
static setUsage(int $a_obj_id, int $a_skill_id, int $a_tref_id, bool $a_use=true)
static removeUsagesFromObject(int $a_obj_id)
getAllUsagesInfoOfTrees(array $a_tree_ids)
getAssignedObjectsForSkillProfile(int $a_profile_id)
ilSkillTreeRepository $tree_repo
static getUsageInfoGeneric(array $a_cskill_ids, string $a_usage_type, string $a_table, string $a_key_field, string $a_skill_field="skill_id", string $a_tref_field="tref_id")
Get standard usage query.
global $DIC
Definition: feed.php:28
Interface ilSkillTreeRepository.
Get info on usages of skills.
$lng