ILIAS  Release_4_2_x_branch Revision 61807
 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("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  "type" => $obj_rec["type"]);
664  }
665  return $obj_list;
666  }
667 
668 
672  function _deleteAllObjectData(&$a_cobj)
673  {
674  global $ilDB;
675 
676  include_once './classes/class.ilNestedSetXML.php';
677 
678  $query = "SELECT * FROM lm_data ".
679  "WHERE lm_id= ".$ilDB->quote($a_cobj->getId(), "integer");
680  $obj_set = $ilDB->query($query);
681 
682  require_once("./Modules/LearningModule/classes/class.ilLMObjectFactory.php");
683  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
684  {
685  $lm_obj = ilLMObjectFactory::getInstance($a_cobj, $obj_rec["obj_id"],false);
686 
687  if (is_object($lm_obj))
688  {
689  $lm_obj->delete(true);
690  }
691  }
692 
693  return true;
694  }
695 
699  function _lookupContObjID($a_id)
700  {
701  global $ilDB;
702 
703  $query = "SELECT * FROM lm_data WHERE obj_id = ".
704  $ilDB->quote($a_id, "integer");
705  $obj_set = $ilDB->query($query);
706  $obj_rec = $ilDB->fetchAssoc($obj_set);
707 
708  return $obj_rec["lm_id"];
709  }
710 
714  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
715  {
716  global $ilLog;
717 
718  $tree = new ilTree($a_obj->getContentObject()->getId());
719  $tree->setTableNames('lm_tree', 'lm_data');
720  $tree->setTreeTablePK("lm_id");
721 
722  // determine parent
723  $parent_id = ($a_parent_id != "")
724  ? $a_parent_id
725  : $tree->getRootId();
726 
727  // determine target
728  if ($a_target_node_id != "")
729  {
730  $target = $a_target_node_id;
731  }
732  else
733  {
734  // determine last child that serves as predecessor
735  if ($a_obj->getType() == "st")
736  {
737  $s_types = array("st", "pg");
738  $childs =& $tree->getChildsByTypeFilter($parent_id, $s_types);
739  }
740  else
741  {
742  $s_types = "pg";
743  $childs =& $tree->getChildsByType($parent_id, $s_types);
744  }
745 
746  if (count($childs) == 0)
747  {
748  $target = IL_FIRST_NODE;
749  }
750  else
751  {
752  $target = $childs[count($childs) - 1]["obj_id"];
753  }
754  }
755 
756  if ($tree->isInTree($parent_id) && !$tree->isInTree($a_obj->getId()))
757  {
758  $ilLog->write("LMObject::putInTree: insertNode, ID: ".$a_obj->getId().
759  "Parent ID: ".$parent_id.", Target: ".$target);
760 
761  $tree->insertNode($a_obj->getId(), $parent_id, $target);
762  }
763  }
764 
772  static function getTree($a_cont_obj_id)
773  {
774  $tree = new ilTree($a_cont_obj_id);
775  $tree->setTableNames('lm_tree', 'lm_data');
776  $tree->setTreeTablePK("lm_id");
777  $tree->readRootId();
778 
779  return $tree;
780  }
781 
785  function clipboardCut($a_cont_obj_id, $a_ids)
786  {
787  $tree = ilLMObject::getTree($a_cont_obj_id);
788 
789  if (!is_array($a_ids))
790  {
791  return false;
792  }
793  else
794  {
795  // get all "top" ids, i.e. remove ids, that have a selected parent
796  foreach($a_ids as $id)
797  {
798  $path = $tree->getPathId($id);
799  $take = true;
800  foreach($path as $path_id)
801  {
802  if ($path_id != $id && in_array($path_id, $a_ids))
803  {
804  $take = false;
805  }
806  }
807  if ($take)
808  {
809  $cut_ids[] = $id;
810  }
811  }
812  }
813 
814  ilLMObject::clipboardCopy($a_cont_obj_id, $cut_ids);
815 
816  // remove the objects from the tree
817  // note: we are getting chapters which are *not* in the tree
818  // we do not delete any pages/chapters here
819  foreach ($cut_ids as $id)
820  {
821  $curnode = $tree->getNodeData($id);
822  if ($tree->isInTree($id))
823  {
824  $tree->deleteTree($curnode);
825  }
826  }
827 
828  }
829 
833  static function clipboardCopy($a_cont_obj_id, $a_ids)
834  {
835  global $ilUser;
836 
837  $tree = ilLMObject::getTree($a_cont_obj_id);
838 
839  $ilUser->clipboardDeleteObjectsOfType("pg");
840  $ilUser->clipboardDeleteObjectsOfType("st");
841 
842  // put them into the clipboard
843  $time = date("Y-m-d H:i:s", time());
844  foreach ($a_ids as $id)
845  {
846  $curnode = "";
847  if ($tree->isInTree($id))
848  {
849  $curnode = $tree->getNodeData($id);
850  $subnodes = $tree->getSubTree($curnode);
851  foreach($subnodes as $subnode)
852  {
853  if ($subnode["child"] != $id)
854  {
855  $ilUser->addObjectToClipboard($subnode["child"],
856  $subnode["type"], $subnode["title"],
857  $subnode["parent"], $time, $subnode["lft"]);
858  }
859  }
860  }
861  $order = ($curnode["lft"] > 0)
862  ? $curnode["lft"]
863  : (int) ($order + 1);
864  $ilUser->addObjectToClipboard($id,
866  $order);
867  }
868  }
869 
873  static function pasteTree($a_target_lm, $a_item_id, $a_parent_id, $a_target, $a_insert_time,
874  &$a_copied_nodes, $a_as_copy = false, $a_source_lm = null)
875  {
876  global $ilUser, $ilias, $ilLog;
877 
878  include_once("./Modules/LearningModule/classes/class.ilStructureObject.php");
879  include_once("./Modules/LearningModule/classes/class.ilLMPageObject.php");
880 
881  $item_lm_id = ilLMObject::_lookupContObjID($a_item_id);
882  $item_type = ilLMObject::_lookupType($a_item_id);
883  $lm_obj = $ilias->obj_factory->getInstanceByObjId($item_lm_id);
884  if ($item_type == "st")
885  {
886  $item = new ilStructureObject($lm_obj, $a_item_id);
887  }
888  else if ($item_type == "pg")
889  {
890  $item = new ilLMPageObject($lm_obj, $a_item_id);
891  }
892 
893  $ilLog->write("Getting from clipboard type ".$item_type.", ".
894  "Item ID: ".$a_item_id.", of original LM: ".$item_lm_id);
895 
896  if ($item_lm_id != $a_target_lm->getId() && !$a_as_copy)
897  {
898  // @todo: check whether st is NOT in tree
899 
900  // "move" metadata to new lm
901  include_once("Services/MetaData/classes/class.ilMD.php");
902  $md = new ilMD($item_lm_id, $item->getId(), $item->getType());
903  $new_md = $md->cloneMD($a_target_lm->getId(), $item->getId(), $item->getType());
904 
905  // update lm object
906  $item->setLMId($a_target_lm->getId());
907  $item->setContentObject($a_target_lm);
908  $item->update();
909 
910  // delete old meta data set
911  $md->deleteAll();
912 
913  if ($item_type == "pg")
914  {
915  $page = $item->getPageObject();
916  $page->buildDom();
917  $page->setParentId($a_target_lm->getId());
918  $page->update();
919  }
920  }
921 
922  if ($a_as_copy)
923  {
924  $target_item = $item->copy($a_target_lm);
925  $a_copied_nodes[$item->getId()] = $target_item->getId();
926  }
927  else
928  {
929  $target_item = $item;
930  }
931 
932  $ilLog->write("Putting into tree type ".$target_item->getType().
933  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
934  "Target: ".$a_target.", Item LM:".$target_item->getContentObject()->getId());
935 
936  ilLMObject::putInTree($target_item, $a_parent_id, $a_target);
937 
938  if ($a_source_lm == null)
939  {
940  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
941  }
942  else
943  {
944  $childs = $a_source_lm->lm_tree->getChilds($item->getId());
945  foreach ($childs as $k => $child)
946  {
947  $childs[$k]["id"] = $childs[$k]["child"];
948  }
949  }
950 
951  foreach($childs as $child)
952  {
953  ilLMObject::pasteTree($a_target_lm, $child["id"], $target_item->getId(),
954  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy, $a_source_lm);
955  }
956 
957  return $target_item->getId();
958  // @todo: write history (see pastePage)
959  }
960 
966  static function saveTitles($a_lm, $a_titles)
967  {
968  if (is_array($a_titles))
969  {
970  include_once("./Services/MetaData/classes/class.ilMD.php");
971  foreach($a_titles as $id => $title)
972  {
973  $lmobj = ilLMObjectFactory::getInstance($a_lm, $id, false);
974  if (is_object($lmobj))
975  {
976  // Update Title and description
977  $md = new ilMD($a_lm->getId(), $id, $lmobj->getType());
978  $md_gen = $md->getGeneral();
979  $md_gen->setTitle($title);
980  $md_gen->update();
981  $md->update();
983  }
984  }
985  }
986  }
987 
991  static function updateInternalLinks($a_copied_nodes, $a_parent_type = "lm")
992  {
993  $all_fixes = array();
994  foreach($a_copied_nodes as $original_id => $copied_id)
995  {
996  $copied_type = ilLMObject::_lookupType($copied_id);
997  $copy_lm = ilLMObject::_lookupContObjID($copied_id);
998 
999  if ($copied_type == "pg")
1000  {
1001  //
1002  // 1. Outgoing links from the copied page.
1003  //
1004  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
1005  $tpg = new ilPageObject($a_parent_type, $copied_id);
1006  $tpg->buildDom();
1007  $il = $tpg->getInternalLinks();
1008  $targets = array();
1009  foreach($il as $l)
1010  {
1011  $targets[] = array("type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
1012  "id" => (int) ilInternalLink::_extractObjIdOfTarget($l["Target"]),
1013  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"]));
1014  }
1015  $fix = array();
1016  foreach($targets as $target)
1017  {
1018  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
1019  ($target["type"] == "pg" || $target["type"] == "st"))
1020  {
1021  // first check, whether target is also within the copied set
1022  if ($a_copied_nodes[$target["id"]] > 0)
1023  {
1024  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
1025  }
1026  else
1027  {
1028  // now check, if a copy if the target is already in the same lm
1029 
1030  // only if target is not already in the same lm!
1031  $trg_lm = ilLMObject::_lookupContObjID($target["id"]);
1032  if ($trg_lm != $copy_lm)
1033  {
1034  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$target["type"]."_".$target["id"]);
1035  $found = false;
1036 
1037  foreach($lm_data as $item)
1038  {
1039  if (!$found && ($item["lm_id"] == $copy_lm))
1040  {
1041  $fix[$target["id"]] = $item["obj_id"];
1042  $found = true;
1043  }
1044  }
1045  }
1046  }
1047  }
1048  }
1049 
1050  // outgoing links to be fixed
1051  if (count($fix) > 0)
1052  {
1053 //echo "<br>--".$copied_id;
1054 //var_dump($fix);
1055  $t = ilObject::_lookupType($copy_lm);
1056  if (is_array($all_fixes[$t.":".$copied_id]))
1057  {
1058  $all_fixes[$t.":".$copied_id] += $fix;
1059  }
1060  else
1061  {
1062  $all_fixes[$t.":".$copied_id] = $fix;
1063  }
1064 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $copied_id);
1065 // $page->moveIntLinks($fix);
1066 // $page->update();
1067  }
1068  }
1069 
1070  if ($copied_type == "pg" ||
1071  $copied_type == "st")
1072  {
1073 
1074  //
1075  // 2. Incoming links to the original pages
1076  //
1077  // A->B A2 (A+B currently copied)
1078  // A->C B2
1079  // B->A
1080  // C->A C2->A (C already copied)
1081  $original_lm = ilLMObject::_lookupContObjID($original_id);
1082  $original_type = ilObject::_lookupType($original_lm);
1083 
1084  if ($original_lm != $copy_lm)
1085  {
1086 
1087  // This gets sources that link to A+B (so we have C here)
1088  // (this also does already the trick when instance map areas are given in C)
1089  // int_link, where target_type, target_id, target_inst -> ok
1090  $sources = ilInternalLink::_getSourcesOfTarget($copied_type,
1091  $original_id, 0);
1092 
1093  // mobs linking to $original_id
1094  // map_area, where link_type, target -> ok
1095  $mobs = ilMapArea::_getMobsForTarget("int", "il__".$copied_type.
1096  "_".$original_id);
1097 
1098  // pages using these mobs
1099  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1100  foreach($mobs as $mob)
1101  {
1102  // mob_usage, where id -> ok
1103  // mep_item, where foreign_id, type -> ok
1104  // mep_tree, where child -> already existed
1105  // il_news_item, where mob_id -> ok
1106  // map_area, where link_type, target -> aready existed
1107  // media_item, where id -> already existed
1108  // personal_clipboard, where item_id, type -> ok
1109  $usages = ilObjMediaObject::lookupUsages($mob);
1110  foreach($usages as $usage)
1111  {
1112  if ($usage["type"] == "lm:pg" | $usage["type"] == "lm:st")
1113  {
1114  $sources[] = $usage;
1115  }
1116  }
1117  }
1118  $fix = array();
1119  foreach($sources as $source)
1120  {
1121  $stype = explode(":", $source["type"]);
1122  $source_type = $stype[1];
1123 
1124  if ($source_type == "pg" || $source_type == "st")
1125  {
1126  // first of all: source must be in original lm
1127  $src_lm = ilLMObject::_lookupContObjID($source["id"]);
1128 
1129  if ($src_lm == $original_lm)
1130  {
1131  // check, if a copy if the source is already in the same lm
1132  // now we look for the latest copy of C in LM2
1133  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$source_type."_".$source["id"],
1134  $copy_lm);
1135  $found = false;
1136  foreach ($lm_data as $item)
1137  {
1138  if (!$found)
1139  {
1140  $fix[$item["obj_id"]][$original_id] = $copied_id;
1141  $found = true;
1142  }
1143  }
1144  }
1145  }
1146  }
1147  // outgoing links to be fixed
1148  if (count($fix) > 0)
1149  {
1150  foreach ($fix as $page_id => $fix_array)
1151  {
1152 //echo "<br>++".$page_id;
1153 //var_dump($fix_array);
1154 
1155  $t = ilObject::_lookupType($copy_lm);
1156  if (is_array($all_fixes[$t.":".$page_id]))
1157  {
1158  $all_fixes[$t.":".$page_id] += $fix_array;
1159  }
1160  else
1161  {
1162  $all_fixes[$t.":".$page_id] = $fix_array;
1163  }
1164 
1165 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $page_id);
1166 // $page->moveIntLinks($fix_array);
1167 // $page->update();
1168  }
1169  }
1170  }
1171  }
1172  }
1173 
1174  foreach ($all_fixes as $pg => $fixes)
1175  {
1176 //echo "<br>**".$pg;
1177 //echo var_dump($fixes);
1178  $pg = explode(":", $pg);
1179  $page = new ilPageObject($pg[0], $pg[1]);
1180  if ($page->moveIntLinks($fixes))
1181  {
1182  $page->update(true, true, true);
1183  }
1184  }
1185  }
1186 
1190  static function uniqueTypesCheck($a_items)
1191  {
1192  $types = array();
1193  if (is_array($a_items))
1194  {
1195  foreach($a_items as $item)
1196  {
1197  $type = ilLMObject::_lookupType($item);
1198  $types[$type] = $type;
1199  }
1200  }
1201 
1202  if (count($types) > 1)
1203  {
1204  return false;
1205  }
1206  return true;
1207  }
1208 
1215  static function writeLayout($a_obj_id, $a_layout, $a_lm = null)
1216  {
1217  global $ilDB;
1218 
1219  $t = ilLMObject::_lookupType($a_obj_id);
1220 
1221  if ($t == "pg")
1222  {
1223  $query = "UPDATE lm_data SET ".
1224  " layout = ".$ilDB->quote($a_layout, "text").
1225  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1226  $ilDB->manipulate($query);
1227  }
1228  else if ($t == "st" && is_object($a_lm))
1229  {
1230  $node = $a_lm->getLMTree()->getNodeData($a_obj_id);
1231  $child_nodes = $a_lm->getLMTree()->getSubTree($node);
1232  if (is_array($child_nodes) && count($child_nodes) > 0)
1233  {
1234  foreach ($child_nodes as $c)
1235  {
1236  if ($c["type"] == "pg")
1237  {
1238  $query = "UPDATE lm_data SET ".
1239  " layout = ".$ilDB->quote($a_layout, "text").
1240  " WHERE obj_id = ".$ilDB->quote($c["child"], "integer");
1241  $ilDB->manipulate($query);
1242  }
1243  }
1244  }
1245  }
1246  }
1247 
1253  static function lookupLayout($a_obj_id)
1254  {
1255  global $ilDB;
1256 
1257  $query = "SELECT layout FROM lm_data WHERE obj_id = ".
1258  $ilDB->quote($a_obj_id, "integer");
1259  $obj_set = $ilDB->query($query);
1260  $obj_rec = $ilDB->fetchAssoc($obj_set);
1261 
1262  return $obj_rec["layout"];
1263  }
1264 
1265 }
1266 ?>