ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLMObject.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
6 
18 {
19  var $ilias;
20  var $lm_id;
21  var $type;
22  var $id;
24  var $data_record; // assoc array of lm_data record
26  var $title;
28  var $active = true;
29 
33  function ilLMObject(&$a_content_obj, $a_id = 0)
34  {
35  global $ilias;
36 
37  $this->ilias =& $ilias;
38  $this->id = $a_id;
39  $this->setContentObject($a_content_obj);
40  $this->setLMId($a_content_obj->getId());
41  if($a_id != 0)
42  {
43  $this->read();
44  }
45  }
46 
58  function MDUpdateListener($a_element)
59  {
60  include_once 'Services/MetaData/classes/class.ilMD.php';
61 
62  switch($a_element)
63  {
64  case 'General':
65 
66  // Update Title and description
67  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
68  $md_gen = $md->getGeneral();
69 
70  ilLMObject::_writeTitle($this->getId(),$md_gen->getTitle());
71 
72  foreach($md_gen->getDescriptionIds() as $id)
73  {
74  $md_des = $md_gen->getDescription($id);
75 // ilLMObject::_writeDescription($this->getId(),$md_des->getDescription());
76  break;
77  }
78 
79  break;
80 
81  default:
82  }
83  return true;
84  }
85 
86 
90  function _lookupNID($a_lm_id, $a_lm_obj_id, $a_type)
91  {
92  include_once 'Services/MetaData/classes/class.ilMD.php';
93 //echo "-".$a_lm_id."-".$a_lm_obj_id."-".$a_type."-";
94  $md = new ilMD($a_lm_id, $a_lm_obj_id, $a_type);
95  $md_gen = $md->getGeneral();
96  if (is_object($md_gen))
97  {
98  foreach($md_gen->getIdentifierIds() as $id)
99  {
100  $md_id = $md_gen->getIdentifier($id);
101  if ($md_id->getCatalog() == "ILIAS_NID")
102  {
103  return $md_id->getEntry();
104  }
105  }
106  }
107 
108  return false;
109  }
110 
111 
115  function createMetaData()
116  {
117  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
118 
119  global $ilUser;
120 
121  $md_creator = new ilMDCreator($this->getLMId(), $this->getId(), $this->getType());
122  $md_creator->setTitle($this->getTitle());
123  $md_creator->setTitleLanguage($ilUser->getPref('language'));
124  $md_creator->setDescription($this->getDescription());
125  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
126  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
127  $md_creator->setLanguage($ilUser->getPref('language'));
128  $md_creator->create();
129 
130  return true;
131  }
132 
136  function updateMetaData()
137  {
138  include_once("Services/MetaData/classes/class.ilMD.php");
139  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
140  include_once("Services/MetaData/classes/class.ilMDDescription.php");
141 
142  $md =& new ilMD($this->getLMId(), $this->getId(), $this->getType());
143  $md_gen =& $md->getGeneral();
144  $md_gen->setTitle($this->getTitle());
145 
146  // sets first description (maybe not appropriate)
147  $md_des_ids =& $md_gen->getDescriptionIds();
148  if (count($md_des_ids) > 0)
149  {
150  $md_des =& $md_gen->getDescription($md_des_ids[0]);
151 // $md_des->setDescription($this->getDescription());
152  $md_des->update();
153  }
154  $md_gen->update();
155 
156  }
157 
158 
162  function deleteMetaData()
163  {
164  // Delete meta data
165  include_once('Services/MetaData/classes/class.ilMD.php');
166  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
167  $md->deleteAll();
168  }
169 
170 
171 
175  function setDataRecord($a_record)
176  {
177  $this->data_record = $a_record;
178  }
179 
180  function read()
181  {
182  global $ilBench, $ilDB;
183 
184  $ilBench->start("ContentPresentation", "ilLMObject_read");
185 
186  if(!isset($this->data_record))
187  {
188  $ilBench->start("ContentPresentation", "ilLMObject_read_getData");
189  $query = "SELECT * FROM lm_data WHERE obj_id = ".
190  $ilDB->quote($this->id, "integer");
191  $obj_set = $ilDB->query($query);
192  $this->data_record = $ilDB->fetchAssoc($obj_set);
193  $ilBench->stop("ContentPresentation", "ilLMObject_read_getData");
194  }
195 
196  $this->type = $this->data_record["type"];
197  $this->setImportId($this->data_record["import_id"]);
198  $this->setTitle($this->data_record["title"]);
199  $this->setLayout($this->data_record["layout"]);
200  //$this->setActive(ilUtil::yn2tf($this->data_record["active"]));
201 
202  $ilBench->stop("ContentPresentation", "ilLMObject_read");
203  }
204 
210  function setTitle($a_title)
211  {
212  $this->title = $a_title;
213  }
214 
220  function getTitle()
221  {
222  return $this->title;
223  }
224 
225 
231  static function _lookupTitle($a_obj_id)
232  {
233  global $ilDB;
234 
235  $query = "SELECT * FROM lm_data WHERE obj_id = ".
236  $ilDB->quote($a_obj_id, "integer");
237  $obj_set = $ilDB->query($query);
238  $obj_rec = $ilDB->fetchAssoc($obj_set);
239 
240  return $obj_rec["title"];
241  }
242 
249  static function _lookupType($a_obj_id, $a_lm_id = 0)
250  {
251  global $ilDB;
252 
253  if($a_lm_id)
254  {
255  $and = ' AND lm_id = '.$ilDB->quote($a_lm_id,'integer');
256  }
257 
258  $query = "SELECT * FROM lm_data WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").$and;
259  $obj_set = $ilDB->query($query);
260  $obj_rec = $ilDB->fetchAssoc($obj_set);
261 
262  return $obj_rec["type"];
263  }
264 
265 
266  function _writeTitle($a_obj_id, $a_title)
267  {
268  global $ilDB;
269 
270  $query = "UPDATE lm_data SET ".
271  " title = ".$ilDB->quote($a_title, "text").
272  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
273  $ilDB->manipulate($query);
274  }
275 
276 
277  function setDescription($a_description)
278  {
279  $this->description = $a_description;
280  }
281 
282  function getDescription()
283  {
284  return $this->description;
285  }
286 
287  function setType($a_type)
288  {
289  $this->type = $a_type;
290  }
291 
292  function getType()
293  {
294  return $this->type;
295  }
296 
297  function setLMId($a_lm_id)
298  {
299  $this->lm_id = $a_lm_id;
300 
301  }
302 
303  function getLMId()
304  {
305  return $this->lm_id;
306  }
307 
308  function setContentObject(&$a_content_obj)
309  {
310  $this->content_object =& $a_content_obj;
311  }
312 
313  function &getContentObject()
314  {
315  return $this->content_object;
316  }
317 
318  function setId($a_id)
319  {
320  $this->id = $a_id;
321  }
322 
323  function getId()
324  {
325  return $this->id;
326  }
327 
328  function getImportId()
329  {
330  return $this->import_id;
331  }
332 
333  function setImportId($a_id)
334  {
335  $this->import_id = $a_id;
336  }
337 
343  function setLayout($a_val)
344  {
345  $this->layout = $a_val;
346  }
347 
353  function getLayout()
354  {
355  return $this->layout;
356  }
357 
365  function _writeImportId($a_id, $a_import_id)
366  {
367  global $ilDB;
368 
369  $q = "UPDATE lm_data ".
370  "SET ".
371  "import_id = ".$ilDB->quote($a_import_id, "text").",".
372  "last_update = ".$ilDB->now()." ".
373  "WHERE obj_id = ".$ilDB->quote($a_id, "integer");
374 
375  $ilDB->manipulate($q);
376  }
377 
378  function create($a_upload = false)
379  {
380  global $ilDB;
381 
382  // insert object data
383  $this->setId($ilDB->nextId("lm_data"));
384  $query = "INSERT INTO lm_data (obj_id, title, type, layout, lm_id, import_id, create_date) ".
385  "VALUES (".
386  $ilDB->quote($this->getId(), "integer").",".
387  $ilDB->quote($this->getTitle(), "text").",".
388  $ilDB->quote($this->getType(), "text").", ".
389  $ilDB->quote($this->getLayout(), "text").", ".
390  $ilDB->quote($this->getLMId(), "integer").",".
391  $ilDB->quote($this->getImportId(), "text").
392  ", ".$ilDB->now().")";
393  $ilDB->manipulate($query);
394 
395  // create history entry
396  include_once("./Services/History/classes/class.ilHistory.php");
397  ilHistory::_createEntry($this->getId(), "create", "",
398  $this->content_object->getType().":".$this->getType());
399 
400  if (!$a_upload)
401  {
402  $this->createMetaData();
403  }
404 
405  }
406 
410  function update()
411  {
412  global $ilDB;
413 
414  $this->updateMetaData();
415 
416  $query = "UPDATE lm_data SET ".
417  " lm_id = ".$ilDB->quote($this->getLMId(), "integer").
418  " ,title = ".$ilDB->quote($this->getTitle(), "text").
419  " ,layout = ".$ilDB->quote($this->getLayout(), "text").
420  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
421 
422  $ilDB->manipulate($query);
423  }
424 
425 
434  function _writePublicAccessStatus($a_pages,$a_cont_obj_id)
435  {
436  global $ilDB,$ilLog,$ilErr,$ilTree;
437 
438  if (!is_array($a_pages))
439  {$a_pages = array(0);
440  /*$message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_pages must be an array');
441  $ilLog->write($message,$ilLog->WARNING);
442  $ilErr->raiseError($message,$ilErr->MESSAGE);
443  return false;*/
444  }
445 
446  if (empty($a_cont_obj_id))
447  {
448  $message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_cont_obj_id is empty');
449  $ilLog->write($message,$ilLog->WARNING);
450  $ilErr->raiseError($message,$ilErr->MESSAGE);
451  return false;
452  }
453 
454  // update structure entries: if at least one page of a chapter is public set chapter to public too
455  $lm_tree = new ilTree($a_cont_obj_id);
456  $lm_tree->setTableNames('lm_tree','lm_data');
457  $lm_tree->setTreeTablePK("lm_id");
458  $lm_tree->readRootId();
459 
460  // get all st entries of cont_obj
461  $q = "SELECT obj_id FROM lm_data " .
462  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
463  "AND type = 'st'";
464  $r = $ilDB->query($q);
465 
466  // add chapters with a public page to a_pages
467  while ($row = $ilDB->fetchAssoc($r))
468  {
469  $childs = $lm_tree->getChilds($row["obj_id"]);
470 
471  foreach ($childs as $page)
472  {
473  if ($page["type"] == "pg" and in_array($page["obj_id"],$a_pages))
474  {
475  array_push($a_pages, $row["obj_id"]);
476  break;
477  }
478  }
479  }
480 
481  // update public access status of all pages of cont_obj
482  $q = "UPDATE lm_data SET " .
483  "public_access = CASE " .
484  "WHEN ".$ilDB->in("obj_id", $a_pages, false, "integer")." ".
485  "THEN ".$ilDB->quote("y", "text").
486  "ELSE ".$ilDB->quote("n", "text").
487  "END " .
488  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
489  "AND ".$ilDB->in("type", array("pg", "st"), false, "text");
490  $ilDB->manipulate($q);
491 
492  return true;
493  }
494 
495  function _isPagePublic($a_node_id,$a_check_public_mode = false)
496  {
497  global $ilDB,$ilLog;
498 
499  if (empty($a_node_id))
500  {
501  $message = sprintf('ilLMObject::_isPagePublic(): Invalid parameter! $a_node_id is empty');
502  $ilLog->write($message,$ilLog->WARNING);
503  return false;
504  }
505 
506  if ($a_check_public_mode === true)
507  {
508  $lm_id = ilLMObject::_lookupContObjId($a_node_id);
509 
510  $q = "SELECT public_access_mode FROM content_object WHERE id = ".
511  $ilDB->quote($lm_id, "integer");
512  $r = $ilDB->query($q);
513  $row = $ilDB->fetchAssoc($r);
514 
515  if ($row["public_access_mode"] == "complete")
516  {
517  return true;
518  }
519  }
520 
521  $q = "SELECT public_access FROM lm_data WHERE obj_id=".
522  $ilDB->quote($a_node_id, "integer");
523  $r = $ilDB->query($q);
524  $row = $ilDB->fetchAssoc($r);
525 
526  return ilUtil::yn2tf($row["public_access"]);
527  }
528 
532  function delete($a_delete_meta_data = true)
533  {
534  global $ilDB;
535 
536  $query = "DELETE FROM lm_data WHERE obj_id = ".
537  $ilDB->quote($this->getId(), "integer");
538  $ilDB->manipulate($query);
539 
540  $this->deleteMetaData();
541  }
542 
554  function _getIdForImportId($a_import_id)
555  {
556  global $ilDB;
557 
558  $q = "SELECT obj_id FROM lm_data WHERE import_id = ".
559  $ilDB->quote($a_import_id, "text")." ".
560  " ORDER BY create_date DESC";
561  $obj_set = $ilDB->query($q);
562  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
563  {
564  $lm_id = ilLMObject::_lookupContObjID($obj_rec["obj_id"]);
565 
566  // link only in learning module, that is not trashed
568  {
569  return $obj_rec["obj_id"];
570  }
571  }
572 
573  return 0;
574  }
575 
585  function _getAllObjectsForImportId($a_import_id, $a_in_lm = 0)
586  {
587  global $ilDB;
588 
589  $where = ($a_in_lm > 0)
590  ? " AND lm_id = ".$ilDB->quote($a_in_lm, "integer")." "
591  : "";
592 
593  $q = "SELECT * FROM lm_data WHERE import_id = ".
594  $ilDB->quote($a_import_id, "text")." ".
595  $where.
596  " ORDER BY create_date DESC";
597  $obj_set = $ilDB->query($q);
598 
599  $items = array();
600  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
601  {
602  // check, whether lm is not trashed
603  if (ilObject::_hasUntrashedReference($obj_rec["lm_id"]))
604  {
605  $items[] = $obj_rec;
606  }
607  }
608 
609  return $items;
610  }
611 
619  function _exists($a_id)
620  {
621  global $ilDB;
622 
623  include_once("./Services/COPage/classes/class.ilInternalLink.php");
624  if (is_int(strpos($a_id, "_")))
625  {
627  }
628 
629  $q = "SELECT * FROM lm_data WHERE obj_id = ".
630  $ilDB->quote($a_id, "integer");
631  $obj_set = $ilDB->query($q);
632  if ($obj_rec = $ilDB->fetchAssoc($obj_set))
633  {
634  return true;
635  }
636  else
637  {
638  return false;
639  }
640 
641  }
642 
646  function getObjectList($lm_id, $type = "")
647  {
648  global $ilDB;
649 
650  $type_str = ($type != "")
651  ? "AND type = ".$ilDB->quote($type, "text")." "
652  : "";
653  $query = "SELECT * FROM lm_data ".
654  "WHERE lm_id= ".$ilDB->quote($lm_id, "integer")." ".
655  $type_str." ".
656  "ORDER BY title";
657  $obj_set = $ilDB->query($query);
658  $obj_list = array();
659  while($obj_rec = $ilDB->fetchAssoc($obj_set))
660  {
661  $obj_list[] = array("obj_id" => $obj_rec["obj_id"],
662  "title" => $obj_rec["title"],
663  "import_id" => $obj_rec["import_id"],
664  "type" => $obj_rec["type"]);
665  }
666  return $obj_list;
667  }
668 
669 
673  function _deleteAllObjectData(&$a_cobj)
674  {
675  global $ilDB;
676 
677  include_once './Services/Xml/classes/class.ilNestedSetXML.php';
678 
679  $query = "SELECT * FROM lm_data ".
680  "WHERE lm_id= ".$ilDB->quote($a_cobj->getId(), "integer");
681  $obj_set = $ilDB->query($query);
682 
683  require_once("./Modules/LearningModule/classes/class.ilLMObjectFactory.php");
684  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
685  {
686  $lm_obj = ilLMObjectFactory::getInstance($a_cobj, $obj_rec["obj_id"],false);
687 
688  if (is_object($lm_obj))
689  {
690  $lm_obj->delete(true);
691  }
692  }
693 
694  return true;
695  }
696 
700  function _lookupContObjID($a_id)
701  {
702  global $ilDB;
703 
704  $query = "SELECT * FROM lm_data WHERE obj_id = ".
705  $ilDB->quote($a_id, "integer");
706  $obj_set = $ilDB->query($query);
707  $obj_rec = $ilDB->fetchAssoc($obj_set);
708 
709  return $obj_rec["lm_id"];
710  }
711 
715  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
716  {
717  global $ilLog;
718 
719  $tree = new ilTree($a_obj->getContentObject()->getId());
720  $tree->setTableNames('lm_tree', 'lm_data');
721  $tree->setTreeTablePK("lm_id");
722 
723  // determine parent
724  $parent_id = ($a_parent_id != "")
725  ? $a_parent_id
726  : $tree->getRootId();
727 
728  // determine target
729  if ($a_target_node_id != "")
730  {
731  $target = $a_target_node_id;
732  }
733  else
734  {
735  // determine last child that serves as predecessor
736  if ($a_obj->getType() == "st")
737  {
738  $s_types = array("st", "pg");
739  $childs =& $tree->getChildsByTypeFilter($parent_id, $s_types);
740  }
741  else
742  {
743  $s_types = "pg";
744  $childs =& $tree->getChildsByType($parent_id, $s_types);
745  }
746 
747  if (count($childs) == 0)
748  {
749  $target = IL_FIRST_NODE;
750  }
751  else
752  {
753  $target = $childs[count($childs) - 1]["obj_id"];
754  }
755  }
756 
757  if ($tree->isInTree($parent_id) && !$tree->isInTree($a_obj->getId()))
758  {
759  $ilLog->write("LMObject::putInTree: insertNode, ID: ".$a_obj->getId().
760  "Parent ID: ".$parent_id.", Target: ".$target);
761 
762  $tree->insertNode($a_obj->getId(), $parent_id, $target);
763  }
764  }
765 
773  static function getTree($a_cont_obj_id)
774  {
775  $tree = new ilTree($a_cont_obj_id);
776  $tree->setTableNames('lm_tree', 'lm_data');
777  $tree->setTreeTablePK("lm_id");
778  $tree->readRootId();
779 
780  return $tree;
781  }
782 
786  function clipboardCut($a_cont_obj_id, $a_ids)
787  {
788  $tree = ilLMObject::getTree($a_cont_obj_id);
789 
790  if (!is_array($a_ids))
791  {
792  return false;
793  }
794  else
795  {
796  // get all "top" ids, i.e. remove ids, that have a selected parent
797  foreach($a_ids as $id)
798  {
799  $path = $tree->getPathId($id);
800  $take = true;
801  foreach($path as $path_id)
802  {
803  if ($path_id != $id && in_array($path_id, $a_ids))
804  {
805  $take = false;
806  }
807  }
808  if ($take)
809  {
810  $cut_ids[] = $id;
811  }
812  }
813  }
814 
815  ilLMObject::clipboardCopy($a_cont_obj_id, $cut_ids);
816 
817  // remove the objects from the tree
818  // note: we are getting chapters which are *not* in the tree
819  // we do not delete any pages/chapters here
820  foreach ($cut_ids as $id)
821  {
822  $curnode = $tree->getNodeData($id);
823  if ($tree->isInTree($id))
824  {
825  $tree->deleteTree($curnode);
826  }
827  }
828 
829  }
830 
834  static function clipboardCopy($a_cont_obj_id, $a_ids)
835  {
836  global $ilUser;
837 
838  $tree = ilLMObject::getTree($a_cont_obj_id);
839 
840  $ilUser->clipboardDeleteObjectsOfType("pg");
841  $ilUser->clipboardDeleteObjectsOfType("st");
842 
843  // put them into the clipboard
844  $time = date("Y-m-d H:i:s", time());
845  foreach ($a_ids as $id)
846  {
847  $curnode = "";
848  if ($tree->isInTree($id))
849  {
850  $curnode = $tree->getNodeData($id);
851  $subnodes = $tree->getSubTree($curnode);
852  foreach($subnodes as $subnode)
853  {
854  if ($subnode["child"] != $id)
855  {
856  $ilUser->addObjectToClipboard($subnode["child"],
857  $subnode["type"], $subnode["title"],
858  $subnode["parent"], $time, $subnode["lft"]);
859  }
860  }
861  }
862  $order = ($curnode["lft"] > 0)
863  ? $curnode["lft"]
864  : (int) ($order + 1);
865  $ilUser->addObjectToClipboard($id,
867  $order);
868  }
869  }
870 
874  static function pasteTree($a_target_lm, $a_item_id, $a_parent_id, $a_target, $a_insert_time,
875  &$a_copied_nodes, $a_as_copy = false, $a_source_lm = null)
876  {
877  global $ilUser, $ilias, $ilLog;
878 
879  include_once("./Modules/LearningModule/classes/class.ilStructureObject.php");
880  include_once("./Modules/LearningModule/classes/class.ilLMPageObject.php");
881 
882  $item_lm_id = ilLMObject::_lookupContObjID($a_item_id);
883  $item_type = ilLMObject::_lookupType($a_item_id);
884  $lm_obj = $ilias->obj_factory->getInstanceByObjId($item_lm_id);
885  if ($item_type == "st")
886  {
887  $item = new ilStructureObject($lm_obj, $a_item_id);
888  }
889  else if ($item_type == "pg")
890  {
891  $item = new ilLMPageObject($lm_obj, $a_item_id);
892  }
893 
894  $ilLog->write("Getting from clipboard type ".$item_type.", ".
895  "Item ID: ".$a_item_id.", of original LM: ".$item_lm_id);
896 
897  if ($item_lm_id != $a_target_lm->getId() && !$a_as_copy)
898  {
899  // @todo: check whether st is NOT in tree
900 
901  // "move" metadata to new lm
902  include_once("Services/MetaData/classes/class.ilMD.php");
903  $md = new ilMD($item_lm_id, $item->getId(), $item->getType());
904  $new_md = $md->cloneMD($a_target_lm->getId(), $item->getId(), $item->getType());
905 
906  // update lm object
907  $item->setLMId($a_target_lm->getId());
908  $item->setContentObject($a_target_lm);
909  $item->update();
910 
911  // delete old meta data set
912  $md->deleteAll();
913 
914  if ($item_type == "pg")
915  {
916  $page = $item->getPageObject();
917  $page->buildDom();
918  $page->setParentId($a_target_lm->getId());
919  $page->update();
920  }
921  }
922 
923  if ($a_as_copy)
924  {
925  $target_item = $item->copy($a_target_lm);
926  $a_copied_nodes[$item->getId()] = $target_item->getId();
927  }
928  else
929  {
930  $target_item = $item;
931  }
932 
933  $ilLog->write("Putting into tree type ".$target_item->getType().
934  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
935  "Target: ".$a_target.", Item LM:".$target_item->getContentObject()->getId());
936 
937  ilLMObject::putInTree($target_item, $a_parent_id, $a_target);
938 
939  if ($a_source_lm == null)
940  {
941  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
942  }
943  else
944  {
945  $childs = $a_source_lm->lm_tree->getChilds($item->getId());
946  foreach ($childs as $k => $child)
947  {
948  $childs[$k]["id"] = $childs[$k]["child"];
949  }
950  }
951 
952  foreach($childs as $child)
953  {
954  ilLMObject::pasteTree($a_target_lm, $child["id"], $target_item->getId(),
955  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy, $a_source_lm);
956  }
957 
958  return $target_item->getId();
959  // @todo: write history (see pastePage)
960  }
961 
967  static function saveTitles($a_lm, $a_titles)
968  {
969  if (is_array($a_titles))
970  {
971  include_once("./Services/MetaData/classes/class.ilMD.php");
972  foreach($a_titles as $id => $title)
973  {
974  $lmobj = ilLMObjectFactory::getInstance($a_lm, $id, false);
975  if (is_object($lmobj))
976  {
977  // Update Title and description
978  $md = new ilMD($a_lm->getId(), $id, $lmobj->getType());
979  $md_gen = $md->getGeneral();
980  $md_gen->setTitle($title);
981  $md_gen->update();
982  $md->update();
984  }
985  }
986  }
987  }
988 
992  static function updateInternalLinks($a_copied_nodes, $a_parent_type = "lm")
993  {
994  $all_fixes = array();
995  foreach($a_copied_nodes as $original_id => $copied_id)
996  {
997  $copied_type = ilLMObject::_lookupType($copied_id);
998  $copy_lm = ilLMObject::_lookupContObjID($copied_id);
999 
1000  if ($copied_type == "pg")
1001  {
1002  //
1003  // 1. Outgoing links from the copied page.
1004  //
1005  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
1006  $tpg = new ilPageObject($a_parent_type, $copied_id);
1007  $tpg->buildDom();
1008  $il = $tpg->getInternalLinks();
1009  $targets = array();
1010  foreach($il as $l)
1011  {
1012  $targets[] = array("type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
1013  "id" => (int) ilInternalLink::_extractObjIdOfTarget($l["Target"]),
1014  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"]));
1015  }
1016  $fix = array();
1017  foreach($targets as $target)
1018  {
1019  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
1020  ($target["type"] == "pg" || $target["type"] == "st"))
1021  {
1022  // first check, whether target is also within the copied set
1023  if ($a_copied_nodes[$target["id"]] > 0)
1024  {
1025  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
1026  }
1027  else
1028  {
1029  // now check, if a copy if the target is already in the same lm
1030 
1031  // only if target is not already in the same lm!
1032  $trg_lm = ilLMObject::_lookupContObjID($target["id"]);
1033  if ($trg_lm != $copy_lm)
1034  {
1035  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$target["type"]."_".$target["id"]);
1036  $found = false;
1037 
1038  foreach($lm_data as $item)
1039  {
1040  if (!$found && ($item["lm_id"] == $copy_lm))
1041  {
1042  $fix[$target["id"]] = $item["obj_id"];
1043  $found = true;
1044  }
1045  }
1046  }
1047  }
1048  }
1049  }
1050 
1051  // outgoing links to be fixed
1052  if (count($fix) > 0)
1053  {
1054 //echo "<br>--".$copied_id;
1055 //var_dump($fix);
1056  $t = ilObject::_lookupType($copy_lm);
1057  if (is_array($all_fixes[$t.":".$copied_id]))
1058  {
1059  $all_fixes[$t.":".$copied_id] += $fix;
1060  }
1061  else
1062  {
1063  $all_fixes[$t.":".$copied_id] = $fix;
1064  }
1065 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $copied_id);
1066 // $page->moveIntLinks($fix);
1067 // $page->update();
1068  }
1069  }
1070 
1071  if ($copied_type == "pg" ||
1072  $copied_type == "st")
1073  {
1074 
1075  //
1076  // 2. Incoming links to the original pages
1077  //
1078  // A->B A2 (A+B currently copied)
1079  // A->C B2
1080  // B->A
1081  // C->A C2->A (C already copied)
1082  $original_lm = ilLMObject::_lookupContObjID($original_id);
1083  $original_type = ilObject::_lookupType($original_lm);
1084 
1085  if ($original_lm != $copy_lm)
1086  {
1087 
1088  // This gets sources that link to A+B (so we have C here)
1089  // (this also does already the trick when instance map areas are given in C)
1090  // int_link, where target_type, target_id, target_inst -> ok
1091  $sources = ilInternalLink::_getSourcesOfTarget($copied_type,
1092  $original_id, 0);
1093 
1094  // mobs linking to $original_id
1095  // map_area, where link_type, target -> ok
1096  $mobs = ilMapArea::_getMobsForTarget("int", "il__".$copied_type.
1097  "_".$original_id);
1098 
1099  // pages using these mobs
1100  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1101  foreach($mobs as $mob)
1102  {
1103  // mob_usage, where id -> ok
1104  // mep_item, where foreign_id, type -> ok
1105  // mep_tree, where child -> already existed
1106  // il_news_item, where mob_id -> ok
1107  // map_area, where link_type, target -> aready existed
1108  // media_item, where id -> already existed
1109  // personal_clipboard, where item_id, type -> ok
1110  $usages = ilObjMediaObject::lookupUsages($mob);
1111  foreach($usages as $usage)
1112  {
1113  if ($usage["type"] == "lm:pg" | $usage["type"] == "lm:st")
1114  {
1115  $sources[] = $usage;
1116  }
1117  }
1118  }
1119  $fix = array();
1120  foreach($sources as $source)
1121  {
1122  $stype = explode(":", $source["type"]);
1123  $source_type = $stype[1];
1124 
1125  if ($source_type == "pg" || $source_type == "st")
1126  {
1127  // first of all: source must be in original lm
1128  $src_lm = ilLMObject::_lookupContObjID($source["id"]);
1129 
1130  if ($src_lm == $original_lm)
1131  {
1132  // check, if a copy if the source is already in the same lm
1133  // now we look for the latest copy of C in LM2
1134  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$source_type."_".$source["id"],
1135  $copy_lm);
1136  $found = false;
1137  foreach ($lm_data as $item)
1138  {
1139  if (!$found)
1140  {
1141  $fix[$item["obj_id"]][$original_id] = $copied_id;
1142  $found = true;
1143  }
1144  }
1145  }
1146  }
1147  }
1148  // outgoing links to be fixed
1149  if (count($fix) > 0)
1150  {
1151  foreach ($fix as $page_id => $fix_array)
1152  {
1153 //echo "<br>++".$page_id;
1154 //var_dump($fix_array);
1155 
1156  $t = ilObject::_lookupType($copy_lm);
1157  if (is_array($all_fixes[$t.":".$page_id]))
1158  {
1159  $all_fixes[$t.":".$page_id] += $fix_array;
1160  }
1161  else
1162  {
1163  $all_fixes[$t.":".$page_id] = $fix_array;
1164  }
1165 
1166 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $page_id);
1167 // $page->moveIntLinks($fix_array);
1168 // $page->update();
1169  }
1170  }
1171  }
1172  }
1173  }
1174 
1175  foreach ($all_fixes as $pg => $fixes)
1176  {
1177 //echo "<br>**".$pg;
1178 //echo var_dump($fixes);
1179  $pg = explode(":", $pg);
1180  $page = new ilPageObject($pg[0], $pg[1]);
1181  if ($page->moveIntLinks($fixes))
1182  {
1183  $page->update(true, true, true);
1184  }
1185  }
1186  }
1187 
1191  static function uniqueTypesCheck($a_items)
1192  {
1193  $types = array();
1194  if (is_array($a_items))
1195  {
1196  foreach($a_items as $item)
1197  {
1198  $type = ilLMObject::_lookupType($item);
1199  $types[$type] = $type;
1200  }
1201  }
1202 
1203  if (count($types) > 1)
1204  {
1205  return false;
1206  }
1207  return true;
1208  }
1209 
1216  static function writeLayout($a_obj_id, $a_layout, $a_lm = null)
1217  {
1218  global $ilDB;
1219 
1220  $t = ilLMObject::_lookupType($a_obj_id);
1221 
1222  if ($t == "pg")
1223  {
1224  $query = "UPDATE lm_data SET ".
1225  " layout = ".$ilDB->quote($a_layout, "text").
1226  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1227  $ilDB->manipulate($query);
1228  }
1229  else if ($t == "st" && is_object($a_lm))
1230  {
1231  $node = $a_lm->getLMTree()->getNodeData($a_obj_id);
1232  $child_nodes = $a_lm->getLMTree()->getSubTree($node);
1233  if (is_array($child_nodes) && count($child_nodes) > 0)
1234  {
1235  foreach ($child_nodes as $c)
1236  {
1237  if ($c["type"] == "pg")
1238  {
1239  $query = "UPDATE lm_data SET ".
1240  " layout = ".$ilDB->quote($a_layout, "text").
1241  " WHERE obj_id = ".$ilDB->quote($c["child"], "integer");
1242  $ilDB->manipulate($query);
1243  }
1244  }
1245  }
1246  }
1247  }
1248 
1254  static function lookupLayout($a_obj_id)
1255  {
1256  global $ilDB;
1257 
1258  $query = "SELECT layout FROM lm_data WHERE obj_id = ".
1259  $ilDB->quote($a_obj_id, "integer");
1260  $obj_set = $ilDB->query($query);
1261  $obj_rec = $ilDB->fetchAssoc($obj_set);
1262 
1263  return $obj_rec["layout"];
1264  }
1265 
1272  static function getPagesOfChapter($a_lm_id, $a_chap_id)
1273  {
1274  // update structure entries: if at least one page of a chapter is public set chapter to public too
1275  $lm_tree = new ilTree($a_lm_id);
1276  $lm_tree->setTableNames('lm_tree','lm_data');
1277  $lm_tree->setTreeTablePK("lm_id");
1278  $lm_tree->readRootId();
1279 
1280  $childs = $lm_tree->getChildsByType($a_chap_id, "pg");
1281 
1282  return $childs;
1283  }
1284 
1285 
1289 
1296  public static function saveExportId($a_lm_id, $a_lmobj_id, $a_exp_id, $a_type = "pg")
1297  {
1298  global $ilDB;
1299 
1300  include_once("Services/MetaData/classes/class.ilMDIdentifier.php");
1301 
1302  if (trim($a_exp_id) == "")
1303  {
1304  // delete export ids, if existing
1306  $a_lm_id, $a_lmobj_id, $a_type);
1307 
1308  foreach ($entries as $id => $e)
1309  {
1310  if ($e["catalog"] == "ILIAS_NID")
1311  {
1312  $identifier = new ilMDIdentifier();
1313  $identifier->setMetaId($id);
1314  $identifier->delete();
1315  }
1316  }
1317  }
1318  else
1319  {
1320  // update existing entry
1322  $a_lm_id, $a_lmobj_id, $a_type);
1323 
1324  $updated = false;
1325  foreach ($entries as $id => $e)
1326  {
1327  if ($e["catalog"] == "ILIAS_NID")
1328  {
1329  $identifier = new ilMDIdentifier();
1330  $identifier->setMetaId($id);
1331  $identifier->read();
1332  $identifier->setEntry($a_exp_id);
1333  $identifier->update();
1334  $updated = true;
1335  }
1336  }
1337 
1338  // nothing updated? create a new one
1339  if (!$updated)
1340  {
1341  include_once("./Services/MetaData/classes/class.ilMD.php");
1342  $md = new ilMD($a_lm_id, $a_lmobj_id, $a_type);
1343  $md_gen = $md->getGeneral();
1344  $identifier = $md_gen->addIdentifier();
1345  $identifier->setEntry($a_exp_id);
1346  $identifier->setCatalog("ILIAS_NID");
1347  $identifier->save();
1348  }
1349  }
1350 
1351  }
1352 
1359  public static function getExportId($a_lm_id, $a_lmobj_id, $a_type = "pg")
1360  {
1361  // look for export id
1362  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1364  $a_lm_id, $a_lmobj_id, $a_type);
1365 
1366  foreach ($entries as $e)
1367  {
1368  if ($e["catalog"] == "ILIAS_NID")
1369  {
1370  return $e["entry"];
1371  }
1372  }
1373  }
1374 
1381  function existsExportID($a_lm_id, $a_exp_id, $a_type = "pg")
1382  {
1383  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1384  return ilMDIdentifier::existsIdInRbacObject($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1385  }
1386 
1390  public static function getDuplicateExportIDs($a_lm_id, $a_type = "pg")
1391  {
1392  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1393  $entries = ilMDIdentifier::_getEntriesForRbacObj($a_lm_id, $a_type);
1394  $res = array();
1395  foreach ($entries as $e)
1396  {
1397  if ($e["catalog"] == "ILIAS_NID")
1398  {
1399  if (ilLMObject::_exists($e["obj_id"]))
1400  {
1401  $res[trim($e["entry"])]++;
1402  }
1403  }
1404  }
1405  return $res;
1406  }
1407 
1414  function getExportIDInfo($a_lm_id, $a_exp_id, $a_type = "pg")
1415  {
1416  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1417  $data = ilMDIdentifier::readIdData($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1418  return $data;
1419  }
1420 
1421 }
1422 ?>