ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSkillTree.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26class ilSkillTree extends ilTree
27{
28 protected array $by_type_data = [];
29
30 public function __construct(int $a_tree_id = 1)
31 {
32 parent::__construct($a_tree_id);
33 $this->setTreeTablePK("skl_tree_id");
34 $this->setTableNames('skl_tree', 'skl_tree_node');
35 }
36
40 public function getSkillTreePath(int $a_base_skill_id, int $a_tref_id = 0): array
41 {
42 if ($a_tref_id > 0) {
43 $path = $this->getPathFull($a_tref_id);
44 $sub_path = $this->getPathFull($a_base_skill_id);
45 if (is_array($path)) {
46 foreach ($path as $k => $v) {
47 if ($v["child"] != $a_tref_id) {
48 $path[$k]["skill_id"] = $v["child"];
49 $path[$k]["tref_id"] = 0;
50 } else {
51 $path[$k]["skill_id"] = ilSkillTemplateReference::_lookupTemplateId($a_tref_id);
52 $path[$k]["tref_id"] = $a_tref_id;
53 }
54 }
55 }
56 $found = false;
57 if (is_array($sub_path)) {
58 foreach ($sub_path as $s) {
59 if ($found) {
60 $s["skill_id"] = $s["child"];
61 $s["tref_id"] = $a_tref_id;
62 $path[] = $s;
63 }
64 if ($s["child"] == ilSkillTemplateReference::_lookupTemplateId($a_tref_id)) {
65 $found = true;
66 }
67 }
68 }
69 } else {
70 $path = $this->getPathFull($a_base_skill_id);
71 if (is_array($path)) {
72 foreach ($path as $k => $v) {
73 $path[$k]["skill_id"] = $v["child"];
74 $path[$k]["tref_id"] = 0;
75 }
76 }
77 }
78
79 if (is_array($path)) {
80 return $path;
81 }
82 return [];
83 }
84
85 public function getSkillTreePathAsString(int $a_base_skill_id, int $a_tref_id = 0): string
86 {
87 $path = $this->getSkillTreePath($a_base_skill_id, $a_tref_id);
88 $str = "";
89 $sep = "";
90 foreach ($path as $p) {
91 if ($p["type"] != "skrt" && $p["child"] != $a_base_skill_id) {
92 $str .= $sep . $p["title"];
93 $sep = " > ";
94 }
95 }
96 return $str;
97 }
98
99 public function getTopParentNodeId(int $a_node_id): int
100 {
101 $path = $this->getPathId($a_node_id);
102 return $path[1];
103 }
104
105 public function getMaxOrderNr(int $a_par_id, bool $a_templates = false): int
106 {
107 if ($a_par_id != $this->readRootId()) {
108 $childs = $this->getChilds($a_par_id);
109 } elseif ($a_templates) {
110 $childs = $this->getChildsByTypeFilter(
111 $a_par_id,
112 array("skrt", "sktp", "sctp")
113 );
114 } else {
115 $childs = $this->getChildsByTypeFilter(
116 $a_par_id,
117 array("skrt", "skll", "scat", "sktr")
118 );
119 }
120
121 $max = 0;
122 foreach ($childs as $k => $c) {
123 $max = max(array((int) $c["order_nr"], $max));
124 }
125
126 return $max;
127 }
128
129 public function initChildsData()
130 {
131 if (isset($this->by_type_data[$this->getTreeId()])) {
132 return;
133 }
134
135 $db = $this->db;
136 $set = $db->queryF(
137 "SELECT * FROM " .
138 "skl_tree JOIN skl_tree_node ON skl_tree.child=skl_tree_node.obj_id " .
139 " WHERE skl_tree.skl_tree_id = %s ",
140 ["integer"],
141 [$this->getTreeId()]
142 );
143 $this->by_type_data[$this->getTreeId()] = [];
144 while ($rec = $db->fetchAssoc($set)) {
145 $this->by_type_data[$this->getTreeId()][$rec["parent"]][$rec["type"]][] = $rec;
146 }
147 }
148
149 public function getChildsByTypeFilter($a_node_id, $a_types, $a_order = "", $a_direction = "ASC"): array
150 {
151 $this->initChildsData();
152 $childs = [];
153 foreach ($a_types as $type) {
154 $type_childs = $this->by_type_data[$this->getTreeId()][$a_node_id][$type] ?? [];
155 $childs = array_merge($childs, $type_childs);
156 }
157
158 if ($a_order != "") {
159 ilArrayUtil::sortArray($childs, $a_order, $a_direction);
160 }
161
162 return $childs;
163 }
164}
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
getMaxOrderNr(int $a_par_id, bool $a_templates=false)
getSkillTreePathAsString(int $a_base_skill_id, int $a_tref_id=0)
getSkillTreePath(int $a_base_skill_id, int $a_tref_id=0)
__construct(int $a_tree_id=1)
getChildsByTypeFilter($a_node_id, $a_types, $a_order="", $a_direction="ASC")
getTopParentNodeId(int $a_node_id)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
getPathFull(int $a_endnode_id, int $a_startnode_id=0)
get path from a given startnode to a given endnode if startnode is not given the rootnode is startnod...
ilDBInterface $db
setTreeTablePK(string $a_column_name)
set column containing primary key in tree table
setTableNames(string $a_table_tree, string $a_table_obj_data, string $a_table_obj_reference="")
set table names The primary key of the table containing your object_data must be 'obj_id' You may use...
getChilds(int $a_node_id, string $a_order="", string $a_direction="ASC")
get child nodes of given node
getPathId(int $a_endnode_id, int $a_startnode_id=0)
get path from a given startnode to a given endnode if startnode is not given the rootnode is startnod...
$c
Definition: deliver.php:25
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc