ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLMObject.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
25 
37 {
38  var $ilias;
39  var $lm_id;
40  var $type;
41  var $id;
43  var $data_record; // assoc array of lm_data record
45  var $title;
47  var $active = true;
48 
52  function ilLMObject(&$a_content_obj, $a_id = 0)
53  {
54  global $ilias;
55 
56  $this->ilias =& $ilias;
57  $this->id = $a_id;
58  $this->setContentObject($a_content_obj);
59  $this->setLMId($a_content_obj->getId());
60  if($a_id != 0)
61  {
62  $this->read();
63  }
64  }
65 
77  function MDUpdateListener($a_element)
78  {
79  include_once 'Services/MetaData/classes/class.ilMD.php';
80 
81  switch($a_element)
82  {
83  case 'General':
84 
85  // Update Title and description
86  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
87  $md_gen = $md->getGeneral();
88 
89  ilLMObject::_writeTitle($this->getId(),$md_gen->getTitle());
90 
91  foreach($md_gen->getDescriptionIds() as $id)
92  {
93  $md_des = $md_gen->getDescription($id);
94 // ilLMObject::_writeDescription($this->getId(),$md_des->getDescription());
95  break;
96  }
97 
98  break;
99 
100  default:
101  }
102  return true;
103  }
104 
105 
109  function _lookupNID($a_lm_id, $a_lm_obj_id, $a_type)
110  {
111  include_once 'Services/MetaData/classes/class.ilMD.php';
112 //echo "-".$a_lm_id."-".$a_lm_obj_id."-".$a_type."-";
113  $md = new ilMD($a_lm_id, $a_lm_obj_id, $a_type);
114  $md_gen = $md->getGeneral();
115  if (is_object($md_gen))
116  {
117  foreach($md_gen->getIdentifierIds() as $id)
118  {
119  $md_id = $md_gen->getIdentifier($id);
120  if ($md_id->getCatalog() == "ILIAS_NID")
121  {
122  return $md_id->getEntry();
123  }
124  }
125  }
126 
127  return false;
128  }
129 
130 
134  function createMetaData()
135  {
136  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
137 
138  global $ilUser;
139 
140  $md_creator = new ilMDCreator($this->getLMId(), $this->getId(), $this->getType());
141  $md_creator->setTitle($this->getTitle());
142  $md_creator->setTitleLanguage($ilUser->getPref('language'));
143  $md_creator->setDescription($this->getDescription());
144  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
145  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
146  $md_creator->setLanguage($ilUser->getPref('language'));
147  $md_creator->create();
148 
149  return true;
150  }
151 
155  function updateMetaData()
156  {
157  include_once("Services/MetaData/classes/class.ilMD.php");
158  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
159  include_once("Services/MetaData/classes/class.ilMDDescription.php");
160 
161  $md =& new ilMD($this->getLMId(), $this->getId(), $this->getType());
162  $md_gen =& $md->getGeneral();
163  $md_gen->setTitle($this->getTitle());
164 
165  // sets first description (maybe not appropriate)
166  $md_des_ids =& $md_gen->getDescriptionIds();
167  if (count($md_des_ids) > 0)
168  {
169  $md_des =& $md_gen->getDescription($md_des_ids[0]);
170 // $md_des->setDescription($this->getDescription());
171  $md_des->update();
172  }
173  $md_gen->update();
174 
175  }
176 
177 
181  function deleteMetaData()
182  {
183  // Delete meta data
184  include_once('Services/MetaData/classes/class.ilMD.php');
185  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
186  $md->deleteAll();
187  }
188 
189 
190 
194  function setDataRecord($a_record)
195  {
196  $this->data_record = $a_record;
197  }
198 
199  function read()
200  {
201  global $ilBench, $ilDB;
202 
203  $ilBench->start("ContentPresentation", "ilLMObject_read");
204 
205  if(!isset($this->data_record))
206  {
207  $ilBench->start("ContentPresentation", "ilLMObject_read_getData");
208  $query = "SELECT * FROM lm_data WHERE obj_id = ".
209  $ilDB->quote($this->id, "integer");
210  $obj_set = $ilDB->query($query);
211  $this->data_record = $ilDB->fetchAssoc($obj_set);
212  $ilBench->stop("ContentPresentation", "ilLMObject_read_getData");
213  }
214 
215  $this->type = $this->data_record["type"];
216  $this->setImportId($this->data_record["import_id"]);
217  $this->setTitle($this->data_record["title"]);
218  $this->setLayout($this->data_record["layout"]);
219  //$this->setActive(ilUtil::yn2tf($this->data_record["active"]));
220 
221  $ilBench->stop("ContentPresentation", "ilLMObject_read");
222  }
223 
229  function setTitle($a_title)
230  {
231  $this->title = $a_title;
232  }
233 
239  function getTitle()
240  {
241  return $this->title;
242  }
243 
244 
250  static function _lookupTitle($a_obj_id)
251  {
252  global $ilDB;
253 
254  $query = "SELECT * FROM lm_data WHERE obj_id = ".
255  $ilDB->quote($a_obj_id, "integer");
256  $obj_set = $ilDB->query($query);
257  $obj_rec = $ilDB->fetchAssoc($obj_set);
258 
259  return $obj_rec["title"];
260  }
261 
267  static function _lookupType($a_obj_id)
268  {
269  global $ilDB;
270 
271  $query = "SELECT * FROM lm_data WHERE obj_id = ".
272  $ilDB->quote($a_obj_id, "integer");
273  $obj_set = $ilDB->query($query);
274  $obj_rec = $ilDB->fetchAssoc($obj_set);
275 
276  return $obj_rec["type"];
277  }
278 
279 
280  function _writeTitle($a_obj_id, $a_title)
281  {
282  global $ilDB;
283 
284  $query = "UPDATE lm_data SET ".
285  " title = ".$ilDB->quote($a_title, "text").
286  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
287  $ilDB->manipulate($query);
288  }
289 
290 
291  function setDescription($a_description)
292  {
293  $this->description = $a_description;
294  }
295 
296  function getDescription()
297  {
298  return $this->description;
299  }
300 
301  function setType($a_type)
302  {
303  $this->type = $a_type;
304  }
305 
306  function getType()
307  {
308  return $this->type;
309  }
310 
311  function setLMId($a_lm_id)
312  {
313  $this->lm_id = $a_lm_id;
314 
315  }
316 
317  function getLMId()
318  {
319  return $this->lm_id;
320  }
321 
322  function setContentObject(&$a_content_obj)
323  {
324  $this->content_object =& $a_content_obj;
325  }
326 
327  function &getContentObject()
328  {
329  return $this->content_object;
330  }
331 
332  function setId($a_id)
333  {
334  $this->id = $a_id;
335  }
336 
337  function getId()
338  {
339  return $this->id;
340  }
341 
342  function getImportId()
343  {
344  return $this->import_id;
345  }
346 
347  function setImportId($a_id)
348  {
349  $this->import_id = $a_id;
350  }
351 
357  function setLayout($a_val)
358  {
359  $this->layout = $a_val;
360  }
361 
367  function getLayout()
368  {
369  return $this->layout;
370  }
371 
379  function _writeImportId($a_id, $a_import_id)
380  {
381  global $ilDB;
382 
383  $q = "UPDATE lm_data ".
384  "SET ".
385  "import_id = ".$ilDB->quote($a_import_id, "text").",".
386  "last_update = ".$ilDB->now()." ".
387  "WHERE obj_id = ".$ilDB->quote($a_id, "integer");
388 
389  $ilDB->manipulate($q);
390  }
391 
392  function create($a_upload = false)
393  {
394  global $ilDB;
395 
396  // insert object data
397  $this->setId($ilDB->nextId("lm_data"));
398  $query = "INSERT INTO lm_data (obj_id, title, type, lm_id, import_id, create_date) ".
399  "VALUES (".
400  $ilDB->quote($this->getId(), "integer").",".
401  $ilDB->quote($this->getTitle(), "text").",".
402  $ilDB->quote($this->getType(), "text").", ".
403  $ilDB->quote($this->getLMId(), "integer").",".
404  $ilDB->quote($this->getImportId(), "text").
405  ", ".$ilDB->now().")";
406  $ilDB->manipulate($query);
407 
408  // create history entry
409  include_once("classes/class.ilHistory.php");
410  ilHistory::_createEntry($this->getId(), "create", "",
411  $this->content_object->getType().":".$this->getType());
412 
413  if (!$a_upload)
414  {
415  $this->createMetaData();
416  }
417 
418  }
419 
423  function update()
424  {
425  global $ilDB;
426 
427  $this->updateMetaData();
428 
429  $query = "UPDATE lm_data SET ".
430  " lm_id = ".$ilDB->quote($this->getLMId(), "integer").
431  " ,title = ".$ilDB->quote($this->getTitle(), "text").
432  " ,layout = ".$ilDB->quote($this->getLayout(), "text").
433  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
434 
435  $ilDB->manipulate($query);
436  }
437 
438 
447  function _writePublicAccessStatus($a_pages,$a_cont_obj_id)
448  {
449  global $ilDB,$ilLog,$ilErr,$ilTree;
450 
451  if (!is_array($a_pages))
452  {$a_pages = array(0);
453  /*$message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_pages must be an array');
454  $ilLog->write($message,$ilLog->WARNING);
455  $ilErr->raiseError($message,$ilErr->MESSAGE);
456  return false;*/
457  }
458 
459  if (empty($a_cont_obj_id))
460  {
461  $message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_cont_obj_id is empty');
462  $ilLog->write($message,$ilLog->WARNING);
463  $ilErr->raiseError($message,$ilErr->MESSAGE);
464  return false;
465  }
466 
467  // update structure entries: if at least one page of a chapter is public set chapter to public too
468  $lm_tree = new ilTree($a_cont_obj_id);
469  $lm_tree->setTableNames('lm_tree','lm_data');
470  $lm_tree->setTreeTablePK("lm_id");
471  $lm_tree->readRootId();
472 
473  // get all st entries of cont_obj
474  $q = "SELECT obj_id FROM lm_data " .
475  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
476  "AND type = 'st'";
477  $r = $ilDB->query($q);
478 
479  // add chapters with a public page to a_pages
480  while ($row = $ilDB->fetchAssoc($r))
481  {
482  $childs = $lm_tree->getChilds($row["obj_id"]);
483 
484  foreach ($childs as $page)
485  {
486  if ($page["type"] == "pg" and in_array($page["obj_id"],$a_pages))
487  {
488  array_push($a_pages, $row["obj_id"]);
489  break;
490  }
491  }
492  }
493 
494  // update public access status of all pages of cont_obj
495  $q = "UPDATE lm_data SET " .
496  "public_access = CASE " .
497  "WHEN ".$ilDB->in("obj_id", $a_pages, false, "integer")." ".
498  "THEN ".$ilDB->quote("y", "text").
499  "ELSE ".$ilDB->quote("n", "text").
500  "END " .
501  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
502  "AND ".$ilDB->in("type", array("pg", "st"), false, "text");
503  $ilDB->manipulate($q);
504 
505  return true;
506  }
507 
508  function _isPagePublic($a_node_id,$a_check_public_mode = false)
509  {
510  global $ilDB,$ilLog;
511 
512  if (empty($a_node_id))
513  {
514  $message = sprintf('ilLMObject::_isPagePublic(): Invalid parameter! $a_node_id is empty');
515  $ilLog->write($message,$ilLog->WARNING);
516  return false;
517  }
518 
519  if ($a_check_public_mode === true)
520  {
521  $lm_id = ilLMObject::_lookupContObjId($a_node_id);
522 
523  $q = "SELECT public_access_mode FROM content_object WHERE id = ".
524  $ilDB->quote($lm_id, "integer");
525  $r = $ilDB->query($q);
526  $row = $ilDB->fetchAssoc($r);
527 
528  if ($row["public_access_mode"] == "complete")
529  {
530  return true;
531  }
532  }
533 
534  $q = "SELECT public_access FROM lm_data WHERE obj_id=".
535  $ilDB->quote($a_node_id, "integer");
536  $r = $ilDB->query($q);
537  $row = $ilDB->fetchAssoc($r);
538 
539  return ilUtil::yn2tf($row["public_access"]);
540  }
541 
545  function delete($a_delete_meta_data = true)
546  {
547  global $ilDB;
548 
549  $query = "DELETE FROM lm_data WHERE obj_id = ".
550  $ilDB->quote($this->getId(), "integer");
551  $ilDB->manipulate($query);
552 
553  $this->deleteMetaData();
554  }
555 
567  function _getIdForImportId($a_import_id)
568  {
569  global $ilDB;
570 
571  $q = "SELECT obj_id FROM lm_data WHERE import_id = ".
572  $ilDB->quote($a_import_id, "text")." ".
573  " ORDER BY create_date DESC";
574  $obj_set = $ilDB->query($q);
575  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
576  {
577  $lm_id = ilLMObject::_lookupContObjID($obj_rec["obj_id"]);
578 
579  // link only in learning module, that is not trashed
581  {
582  return $obj_rec["obj_id"];
583  }
584  }
585 
586  return 0;
587  }
588 
598  function _getAllObjectsForImportId($a_import_id, $a_in_lm = 0)
599  {
600  global $ilDB;
601 
602  $where = ($a_in_lm > 0)
603  ? " AND lm_id = ".$ilDB->quote($a_in_lm, "integer")." "
604  : "";
605 
606  $q = "SELECT * FROM lm_data WHERE import_id = ".
607  $ilDB->quote($a_import_id, "text")." ".
608  $where.
609  " ORDER BY create_date DESC";
610  $obj_set = $ilDB->query($q);
611 
612  $items = array();
613  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
614  {
615  // check, whether lm is not trashed
616  if (ilObject::_hasUntrashedReference($obj_rec["lm_id"]))
617  {
618  $items[] = $obj_rec;
619  }
620  }
621 
622  return $items;
623  }
624 
632  function _exists($a_id)
633  {
634  global $ilDB;
635 
636  include_once("./Services/COPage/classes/class.ilInternalLink.php");
637  if (is_int(strpos($a_id, "_")))
638  {
640  }
641 
642  $q = "SELECT * FROM lm_data WHERE obj_id = ".
643  $ilDB->quote($a_id, "integer");
644  $obj_set = $ilDB->query($q);
645  if ($obj_rec = $ilDB->fetchAssoc($obj_set))
646  {
647  return true;
648  }
649  else
650  {
651  return false;
652  }
653 
654  }
655 
659  function getObjectList($lm_id, $type = "")
660  {
661  global $ilDB;
662 
663  $type_str = ($type != "")
664  ? "AND type = ".$ilDB->quote($type, "text")." "
665  : "";
666  $query = "SELECT * FROM lm_data ".
667  "WHERE lm_id= ".$ilDB->quote($lm_id, "integer")." ".
668  $type_str." ".
669  "ORDER BY title";
670  $obj_set = $ilDB->query($query);
671  $obj_list = array();
672  while($obj_rec = $ilDB->fetchAssoc($obj_set))
673  {
674  $obj_list[] = array("obj_id" => $obj_rec["obj_id"],
675  "title" => $obj_rec["title"],
676  "type" => $obj_rec["type"]);
677  }
678  return $obj_list;
679  }
680 
681 
685  function _deleteAllObjectData(&$a_cobj)
686  {
687  global $ilDB;
688 
689  include_once './classes/class.ilNestedSetXML.php';
690 
691  $query = "SELECT * FROM lm_data ".
692  "WHERE lm_id= ".$ilDB->quote($a_cobj->getId(), "integer");
693  $obj_set = $ilDB->query($query);
694 
695  require_once("./Modules/LearningModule/classes/class.ilLMObjectFactory.php");
696  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
697  {
698  $lm_obj = ilLMObjectFactory::getInstance($a_cobj, $obj_rec["obj_id"],false);
699 
700  if (is_object($lm_obj))
701  {
702  $lm_obj->delete(true);
703  }
704  }
705 
706  return true;
707  }
708 
712  function _lookupContObjID($a_id)
713  {
714  global $ilDB;
715 
716  $query = "SELECT * FROM lm_data WHERE obj_id = ".
717  $ilDB->quote($a_id, "integer");
718  $obj_set = $ilDB->query($query);
719  $obj_rec = $ilDB->fetchAssoc($obj_set);
720 
721  return $obj_rec["lm_id"];
722  }
723 
727  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
728  {
729  global $ilLog;
730 
731  $tree = new ilTree($a_obj->getContentObject()->getId());
732  $tree->setTableNames('lm_tree', 'lm_data');
733  $tree->setTreeTablePK("lm_id");
734 
735  // determine parent
736  $parent_id = ($a_parent_id != "")
737  ? $a_parent_id
738  : $tree->getRootId();
739 
740  // determine target
741  if ($a_target_node_id != "")
742  {
743  $target = $a_target_node_id;
744  }
745  else
746  {
747  // determine last child that serves as predecessor
748  if ($a_obj->getType() == "st")
749  {
750  $s_types = array("st", "pg");
751  $childs =& $tree->getChildsByTypeFilter($parent_id, $s_types);
752  }
753  else
754  {
755  $s_types = "pg";
756  $childs =& $tree->getChildsByType($parent_id, $s_types);
757  }
758 
759  if (count($childs) == 0)
760  {
761  $target = IL_FIRST_NODE;
762  }
763  else
764  {
765  $target = $childs[count($childs) - 1]["obj_id"];
766  }
767  }
768 
769  if ($tree->isInTree($parent_id) && !$tree->isInTree($a_obj->getId()))
770  {
771  $ilLog->write("LMObject::putInTree: insertNode, ID: ".$a_obj->getId().
772  "Parent ID: ".$parent_id.", Target: ".$target);
773 
774  $tree->insertNode($a_obj->getId(), $parent_id, $target);
775  }
776  }
777 
785  static function getTree($a_cont_obj_id)
786  {
787  $tree = new ilTree($a_cont_obj_id);
788  $tree->setTableNames('lm_tree', 'lm_data');
789  $tree->setTreeTablePK("lm_id");
790  $tree->readRootId();
791 
792  return $tree;
793  }
794 
798  function clipboardCut($a_cont_obj_id, $a_ids)
799  {
800  $tree = ilLMObject::getTree($a_cont_obj_id);
801 
802  if (!is_array($a_ids))
803  {
804  return false;
805  }
806  else
807  {
808  // get all "top" ids, i.e. remove ids, that have a selected parent
809  foreach($a_ids as $id)
810  {
811  $path = $tree->getPathId($id);
812  $take = true;
813  foreach($path as $path_id)
814  {
815  if ($path_id != $id && in_array($path_id, $a_ids))
816  {
817  $take = false;
818  }
819  }
820  if ($take)
821  {
822  $cut_ids[] = $id;
823  }
824  }
825  }
826 
827  ilLMObject::clipboardCopy($a_cont_obj_id, $cut_ids);
828 
829  // remove the objects from the tree
830  // note: we are getting chapters which are *not* in the tree
831  // we do not delete any pages/chapters here
832  foreach ($cut_ids as $id)
833  {
834  $curnode = $tree->getNodeData($id);
835  if ($tree->isInTree($id))
836  {
837  $tree->deleteTree($curnode);
838  }
839  }
840 
841  }
842 
846  static function clipboardCopy($a_cont_obj_id, $a_ids)
847  {
848  global $ilUser;
849 
850  $tree = ilLMObject::getTree($a_cont_obj_id);
851 
852  $ilUser->clipboardDeleteObjectsOfType("pg");
853  $ilUser->clipboardDeleteObjectsOfType("st");
854 
855  // put them into the clipboard
856  $time = date("Y-m-d H:i:s", time());
857  foreach ($a_ids as $id)
858  {
859  $curnode = "";
860  if ($tree->isInTree($id))
861  {
862  $curnode = $tree->getNodeData($id);
863  $subnodes = $tree->getSubTree($curnode);
864  foreach($subnodes as $subnode)
865  {
866  if ($subnode["child"] != $id)
867  {
868  $ilUser->addObjectToClipboard($subnode["child"],
869  $subnode["type"], $subnode["title"],
870  $subnode["parent"], $time, $subnode["lft"]);
871  }
872  }
873  }
874  $order = ($curnode["lft"] > 0)
875  ? $curnode["lft"]
876  : (int) ($order + 1);
877  $ilUser->addObjectToClipboard($id,
879  $order);
880  }
881  }
882 
889  static function pasteTree($a_target_lm, $a_item_id, $a_parent_id, $a_target, $a_insert_time,
890  &$a_copied_nodes, $a_as_copy = false)
891  {
892  global $ilUser, $ilias, $ilLog;
893 
894  $item_lm_id = ilLMObject::_lookupContObjID($a_item_id);
895  $item_type = ilLMObject::_lookupType($a_item_id);
896  $lm_obj = $ilias->obj_factory->getInstanceByObjId($item_lm_id);
897  if ($item_type == "st")
898  {
899  $item = new ilStructureObject($lm_obj, $a_item_id);
900  }
901  else if ($item_type == "pg")
902  {
903  $item = new ilLMPageObject($lm_obj, $a_item_id);
904  }
905 
906  $ilLog->write("Getting from clipboard type ".$item_type.", ".
907  "Item ID: ".$a_item_id.", of original LM: ".$item_lm_id);
908 
909  if ($item_lm_id != $a_target_lm->getId() && !$a_as_copy)
910  {
911  // @todo: check whether st is NOT in tree
912 
913  // "move" metadata to new lm
914  include_once("Services/MetaData/classes/class.ilMD.php");
915  $md = new ilMD($item_lm_id, $item->getId(), $item->getType());
916  $new_md = $md->cloneMD($a_target_lm->getId(), $item->getId(), $item->getType());
917 
918  // update lm object
919  $item->setLMId($a_target_lm->getId());
920  $item->setContentObject($a_target_lm);
921  $item->update();
922 
923  // delete old meta data set
924  $md->deleteAll();
925 
926  if ($item_type == "pg")
927  {
928  $page = $item->getPageObject();
929  $page->buildDom();
930  $page->setParentId($a_target_lm->getId());
931  $page->update();
932  }
933  }
934 
935  if ($a_as_copy)
936  {
937  $target_item = $item->copy($a_target_lm);
938  $a_copied_nodes[$item->getId()] = $target_item->getId();
939  }
940  else
941  {
942  $target_item = $item;
943  }
944 
945  $ilLog->write("Putting into tree type ".$target_item->getType().
946  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
947  "Target: ".$a_target.", Item LM:".$target_item->getContentObject()->getId());
948 
949  ilLMObject::putInTree($target_item, $a_parent_id, $a_target);
950 
951  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
952 
953  foreach($childs as $child)
954  {
955  ilLMObject::pasteTree($a_target_lm, $child["id"], $target_item->getId(),
956  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy);
957  }
958 
959  return $target_item->getId();
960  // @todo: write history (see pastePage)
961  }
962 
968  static function saveTitles($a_lm, $a_titles)
969  {
970  if (is_array($a_titles))
971  {
972  include_once("./Services/MetaData/classes/class.ilMD.php");
973  foreach($a_titles as $id => $title)
974  {
975  $lmobj = ilLMObjectFactory::getInstance($a_lm, $id, false);
976  if (is_object($lmobj))
977  {
978  // Update Title and description
979  $md = new ilMD($a_lm->getId(), $id, $lmobj->getType());
980  $md_gen = $md->getGeneral();
981  $md_gen->setTitle($title);
982  $md_gen->update();
983  $md->update();
985  }
986  }
987  }
988  }
989 
993  static function updateInternalLinks($a_copied_nodes, $a_parent_type = "lm")
994  {
995  $all_fixes = array();
996  foreach($a_copied_nodes as $original_id => $copied_id)
997  {
998  $copied_type = ilLMObject::_lookupType($copied_id);
999  $copy_lm = ilLMObject::_lookupContObjID($copied_id);
1000 
1001  if ($copied_type == "pg")
1002  {
1003  //
1004  // 1. Outgoing links from the copied page.
1005  //
1006  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
1007  $tpg = new ilPageObject($a_parent_type, $copied_id);
1008  $tpg->buildDom();
1009  $il = $tpg->getInternalLinks();
1010  $targets = array();
1011  foreach($il as $l)
1012  {
1013  $targets[] = array("type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
1014  "id" => (int) ilInternalLink::_extractObjIdOfTarget($l["Target"]),
1015  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"]));
1016  }
1017  $fix = array();
1018  foreach($targets as $target)
1019  {
1020  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
1021  ($target["type"] == "pg" || $target["type"] == "st"))
1022  {
1023  // first check, whether target is also within the copied set
1024  if ($a_copied_nodes[$target["id"]] > 0)
1025  {
1026  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
1027  }
1028  else
1029  {
1030  // now check, if a copy if the target is already in the same lm
1031 // lm_data, where import_id -> ok
1032 
1033  // only if target is not already in the same lm!
1034  $trg_lm = ilLMObject::_lookupContObjID($target["id"]);
1035  if ($trg_lm != $copy_lm)
1036  {
1037  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$target["type"]."_".$target["id"]);
1038  $found = false;
1039 
1040  foreach($lm_data as $item)
1041  {
1042  if (!$found && ($item["lm_id"] == $copy_lm))
1043  {
1044  $fix[$target["id"]] = $item["obj_id"];
1045  $found = true;
1046  }
1047  }
1048  }
1049  }
1050  }
1051  }
1052 
1053  // outgoing links to be fixed
1054  if (count($fix) > 0)
1055  {
1056 //echo "<br>--".$copied_id;
1057 //var_dump($fix);
1058  $t = ilObject::_lookupType($copy_lm);
1059  if (is_array($all_fixes[$t.":".$copied_id]))
1060  {
1061  $all_fixes[$t.":".$copied_id] += $fix;
1062  }
1063  else
1064  {
1065  $all_fixes[$t.":".$copied_id] = $fix;
1066  }
1067 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $copied_id);
1068 // $page->moveIntLinks($fix);
1069 // $page->update();
1070  }
1071  }
1072 
1073  if ($copied_type == "pg" ||
1074  $copied_type == "st")
1075  {
1076 
1077  //
1078  // 2. Incoming links to the original pages
1079  //
1080  // A->B A2 (A+B currently copied)
1081  // A->C B2
1082  // B->A
1083  // C->A C2->A (C already copied)
1084  $original_lm = ilLMObject::_lookupContObjID($original_id);
1085  $original_type = ilObject::_lookupType($original_lm);
1086 
1087  if ($original_lm != $copy_lm)
1088  {
1089 
1090  // This gets sources that link to A+B (so we have C here)
1091  // (this also does already the trick when instance map areas are given in C)
1092  // int_link, where target_type, target_id, target_inst -> ok
1093  $sources = ilInternalLink::_getSourcesOfTarget($copied_type,
1094  $original_id, 0);
1095 
1096  // mobs linking to $original_id
1097  // map_area, where link_type, target -> ok
1098  $mobs = ilMapArea::_getMobsForTarget("int", "il__".$copied_type.
1099  "_".$original_id);
1100 
1101  // pages using these mobs
1102  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1103  foreach($mobs as $mob)
1104  {
1105  // mob_usage, where id -> ok
1106  // mep_item, where foreign_id, type -> ok
1107  // mep_tree, where child -> already existed
1108  // il_news_item, where mob_id -> ok
1109  // map_area, where link_type, target -> aready existed
1110  // media_item, where id -> already existed
1111  // personal_clipboard, where item_id, type -> ok
1112  $usages = ilObjMediaObject::lookupUsages($mob);
1113  foreach($usages as $usage)
1114  {
1115  if ($usage["type"] == "lm:pg" | $usage["type"] == "lm:st")
1116  {
1117  $sources[] = $usage;
1118  }
1119  }
1120  }
1121  $fix = array();
1122  foreach($sources as $source)
1123  {
1124  $stype = explode(":", $source["type"]);
1125  $source_type = $stype[1];
1126 
1127  if ($source_type == "pg" || $source_type == "st")
1128  {
1129  // first of all: source must be in original lm
1130  $src_lm = ilLMObject::_lookupContObjID($source["id"]);
1131 
1132  if ($src_lm == $original_lm)
1133  {
1134  // check, if a copy if the source is already in the same lm
1135  // now we look for the latest copy of C in LM2
1136  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$source_type."_".$source["id"],
1137  $copy_lm);
1138  $found = false;
1139  foreach ($lm_data as $item)
1140  {
1141  if (!$found)
1142  {
1143  $fix[$item["obj_id"]][$original_id] = $copied_id;
1144  $found = true;
1145  }
1146  }
1147  }
1148  }
1149  }
1150  // outgoing links to be fixed
1151  if (count($fix) > 0)
1152  {
1153  foreach ($fix as $page_id => $fix_array)
1154  {
1155 //echo "<br>++".$page_id;
1156 //var_dump($fix_array);
1157 
1158  $t = ilObject::_lookupType($copy_lm);
1159  if (is_array($all_fixes[$t.":".$page_id]))
1160  {
1161  $all_fixes[$t.":".$page_id] += $fix_array;
1162  }
1163  else
1164  {
1165  $all_fixes[$t.":".$page_id] = $fix_array;
1166  }
1167 
1168 // $page = new ilPageObject(ilObject::_lookupType($copy_lm), $page_id);
1169 // $page->moveIntLinks($fix_array);
1170 // $page->update();
1171  }
1172  }
1173  }
1174  }
1175  }
1176 
1177  foreach ($all_fixes as $pg => $fixes)
1178  {
1179 //echo "<br>**".$pg;
1180 //echo var_dump($fixes);
1181  $pg = explode(":", $pg);
1182  $page = new ilPageObject($pg[0], $pg[1]);
1183  if ($page->moveIntLinks($fixes))
1184  {
1185  $page->update(true, true, true);
1186  }
1187  }
1188  }
1189 
1193  static function uniqueTypesCheck($a_items)
1194  {
1195  $types = array();
1196  if (is_array($a_items))
1197  {
1198  foreach($a_items as $item)
1199  {
1200  $type = ilLMObject::_lookupType($item);
1201  $types[$type] = $type;
1202  }
1203  }
1204 
1205  if (count($types) > 1)
1206  {
1207  return false;
1208  }
1209  return true;
1210  }
1211 
1218  static function writeLayout($a_obj_id, $a_layout)
1219  {
1220  global $ilDB;
1221 
1222  $query = "UPDATE lm_data SET ".
1223  " layout = ".$ilDB->quote($a_layout, "text").
1224  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1225 
1226  $ilDB->manipulate($query);
1227  }
1228 
1234  static function lookupLayout($a_obj_id)
1235  {
1236  global $ilDB;
1237 
1238  $query = "SELECT layout FROM lm_data WHERE obj_id = ".
1239  $ilDB->quote($a_obj_id, "integer");
1240  $obj_set = $ilDB->query($query);
1241  $obj_rec = $ilDB->fetchAssoc($obj_set);
1242 
1243  return $obj_rec["layout"];
1244  }
1245 
1246 }
1247 ?>