ILIAS  Release_4_4_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 /* 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  static protected $data_records = array();
30 
34  function ilLMObject($a_content_obj, $a_id = 0)
35  {
36  global $ilias;
37 
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 
59  function MDUpdateListener($a_element)
60  {
61  include_once 'Services/MetaData/classes/class.ilMD.php';
62 
63  switch($a_element)
64  {
65  case 'General':
66 
67  // Update Title and description
68  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
69  $md_gen = $md->getGeneral();
70 
71  ilLMObject::_writeTitle($this->getId(),$md_gen->getTitle());
72 
73  foreach($md_gen->getDescriptionIds() as $id)
74  {
75  $md_des = $md_gen->getDescription($id);
76 // ilLMObject::_writeDescription($this->getId(),$md_des->getDescription());
77  break;
78  }
79 
80  break;
81 
82  default:
83  }
84  return true;
85  }
86 
87 
91  function _lookupNID($a_lm_id, $a_lm_obj_id, $a_type)
92  {
93  include_once 'Services/MetaData/classes/class.ilMD.php';
94 //echo "-".$a_lm_id."-".$a_lm_obj_id."-".$a_type."-";
95  $md = new ilMD($a_lm_id, $a_lm_obj_id, $a_type);
96  $md_gen = $md->getGeneral();
97  if (is_object($md_gen))
98  {
99  foreach($md_gen->getIdentifierIds() as $id)
100  {
101  $md_id = $md_gen->getIdentifier($id);
102  if ($md_id->getCatalog() == "ILIAS_NID")
103  {
104  return $md_id->getEntry();
105  }
106  }
107  }
108 
109  return false;
110  }
111 
112 
116  function createMetaData()
117  {
118  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
119 
120  global $ilUser;
121 
122  $md_creator = new ilMDCreator($this->getLMId(), $this->getId(), $this->getType());
123  $md_creator->setTitle($this->getTitle());
124  $md_creator->setTitleLanguage($ilUser->getPref('language'));
125  $md_creator->setDescription($this->getDescription());
126  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
127  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
128  $md_creator->setLanguage($ilUser->getPref('language'));
129  $md_creator->create();
130 
131  return true;
132  }
133 
137  function updateMetaData()
138  {
139  include_once("Services/MetaData/classes/class.ilMD.php");
140  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
141  include_once("Services/MetaData/classes/class.ilMDDescription.php");
142 
143  $md =& new ilMD($this->getLMId(), $this->getId(), $this->getType());
144  $md_gen =& $md->getGeneral();
145  $md_gen->setTitle($this->getTitle());
146 
147  // sets first description (maybe not appropriate)
148  $md_des_ids =& $md_gen->getDescriptionIds();
149  if (count($md_des_ids) > 0)
150  {
151  $md_des =& $md_gen->getDescription($md_des_ids[0]);
152 // $md_des->setDescription($this->getDescription());
153  $md_des->update();
154  }
155  $md_gen->update();
156 
157  }
158 
159 
163  function deleteMetaData()
164  {
165  // Delete meta data
166  include_once('Services/MetaData/classes/class.ilMD.php');
167  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
168  $md->deleteAll();
169  }
170 
171 
172 
176  function setDataRecord($a_record)
177  {
178  $this->data_record = $a_record;
179  }
180 
181  function read()
182  {
183  global $ilBench, $ilDB;
184 
185  $ilBench->start("ContentPresentation", "ilLMObject_read");
186 
187  if(!isset($this->data_record))
188  {
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  }
194 
195  $this->type = $this->data_record["type"];
196  $this->setImportId($this->data_record["import_id"]);
197  $this->setTitle($this->data_record["title"]);
198  $this->setLayout($this->data_record["layout"]);
199  //$this->setActive(ilUtil::yn2tf($this->data_record["active"]));
200 
201  $ilBench->stop("ContentPresentation", "ilLMObject_read");
202  }
203 
204 
211  static function preloadDataByLM($a_lm_id)
212  {
213  global $ilDB;
214 
215  $set = $ilDB->query("SELECT * FROM lm_data ".
216  " WHERE lm_id = ".$ilDB->quote($a_lm_id, "integer")
217  );
218  while ($rec = $ilDB->fetchAssoc($set))
219  {
220  self::$data_records[$rec["obj_id"]] = $rec;
221  }
222  return count(self::$data_records);
223  }
224 
225 
231  function setTitle($a_title)
232  {
233  $this->title = $a_title;
234  }
235 
241  function getTitle()
242  {
243  return $this->title;
244  }
245 
246 
252  static function _lookupTitle($a_obj_id)
253  {
254  global $ilDB;
255 
256  if (isset(self::$data_records[$a_obj_id]))
257  {
258  return self::$data_records[$a_obj_id]["title"];
259  }
260 
261  $query = "SELECT title FROM lm_data WHERE obj_id = ".
262  $ilDB->quote($a_obj_id, "integer");
263  $obj_set = $ilDB->query($query);
264  $obj_rec = $ilDB->fetchAssoc($obj_set);
265 
266  return $obj_rec["title"];
267  }
268 
275  static function _lookupType($a_obj_id, $a_lm_id = 0)
276  {
277  global $ilDB;
278 
279  if (isset(self::$data_records[$a_obj_id]))
280  {
281  if ($a_lm_id == 0 || self::$data_records[$a_obj_id]["lm_id"] == $a_lm_id)
282  {
283  return self::$data_records[$a_obj_id]["type"];
284  }
285  }
286 
287  if($a_lm_id)
288  {
289  $and = ' AND lm_id = '.$ilDB->quote($a_lm_id,'integer');
290  }
291 
292  $query = "SELECT type FROM lm_data WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").$and;
293  $obj_set = $ilDB->query($query);
294  $obj_rec = $ilDB->fetchAssoc($obj_set);
295 
296  return $obj_rec["type"];
297  }
298 
299 
300  function _writeTitle($a_obj_id, $a_title)
301  {
302  global $ilDB;
303 
304  $query = "UPDATE lm_data SET ".
305  " title = ".$ilDB->quote($a_title, "text").
306  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
307  $ilDB->manipulate($query);
308  }
309 
310 
311  function setDescription($a_description)
312  {
313  $this->description = $a_description;
314  }
315 
316  function getDescription()
317  {
318  return $this->description;
319  }
320 
321  function setType($a_type)
322  {
323  $this->type = $a_type;
324  }
325 
326  function getType()
327  {
328  return $this->type;
329  }
330 
331  function setLMId($a_lm_id)
332  {
333  $this->lm_id = $a_lm_id;
334 
335  }
336 
337  function getLMId()
338  {
339  return $this->lm_id;
340  }
341 
342  function setContentObject(&$a_content_obj)
343  {
344  $this->content_object =& $a_content_obj;
345  }
346 
347  function &getContentObject()
348  {
349  return $this->content_object;
350  }
351 
352  function setId($a_id)
353  {
354  $this->id = $a_id;
355  }
356 
357  function getId()
358  {
359  return $this->id;
360  }
361 
362  function getImportId()
363  {
364  return $this->import_id;
365  }
366 
367  function setImportId($a_id)
368  {
369  $this->import_id = $a_id;
370  }
371 
377  function setLayout($a_val)
378  {
379  $this->layout = $a_val;
380  }
381 
387  function getLayout()
388  {
389  return $this->layout;
390  }
391 
399  function _writeImportId($a_id, $a_import_id)
400  {
401  global $ilDB;
402 
403  $q = "UPDATE lm_data ".
404  "SET ".
405  "import_id = ".$ilDB->quote($a_import_id, "text").",".
406  "last_update = ".$ilDB->now()." ".
407  "WHERE obj_id = ".$ilDB->quote($a_id, "integer");
408 
409  $ilDB->manipulate($q);
410  }
411 
412  function create($a_upload = false)
413  {
414  global $ilDB;
415 
416  // insert object data
417  $this->setId($ilDB->nextId("lm_data"));
418  $query = "INSERT INTO lm_data (obj_id, title, type, layout, lm_id, import_id, create_date) ".
419  "VALUES (".
420  $ilDB->quote($this->getId(), "integer").",".
421  $ilDB->quote($this->getTitle(), "text").",".
422  $ilDB->quote($this->getType(), "text").", ".
423  $ilDB->quote($this->getLayout(), "text").", ".
424  $ilDB->quote($this->getLMId(), "integer").",".
425  $ilDB->quote($this->getImportId(), "text").
426  ", ".$ilDB->now().")";
427  $ilDB->manipulate($query);
428 
429  // create history entry
430  include_once("./Services/History/classes/class.ilHistory.php");
431  ilHistory::_createEntry($this->getId(), "create", "",
432  $this->content_object->getType().":".$this->getType());
433 
434  if (!$a_upload)
435  {
436  $this->createMetaData();
437  }
438 
439  }
440 
444  function update()
445  {
446  global $ilDB;
447 
448  $this->updateMetaData();
449 
450  $query = "UPDATE lm_data SET ".
451  " lm_id = ".$ilDB->quote($this->getLMId(), "integer").
452  " ,title = ".$ilDB->quote($this->getTitle(), "text").
453  " ,layout = ".$ilDB->quote($this->getLayout(), "text").
454  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
455 
456  $ilDB->manipulate($query);
457  }
458 
459 
468  function _writePublicAccessStatus($a_pages,$a_cont_obj_id)
469  {
470  global $ilDB,$ilLog,$ilErr,$ilTree;
471 
472  if (!is_array($a_pages))
473  {$a_pages = array(0);
474  /*$message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_pages must be an array');
475  $ilLog->write($message,$ilLog->WARNING);
476  $ilErr->raiseError($message,$ilErr->MESSAGE);
477  return false;*/
478  }
479 
480  if (empty($a_cont_obj_id))
481  {
482  $message = sprintf('ilLMObject::_writePublicAccessStatus(): Invalid parameter! $a_cont_obj_id is empty');
483  $ilLog->write($message,$ilLog->WARNING);
484  $ilErr->raiseError($message,$ilErr->MESSAGE);
485  return false;
486  }
487 
488  // update structure entries: if at least one page of a chapter is public set chapter to public too
489  $lm_tree = new ilTree($a_cont_obj_id);
490  $lm_tree->setTableNames('lm_tree','lm_data');
491  $lm_tree->setTreeTablePK("lm_id");
492  $lm_tree->readRootId();
493 
494  // get all st entries of cont_obj
495  $q = "SELECT obj_id FROM lm_data " .
496  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
497  "AND type = 'st'";
498  $r = $ilDB->query($q);
499 
500  // add chapters with a public page to a_pages
501  while ($row = $ilDB->fetchAssoc($r))
502  {
503  $childs = $lm_tree->getChilds($row["obj_id"]);
504 
505  foreach ($childs as $page)
506  {
507  if ($page["type"] == "pg" and in_array($page["obj_id"],$a_pages))
508  {
509  array_push($a_pages, $row["obj_id"]);
510  break;
511  }
512  }
513  }
514 
515  // update public access status of all pages of cont_obj
516  $q = "UPDATE lm_data SET " .
517  "public_access = CASE " .
518  "WHEN ".$ilDB->in("obj_id", $a_pages, false, "integer")." ".
519  "THEN ".$ilDB->quote("y", "text").
520  "ELSE ".$ilDB->quote("n", "text").
521  "END " .
522  "WHERE lm_id = ".$ilDB->quote($a_cont_obj_id, "integer")." " .
523  "AND ".$ilDB->in("type", array("pg", "st"), false, "text");
524  $ilDB->manipulate($q);
525 
526  return true;
527  }
528 
529  static function _isPagePublic($a_node_id,$a_check_public_mode = false)
530  {
531  global $ilDB,$ilLog;
532 
533  if (empty($a_node_id))
534  {
535  $message = sprintf('ilLMObject::_isPagePublic(): Invalid parameter! $a_node_id is empty');
536  $ilLog->write($message,$ilLog->WARNING);
537  return false;
538  }
539 
540  if ($a_check_public_mode === true)
541  {
542  $lm_id = ilLMObject::_lookupContObjId($a_node_id);
543 
544  $q = "SELECT public_access_mode FROM content_object WHERE id = ".
545  $ilDB->quote($lm_id, "integer");
546  $r = $ilDB->query($q);
547  $row = $ilDB->fetchAssoc($r);
548 
549  if ($row["public_access_mode"] == "complete")
550  {
551  return true;
552  }
553  }
554 
555  $q = "SELECT public_access FROM lm_data WHERE obj_id=".
556  $ilDB->quote($a_node_id, "integer");
557  $r = $ilDB->query($q);
558  $row = $ilDB->fetchAssoc($r);
559 
560  return ilUtil::yn2tf($row["public_access"]);
561  }
562 
566  function delete($a_delete_meta_data = true)
567  {
568  global $ilDB;
569 
570  $query = "DELETE FROM lm_data WHERE obj_id = ".
571  $ilDB->quote($this->getId(), "integer");
572  $ilDB->manipulate($query);
573 
574  $this->deleteMetaData();
575  }
576 
588  function _getIdForImportId($a_import_id)
589  {
590  global $ilDB;
591 
592  $q = "SELECT obj_id FROM lm_data WHERE import_id = ".
593  $ilDB->quote($a_import_id, "text")." ".
594  " ORDER BY create_date DESC";
595  $obj_set = $ilDB->query($q);
596  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
597  {
598  $lm_id = ilLMObject::_lookupContObjID($obj_rec["obj_id"]);
599 
600  // link only in learning module, that is not trashed
601  include_once("./Services/Help/classes/class.ilObjHelpSettings.php");
604  {
605  return $obj_rec["obj_id"];
606  }
607  }
608 
609  return 0;
610  }
611 
621  function _getAllObjectsForImportId($a_import_id, $a_in_lm = 0)
622  {
623  global $ilDB;
624 
625  $where = ($a_in_lm > 0)
626  ? " AND lm_id = ".$ilDB->quote($a_in_lm, "integer")." "
627  : "";
628 
629  $q = "SELECT * FROM lm_data WHERE import_id = ".
630  $ilDB->quote($a_import_id, "text")." ".
631  $where.
632  " ORDER BY create_date DESC";
633  $obj_set = $ilDB->query($q);
634 
635  $items = array();
636  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
637  {
638  // check, whether lm is not trashed
639  if (ilObject::_hasUntrashedReference($obj_rec["lm_id"]))
640  {
641  $items[] = $obj_rec;
642  }
643  }
644 
645  return $items;
646  }
647 
655  function _exists($a_id)
656  {
657  global $ilDB;
658 
659  include_once("./Services/COPage/classes/class.ilInternalLink.php");
660  if (is_int(strpos($a_id, "_")))
661  {
663  }
664 
665  $q = "SELECT * FROM lm_data WHERE obj_id = ".
666  $ilDB->quote($a_id, "integer");
667  $obj_set = $ilDB->query($q);
668  if ($obj_rec = $ilDB->fetchAssoc($obj_set))
669  {
670  return true;
671  }
672  else
673  {
674  return false;
675  }
676 
677  }
678 
682  function getObjectList($lm_id, $type = "")
683  {
684  global $ilDB;
685 
686  $type_str = ($type != "")
687  ? "AND type = ".$ilDB->quote($type, "text")." "
688  : "";
689 
690  $query = "SELECT * FROM lm_data ".
691  "WHERE lm_id= ".$ilDB->quote($lm_id, "integer")." ".
692  $type_str." ".
693  "ORDER BY title";
694  $obj_set = $ilDB->query($query);
695  $obj_list = array();
696  while($obj_rec = $ilDB->fetchAssoc($obj_set))
697  {
698  $obj_list[] = array("obj_id" => $obj_rec["obj_id"],
699  "title" => $obj_rec["title"],
700  "import_id" => $obj_rec["import_id"],
701  "type" => $obj_rec["type"]);
702  }
703  return $obj_list;
704  }
705 
706 
710  function _deleteAllObjectData(&$a_cobj)
711  {
712  global $ilDB;
713 
714  include_once './Services/Xml/classes/class.ilNestedSetXML.php';
715 
716  $query = "SELECT * FROM lm_data ".
717  "WHERE lm_id= ".$ilDB->quote($a_cobj->getId(), "integer");
718  $obj_set = $ilDB->query($query);
719 
720  require_once("./Modules/LearningModule/classes/class.ilLMObjectFactory.php");
721  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
722  {
723  $lm_obj = ilLMObjectFactory::getInstance($a_cobj, $obj_rec["obj_id"],false);
724 
725  if (is_object($lm_obj))
726  {
727  $lm_obj->delete(true);
728  }
729  }
730 
731  return true;
732  }
733 
737  function _lookupContObjID($a_id)
738  {
739  global $ilDB;
740 
741  if (isset(self::$data_records[$a_id]))
742  {
743  return self::$data_records[$a_id]["lm_id"];
744  }
745 
746  $query = "SELECT lm_id FROM lm_data WHERE obj_id = ".
747  $ilDB->quote($a_id, "integer");
748  $obj_set = $ilDB->query($query);
749  $obj_rec = $ilDB->fetchAssoc($obj_set);
750 
751  return $obj_rec["lm_id"];
752  }
753 
757  static function putInTree($a_obj, $a_parent_id = "", $a_target_node_id = "")
758  {
759  global $ilLog;
760 
761  $tree = new ilTree($a_obj->getContentObject()->getId());
762  $tree->setTableNames('lm_tree', 'lm_data');
763  $tree->setTreeTablePK("lm_id");
764 
765  // determine parent
766  $parent_id = ($a_parent_id != "")
767  ? $a_parent_id
768  : $tree->getRootId();
769 
770  // determine target
771  if ($a_target_node_id != "")
772  {
773  $target = $a_target_node_id;
774  }
775  else
776  {
777  // determine last child that serves as predecessor
778  if ($a_obj->getType() == "st")
779  {
780  $s_types = array("st", "pg");
781  $childs =& $tree->getChildsByTypeFilter($parent_id, $s_types);
782  }
783  else
784  {
785  $s_types = "pg";
786  $childs =& $tree->getChildsByType($parent_id, $s_types);
787  }
788 
789  if (count($childs) == 0)
790  {
791  $target = IL_FIRST_NODE;
792  }
793  else
794  {
795  $target = $childs[count($childs) - 1]["obj_id"];
796  }
797  }
798 
799  if ($tree->isInTree($parent_id) && !$tree->isInTree($a_obj->getId()))
800  {
801  $ilLog->write("LMObject::putInTree: insertNode, ID: ".$a_obj->getId().
802  "Parent ID: ".$parent_id.", Target: ".$target);
803 
804  $tree->insertNode($a_obj->getId(), $parent_id, $target);
805  }
806  }
807 
815  static function getTree($a_cont_obj_id)
816  {
817  $tree = new ilTree($a_cont_obj_id);
818  $tree->setTableNames('lm_tree', 'lm_data');
819  $tree->setTreeTablePK("lm_id");
820  $tree->readRootId();
821 
822  return $tree;
823  }
824 
828  function clipboardCut($a_cont_obj_id, $a_ids)
829  {
830  $tree = ilLMObject::getTree($a_cont_obj_id);
831 
832  if (!is_array($a_ids))
833  {
834  return false;
835  }
836  else
837  {
838  // get all "top" ids, i.e. remove ids, that have a selected parent
839  foreach($a_ids as $id)
840  {
841  $path = $tree->getPathId($id);
842  $take = true;
843  foreach($path as $path_id)
844  {
845  if ($path_id != $id && in_array($path_id, $a_ids))
846  {
847  $take = false;
848  }
849  }
850  if ($take)
851  {
852  $cut_ids[] = $id;
853  }
854  }
855  }
856 
857  ilLMObject::clipboardCopy($a_cont_obj_id, $cut_ids);
858 
859  // remove the objects from the tree
860  // note: we are getting chapters which are *not* in the tree
861  // we do not delete any pages/chapters here
862  foreach ($cut_ids as $id)
863  {
864  $curnode = $tree->getNodeData($id);
865  if ($tree->isInTree($id))
866  {
867  $tree->deleteTree($curnode);
868  }
869  }
870 
871  }
872 
876  static function clipboardCopy($a_cont_obj_id, $a_ids)
877  {
878  global $ilUser;
879 
880  $tree = ilLMObject::getTree($a_cont_obj_id);
881 
882  $ilUser->clipboardDeleteObjectsOfType("pg");
883  $ilUser->clipboardDeleteObjectsOfType("st");
884 
885  // put them into the clipboard
886  $time = date("Y-m-d H:i:s", time());
887  foreach ($a_ids as $id)
888  {
889  $curnode = "";
890  if ($tree->isInTree($id))
891  {
892  $curnode = $tree->getNodeData($id);
893  $subnodes = $tree->getSubTree($curnode);
894  foreach($subnodes as $subnode)
895  {
896  if ($subnode["child"] != $id)
897  {
898  $ilUser->addObjectToClipboard($subnode["child"],
899  $subnode["type"], $subnode["title"],
900  $subnode["parent"], $time, $subnode["lft"]);
901  }
902  }
903  }
904  $order = ($curnode["lft"] > 0)
905  ? $curnode["lft"]
906  : (int) ($order + 1);
907  $ilUser->addObjectToClipboard($id,
909  $order);
910  }
911  }
912 
916  static function pasteTree($a_target_lm, $a_item_id, $a_parent_id, $a_target, $a_insert_time,
917  &$a_copied_nodes, $a_as_copy = false, $a_source_lm = null)
918  {
919  global $ilUser, $ilias, $ilLog;
920 
921  include_once("./Modules/LearningModule/classes/class.ilStructureObject.php");
922  include_once("./Modules/LearningModule/classes/class.ilLMPageObject.php");
923 
924  $item_lm_id = ilLMObject::_lookupContObjID($a_item_id);
925  $item_type = ilLMObject::_lookupType($a_item_id);
926  $lm_obj = $ilias->obj_factory->getInstanceByObjId($item_lm_id);
927  if ($item_type == "st")
928  {
929  $item = new ilStructureObject($lm_obj, $a_item_id);
930  }
931  else if ($item_type == "pg")
932  {
933  $item = new ilLMPageObject($lm_obj, $a_item_id);
934  }
935 
936  $ilLog->write("Getting from clipboard type ".$item_type.", ".
937  "Item ID: ".$a_item_id.", of original LM: ".$item_lm_id);
938 
939  if ($item_lm_id != $a_target_lm->getId() && !$a_as_copy)
940  {
941  // @todo: check whether st is NOT in tree
942 
943  // "move" metadata to new lm
944  include_once("Services/MetaData/classes/class.ilMD.php");
945  $md = new ilMD($item_lm_id, $item->getId(), $item->getType());
946  $new_md = $md->cloneMD($a_target_lm->getId(), $item->getId(), $item->getType());
947 
948  // update lm object
949  $item->setLMId($a_target_lm->getId());
950  $item->setContentObject($a_target_lm);
951  $item->update();
952 
953  // delete old meta data set
954  $md->deleteAll();
955 
956  if ($item_type == "pg")
957  {
958  $page = $item->getPageObject();
959  $page->buildDom();
960  $page->setParentId($a_target_lm->getId());
961  $page->update();
962  }
963  }
964 
965  if ($a_as_copy)
966  {
967  $target_item = $item->copy($a_target_lm);
968  $a_copied_nodes[$item->getId()] = $target_item->getId();
969  }
970  else
971  {
972  $target_item = $item;
973  }
974 
975  $ilLog->write("Putting into tree type ".$target_item->getType().
976  "Item ID: ".$target_item->getId().", Parent: ".$a_parent_id.", ".
977  "Target: ".$a_target.", Item LM:".$target_item->getContentObject()->getId());
978 
979  ilLMObject::putInTree($target_item, $a_parent_id, $a_target);
980 
981  if ($a_source_lm == null)
982  {
983  $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
984  }
985  else
986  {
987  $childs = $a_source_lm->lm_tree->getChilds($item->getId());
988  foreach ($childs as $k => $child)
989  {
990  $childs[$k]["id"] = $childs[$k]["child"];
991  }
992  }
993 
994  foreach($childs as $child)
995  {
996  ilLMObject::pasteTree($a_target_lm, $child["id"], $target_item->getId(),
997  IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy, $a_source_lm);
998  }
999 
1000  return $target_item->getId();
1001  // @todo: write history (see pastePage)
1002  }
1003 
1009  static function saveTitles($a_lm, $a_titles, $a_lang = "-")
1010  {
1011  include_once("./Modules/LearningModule/classes/class.ilLMObjTranslation.php");
1012 
1013  if ($a_lang == "")
1014  {
1015  $a_lang = "-";
1016  }
1017  if (is_array($a_titles))
1018  {
1019  include_once("./Services/MetaData/classes/class.ilMD.php");
1020  foreach($a_titles as $id => $title)
1021  {
1022  if ($a_lang == "-")
1023  {
1024  $lmobj = ilLMObjectFactory::getInstance($a_lm, $id, false);
1025  if (is_object($lmobj))
1026  {
1027  // Update Title and description
1028  $md = new ilMD($a_lm->getId(), $id, $lmobj->getType());
1029  $md_gen = $md->getGeneral();
1030  $md_gen->setTitle($title);
1031  $md_gen->update();
1032  $md->update();
1034  }
1035  }
1036  else
1037  {
1038  $lmobjtrans = new ilLMObjTranslation($id, $a_lang);
1039  $lmobjtrans->setTitle($title);
1040  $lmobjtrans->save();
1041  }
1042  }
1043  }
1044  }
1045 
1049  static function updateInternalLinks($a_copied_nodes, $a_parent_type = "lm")
1050  {
1051  $all_fixes = array();
1052  foreach($a_copied_nodes as $original_id => $copied_id)
1053  {
1054  $copied_type = ilLMObject::_lookupType($copied_id);
1055  $copy_lm = ilLMObject::_lookupContObjID($copied_id);
1056 
1057  if ($copied_type == "pg")
1058  {
1059  //
1060  // 1. Outgoing links from the copied page.
1061  //
1062  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
1063  include_once("./Modules/LearningModule/classes/class.ilLMPage.php");
1064  $tpg = new ilLMPage($copied_id);
1065  $tpg->buildDom();
1066  $il = $tpg->getInternalLinks();
1067  $targets = array();
1068  foreach($il as $l)
1069  {
1070  $targets[] = array("type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
1071  "id" => (int) ilInternalLink::_extractObjIdOfTarget($l["Target"]),
1072  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"]));
1073  }
1074  $fix = array();
1075  foreach($targets as $target)
1076  {
1077  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
1078  ($target["type"] == "pg" || $target["type"] == "st"))
1079  {
1080  // first check, whether target is also within the copied set
1081  if ($a_copied_nodes[$target["id"]] > 0)
1082  {
1083  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
1084  }
1085  else
1086  {
1087  // now check, if a copy if the target is already in the same lm
1088 
1089  // only if target is not already in the same lm!
1090  $trg_lm = ilLMObject::_lookupContObjID($target["id"]);
1091  if ($trg_lm != $copy_lm)
1092  {
1093  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$target["type"]."_".$target["id"]);
1094  $found = false;
1095 
1096  foreach($lm_data as $item)
1097  {
1098  if (!$found && ($item["lm_id"] == $copy_lm))
1099  {
1100  $fix[$target["id"]] = $item["obj_id"];
1101  $found = true;
1102  }
1103  }
1104  }
1105  }
1106  }
1107  }
1108 
1109  // outgoing links to be fixed
1110  if (count($fix) > 0)
1111  {
1112 //echo "<br>--".$copied_id;
1113 //var_dump($fix);
1114  $t = ilObject::_lookupType($copy_lm);
1115  if (is_array($all_fixes[$t.":".$copied_id]))
1116  {
1117  $all_fixes[$t.":".$copied_id] += $fix;
1118  }
1119  else
1120  {
1121  $all_fixes[$t.":".$copied_id] = $fix;
1122  }
1123  }
1124  }
1125 
1126  if ($copied_type == "pg" ||
1127  $copied_type == "st")
1128  {
1129 
1130  //
1131  // 2. Incoming links to the original pages
1132  //
1133  // A->B A2 (A+B currently copied)
1134  // A->C B2
1135  // B->A
1136  // C->A C2->A (C already copied)
1137  $original_lm = ilLMObject::_lookupContObjID($original_id);
1138  $original_type = ilObject::_lookupType($original_lm);
1139 
1140  if ($original_lm != $copy_lm)
1141  {
1142 
1143  // This gets sources that link to A+B (so we have C here)
1144  // (this also does already the trick when instance map areas are given in C)
1145  // int_link, where target_type, target_id, target_inst -> ok
1146  $sources = ilInternalLink::_getSourcesOfTarget($copied_type,
1147  $original_id, 0);
1148 
1149  // mobs linking to $original_id
1150  // map_area, where link_type, target -> ok
1151  $mobs = ilMapArea::_getMobsForTarget("int", "il__".$copied_type.
1152  "_".$original_id);
1153 
1154  // pages using these mobs
1155  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1156  foreach($mobs as $mob)
1157  {
1158  // mob_usage, where id -> ok
1159  // mep_item, where foreign_id, type -> ok
1160  // mep_tree, where child -> already existed
1161  // il_news_item, where mob_id -> ok
1162  // map_area, where link_type, target -> aready existed
1163  // media_item, where id -> already existed
1164  // personal_clipboard, where item_id, type -> ok
1165  $usages = ilObjMediaObject::lookupUsages($mob);
1166  foreach($usages as $usage)
1167  {
1168  if ($usage["type"] == "lm:pg" | $usage["type"] == "lm:st")
1169  {
1170  $sources[] = $usage;
1171  }
1172  }
1173  }
1174  $fix = array();
1175  foreach($sources as $source)
1176  {
1177  $stype = explode(":", $source["type"]);
1178  $source_type = $stype[1];
1179 
1180  if ($source_type == "pg" || $source_type == "st")
1181  {
1182  // first of all: source must be in original lm
1183  $src_lm = ilLMObject::_lookupContObjID($source["id"]);
1184 
1185  if ($src_lm == $original_lm)
1186  {
1187  // check, if a copy if the source is already in the same lm
1188  // now we look for the latest copy of C in LM2
1189  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$source_type."_".$source["id"],
1190  $copy_lm);
1191  $found = false;
1192  foreach ($lm_data as $item)
1193  {
1194  if (!$found)
1195  {
1196  $fix[$item["obj_id"]][$original_id] = $copied_id;
1197  $found = true;
1198  }
1199  }
1200  }
1201  }
1202  }
1203  // outgoing links to be fixed
1204  if (count($fix) > 0)
1205  {
1206  foreach ($fix as $page_id => $fix_array)
1207  {
1208  $t = ilObject::_lookupType($copy_lm);
1209  if (is_array($all_fixes[$t.":".$page_id]))
1210  {
1211  $all_fixes[$t.":".$page_id] += $fix_array;
1212  }
1213  else
1214  {
1215  $all_fixes[$t.":".$page_id] = $fix_array;
1216  }
1217 
1218  }
1219  }
1220  }
1221  }
1222  }
1223 
1224  foreach ($all_fixes as $pg => $fixes)
1225  {
1226  $pg = explode(":", $pg);
1227  include_once("./Services/COPage/classes/class.ilPageObjectFactory.php");
1228  $page = ilPageObjectFactory::getInstance($pg[0], $pg[1]);
1229  if ($page->moveIntLinks($fixes))
1230  {
1231  $page->update(true, true);
1232  }
1233  }
1234  }
1235 
1239  static function uniqueTypesCheck($a_items)
1240  {
1241  $types = array();
1242  if (is_array($a_items))
1243  {
1244  foreach($a_items as $item)
1245  {
1246  $type = ilLMObject::_lookupType($item);
1247  $types[$type] = $type;
1248  }
1249  }
1250 
1251  if (count($types) > 1)
1252  {
1253  return false;
1254  }
1255  return true;
1256  }
1257 
1264  static function writeLayout($a_obj_id, $a_layout, $a_lm = null)
1265  {
1266  global $ilDB;
1267 
1268  $t = ilLMObject::_lookupType($a_obj_id);
1269 
1270  if ($t == "pg")
1271  {
1272  $query = "UPDATE lm_data SET ".
1273  " layout = ".$ilDB->quote($a_layout, "text").
1274  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1275  $ilDB->manipulate($query);
1276  }
1277  else if ($t == "st" && is_object($a_lm))
1278  {
1279  $node = $a_lm->getLMTree()->getNodeData($a_obj_id);
1280  $child_nodes = $a_lm->getLMTree()->getSubTree($node);
1281  if (is_array($child_nodes) && count($child_nodes) > 0)
1282  {
1283  foreach ($child_nodes as $c)
1284  {
1285  if ($c["type"] == "pg")
1286  {
1287  $query = "UPDATE lm_data SET ".
1288  " layout = ".$ilDB->quote($a_layout, "text").
1289  " WHERE obj_id = ".$ilDB->quote($c["child"], "integer");
1290  $ilDB->manipulate($query);
1291  }
1292  }
1293  }
1294  }
1295  }
1296 
1302  static function lookupLayout($a_obj_id)
1303  {
1304  global $ilDB;
1305 
1306  $query = "SELECT layout FROM lm_data WHERE obj_id = ".
1307  $ilDB->quote($a_obj_id, "integer");
1308  $obj_set = $ilDB->query($query);
1309  $obj_rec = $ilDB->fetchAssoc($obj_set);
1310 
1311  return $obj_rec["layout"];
1312  }
1313 
1320  static function getPagesOfChapter($a_lm_id, $a_chap_id)
1321  {
1322  // update structure entries: if at least one page of a chapter is public set chapter to public too
1323  $lm_tree = new ilTree($a_lm_id);
1324  $lm_tree->setTableNames('lm_tree','lm_data');
1325  $lm_tree->setTreeTablePK("lm_id");
1326  $lm_tree->readRootId();
1327 
1328  $childs = $lm_tree->getChildsByType($a_chap_id, "pg");
1329 
1330  return $childs;
1331  }
1332 
1333 
1337 
1344  public static function saveExportId($a_lm_id, $a_lmobj_id, $a_exp_id, $a_type = "pg")
1345  {
1346  global $ilDB;
1347 
1348  include_once("Services/MetaData/classes/class.ilMDIdentifier.php");
1349 
1350  if (trim($a_exp_id) == "")
1351  {
1352  // delete export ids, if existing
1354  $a_lm_id, $a_lmobj_id, $a_type);
1355 
1356  foreach ($entries as $id => $e)
1357  {
1358  if ($e["catalog"] == "ILIAS_NID")
1359  {
1360  $identifier = new ilMDIdentifier();
1361  $identifier->setMetaId($id);
1362  $identifier->delete();
1363  }
1364  }
1365  }
1366  else
1367  {
1368  // update existing entry
1370  $a_lm_id, $a_lmobj_id, $a_type);
1371 
1372  $updated = false;
1373  foreach ($entries as $id => $e)
1374  {
1375  if ($e["catalog"] == "ILIAS_NID")
1376  {
1377  $identifier = new ilMDIdentifier();
1378  $identifier->setMetaId($id);
1379  $identifier->read();
1380  $identifier->setEntry($a_exp_id);
1381  $identifier->update();
1382  $updated = true;
1383  }
1384  }
1385 
1386  // nothing updated? create a new one
1387  if (!$updated)
1388  {
1389  include_once("./Services/MetaData/classes/class.ilMD.php");
1390  $md = new ilMD($a_lm_id, $a_lmobj_id, $a_type);
1391  $md_gen = $md->getGeneral();
1392  $identifier = $md_gen->addIdentifier();
1393  $identifier->setEntry($a_exp_id);
1394  $identifier->setCatalog("ILIAS_NID");
1395  $identifier->save();
1396  }
1397  }
1398 
1399  }
1400 
1407  public static function getExportId($a_lm_id, $a_lmobj_id, $a_type = "pg")
1408  {
1409  // look for export id
1410  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1412  $a_lm_id, $a_lmobj_id, $a_type);
1413 
1414  foreach ($entries as $e)
1415  {
1416  if ($e["catalog"] == "ILIAS_NID")
1417  {
1418  return $e["entry"];
1419  }
1420  }
1421  }
1422 
1429  function existsExportID($a_lm_id, $a_exp_id, $a_type = "pg")
1430  {
1431  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1432  return ilMDIdentifier::existsIdInRbacObject($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1433  }
1434 
1438  public static function getDuplicateExportIDs($a_lm_id, $a_type = "pg")
1439  {
1440  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1441  $entries = ilMDIdentifier::_getEntriesForRbacObj($a_lm_id, $a_type);
1442  $res = array();
1443  foreach ($entries as $e)
1444  {
1445  if ($e["catalog"] == "ILIAS_NID")
1446  {
1447  if (ilLMObject::_exists($e["obj_id"]))
1448  {
1449  $res[trim($e["entry"])]++;
1450  }
1451  }
1452  }
1453  return $res;
1454  }
1455 
1462  function getExportIDInfo($a_lm_id, $a_exp_id, $a_type = "pg")
1463  {
1464  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1465  $data = ilMDIdentifier::readIdData($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1466  return $data;
1467  }
1468 
1475  static function _getPresentationTitle($a_node, $a_mode = IL_PAGE_TITLE,
1476  $a_include_numbers = false, $a_time_scheduled_activation = false,
1477  $a_force_content = false, $a_lm_id = 0, $a_lang = "-")
1478  {
1479  if ($a_lang == "")
1480  {
1481  $a_lang = "-";
1482  }
1483 
1484  if ($a_node["type"] == "st")
1485  {
1486  return ilStructureObject::_getPresentationTitle($a_node["child"],
1487  $a_include_numbers, $a_time_scheduled_activation, $a_lm_id, $a_lang);
1488  }
1489  else
1490  {
1491  return ilLMPageObject::_getPresentationTitle($a_node["child"],
1492  $a_mode, $a_include_numbers, $a_time_scheduled_activation,
1493  $a_force_content, $a_lm_id, $a_lang);
1494  }
1495  }
1496 
1497 }
1498 ?>