ILIAS  Release_4_4_x_branch Revision 61816
 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  const STATUS_PUBLISH = 0;
18  const STATUS_DRAFT = 1;
19  const STATUS_OUTDATED = 2;
20  var $type;
21  var $id;
22  var $title;
23 
27  function ilSkillTreeNode($a_id = 0)
28  {
29  $this->id = $a_id;
30 
31  $this->skill_tree = new ilSkillTree();
32 
33  if($a_id != 0)
34  {
35  $this->read();
36  }
37  }
38 
44  function setTitle($a_title)
45  {
46  $this->title = $a_title;
47  }
48 
54  function getTitle()
55  {
56  return $this->title;
57  }
58 
64  function setType($a_type)
65  {
66  $this->type = $a_type;
67  }
68 
74  function getType()
75  {
76  return $this->type;
77  }
78 
84  function setId($a_id)
85  {
86  $this->id = $a_id;
87  }
88 
94  function getId()
95  {
96  return $this->id;
97  }
98 
104  function setSelfEvaluation($a_val)
105  {
106  $this->self_eval = $a_val;
107  }
108 
114  function getSelfEvaluation()
115  {
116  return $this->self_eval;
117  }
118 
124  function setOrderNr($a_val)
125  {
126  $this->order_nr = $a_val;
127  }
128 
134  function getOrderNr()
135  {
136  return $this->order_nr;
137  }
138 
144  static function getAllStatus()
145  {
146  global $lng;
147 
148  return array(
149  self::STATUS_DRAFT => $lng->txt("skmg_status_draft"),
150  self::STATUS_PUBLISH => $lng->txt("skmg_status_publish"),
151  self::STATUS_OUTDATED => $lng->txt("skmg_status_outdated")
152  );
153  }
154 
161  static function getStatusInfo($a_status)
162  {
163  global $lng;
164 
165  switch($a_status)
166  {
167  case self::STATUS_PUBLISH: return $lng->txt("skmg_status_publish_info");
168  case self::STATUS_DRAFT: return $lng->txt("skmg_status_draft_info");
169  case self::STATUS_OUTDATED: return $lng->txt("skmg_status_outdated_info");
170  }
171  return "";
172  }
173 
177  function read()
178  {
179  global $ilBench, $ilDB;
180 
181  if(!isset($this->data_record))
182  {
183  $query = "SELECT * FROM skl_tree_node WHERE obj_id = ".
184  $ilDB->quote($this->id, "integer");
185  $obj_set = $ilDB->query($query);
186  $this->data_record = $ilDB->fetchAssoc($obj_set);
187  }
188  $this->setType($this->data_record["type"]);
189  $this->setTitle($this->data_record["title"]);
190  $this->setOrderNr($this->data_record["order_nr"]);
191  $this->setSelfEvaluation($this->data_record["self_eval"]);
192  $this->setStatus($this->data_record["status"]);
193  }
194 
198  function setDataRecord($a_record)
199  {
200  $this->data_record = $a_record;
201  }
202 
209  protected static function _lookup($a_obj_id, $a_field)
210  {
211  global $ilDB;
212 
213  $query = "SELECT $a_field FROM skl_tree_node WHERE obj_id = ".
214  $ilDB->quote($a_obj_id, "integer");
215  $obj_set = $ilDB->query($query);
216  $obj_rec = $ilDB->fetchAssoc($obj_set);
217 
218  return $obj_rec[$a_field];
219  }
220 
227  static function _lookupTitle($a_obj_id, $a_tref_id = 0)
228  {
229  global $ilDB;
230 
231  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
232  if ($a_tref_id > 0 && ilSkillTemplateReference::_lookupTemplateId($a_tref_id) == $a_obj_id)
233  {
234  return self::_lookup($a_tref_id, "title");
235  }
236  return self::_lookup($a_obj_id, "title");
237  }
238 
245  static function _lookupSelfEvaluation($a_obj_id)
246  {
247  global $ilDB;
248 
249  return self::_lookup($a_obj_id, "self_eval");
250  }
251 
258  static function _lookupStatus($a_obj_id)
259  {
260  global $ilDB;
261 
262  return self::_lookup($a_obj_id, "status");
263  }
264 
271  static function _lookupType($a_obj_id)
272  {
273  global $ilDB;
274 
275  $query = "SELECT * FROM skl_tree_node WHERE obj_id = ".
276  $ilDB->quote($a_obj_id, "integer");
277  $obj_set = $ilDB->query($query);
278  $obj_rec = $ilDB->fetchAssoc($obj_set);
279 
280  return $obj_rec["type"];
281  }
282 
288  function setStatus($a_val)
289  {
290  $this->status = $a_val;
291  }
292 
298  function getStatus()
299  {
300  return $this->status;
301  }
302 
309  static function _writeTitle($a_obj_id, $a_title)
310  {
311  global $ilDB;
312 
313  $query = "UPDATE skl_tree_node SET ".
314  " title = ".$ilDB->quote($a_title, "text").
315  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
316 
317  $ilDB->manipulate($query);
318  }
319 
326  static function _writeOrderNr($a_obj_id, $a_nr)
327  {
328  global $ilDB;
329 
330  $query = "UPDATE skl_tree_node SET ".
331  " order_nr = ".$ilDB->quote($a_nr, "integer").
332  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
333  $ilDB->manipulate($query);
334  }
335 
341  function create()
342  {
343  global $ilDB;
344 
345  // insert object data
346  $id = $ilDB->nextId("skl_tree_node");
347  $query = "INSERT INTO skl_tree_node (obj_id, title, type, create_date, self_eval, order_nr, status) ".
348  "VALUES (".
349  $ilDB->quote($id, "integer").",".
350  $ilDB->quote($this->getTitle(), "text").",".
351  $ilDB->quote($this->getType(), "text").", ".
352  $ilDB->now().", ".
353  $ilDB->quote((int) $this->getSelfEvaluation(), "integer").", ".
354  $ilDB->quote((int) $this->getOrderNr(), "integer").", ".
355  $ilDB->quote((int) $this->getStatus(), "integer").
356  ")";
357  $ilDB->manipulate($query);
358  $this->setId($id);
359  }
360 
364  function update()
365  {
366  global $ilDB;
367 
368  $query = "UPDATE skl_tree_node SET ".
369  " title = ".$ilDB->quote($this->getTitle(), "text").
370  " ,self_eval = ".$ilDB->quote((int) $this->getSelfEvaluation(), "integer").
371  " ,order_nr = ".$ilDB->quote((int) $this->getOrderNr(), "integer").
372  " ,status = ".$ilDB->quote((int) $this->getStatus(), "integer").
373  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
374 
375  $ilDB->manipulate($query);
376  }
377 
381  function delete()
382  {
383  global $ilDB;
384 
385  $query = "DELETE FROM skl_tree_node WHERE obj_id= ".
386  $ilDB->quote($this->getId(), "integer");
387  $ilDB->manipulate($query);
388  }
389 
393  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
394  {
395  $skill_tree = new ilSkillTree();
396 
397  // determine parent
398  $parent_id = ($a_parent_id != "")
399  ? $a_parent_id
400  : $skill_tree->getRootId();
401 
402  // make a check, whether the type of object is allowed under
403  // the parent
404  $allowed = array(
405  "skrt" => array("skll", "scat", "sktr", "sktp", "sctp"),
406  "scat" => array("skll", "scat", "sktr"),
407  "sctp" => array("sktp", "sctp"));
408  $par_type = self::_lookupType($parent_id);
409  if (!is_array($allowed[$par_type]) ||
410  !in_array($a_obj->getType(), $allowed[$par_type]))
411  {
412  return;
413  }
414 
415  // determine target
416  if ($a_target_node_id != "")
417  {
418  $target = $a_target_node_id;
419  }
420  else
421  {
422  // determine last child that serves as predecessor
423  $childs = $skill_tree->getChilds($parent_id);
424 
425  if (count($childs) == 0)
426  {
427  $target = IL_FIRST_NODE;
428  }
429  else
430  {
431  $target = $childs[count($childs) - 1]["obj_id"];
432  }
433  }
434 
435  if ($skill_tree->isInTree($parent_id) && !$skill_tree->isInTree($a_obj->getId()))
436  {
437  $skill_tree->insertNode($a_obj->getId(), $parent_id, $target);
438  }
439  }
440 
448  static function getTree($a_slm_obj_id)
449  {
450  $tree = new ilSkillTree();
451 
452  return $tree;
453  }
454 
458  static function uniqueTypesCheck($a_items)
459  {
460  $types = array();
461  if (is_array($a_items))
462  {
463  foreach($a_items as $item)
464  {
466  $types[$type] = $type;
467  }
468  }
469 
470  if (count($types) > 1)
471  {
472  return false;
473  }
474  return true;
475  }
476 
480  function clipboardCut($a_tree_id, $a_ids)
481  {
483  include_once("./Services/Skill/classes/class.ilSkillTree.php");
484  $tree = new ilSkillTree();
485 
486  if (!is_array($a_ids))
487  {
488  return false;
489  }
490  else
491  {
492  // get all "top" ids, i.e. remove ids, that have a selected parent
493  foreach($a_ids as $id)
494  {
495  $path = $tree->getPathId($id);
496  $take = true;
497  foreach($path as $path_id)
498  {
499  if ($path_id != $id && in_array($path_id, $a_ids))
500  {
501  $take = false;
502  }
503  }
504  if ($take)
505  {
506  $cut_ids[] = $id;
507  }
508  }
509  }
510 
511  ilSkillTreeNode::clipboardCopy($a_tree_id, $cut_ids);
512 
513  // remove the objects from the tree
514  // note: we are getting skills/categories which are *not* in the tree
515  // we do not delete any pages/chapters here
516  foreach ($cut_ids as $id)
517  {
518  $curnode = $tree->getNodeData($id);
519  if ($tree->isInTree($id))
520  {
521  $tree->deleteTree($curnode);
522  }
523  }
524 
525  }
526 
527 
531  static function clipboardCopy($a_tree_id, $a_ids)
532  {
533  global $ilUser;
534 
536  include_once("./Services/Skill/classes/class.ilSkillTree.php");
537  $tree = new ilSkillTree();
538 
539  // put them into the clipboard
540  $time = date("Y-m-d H:i:s", time());
541  foreach ($a_ids as $id)
542  {
543  $curnode = "";
544  if ($tree->isInTree($id))
545  {
546  $curnode = $tree->getNodeData($id);
547  $subnodes = $tree->getSubTree($curnode);
548  foreach($subnodes as $subnode)
549  {
550  if ($subnode["child"] != $id)
551  {
552  $ilUser->addObjectToClipboard($subnode["child"],
553  $subnode["type"], $subnode["title"],
554  $subnode["parent"], $time, $subnode["lft"]);
555  }
556  }
557  }
558  $order = ($curnode["lft"] > 0)
559  ? $curnode["lft"]
560  : (int) ($order + 1);
561  $ilUser->addObjectToClipboard($id,
563  $order);
564  }
565  }
566 
567 
571  static function insertItemsFromClip($a_type, $a_obj_id)
572  {
573  global $ilCtrl, $ilUser;
574 
575  // @todo: move this to a service since it can be used here, too
576  include_once("./Modules/LearningModule/classes/class.ilEditClipboard.php");
577 
578  include_once("./Services/Skill/classes/class.ilSkillTree.php");
579  $tree = new ilSkillTree();
580 
581  $parent_id = $a_obj_id;
582  $target = IL_LAST_NODE;
583 
584  // cut and paste
585  $skills = $ilUser->getClipboardObjects($a_type); // this will get all skills _regardless_ of level
586  $copied_nodes = array();
587  foreach ($skills as $skill)
588  {
589  // if skill was already copied as part of tree - do not copy it again
590  if(!in_array($skill["id"], array_keys($copied_nodes)))
591  {
592  $cid = ilSkillTreeNode::pasteTree($skill["id"], $parent_id, $target,
593  $skill["insert_time"], $copied_nodes,
594  (ilEditClipboard::getAction() == "copy"), true);
595 // $target = $cid;
596  }
597  }
598 
599 // if (ilEditClipboard::getAction() == "cut")
600 // {
602 // }
603 
604  ilSkillTreeNode::saveChildsOrder($a_obj_id, array(),
605  in_array($a_type, array("sktp", "sctp")));
606 
607  return $copied_nodes;
608  }
609 
616  static function clearClipboard()
617  {
618  global $ilUser;
619 
620  $ilUser->clipboardDeleteObjectsOfType("skll");
621  $ilUser->clipboardDeleteObjectsOfType("scat");
622  $ilUser->clipboardDeleteObjectsOfType("sktr");
623  $ilUser->clipboardDeleteObjectsOfType("sktp");
624  $ilUser->clipboardDeleteObjectsOfType("sctp");
625  include_once("./Modules/LearningModule/classes/class.ilEditClipboard.php");
627  }
628 
629 
633  static function pasteTree($a_item_id, $a_parent_id, $a_target, $a_insert_time,
634  &$a_copied_nodes, $a_as_copy = false, $a_add_suffix = false)
635  {
636  global $ilUser, $ilias, $ilLog, $lng;
637 
638  $item_type = ilSkillTreeNode::_lookupType($a_item_id);
639 
640  if ($item_type == "scat")
641  {
642  include_once("./Services/Skill/classes/class.ilSkillCategory.php");
643  $item = new ilSkillCategory($a_item_id);
644  }
645  else if ($item_type == "skll")
646  {
647  include_once("./Services/Skill/classes/class.ilBasicSkill.php");
648  $item = new ilBasicSkill($a_item_id);
649  }
650  else if ($item_type == "sktr")
651  {
652  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
653  $item = new ilSkillTemplateReference($a_item_id);
654  }
655  else if ($item_type == "sktp")
656  {
657  include_once("./Services/Skill/classes/class.ilBasicSkillTemplate.php");
658  $item = new ilBasicSkillTemplate($a_item_id);
659  }
660  else if ($item_type == "sctp")
661  {
662  include_once("./Services/Skill/classes/class.ilSkillTemplateCategory.php");
663  $item = new ilSkillTemplateCategory($a_item_id);
664  }
665 
666  $ilLog->write("Getting from clipboard type ".$item_type.", ".
667  "Item ID: ".$a_item_id);
668 
669  if ($a_as_copy)
670  {
671  $target_item = $item->copy();
672  if($a_add_suffix)
673  {
674  $target_item->setTitle($target_item->getTitle()." ".$lng->txt("copy_of_suffix"));
675  $target_item->update();
676  }
677  $a_copied_nodes[$item->getId()] = $target_item->getId();
678  }
679  else
680  {
681  $target_item = $item;
682  }
683 
684  $ilLog->write("Putting into skill tree type ".$target_item->getType().
685  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
686  "Target: ".$a_target);
687 
688  ilSkillTreeNode::putInTree($target_item, $a_parent_id, $a_target);
689 
690  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
691 
692  foreach($childs as $child)
693  {
694  ilSkillTreeNode::pasteTree($child["id"], $target_item->getId(),
695  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy);
696  }
697 
698  return $target_item->getId();
699  }
700 
707  static function isInTree($a_id)
708  {
709  $skill_tree = new ilSkillTree();
710  if ($skill_tree->isInTree($a_id))
711  {
712  return true;
713  }
714  return false;
715  }
716 
723  static function getAllSelfEvaluationNodes()
724  {
725  global $ilDB;
726 
727  $set = $ilDB->query("SELECT obj_id, title FROM skl_tree_node WHERE ".
728  " self_eval = ".$ilDB->quote(true, "integer")." ORDER BY TITLE "
729  );
730  $nodes = array();
731  while ($rec = $ilDB->fetchAssoc($set))
732  {
733  $nodes[$rec["obj_id"]] = $rec["title"];
734  }
735  return $nodes;
736  }
737 
744  static function getTopTemplates()
745  {
746  $tr = new ilSkillTree();
747  $childs = $tr->getChildsByTypeFilter($tr->getRootId(), array("sktp", "sctp"));
748 
749  return $childs;
750  }
751 
758  static function getSelectableSkills()
759  {
760  global $ilDB;
761 
762  $set = $ilDB->query("SELECT * FROM skl_tree_node ".
763  " WHERE self_eval = ".$ilDB->quote(1, "integer")
764  );
765 
766  $sel_skills = array();
767  while ($rec = $ilDB->fetchAssoc($set))
768  {
769  $sel_skills[] = $rec;
770  }
771 
772  return $sel_skills;
773  }
774 
781  static function saveChildsOrder($a_par_id, $a_childs_order, $a_templates = false)
782  {
783  include_once("./Services/Skill/classes/class.ilSkillTree.php");
784  $skill_tree = new ilSkillTree();
785 
786  if ($a_par_id != $skill_tree->readRootId())
787  {
788  $childs = $skill_tree->getChilds($a_par_id);
789  }
790  else
791  {
792  if ($a_templates)
793  {
794  $childs = $skill_tree->getChildsByTypeFilter($a_par_id,
795  array("skrt", "sktp", "sctp"));
796  }
797  else
798  {
799  $childs = $skill_tree->getChildsByTypeFilter($a_par_id,
800  array("skrt", "skll", "scat", "sktr"));
801  }
802  }
803 
804  foreach ($childs as $k => $c)
805  {
806  if (isset($a_childs_order[$c["child"]]))
807  {
808  $childs[$k]["order_nr"] = (int) $a_childs_order[$c["child"]];
809  }
810  }
811 
812  $childs = ilUtil::sortArray($childs, "order_nr", "asc", true);
813 
814  $cnt = 10;
815  foreach ($childs as $c)
816  {
817  ilSkillTreeNode::_writeOrderNr($c["child"], $cnt);
818  $cnt += 10;
819  }
820  }
821 
831  static function getIconPath($a_obj_id, $a_type, $a_size = "", $a_status = 0)
832  {
833  if ($a_status == self::STATUS_DRAFT && $a_type == "sctp")
834  {
835  $a_type = "scat";
836  }
837  if ($a_status == self::STATUS_DRAFT && $a_type == "sktp")
838  {
839  $a_type = "skll";
840  }
841 
842  $off = ($a_status == self::STATUS_DRAFT)
843  ? "_off"
844  : "";
845 
846  $a_name = "icon_".$a_type.$a_size.$off.".png";
847  if ($a_type == "sktr")
848  {
849  include_once("./Services/Skill/classes/class.ilSkillTemplateReference.php");
852  if ($type == "sctp")
853  {
854  $a_name = "icon_sctr".$a_size.$off.".png";
855  }
856  }
857  $vers = "vers=".str_replace(array(".", " "), "-", ILIAS_VERSION);
858  return ilUtil::getImagePath($a_name)."?".$vers;
859  }
860 
861 }
862 ?>