ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSkillTreeNode.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/Skill/classes/class.ilSkillTree.php");
6 
16 {
17  var $type;
18  var $id;
19  var $title;
20 
24  function ilSkillTreeNode($a_id = 0)
25  {
26  $this->id = $a_id;
27 
28  $this->skill_tree = new ilSkillTree();
29 
30  if($a_id != 0)
31  {
32  $this->read();
33  }
34  }
35 
41  function setTitle($a_title)
42  {
43  $this->title = $a_title;
44  }
45 
51  function getTitle()
52  {
53  return $this->title;
54  }
55 
61  function setType($a_type)
62  {
63  $this->type = $a_type;
64  }
65 
71  function getType()
72  {
73  return $this->type;
74  }
75 
81  function setId($a_id)
82  {
83  $this->id = $a_id;
84  }
85 
91  function getId()
92  {
93  return $this->id;
94  }
95 
101  function setSelfEvaluation($a_val)
102  {
103  $this->self_eval = $a_val;
104  }
105 
111  function getSelfEvaluation()
112  {
113  return $this->self_eval;
114  }
115 
121  function setOrderNr($a_val)
122  {
123  $this->order_nr = $a_val;
124  }
125 
131  function getOrderNr()
132  {
133  return $this->order_nr;
134  }
135 
139  function read()
140  {
141  global $ilBench, $ilDB;
142 
143  if(!isset($this->data_record))
144  {
145  $query = "SELECT * FROM skl_tree_node WHERE obj_id = ".
146  $ilDB->quote($this->id, "integer");
147  $obj_set = $ilDB->query($query);
148  $this->data_record = $ilDB->fetchAssoc($obj_set);
149  }
150  $this->setType($this->data_record["type"]);
151  $this->setTitle($this->data_record["title"]);
152  $this->setOrderNr($this->data_record["order_nr"]);
153  $this->setSelfEvaluation($this->data_record["self_eval"]);
154  $this->setDraft($this->data_record["draft"]);
155  }
156 
160  function setDataRecord($a_record)
161  {
162  $this->data_record = $a_record;
163  }
164 
171  protected static function _lookup($a_obj_id, $a_field)
172  {
173  global $ilDB;
174 
175  $query = "SELECT $a_field FROM skl_tree_node WHERE obj_id = ".
176  $ilDB->quote($a_obj_id, "integer");
177  $obj_set = $ilDB->query($query);
178  $obj_rec = $ilDB->fetchAssoc($obj_set);
179 
180  return $obj_rec[$a_field];
181  }
182 
189  static function _lookupTitle($a_obj_id)
190  {
191  global $ilDB;
192 
193  return self::_lookup($a_obj_id, "title");
194  }
195 
202  static function _lookupSelfEvaluation($a_obj_id)
203  {
204  global $ilDB;
205 
206  return self::_lookup($a_obj_id, "self_eval");
207  }
208 
215  static function _lookupDraft($a_obj_id)
216  {
217  global $ilDB;
218 
219  return self::_lookup($a_obj_id, "draft");
220  }
221 
228  static function _lookupType($a_obj_id)
229  {
230  global $ilDB;
231 
232  $query = "SELECT * FROM skl_tree_node WHERE obj_id = ".
233  $ilDB->quote($a_obj_id, "integer");
234  $obj_set = $ilDB->query($query);
235  $obj_rec = $ilDB->fetchAssoc($obj_set);
236 
237  return $obj_rec["type"];
238  }
239 
245  function setDraft($a_val)
246  {
247  $this->draft = $a_val;
248  }
249 
255  function getDraft()
256  {
257  return $this->draft;
258  }
259 
266  static function _writeTitle($a_obj_id, $a_title)
267  {
268  global $ilDB;
269 
270  $query = "UPDATE skl_tree_node SET ".
271  " title = ".$ilDB->quote($a_title, "text").
272  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
273 
274  $ilDB->manipulate($query);
275  }
276 
283  static function _writeOrderNr($a_obj_id, $a_nr)
284  {
285  global $ilDB;
286 
287  $query = "UPDATE skl_tree_node SET ".
288  " order_nr = ".$ilDB->quote($a_nr, "integer").
289  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
290  $ilDB->manipulate($query);
291  }
292 
298  function create()
299  {
300  global $ilDB;
301 
302  // insert object data
303  $id = $ilDB->nextId("skl_tree_node");
304  $query = "INSERT INTO skl_tree_node (obj_id, title, type, create_date, self_eval, order_nr, draft) ".
305  "VALUES (".
306  $ilDB->quote($id, "integer").",".
307  $ilDB->quote($this->getTitle(), "text").",".
308  $ilDB->quote($this->getType(), "text").", ".
309  $ilDB->now().", ".
310  $ilDB->quote((int) $this->getSelfEvaluation(), "integer").", ".
311  $ilDB->quote((int) $this->getOrderNr(), "integer").", ".
312  $ilDB->quote((int) $this->getDraft(), "integer").
313  ")";
314  $ilDB->manipulate($query);
315  $this->setId($id);
316  }
317 
321  function update()
322  {
323  global $ilDB;
324 
325  $query = "UPDATE skl_tree_node SET ".
326  " title = ".$ilDB->quote($this->getTitle(), "text").
327  " ,self_eval = ".$ilDB->quote((int) $this->getSelfEvaluation(), "integer").
328  " ,order_nr = ".$ilDB->quote((int) $this->getOrderNr(), "integer").
329  " ,draft = ".$ilDB->quote((int) $this->getDraft(), "integer").
330  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
331 
332  $ilDB->manipulate($query);
333  }
334 
338  function delete()
339  {
340  global $ilDB;
341 
342  $query = "DELETE FROM skl_tree_node WHERE obj_id= ".
343  $ilDB->quote($this->getId(), "integer");
344  $ilDB->manipulate($query);
345  }
346 
350  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
351  {
352  $skill_tree = new ilSkillTree();
353 
354  // determine parent
355  $parent_id = ($a_parent_id != "")
356  ? $a_parent_id
357  : $skill_tree->getRootId();
358 
359  // make a check, whether the type of object is allowed under
360  // the parent
361  $allowed = array(
362  "skrt" => array("skll", "scat", "sktr", "sktp", "sctp"),
363  "scat" => array("skll", "scat", "sktr"),
364  "sctp" => array("sktp", "sctp"));
365  $par_type = self::_lookupType($parent_id);
366  if (!is_array($allowed[$par_type]) ||
367  !in_array($a_obj->getType(), $allowed[$par_type]))
368  {
369  return;
370  }
371 
372  // determine target
373  if ($a_target_node_id != "")
374  {
375  $target = $a_target_node_id;
376  }
377  else
378  {
379  // determine last child that serves as predecessor
380  $childs = $skill_tree->getChilds($parent_id);
381 
382  if (count($childs) == 0)
383  {
384  $target = IL_FIRST_NODE;
385  }
386  else
387  {
388  $target = $childs[count($childs) - 1]["obj_id"];
389  }
390  }
391 
392  if ($skill_tree->isInTree($parent_id) && !$skill_tree->isInTree($a_obj->getId()))
393  {
394  $skill_tree->insertNode($a_obj->getId(), $parent_id, $target);
395  }
396  }
397 
405  static function getTree($a_slm_obj_id)
406  {
407  $tree = new ilSkillTree();
408 
409  return $tree;
410  }
411 
415  static function uniqueTypesCheck($a_items)
416  {
417  $types = array();
418  if (is_array($a_items))
419  {
420  foreach($a_items as $item)
421  {
423  $types[$type] = $type;
424  }
425  }
426 
427  if (count($types) > 1)
428  {
429  return false;
430  }
431  return true;
432  }
433 
437  function clipboardCut($a_tree_id, $a_ids)
438  {
440  include_once("./Services/Skill/classes/class.ilSkillTree.php");
441  $tree = new ilSkillTree();
442 
443  if (!is_array($a_ids))
444  {
445  return false;
446  }
447  else
448  {
449  // get all "top" ids, i.e. remove ids, that have a selected parent
450  foreach($a_ids as $id)
451  {
452  $path = $tree->getPathId($id);
453  $take = true;
454  foreach($path as $path_id)
455  {
456  if ($path_id != $id && in_array($path_id, $a_ids))
457  {
458  $take = false;
459  }
460  }
461  if ($take)
462  {
463  $cut_ids[] = $id;
464  }
465  }
466  }
467 
468  ilSkillTreeNode::clipboardCopy($a_tree_id, $cut_ids);
469 
470  // remove the objects from the tree
471  // note: we are getting skills/categories which are *not* in the tree
472  // we do not delete any pages/chapters here
473  foreach ($cut_ids as $id)
474  {
475  $curnode = $tree->getNodeData($id);
476  if ($tree->isInTree($id))
477  {
478  $tree->deleteTree($curnode);
479  }
480  }
481 
482  }
483 
484 
488  static function clipboardCopy($a_tree_id, $a_ids)
489  {
490  global $ilUser;
491 
493  include_once("./Services/Skill/classes/class.ilSkillTree.php");
494  $tree = new ilSkillTree();
495 
496  // put them into the clipboard
497  $time = date("Y-m-d H:i:s", time());
498  foreach ($a_ids as $id)
499  {
500  $curnode = "";
501  if ($tree->isInTree($id))
502  {
503  $curnode = $tree->getNodeData($id);
504  $subnodes = $tree->getSubTree($curnode);
505  foreach($subnodes as $subnode)
506  {
507  if ($subnode["child"] != $id)
508  {
509  $ilUser->addObjectToClipboard($subnode["child"],
510  $subnode["type"], $subnode["title"],
511  $subnode["parent"], $time, $subnode["lft"]);
512  }
513  }
514  }
515  $order = ($curnode["lft"] > 0)
516  ? $curnode["lft"]
517  : (int) ($order + 1);
518  $ilUser->addObjectToClipboard($id,
520  $order);
521  }
522  }
523 
524 
528  static function insertItemsFromClip($a_type, $a_obj_id)
529  {
530  global $ilCtrl, $ilUser;
531 
532  // @todo: move this to a service since it can be used here, too
533  include_once("./Modules/LearningModule/classes/class.ilEditClipboard.php");
534 
535  include_once("./Services/Skill/classes/class.ilSkillTree.php");
536  $tree = new ilSkillTree();
537 
538  $parent_id = $a_obj_id;
539  $target = IL_LAST_NODE;
540 
541  // cut and paste
542  $skills = $ilUser->getClipboardObjects($a_type); // this will get all skills _regardless_ of level
543  $copied_nodes = array();
544  foreach ($skills as $skill)
545  {
546  // if skill was already copied as part of tree - do not copy it again
547  if(!in_array($skill["id"], array_keys($copied_nodes)))
548  {
549  $cid = ilSkillTreeNode::pasteTree($skill["id"], $parent_id, $target,
550  $skill["insert_time"], $copied_nodes,
551  (ilEditClipboard::getAction() == "copy"), true);
552 // $target = $cid;
553  }
554  }
555 
556 // if (ilEditClipboard::getAction() == "cut")
557 // {
559 // }
560 
561  ilSkillTreeNode::saveChildsOrder($a_obj_id, array(),
562  in_array($a_type, array("sktp", "sctp")));
563 
564  return $copied_nodes;
565  }
566 
573  static function clearClipboard()
574  {
575  global $ilUser;
576 
577  $ilUser->clipboardDeleteObjectsOfType("skll");
578  $ilUser->clipboardDeleteObjectsOfType("scat");
579  $ilUser->clipboardDeleteObjectsOfType("sktr");
580  $ilUser->clipboardDeleteObjectsOfType("sktp");
581  $ilUser->clipboardDeleteObjectsOfType("sctp");
582  include_once("./Modules/LearningModule/classes/class.ilEditClipboard.php");
584  }
585 
586 
590  static function pasteTree($a_item_id, $a_parent_id, $a_target, $a_insert_time,
591  &$a_copied_nodes, $a_as_copy = false, $a_add_suffix = false)
592  {
593  global $ilUser, $ilias, $ilLog, $lng;
594 
595  $item_type = ilSkillTreeNode::_lookupType($a_item_id);
596 
597  if ($item_type == "scat")
598  {
599  include_once("./Services/Skill/classes/class.ilSkillCategory.php");
600  $item = new ilSkillCategory($a_item_id);
601  }
602  else if ($item_type == "skll")
603  {
604  include_once("./Services/Skill/classes/class.ilBasicSkill.php");
605  $item = new ilBasicSkill($a_item_id);
606  }
607  else if ($item_type == "sktr")
608  {
609  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
610  $item = new ilSkillTemplateReference($a_item_id);
611  }
612  else if ($item_type == "sktp")
613  {
614  include_once("./Services/Skill/classes/class.ilBasicSkillTemplate.php");
615  $item = new ilBasicSkillTemplate($a_item_id);
616  }
617  else if ($item_type == "sctp")
618  {
619  include_once("./Services/Skill/classes/class.ilSkillTemplateCategory.php");
620  $item = new ilSkillTemplateCategory($a_item_id);
621  }
622 
623  $ilLog->write("Getting from clipboard type ".$item_type.", ".
624  "Item ID: ".$a_item_id);
625 
626  if ($a_as_copy)
627  {
628  $target_item = $item->copy();
629  if($a_add_suffix)
630  {
631  $target_item->setTitle($target_item->getTitle()." ".$lng->txt("copy_of_suffix"));
632  $target_item->update();
633  }
634  $a_copied_nodes[$item->getId()] = $target_item->getId();
635  }
636  else
637  {
638  $target_item = $item;
639  }
640 
641  $ilLog->write("Putting into skill tree type ".$target_item->getType().
642  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
643  "Target: ".$a_target);
644 
645  ilSkillTreeNode::putInTree($target_item, $a_parent_id, $a_target);
646 
647  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
648 
649  foreach($childs as $child)
650  {
651  ilSkillTreeNode::pasteTree($child["id"], $target_item->getId(),
652  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy);
653  }
654 
655  return $target_item->getId();
656  }
657 
664  static function isInTree($a_id)
665  {
666  $skill_tree = new ilSkillTree();
667  if ($skill_tree->isInTree($a_id))
668  {
669  return true;
670  }
671  return false;
672  }
673 
680  static function getAllSelfEvaluationNodes()
681  {
682  global $ilDB;
683 
684  $set = $ilDB->query("SELECT obj_id, title FROM skl_tree_node WHERE ".
685  " self_eval = ".$ilDB->quote(true, "integer")." ORDER BY TITLE "
686  );
687  $nodes = array();
688  while ($rec = $ilDB->fetchAssoc($set))
689  {
690  $nodes[$rec["obj_id"]] = $rec["title"];
691  }
692  return $nodes;
693  }
694 
701  static function getTopTemplates()
702  {
703  $tr = new ilSkillTree();
704  $childs = $tr->getChildsByTypeFilter($tr->getRootId(), array("sktp", "sctp"));
705 
706  return $childs;
707  }
708 
715  static function getSelectableSkills()
716  {
717  global $ilDB;
718 
719  $set = $ilDB->query("SELECT * FROM skl_tree_node ".
720  " WHERE self_eval = ".$ilDB->quote(1, "integer")
721  );
722 
723  $sel_skills = array();
724  while ($rec = $ilDB->fetchAssoc($set))
725  {
726  $sel_skills[] = $rec;
727  }
728 
729  return $sel_skills;
730  }
731 
732 
739  static function getSkillTreeNodes($a_node_id, $a_only_basic = false)
740  {
741  $skills = array();
742  if ($a_node_id > 0)
743  {
744  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
745  include_once("./Services/Skill/classes/class.ilSkillTree.php");
746  $stree = new ilSkillTree();
747 
748  if ($stree->isInTree($a_node_id))
749  {
750  $cnode = $stree->getNodeData($a_node_id);
751 
752  // is node basic skill?
753  if ($cnode["type"] == "skll" || !$a_only_basic)
754  {
755  $skills[] = array("id" => $a_node_id,
756  "type" => $cnode["type"], "parent" => $cnode["parent"],
757  "tref" => 0);
758  }
759 
760  // is node skill template reference?
761  if ($cnode["type"] == "sktr")
762  {
763  $tr_ref = new ilSkillTemplateReference($cnode["child"]);
764  if ($tr_ref->getSkillTemplateId() > 0)
765  {
766  $cnode2 = $stree->getNodeData($tr_ref->getSkillTemplateId());
767  if ($cnode2["child"] > 0)
768  {
769  $childs2 = $stree->getSubTree($cnode2);
770  foreach ($childs2 as $child2)
771  {
772  // find basic skills templates
773  if ($child2["type"] == "sktp" || !$a_only_basic)
774  {
775  $par = ($tr_ref->getSkillTemplateId() == $child2["child"])
776  ? $cnode["child"]
777  : $child2["parent"];
778  $skills[] = array("id" => $child2["child"],
779  "type" => $child2["type"], "parent" => $par,
780  "tref" => $cnode["child"]);
781  }
782  }
783  }
784  }
785  }
786  else
787  {
788  $childs = $stree->getSubTree($cnode);
789  foreach ($childs as $child)
790  {
791  // getSubTree($cnode) will also return $cnode
792  if($child["child"] == $cnode["child"])
793  {
794  continue;
795  }
796 
797  // find basic skills
798  if ($child["type"] == "skll" || !$a_only_basic)
799  {
800  $skills[] = array("id" => $child["child"],
801  "type" => $child["type"], "parent" => $child["parent"],
802  "tref" => 0);
803  }
804 
805  // handle template references
806  if ($child["type"] == "sktr")
807  {
808  $tr_ref = new ilSkillTemplateReference($child["child"]);
809  $cnode2 = $stree->getNodeData($tr_ref->getSkillTemplateId());
810  if ($tr_ref->getSkillTemplateId() > 0)
811  {
812  if ($cnode2["child"] > 0)
813  {
814  $childs2 = $stree->getSubTree($cnode2);
815  foreach ($childs2 as $child2)
816  {
817  $par = ($tr_ref->getSkillTemplateId() == $child2["child"])
818  ? $cnode2["child"]
819  : $child2["parent"];
820  // find basic skills templates
821  if ($child2["type"] == "sktp" || !$a_only_basic)
822  {
823  $skills[] = array("id" => $child2["child"],
824  "type" => $child2["type"], "parent" => $par,
825  "tref" => $child["child"]);
826  }
827  }
828  }
829  }
830  }
831  }
832  }
833  }
834  }
835 //var_dump($skills);
836  return $skills;
837  }
838 
845  static function saveChildsOrder($a_par_id, $a_childs_order, $a_templates = false)
846  {
847  include_once("./Services/Skill/classes/class.ilSkillTree.php");
848  $skill_tree = new ilSkillTree();
849 
850  if ($a_par_id != $skill_tree->readRootId())
851  {
852  $childs = $skill_tree->getChilds($a_par_id);
853  }
854  else
855  {
856  if ($a_templates)
857  {
858  $childs = $skill_tree->getChildsByTypeFilter($a_par_id,
859  array("skrt", "sktp", "sctp"));
860  }
861  else
862  {
863  $childs = $skill_tree->getChildsByTypeFilter($a_par_id,
864  array("skrt", "skll", "scat", "sktr"));
865  }
866  }
867 
868  foreach ($childs as $k => $c)
869  {
870  if (isset($a_childs_order[$c["child"]]))
871  {
872  $childs[$k]["order_nr"] = (int) $a_childs_order[$c["child"]];
873  }
874  }
875 
876  $childs = ilUtil::sortArray($childs, "order_nr", "asc", true);
877 
878  $cnt = 10;
879  foreach ($childs as $c)
880  {
881  ilSkillTreeNode::_writeOrderNr($c["child"], $cnt);
882  $cnt += 10;
883  }
884  }
885 
892  function getIconPath($a_obj_id, $a_type, $a_size = "", $a_draft = false)
893  {
894  $off = ($a_draft)
895  ? "_off"
896  : "";
897 
898  $a_name = "icon_".$a_type.$a_size.$off.".gif";
899  if ($a_type == "sktr")
900  {
901  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
904  if ($type == "sctp")
905  {
906  $a_name = "icon_sctr".$a_size.$off.".gif";
907  }
908  }
909  $vers = "vers=".str_replace(array(".", " "), "-", ILIAS_VERSION);
910  return ilUtil::getImagePath($a_name)."?".$vers;
911  }
912 
913 }
914 ?>