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