ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilVirtualSkillTree.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
14 {
18  protected $lng;
19 
20  protected static $order_node_data = null;
21  protected $include_drafts = false;
22  protected $drafts = array();
23  protected $include_outdated = false;
24  protected $outdated = array();
25 
29  public function __construct()
30  {
31  global $DIC;
32 
33  $this->lng = $DIC->language();
34  include_once("./Services/Skill/classes/class.ilSkillTree.php");
35  $this->tree = new ilSkillTree();
36  }
37 
43  public function getRootNode()
44  {
45  $root_id = $this->tree->readRootId();
46  $root_node = $this->tree->getNodeData($root_id);
47  unset($root_node["child"]);
48  $root_node["id"] = $root_id . ":0";
49  $root_node["cskill_id"] = $root_id . ":0";
50 
51  return $root_node;
52  }
53 
59  public function setIncludeDrafts($a_val)
60  {
61  $this->include_drafts = $a_val;
62  }
63 
69  public function getIncludeDrafts()
70  {
71  return $this->include_drafts;
72  }
73 
79  public function setIncludeOutdated($a_val)
80  {
81  $this->include_outdated = $a_val;
82  }
83 
89  public function getIncludeOutdated()
90  {
92  }
93 
100  public function getNode($a_id)
101  {
102  $id_parts = explode(":", $a_id);
103  $skl_tree_id = $id_parts[0];
104  $skl_template_tree_id = $id_parts[1];
105 
106  if ($skl_template_tree_id == 0 || (ilSkillTemplateReference::_lookupTemplateId($skl_tree_id)
107  == $skl_template_tree_id)) {
108  $node_data = $this->tree->getNodeData($skl_tree_id);
109  $node_data["parent"] = $node_data["parent"] . ":0";
110  } else {
111  $node_data = $this->tree->getNodeData($skl_template_tree_id);
112  $node_data["parent"] = $skl_tree_id . ":" . $node_data["parent"];
113  }
114 
115  unset($node_data["child"]);
116  unset($node_data["skl_tree_id"]);
117  unset($node_data["lft"]);
118  unset($node_data["rgt"]);
119  unset($node_data["depth"]);
120 
121  $node_data["id"] = $a_id;
122  $cid = $this->getCSkillIdForVTreeId($a_id);
123  $cid_parts = explode(":", $cid);
124  $node_data["skill_id"] = $cid_parts[0];
125  $node_data["tref_id"] = $cid_parts[1];
126  $node_data["cskill_id"] = $cid;
127 
128 
129  return $node_data;
130  }
131 
132 
139  public function getChildsOfNode($a_parent_id)
140  {
141  $a_parent_id_parts = explode(":", $a_parent_id);
142  $a_parent_skl_tree_id = $a_parent_id_parts[0];
143  $a_parent_skl_template_tree_id = $a_parent_id_parts[1];
144 
145  if ($a_parent_skl_template_tree_id == 0) {
146  $childs = $this->tree->getChildsByTypeFilter($a_parent_skl_tree_id, array("scat", "skll", "sktr"), "order_nr");
147  } else {
148  $childs = $this->tree->getChildsByTypeFilter($a_parent_skl_template_tree_id, array("sktp", "sctp"), "order_nr");
149  }
150 
151  include_once("./Services/Skill/classes/class.ilSkillTreeNode.php");
152  $drafts = array();
153  $outdated = array();
154  foreach ($childs as $k => $c) {
155  if ($a_parent_skl_template_tree_id > 0) {
156  // we are in template tree only
157  $child_id = $a_parent_skl_tree_id . ":" . $c["child"];
158  } elseif (!in_array($c["type"], array("sktr", "sctr"))) {
159  // we are in main tree only
160  $child_id = $c["child"] . ":0";
161  } else {
162  // get template id for references
163  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
164  $child_id = $c["child"] . ":" . ilSkillTemplateReference::_lookupTemplateId($c["child"]);
165  }
166  unset($childs[$k]["child"]);
167  unset($childs[$k]["skl_tree_id"]);
168  unset($childs[$k]["lft"]);
169  unset($childs[$k]["rgt"]);
170  unset($childs[$k]["depth"]);
171  $childs[$k]["id"] = $child_id;
172  //echo "-".$child_id."-";
173  $cid = $this->getCSkillIdForVTreeId($child_id);
174  //echo "-".$cid."-";
175  $cid_parts = explode(":", $cid);
176  $childs[$k]["skill_id"] = $cid_parts[0];
177  $childs[$k]["tref_id"] = $cid_parts[1];
178  $childs[$k]["cskill_id"] = $cid;
179  $childs[$k]["parent"] = $a_parent_id;
180 
181  $this->parent[$c["id"]] = $a_parent_id;
182 
183  // @todo: prepare this for tref id?
185  in_array($a_parent_id, $this->drafts)) {
186  $this->drafts[] = $child_id;
187  $drafts[] = $k;
188  }
190  in_array($a_parent_id, $this->outdated)) {
191  $this->outdated[] = $child_id;
192  $outdated[] = $k;
193  }
194  }
195  if (!$this->getIncludeDrafts()) {
196  foreach ($drafts as $d) {
197  unset($childs[$d]);
198  }
199  }
200  if (!$this->getIncludeOutdated()) {
201  foreach ($outdated as $d) {
202  unset($childs[$d]);
203  }
204  }
205 
206  return $childs;
207  }
208 
215  public function getChildsOfNodeForCSkillId($a_cskill_id)
216  {
217  $id_parts = explode(":", $a_cskill_id);
218  if ($id_parts[1] == 0) {
219  $id = $id_parts[0] . ":0";
220  } else {
221  $id = $id_parts[1] . ":" . $id_parts[0];
222  }
223  return $this->getChildsOfNode($id);
224  }
225 
232  public function getCSkillIdForVTreeId($a_vtree_id)
233  {
234  $id_parts = explode(":", $a_vtree_id);
235  if ($id_parts[1] == 0) {
236  // skill in main tree
237  $skill_id = $id_parts[0];
238  $tref_id = 0;
239  } else {
240  // skill in template
241  $tref_id = $id_parts[0];
242  $skill_id = $id_parts[1];
243  }
244  return $skill_id . ":" . $tref_id;
245  }
246 
253  public function getVTreeIdForCSkillId($a_cskill_id)
254  {
255  $id_parts = explode(":", $a_cskill_id);
256  if ($id_parts[1] == 0) {
257  $id = $id_parts[0] . ":0";
258  } else {
259  $id = $id_parts[1] . ":" . $id_parts[0];
260  }
261  return $id;
262  }
263 
264 
265 
272  public function getNodeTitle($a_node)
273  {
274  $lng = $this->lng;
275 
276  $a_parent_id_parts = explode(":", $a_node["id"]);
277  $a_parent_skl_tree_id = $a_parent_id_parts[0];
278  $a_parent_skl_template_tree_id = $a_parent_id_parts[1];
279 
280  // title
281  $title = $a_node["title"];
282 
283  // root?
284  if ($a_node["type"] == "skrt") {
285  $lng->txt("skmg_skills");
286  } else {
287  if ($a_node["type"] == "sktr") {
288  // include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
289 // $title.= " (".ilSkillTreeNode::_lookupTitle($a_parent_skl_template_tree_id).")";
290  }
291  }
292 
293  return $title;
294  }
295 
303  public function getSubTreeForCSkillId($a_cskill_id, $a_only_basic = false)
304  {
305  $id_parts = explode(":", $a_cskill_id);
306  if ($id_parts[1] == 0) {
307  $id = $id_parts[0] . ":0";
308  } else {
309  $id = $id_parts[1] . ":" . $id_parts[0];
310  }
311 
312  $result = array();
313 
314  $node = $this->getNode($id);
315  if (!$a_only_basic || in_array($node["type"], array("skll", "sktp")) ||
316  ($node["type"] == "sktr" && ilSkillTreeNode::_lookupType($node["skill_id"]) == "sktp")) {
317  $result[] = $node;
318  }
319  $this->__getSubTreeRec($id, $result, $a_only_basic);
320 
321  return $result;
322  }
323 
331  private function __getSubTreeRec($id, &$result, $a_only_basic)
332  {
333  $childs = $this->getChildsOfNode($id);
334  foreach ($childs as $c) {
335  if (!$a_only_basic || in_array($c["type"], array("skll", "sktp")) ||
336  ($c["type"] == "sktr" && ilSkillTreeNode::_lookupType($c["skill_id"]) == "sktp")) {
337  $result[] = $c;
338  }
339  $this->__getSubTreeRec($c["id"], $result, $a_only_basic);
340  }
341  }
342 
349  public function isDraft($a_node_id)
350  {
351  return in_array($a_node_id, $this->drafts);
352  }
353 
360  public function isOutdated($a_node_id)
361  {
362  return in_array($a_node_id, $this->outdated);
363  }
364 
373  public function getOrderedNodeset($c_skill_ids, $a_skill_id_key = "", $a_tref_id_key = "")
374  {
375  global $DIC;
376 
377  $db = $DIC->database();
378 
379  if (self::$order_node_data == null) {
380  $node_data = array();
381  $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)");
382  while ($rec = $db->fetchAssoc($set)) {
383  $node_data[$rec["child"]] = array(
384  "parent" => $rec["parent"],
385  "lft" => $rec["lft"],
386  "order_nr" => $rec["order_nr"],
387  );
388  }
389  self::$order_node_data = $node_data;
390  } else {
391  $node_data = self::$order_node_data;
392  }
393 
394  uasort($c_skill_ids, function ($a, $b) use ($node_data, $a_skill_id_key, $a_tref_id_key) {
395 
396  // normalize to cskill strings
397  if (is_array($a)) {
398  $cskilla = $a[$a_skill_id_key] . ":" . $a[$a_tref_id_key];
399  $cskillb = $b[$a_skill_id_key] . ":" . $b[$a_tref_id_key];
400  } else {
401  $cskilla = $a;
402  $cskillb = $b;
403  }
404 
405  // get vtree ids
406  $vida = explode(":", $this->getVTreeIdForCSkillId($cskilla));
407  $vidb = explode(":", $this->getVTreeIdForCSkillId($cskillb));
408 
409  $ua = $this->getFirstUncommonAncestors($vida[0], $vidb[0], $node_data);
410  if (is_array($ua)) {
411  return ($node_data[$ua[0]]["order_nr"] - $node_data[$ua[1]]["order_nr"]);
412  }
413  // if we did not find a first uncommon ancestor, we are in the same node in the
414  // main tree, here, if we have tref ids, we let the template tree decide
415  if ($vida[1] > 0 && $vidb[1] > 0) {
416  $ua = $this->getFirstUncommonAncestors($vida[1], $vidb[1], $node_data);
417  if (is_array($ua)) {
418  return ($node_data[$ua[0]]["order_nr"] - $node_data[$ua[1]]["order_nr"]);
419  }
420  }
421 
422  return 0;
423  });
424 
425  return $c_skill_ids;
426  }
427 
428  // get path in node data
429  protected function getPath($a, $node_data)
430  {
431  $path[] = $a;
432  while ($node_data[$a]["parent"] != 0) {
433  $a = $node_data[$a]["parent"];
434  $path[] = $a;
435  }
436  return array_reverse($path);
437  }
438 
439  // get first uncommon ancestors of $a and $b in $node_data
440  protected function getFirstUncommonAncestors($a, $b, $node_data)
441  {
442  $path_a = $this->getPath($a, $node_data);
443  $path_b = $this->getPath($b, $node_data);
444  foreach ($path_a as $k => $v) {
445  if ($v != $path_b[$k]) {
446  return array($v, $path_b[$k]);
447  }
448  }
449  return false;
450  }
451 }
isOutdated($a_node_id)
Is outdated.
$result
getChildsOfNodeForCSkillId($a_cskill_id)
Get childs of node for cskill id.
getVTreeIdForCSkillId($a_cskill_id)
Get tree id for common skill id.
global $DIC
Definition: saml.php:7
Skill tree.
if(!array_key_exists('StateId', $_REQUEST)) $id
getChildsOfNode($a_parent_id)
Get childs of node.
static _lookupType($a_obj_id)
Lookup Type.
getOrderedNodeset($c_skill_ids, $a_skill_id_key="", $a_tref_id_key="")
Get ordererd nodeset for common skill ids.
getNodeTitle($a_node)
Get node content.
setIncludeDrafts($a_val)
Set include drafts.
isDraft($a_node_id)
Is draft.
static _lookupTemplateId($a_obj_id)
Lookup template ID.
Create styles array
The data for the language used.
getIncludeOutdated()
Get include outdated.
getCSkillIdForVTreeId($a_vtree_id)
Get common skill id for tree id.
getFirstUncommonAncestors($a, $b, $node_data)
getIncludeDrafts()
Get include drafts.
static _lookupStatus($a_obj_id)
Lookup Status.
getSubTreeForCSkillId($a_cskill_id, $a_only_basic=false)
Get sub tree.
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
__getSubTreeRec($id, &$result, $a_only_basic)
Get subtree, internal.
setIncludeOutdated($a_val)
Set include outdated.