ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilVirtualSkillTree.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
31 protected ilLanguage $lng;
32 protected ilSkillTree $tree;
33
37 protected static ?array $order_node_data = null;
38 protected bool $include_drafts = false;
39
43 protected array $drafts = [];
44 protected bool $include_outdated = false;
45
49 protected array $outdated = [];
50
51 public function __construct(int $tree_id)
52 {
53 global $DIC;
54
55 $this->lng = $DIC->language();
56 $this->tree = $DIC->skills()->internal()->factory()->tree()->getTreeById($tree_id);
57 }
58
62 public function getRootNode(): array
63 {
64 $root_id = $this->tree->readRootId();
65 $root_node = $this->tree->getNodeData($root_id);
66 unset($root_node["child"]);
67 $root_node["id"] = $root_id . ":0";
68 $root_node["cskill_id"] = $root_id . ":0";
69
70 return $root_node;
71 }
72
73 public function setIncludeDrafts(bool $a_val): void
74 {
75 $this->include_drafts = $a_val;
76 }
77
78 public function getIncludeDrafts(): bool
79 {
81 }
82
83 public function setIncludeOutdated(bool $a_val): void
84 {
85 $this->include_outdated = $a_val;
86 }
87
88 public function getIncludeOutdated(): bool
89 {
91 }
92
93 public function getNode(string $a_vtree_id): array
94 {
95 $id_parts = explode(":", $a_vtree_id);
96 $skl_tree_id = (int) $id_parts[0];
97 $skl_template_tree_id = isset($id_parts[1]) ? (int) $id_parts[1] : 0;
98
99 if ($skl_template_tree_id == 0
100 || (ilSkillTemplateReference::_lookupTemplateId($skl_tree_id) == $skl_template_tree_id)) {
101 $node_data = $this->tree->getNodeData($skl_tree_id);
102 if (isset($node_data["parent"])) {
103 $node_data["parent"] = $node_data["parent"] . ":0";
104 }
105 } else {
106 $node_data = $this->tree->getNodeData($skl_template_tree_id);
107 $node_data["parent"] = $skl_tree_id . ":" . $node_data["parent"];
108 }
109
110 unset($node_data["child"]);
111 unset($node_data["skl_tree_id"]);
112 unset($node_data["lft"]);
113 unset($node_data["rgt"]);
114 unset($node_data["depth"]);
115
116 $node_data["id"] = $a_vtree_id;
117 $cid = $this->getCSkillIdForVTreeId($a_vtree_id);
118 $cid_parts = explode(":", $cid);
119 $node_data["skill_id"] = $cid_parts[0];
120 $node_data["tref_id"] = $cid_parts[1];
121 $node_data["cskill_id"] = $cid;
122
123 return $node_data;
124 }
125
129 public function getChildsOfNode(string $a_parent_id): array
130 {
131 $a_parent_id_parts = explode(":", $a_parent_id);
132 $a_parent_skl_tree_id = (int) $a_parent_id_parts[0];
133 $a_parent_skl_template_tree_id = isset($a_parent_id_parts[1]) ? (int) $a_parent_id_parts[1] : 0;
134
135 if ($a_parent_skl_template_tree_id == 0) {
136 $childs = $this->tree->getChildsByTypeFilter($a_parent_skl_tree_id, array("scat", "skll", "sktr"), "order_nr");
137 } else {
138 $childs = $this->tree->getChildsByTypeFilter($a_parent_skl_template_tree_id, array("sktp", "sctp"), "order_nr");
139 }
140
141 $drafts = [];
142 $outdated = [];
143 foreach ($childs as $k => $c) {
144 if ($a_parent_skl_template_tree_id > 0) {
145 // we are in template tree only
146 $child_id = $a_parent_skl_tree_id . ":" . $c["child"];
147 } elseif (!in_array($c["type"], array("sktr", "sctr"))) {
148 // we are in main tree only
149 $child_id = $c["child"] . ":0";
150 } else {
151 // get template id for references
152 $child_id = $c["child"] . ":" . ilSkillTemplateReference::_lookupTemplateId((int) $c["child"]);
153 }
154 unset($childs[$k]["child"]);
155 unset($childs[$k]["skl_tree_id"]);
156 unset($childs[$k]["lft"]);
157 unset($childs[$k]["rgt"]);
158 unset($childs[$k]["depth"]);
159 $childs[$k]["id"] = $child_id;
160 $cid = $this->getCSkillIdForVTreeId($child_id);
161 $cid_parts = explode(":", $cid);
162 $childs[$k]["skill_id"] = $cid_parts[0];
163 $childs[$k]["tref_id"] = $cid_parts[1];
164 $childs[$k]["cskill_id"] = $cid;
165 $childs[$k]["parent"] = $a_parent_id;
166
167 // @todo: prepare this for tref id?
169 in_array($a_parent_id, $this->drafts)) {
170 $this->drafts[] = $child_id;
171 $drafts[] = $k;
172 }
174 in_array($a_parent_id, $this->outdated)) {
175 $this->outdated[] = $child_id;
176 $outdated[] = $k;
177 }
178 }
179 if (!$this->getIncludeDrafts()) {
180 foreach ($drafts as $d) {
181 unset($childs[$d]);
182 }
183 }
184 if (!$this->getIncludeOutdated()) {
185 foreach ($outdated as $d) {
186 unset($childs[$d]);
187 }
188 }
189
190 return $childs;
191 }
192
193 public function getChildsOfNodeForCSkillId(string $a_cskill_id): array
194 {
195 $id_parts = explode(":", $a_cskill_id);
196 if (!isset($id_parts[1]) || $id_parts[1] == 0) {
197 $id = $id_parts[0] . ":0";
198 } else {
199 $id = $id_parts[1] . ":" . $id_parts[0];
200 }
201 return $this->getChildsOfNode($id);
202 }
203
204 public function getCSkillIdForVTreeId(string $a_vtree_id): string
205 {
206 $id_parts = explode(":", $a_vtree_id);
207 if (!isset($id_parts[1]) || $id_parts[1] == 0) {
208 // skill in main tree
209 $skill_id = $id_parts[0];
210 $tref_id = 0;
211 } else {
212 // skill in template
213 $tref_id = $id_parts[0];
214 $skill_id = $id_parts[1];
215 }
216 return $skill_id . ":" . $tref_id;
217 }
218
219 public function getVTreeIdForCSkillId(string $a_cskill_id): string
220 {
221 $id_parts = explode(":", $a_cskill_id);
222 if (!isset($id_parts[1]) || $id_parts[1] == 0) {
223 $id = $id_parts[0] . ":0";
224 } else {
225 $id = $id_parts[1] . ":" . $id_parts[0];
226 }
227 return $id;
228 }
229
230 public function getNodeTitle(array $a_node): string
231 {
233
234 $a_parent_id_parts = explode(":", $a_node["id"]);
235 $a_parent_skl_tree_id = (int) $a_parent_id_parts[0];
236 $a_parent_skl_template_tree_id = isset($a_parent_id_parts[1]) ? (int) $a_parent_id_parts[1] : 0;
237
238 // title
239 $title = $a_node["title"];
240
241 // root?
242 if ($a_node["type"] == "skrt") {
243 $lng->txt("skmg_skills");
244 } elseif ($a_node["type"] == "sktr") {
245 // $title.= " (".ilSkillTreeNode::_lookupTitle($a_parent_skl_template_tree_id).")";
246 }
247
248 return $title;
249 }
250
254 public function getSubTreeForCSkillId(string $a_cskill_id, bool $a_only_basic = false): array
255 {
256 $id_parts = explode(":", $a_cskill_id);
257 if (!isset($id_parts[1]) || $id_parts[1] == 0) {
258 $id = $id_parts[0] . ":0";
259 } else {
260 $id = $id_parts[1] . ":" . $id_parts[0];
261 }
262
263 $result = [];
264
265 $node = $this->getNode($id);
266 if (!$a_only_basic || isset($node["type"]) && in_array($node["type"], array("skll", "sktp")) ||
267 (isset($node["type"]) && $node["type"] == "sktr" && ilSkillTreeNode::_lookupType((int) $node["skill_id"]) == "sktp")) {
268 $result[] = $node;
269 }
270 return array_merge($result, $this->__getSubTreeRec($id, $a_only_basic));
271 }
272
276 protected function __getSubTreeRec(string $id, bool $a_only_basic): array
277 {
278 $result = [];
279 $childs = $this->getChildsOfNode($id);
280 foreach ($childs as $c) {
281 if (!$a_only_basic || in_array($c["type"], array("skll", "sktp")) ||
282 ($c["type"] == "sktr" && ilSkillTreeNode::_lookupType((int) $c["skill_id"]) == "sktp")) {
283 $result[] = $c;
284 }
285 $result = array_merge($result, $this->__getSubTreeRec($c["id"], $a_only_basic));
286 }
287
288 return $result;
289 }
290
291 public function isDraft(string $a_node_id): bool
292 {
293 return in_array($a_node_id, $this->drafts);
294 }
295
296 public function isOutdated(string $a_node_id): bool
297 {
298 return in_array($a_node_id, $this->outdated);
299 }
300
309 public function getOrderedNodeset(array $c_skill_ids, string $a_skill_id_key = "", string $a_tref_id_key = ""): array
310 {
311 global $DIC;
312
313 $db = $DIC->database();
314
315 if (self::$order_node_data == null) {
316 $node_data = [];
317 $set = $db->query("SELECT t.child, t.parent, t.lft, n.order_nr FROM skl_tree t JOIN skl_tree_node n ON (t.child = n.obj_id)");
318 while ($rec = $db->fetchAssoc($set)) {
319 $node_data[(int) $rec["child"]] = array(
320 "parent" => null === $rec["parent"] ? null : (int) $rec["parent"],
321 "lft" => (int) $rec["lft"],
322 "order_nr" => (int) $rec["order_nr"],
323 );
324 }
325 self::$order_node_data = $node_data;
326 } else {
327 $node_data = self::$order_node_data;
328 }
329
330 uasort($c_skill_ids, function ($a, $b) use ($node_data, $a_skill_id_key, $a_tref_id_key): int {
331 // normalize to cskill strings
332 if (is_array($a)) {
333 $cskilla = $a[$a_skill_id_key] . ":" . $a[$a_tref_id_key];
334 $cskillb = $b[$a_skill_id_key] . ":" . $b[$a_tref_id_key];
335 } else {
336 $cskilla = $a;
337 $cskillb = $b;
338 }
339
340 // get vtree ids
341 $vida = explode(":", $this->getVTreeIdForCSkillId($cskilla));
342 $vidb = explode(":", $this->getVTreeIdForCSkillId($cskillb));
343
344 $ua = $this->getFirstUncommonAncestors($vida[0], $vidb[0], $node_data);
345 if (is_array($ua) && isset($node_data[$ua[0]]) && isset($node_data[$ua[1]])) {
346 return ($node_data[$ua[0]]["order_nr"] - $node_data[$ua[1]]["order_nr"]);
347 }
348 // if we did not find a first uncommon ancestor, we are in the same node in the
349 // main tree, here, if we have tref ids, we let the template tree decide
350 if ($vida[1] > 0 && $vidb[1] > 0) {
351 $ua = $this->getFirstUncommonAncestors($vida[1], $vidb[1], $node_data);
352 if (is_array($ua) && isset($node_data[$ua[0]]) && isset($node_data[$ua[1]])) {
353 return ($node_data[$ua[0]]["order_nr"] - $node_data[$ua[1]]["order_nr"]);
354 }
355 }
356
357 return 0;
358 });
359
360 return $c_skill_ids;
361 }
362
366 protected function getPath(string $a, array $node_data): array
367 {
368 $path[] = $a;
369 if (isset($node_data[$a]) && isset($node_data[$a]["parent"])) {
370 while ($node_data[$a]["parent"] != 0) {
371 $a = $node_data[$a]["parent"];
372 $path[] = $a;
373 }
374 }
375 return array_reverse($path);
376 }
377
383 protected function getFirstUncommonAncestors(string $a, string $b, array $node_data)
384 {
385 $path_a = $this->getPath($a, $node_data);
386 $path_b = $this->getPath($b, $node_data);
387 foreach ($path_a as $k => $v) {
388 if ($v != $path_b[$k]) {
389 return array($v, $path_b[$k]);
390 }
391 }
392 return false;
393 }
394}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _lookupType(int $a_obj_id)
static _lookupStatus(int $a_obj_id)
getPath(string $a, array $node_data)
get path in node data
getChildsOfNodeForCSkillId(string $a_cskill_id)
isOutdated(string $a_node_id)
getNode(string $a_vtree_id)
__getSubTreeRec(string $id, bool $a_only_basic)
getVTreeIdForCSkillId(string $a_cskill_id)
getChildsOfNode(string $a_parent_id)
getOrderedNodeset(array $c_skill_ids, string $a_skill_id_key="", string $a_tref_id_key="")
Get ordererd nodeset for common skill ids.
getCSkillIdForVTreeId(string $a_vtree_id)
getFirstUncommonAncestors(string $a, string $b, array $node_data)
get first uncommon ancestors of $a and $b in $node_data
getSubTreeForCSkillId(string $a_cskill_id, bool $a_only_basic=false)
$c
Definition: deliver.php:25
$path
Definition: ltiservices.php:30
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26