ILIAS  Release_5_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 /* 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/Link/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  if (is_object($md_gen)) // see bug #0015843
1031  {
1032  $md_gen->setTitle($title);
1033  $md_gen->update();
1034  $md->update();
1035  }
1037  }
1038  }
1039  else
1040  {
1041  $lmobjtrans = new ilLMObjTranslation($id, $a_lang);
1042  $lmobjtrans->setTitle($title);
1043  $lmobjtrans->save();
1044  }
1045  }
1046  }
1047  }
1048 
1052  static function updateInternalLinks($a_copied_nodes, $a_parent_type = "lm")
1053  {
1054  $all_fixes = array();
1055  foreach($a_copied_nodes as $original_id => $copied_id)
1056  {
1057  $copied_type = ilLMObject::_lookupType($copied_id);
1058  $copy_lm = ilLMObject::_lookupContObjID($copied_id);
1059 
1060  if ($copied_type == "pg")
1061  {
1062  //
1063  // 1. Outgoing links from the copied page.
1064  //
1065  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
1066  include_once("./Modules/LearningModule/classes/class.ilLMPage.php");
1067  $tpg = new ilLMPage($copied_id);
1068  $tpg->buildDom();
1069  $il = $tpg->getInternalLinks();
1070  $targets = array();
1071  foreach($il as $l)
1072  {
1073  $targets[] = array("type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
1074  "id" => (int) ilInternalLink::_extractObjIdOfTarget($l["Target"]),
1075  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"]));
1076  }
1077  $fix = array();
1078  foreach($targets as $target)
1079  {
1080  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
1081  ($target["type"] == "pg" || $target["type"] == "st"))
1082  {
1083  // first check, whether target is also within the copied set
1084  if ($a_copied_nodes[$target["id"]] > 0)
1085  {
1086  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
1087  }
1088  else
1089  {
1090  // now check, if a copy if the target is already in the same lm
1091 
1092  // only if target is not already in the same lm!
1093  $trg_lm = ilLMObject::_lookupContObjID($target["id"]);
1094  if ($trg_lm != $copy_lm)
1095  {
1096  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$target["type"]."_".$target["id"]);
1097  $found = false;
1098 
1099  foreach($lm_data as $item)
1100  {
1101  if (!$found && ($item["lm_id"] == $copy_lm))
1102  {
1103  $fix[$target["id"]] = $item["obj_id"];
1104  $found = true;
1105  }
1106  }
1107  }
1108  }
1109  }
1110  }
1111 
1112  // outgoing links to be fixed
1113  if (count($fix) > 0)
1114  {
1115 //echo "<br>--".$copied_id;
1116 //var_dump($fix);
1117  $t = ilObject::_lookupType($copy_lm);
1118  if (is_array($all_fixes[$t.":".$copied_id]))
1119  {
1120  $all_fixes[$t.":".$copied_id] += $fix;
1121  }
1122  else
1123  {
1124  $all_fixes[$t.":".$copied_id] = $fix;
1125  }
1126  }
1127  }
1128 
1129  if ($copied_type == "pg" ||
1130  $copied_type == "st")
1131  {
1132 
1133  //
1134  // 2. Incoming links to the original pages
1135  //
1136  // A->B A2 (A+B currently copied)
1137  // A->C B2
1138  // B->A
1139  // C->A C2->A (C already copied)
1140  $original_lm = ilLMObject::_lookupContObjID($original_id);
1141  $original_type = ilObject::_lookupType($original_lm);
1142 
1143  if ($original_lm != $copy_lm)
1144  {
1145 
1146  // This gets sources that link to A+B (so we have C here)
1147  // (this also does already the trick when instance map areas are given in C)
1148  // int_link, where target_type, target_id, target_inst -> ok
1149  $sources = ilInternalLink::_getSourcesOfTarget($copied_type,
1150  $original_id, 0);
1151 
1152  // mobs linking to $original_id
1153  // map_area, where link_type, target -> ok
1154  $mobs = ilMapArea::_getMobsForTarget("int", "il__".$copied_type.
1155  "_".$original_id);
1156 
1157  // pages using these mobs
1158  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1159  foreach($mobs as $mob)
1160  {
1161  // mob_usage, where id -> ok
1162  // mep_item, where foreign_id, type -> ok
1163  // mep_tree, where child -> already existed
1164  // il_news_item, where mob_id -> ok
1165  // map_area, where link_type, target -> aready existed
1166  // media_item, where id -> already existed
1167  // personal_clipboard, where item_id, type -> ok
1168  $usages = ilObjMediaObject::lookupUsages($mob);
1169  foreach($usages as $usage)
1170  {
1171  if ($usage["type"] == "lm:pg" | $usage["type"] == "lm:st")
1172  {
1173  $sources[] = $usage;
1174  }
1175  }
1176  }
1177  $fix = array();
1178  foreach($sources as $source)
1179  {
1180  $stype = explode(":", $source["type"]);
1181  $source_type = $stype[1];
1182 
1183  if ($source_type == "pg" || $source_type == "st")
1184  {
1185  // first of all: source must be in original lm
1186  $src_lm = ilLMObject::_lookupContObjID($source["id"]);
1187 
1188  if ($src_lm == $original_lm)
1189  {
1190  // check, if a copy if the source is already in the same lm
1191  // now we look for the latest copy of C in LM2
1192  $lm_data = ilLMObject::_getAllObjectsForImportId("il__".$source_type."_".$source["id"],
1193  $copy_lm);
1194  $found = false;
1195  foreach ($lm_data as $item)
1196  {
1197  if (!$found)
1198  {
1199  $fix[$item["obj_id"]][$original_id] = $copied_id;
1200  $found = true;
1201  }
1202  }
1203  }
1204  }
1205  }
1206  // outgoing links to be fixed
1207  if (count($fix) > 0)
1208  {
1209  foreach ($fix as $page_id => $fix_array)
1210  {
1211  $t = ilObject::_lookupType($copy_lm);
1212  if (is_array($all_fixes[$t.":".$page_id]))
1213  {
1214  $all_fixes[$t.":".$page_id] += $fix_array;
1215  }
1216  else
1217  {
1218  $all_fixes[$t.":".$page_id] = $fix_array;
1219  }
1220 
1221  }
1222  }
1223  }
1224  }
1225  }
1226 
1227  foreach ($all_fixes as $pg => $fixes)
1228  {
1229  $pg = explode(":", $pg);
1230  include_once("./Services/COPage/classes/class.ilPageObjectFactory.php");
1231  $page = ilPageObjectFactory::getInstance($pg[0], $pg[1]);
1232  if ($page->moveIntLinks($fixes))
1233  {
1234  $page->update(true, true);
1235  }
1236  }
1237  }
1238 
1242  static function uniqueTypesCheck($a_items)
1243  {
1244  $types = array();
1245  if (is_array($a_items))
1246  {
1247  foreach($a_items as $item)
1248  {
1249  $type = ilLMObject::_lookupType($item);
1250  $types[$type] = $type;
1251  }
1252  }
1253 
1254  if (count($types) > 1)
1255  {
1256  return false;
1257  }
1258  return true;
1259  }
1260 
1267  static function writeLayout($a_obj_id, $a_layout, $a_lm = null)
1268  {
1269  global $ilDB;
1270 
1271  $t = ilLMObject::_lookupType($a_obj_id);
1272 
1273  if ($t == "pg")
1274  {
1275  $query = "UPDATE lm_data SET ".
1276  " layout = ".$ilDB->quote($a_layout, "text").
1277  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1278  $ilDB->manipulate($query);
1279  }
1280  else if ($t == "st" && is_object($a_lm))
1281  {
1282  $node = $a_lm->getLMTree()->getNodeData($a_obj_id);
1283  $child_nodes = $a_lm->getLMTree()->getSubTree($node);
1284  if (is_array($child_nodes) && count($child_nodes) > 0)
1285  {
1286  foreach ($child_nodes as $c)
1287  {
1288  if ($c["type"] == "pg")
1289  {
1290  $query = "UPDATE lm_data SET ".
1291  " layout = ".$ilDB->quote($a_layout, "text").
1292  " WHERE obj_id = ".$ilDB->quote($c["child"], "integer");
1293  $ilDB->manipulate($query);
1294  }
1295  }
1296  }
1297  }
1298  }
1299 
1305  static function lookupLayout($a_obj_id)
1306  {
1307  global $ilDB;
1308 
1309  $query = "SELECT layout FROM lm_data WHERE obj_id = ".
1310  $ilDB->quote($a_obj_id, "integer");
1311  $obj_set = $ilDB->query($query);
1312  $obj_rec = $ilDB->fetchAssoc($obj_set);
1313 
1314  return $obj_rec["layout"];
1315  }
1316 
1323  static function getPagesOfChapter($a_lm_id, $a_chap_id)
1324  {
1325  // update structure entries: if at least one page of a chapter is public set chapter to public too
1326  $lm_tree = new ilTree($a_lm_id);
1327  $lm_tree->setTableNames('lm_tree','lm_data');
1328  $lm_tree->setTreeTablePK("lm_id");
1329  $lm_tree->readRootId();
1330 
1331  $childs = $lm_tree->getChildsByType($a_chap_id, "pg");
1332 
1333  return $childs;
1334  }
1335 
1342  static function _getAllLMObjectsOfLM($a_lm_id, $a_type = "")
1343  {
1344  global $ilDB;
1345 
1346  $and = ($a_type != "")
1347  ? " AND type = ".$ilDB->quote($a_type, "text")
1348  : "";
1349 
1350  $set = $ilDB->query("SELECT obj_id FROM lm_data ".
1351  " WHERE lm_id = ".$ilDB->quote($a_lm_id, "integer").$and);
1352  $obj_ids = array();
1353  while ($rec = $ilDB->fetchAssoc($set))
1354  {
1355  $obj_ids[] = $rec["obj_id"];
1356  }
1357 
1358  return $obj_ids;
1359  }
1360 
1361 
1365 
1372  public static function saveExportId($a_lm_id, $a_lmobj_id, $a_exp_id, $a_type = "pg")
1373  {
1374  global $ilDB;
1375 
1376  include_once("Services/MetaData/classes/class.ilMDIdentifier.php");
1377 
1378  if (trim($a_exp_id) == "")
1379  {
1380  // delete export ids, if existing
1382  $a_lm_id, $a_lmobj_id, $a_type);
1383 
1384  foreach ($entries as $id => $e)
1385  {
1386  if ($e["catalog"] == "ILIAS_NID")
1387  {
1388  $identifier = new ilMDIdentifier();
1389  $identifier->setMetaId($id);
1390  $identifier->delete();
1391  }
1392  }
1393  }
1394  else
1395  {
1396  // update existing entry
1398  $a_lm_id, $a_lmobj_id, $a_type);
1399 
1400  $updated = false;
1401  foreach ($entries as $id => $e)
1402  {
1403  if ($e["catalog"] == "ILIAS_NID")
1404  {
1405  $identifier = new ilMDIdentifier();
1406  $identifier->setMetaId($id);
1407  $identifier->read();
1408  $identifier->setEntry($a_exp_id);
1409  $identifier->update();
1410  $updated = true;
1411  }
1412  }
1413 
1414  // nothing updated? create a new one
1415  if (!$updated)
1416  {
1417  include_once("./Services/MetaData/classes/class.ilMD.php");
1418  $md = new ilMD($a_lm_id, $a_lmobj_id, $a_type);
1419  $md_gen = $md->getGeneral();
1420  $identifier = $md_gen->addIdentifier();
1421  $identifier->setEntry($a_exp_id);
1422  $identifier->setCatalog("ILIAS_NID");
1423  $identifier->save();
1424  }
1425  }
1426 
1427  }
1428 
1435  public static function getExportId($a_lm_id, $a_lmobj_id, $a_type = "pg")
1436  {
1437  // look for export id
1438  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1440  $a_lm_id, $a_lmobj_id, $a_type);
1441 
1442  foreach ($entries as $e)
1443  {
1444  if ($e["catalog"] == "ILIAS_NID")
1445  {
1446  return $e["entry"];
1447  }
1448  }
1449  }
1450 
1457  function existsExportID($a_lm_id, $a_exp_id, $a_type = "pg")
1458  {
1459  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1460  return ilMDIdentifier::existsIdInRbacObject($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1461  }
1462 
1466  public static function getDuplicateExportIDs($a_lm_id, $a_type = "pg")
1467  {
1468  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1469  $entries = ilMDIdentifier::_getEntriesForRbacObj($a_lm_id, $a_type);
1470  $res = array();
1471  foreach ($entries as $e)
1472  {
1473  if ($e["catalog"] == "ILIAS_NID")
1474  {
1475  if (ilLMObject::_exists($e["obj_id"]))
1476  {
1477  $res[trim($e["entry"])]++;
1478  }
1479  }
1480  }
1481  return $res;
1482  }
1483 
1490  function getExportIDInfo($a_lm_id, $a_exp_id, $a_type = "pg")
1491  {
1492  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
1493  $data = ilMDIdentifier::readIdData($a_lm_id, $a_type, "ILIAS_NID", $a_exp_id);
1494  return $data;
1495  }
1496 
1503  static function _getPresentationTitle($a_node, $a_mode = IL_PAGE_TITLE,
1504  $a_include_numbers = false, $a_time_scheduled_activation = false,
1505  $a_force_content = false, $a_lm_id = 0, $a_lang = "-")
1506  {
1507  if ($a_lang == "")
1508  {
1509  $a_lang = "-";
1510  }
1511 
1512  if ($a_node["type"] == "st")
1513  {
1514  return ilStructureObject::_getPresentationTitle($a_node["child"],
1515  $a_include_numbers, $a_time_scheduled_activation, $a_lm_id, $a_lang);
1516  }
1517  else
1518  {
1519  return ilLMPageObject::_getPresentationTitle($a_node["child"],
1520  $a_mode, $a_include_numbers, $a_time_scheduled_activation,
1521  $a_force_content, $a_lm_id, $a_lang);
1522  }
1523  }
1524 
1525 
1526 }
1527 ?>