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