ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLMPageObject.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once("./Modules/LearningModule/classes/class.ilLMObject.php");
6 require_once("./Services/COPage/classes/class.ilPageObject.php");
7 
8 define ("IL_CHAPTER_TITLE", "st_title");
9 define ("IL_PAGE_TITLE", "pg_title");
10 define ("IL_NO_HEADER", "none");
11 
30 {
31  var $is_alias;
33  var $id;
34  var $ilias;
35  var $dom;
37 
42  function ilLMPageObject(&$a_content_obj, $a_id = 0, $a_halt = true)
43  {
44  global $ilias, $ilBench;
45 
46  $ilBench->start("ContentPresentation", "ilLMPageObject_Constructor");
47 
48  parent::ilLMObject($a_content_obj, $a_id);
49  $this->setType("pg");
50  $this->id = $a_id;
51  $this->ilias =& $ilias;
52 
53  $this->is_alias = false;
54  $this->contains_int_link = false;
55  $this->mobs_contained = array();
56  $this->files_contained = array();
57  $this->halt_on_error = $a_halt;
58 
59  if($a_id != 0)
60  {
61  $this->read();
62  }
63 
64  $ilBench->stop("ContentPresentation", "ilLMPageObject_Constructor");
65  }
66 
67  function _ilLMPageObject()
68  {
69  if(is_object($this->page_object))
70  {
71  unset($this->page_object);
72  }
73  }
74 
78  function read()
79  {
80  global $ilBench;
81 
82  $ilBench->start("ContentPresentation", "ilLMPageObject_read");
83  parent::read();
84 
85  $this->page_object =& new ilPageObject($this->content_object->getType(),
86  $this->id, 0, $this->halt_on_error);
87 
88  $ilBench->stop("ContentPresentation", "ilLMPageObject_read");
89  }
90 
91  function create($a_upload = false)
92  {
93  parent::create($a_upload);
94  if(!is_object($this->page_object))
95  {
96  $this->page_object =& new ilPageObject($this->content_object->getType());
97  }
98  $this->page_object->setId($this->getId());
99  $this->page_object->setParentId($this->getLMId());
100  $this->page_object->create($a_upload);
101  }
102 
103  function delete($a_delete_meta_data = true)
104  {
105  parent::delete($a_delete_meta_data);
106  $this->page_object->delete();
107  }
108 
109 
113  function copy($a_target_lm)
114  {
115  // copy page
116  $lm_page = new ilLMPageObject($a_target_lm);
117  $lm_page->setTitle($this->getTitle());
118  $lm_page->setLayout($this->getLayout());
119  $lm_page->setLMId($a_target_lm->getId());
120  $lm_page->setType($this->getType());
121  $lm_page->setDescription($this->getDescription());
122  $lm_page->setImportId("il__pg_".$this->getId());
123  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
124 
125  // check whether export id already exists in the target lm
126  $del_exp_id = false;
127  $exp_id = ilLMPageObject::getExportId($this->getLMId(), $this->getId());
128  if (trim($exp_id) != "")
129  {
130  if (ilLMPageObject::existsExportID($a_target_lm->getId(), $exp_id))
131  {
132  $del_exp_id = true;
133  }
134  }
135 
136  // copy meta data
137  include_once("Services/MetaData/classes/class.ilMD.php");
138  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
139  $new_md = $md->cloneMD($a_target_lm->getId(), $lm_page->getId(), $this->getType());
140 
141  // check whether export id already exists in the target lm
142  if ($del_exp_id)
143  {
144  ilLMPageObject::saveExportId($a_target_lm->getId(), $lm_page->getId(), "");
145  }
146  else
147  {
148  ilLMPageObject::saveExportId($a_target_lm->getId(), $lm_page->getId(),
149  trim($exp_id));
150  }
151 
152  // copy page content and activation
153  $page = $lm_page->getPageObject();
154  $page->setXMLContent($this->page_object->copyXMLContent());
155  $page->setActive($this->page_object->getActive());
156  $page->setActivationStart($this->page_object->getActivationStart());
157  $page->setActivationEnd($this->page_object->getActivationEnd());
158  $page->buildDom();
159  $page->update();
160 
161  return $lm_page;
162  }
163 
167  function &copyToOtherContObject(&$a_cont_obj, &$a_copied_nodes)
168  {
169  // copy page
170  $lm_page =& new ilLMPageObject($a_cont_obj);
171  $lm_page->setTitle($this->getTitle());
172  $lm_page->setLMId($a_cont_obj->getId());
173  $lm_page->setImportId("il__pg_".$this->getId());
174  $lm_page->setType($this->getType());
175  $lm_page->setDescription($this->getDescription());
176  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
177  $a_copied_nodes[$this->getId()] = $lm_page->getId();
178 
179  // copy meta data
180  include_once("Services/MetaData/classes/class.ilMD.php");
181  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
182  $new_md =& $md->cloneMD($a_cont_obj->getId(), $lm_page->getId(), $this->getType());
183 
184  // copy page content
185  $page =& $lm_page->getPageObject();
186  $page->setXMLContent($this->page_object->getXMLContent());
187  $page->buildDom();
188  $page->update();
189 
190  return $lm_page;
191  }
192 
200  function _splitPage($a_page_id, $a_pg_parent_type, $a_hier_id)
201  {
202  // get content object (learning module / digilib book)
203  $lm_id = ilLMObject::_lookupContObjID($a_page_id);
205  switch ($type)
206  {
207  case "lm":
208  include_once ("./Modules/LearningModule/classes/class.ilObjLearningModule.php");
209  $cont_obj = new ilObjLearningModule($lm_id, false);
210  break;
211 
212  case "dbk":
213  include_once ("./Modules/LearningModule/classes/class.ilObjDlBook.php");
214  $cont_obj = new ilObjDlBook($lm_id, false);
215  break;
216  }
217 
218  $source_lm_page =& new ilLMPageObject($cont_obj, $a_page_id);
219 
220  // create new page
221  $lm_page =& new ilLMPageObject($cont_obj);
222  $lm_page->setTitle($source_lm_page->getTitle());
223  $lm_page->setLMId($source_lm_page->getLMId());
224  $lm_page->setType($source_lm_page->getType());
225  $lm_page->setDescription($source_lm_page->getDescription());
226  $lm_page->create(true);
227 
228  // copy meta data
229  include_once("Services/MetaData/classes/class.ilMD.php");
230  $md = new ilMD($source_lm_page->getLMId(), $a_page_id, $source_lm_page->getType());
231  $new_md =& $md->cloneMD($source_lm_page->getLMId(), $lm_page->getId(), $source_lm_page->getType());
232 
233  // copy complete content of source page to new page
234  $source_page =& $source_lm_page->getPageObject();
235  $page =& $lm_page->getPageObject();
236  $page->setXMLContent($source_page->getXMLContent());
237  $page->buildDom();
238 
239  // insert new page in tree (after original page)
240  $tree = new ilTree($cont_obj->getId());
241  $tree->setTableNames('lm_tree','lm_data');
242  $tree->setTreeTablePK("lm_id");
243  if ($tree->isInTree($source_lm_page->getId()))
244  {
245  $parent_node = $tree->getParentNodeData($source_lm_page->getId());
246  $tree->insertNode($lm_page->getId(), $parent_node["child"], $source_lm_page->getId());
247  }
248 
249  // remove all nodes < hierarchical id from new page (incl. update)
250  $page->addHierIds();
251  $page->deleteContentBeforeHierId($a_hier_id);
252 
253  // remove all nodes >= hierarchical id from source page
254  $source_page->buildDom();
255  $source_page->addHierIds();
256  $source_page->deleteContentFromHierId($a_hier_id);
257 
258  return $lm_page;
259 
260  }
261 
269  function _splitPageNext($a_page_id, $a_pg_parent_type, $a_hier_id)
270  {
271  // get content object (learning module / digilib book)
272  $lm_id = ilLMObject::_lookupContObjID($a_page_id);
274  switch ($type)
275  {
276  case "lm":
277  include_once ("./Modules/LearningModule/classes/class.ilObjLearningModule.php");
278  $cont_obj = new ilObjLearningModule($lm_id, false);
279  break;
280 
281  case "dbk":
282  include_once ("./Modules/LearningModule/classes/class.ilObjDlBook.php");
283  $cont_obj = new ilObjDlBook($lm_id, false);
284  break;
285  }
286  $tree = new ilTree($cont_obj->getId());
287  $tree->setTableNames('lm_tree','lm_data');
288  $tree->setTreeTablePK("lm_id");
289 
290  $source_lm_page =& new ilLMPageObject($cont_obj, $a_page_id);
291  $source_page =& $source_lm_page->getPageObject();
292 
293  // get next page
294  $succ = $tree->fetchSuccessorNode($a_page_id, "pg");
295  if ($succ["child"] > 0)
296  {
297  $target_lm_page =& new ilLMPageObject($cont_obj, $succ["child"]);
298  $target_page =& $target_lm_page->getPageObject();
299  $target_page->buildDom();
300  $target_page->addHierIds();
301 
302  // move nodes to target page
303  $source_page->buildDom();
304  $source_page->addHierIds();
305  ilPageObject::_moveContentAfterHierId($source_page, $target_page, $a_hier_id);
306  //$source_page->deleteContentFromHierId($a_hier_id);
307 
308  return $succ["child"];
309  }
310 
311  }
312 
313 
319  function assignPageObject(&$a_page_obj)
320  {
321  $this->page_object =& $a_page_obj;
322  }
323 
324 
330  function &getPageObject()
331  {
332  return $this->page_object;
333  }
334 
335 
339  function setId($a_id)
340  {
341  $this->id = $a_id;
342  }
343 
344  function getId()
345  {
346  return $this->id;
347  }
348 
352  function setAlias($a_is_alias)
353  {
354  $this->is_alias = $a_is_alias;
355  }
356 
357  function isAlias()
358  {
359  return $this->is_alias;
360  }
361 
362  // only for page aliases
363  function setOriginID($a_id)
364  {
365  return $this->origin_id = $a_id;
366  }
367 
368  // only for page aliases
369  function getOriginID()
370  {
371  return $this->origin_id;
372  }
373 
377  function getPageList($lm_id)
378  {
379  return ilLMObject::getObjectList($lm_id, "pg");
380  }
381 
385  function getPagesWithLinksList($a_lm_id, $a_par_type)
386  {
387  $pages = ilLMPageObject::getPageList($a_lm_id);
388  $ids = array();
389  foreach($pages as $page)
390  {
391  $ids[] = $page["obj_id"];
392  }
393 
394  $linked_pages = ilPageObject::getPagesWithLinks($a_par_type, $a_lm_id);
395  $result = array();
396  foreach($pages as $page)
397  {
398  if (is_array($linked_pages[$page["obj_id"]]))
399  {
400  $result[] = $page;
401  }
402  }
403  return $result;
404  }
405 
412  function _getPresentationTitle($a_pg_id, $a_mode = IL_CHAPTER_TITLE,
413  $a_include_numbers = false, $a_time_scheduled_activation = false,
414  $a_force_content = false)
415  {
416  global $ilDB;
417 
418  // select
419  $query = "SELECT * FROM lm_data WHERE obj_id = ".
420  $ilDB->quote($a_pg_id, "integer");
421  $pg_set = $ilDB->query($query);
422  $pg_rec = $ilDB->fetchAssoc($pg_set);
423 
424  if($a_mode == IL_NO_HEADER && !$a_force_content)
425  {
426  return "";
427  }
428 
429  $tree = new ilTree($pg_rec["lm_id"]);
430  $tree->setTableNames('lm_tree','lm_data');
431  $tree->setTreeTablePK("lm_id");
432 
433  if($a_mode == IL_PAGE_TITLE)
434  {
435  $nr = "";
436  return $nr.$pg_rec["title"];
437  }
438 
439  if ($tree->isInTree($pg_rec["obj_id"]))
440  {
441  $pred_node = $tree->fetchPredecessorNode($pg_rec["obj_id"], "st");
442  $childs = $tree->getChildsByType($pred_node["obj_id"], "pg");
443  $cnt_str = "";
444  if(count($childs) > 1)
445  {
446  $cnt = 0;
447  foreach($childs as $child)
448  {
449  include_once("./Services/COPage/classes/class.ilPageObject.php");
450  $active = ilPageObject::_lookupActive($child["obj_id"],
451  ilObject::_lookupType($pg_rec["lm_id"]), $a_time_scheduled_activation);
452 
453  if ($child["type"] != "pg" || $active)
454  {
455  $cnt++;
456  }
457  if($child["obj_id"] == $pg_rec["obj_id"])
458  {
459  $cur_cnt = $cnt;
460  }
461  }
462  if ($cnt > 1)
463  {
464  $cnt_str = " (".$cur_cnt."/".$cnt.")";
465  }
466  }
467  require_once("./Modules/LearningModule/classes/class.ilStructureObject.php");
468  //$struct_obj =& new ilStructureObject($pred_node["obj_id"]);
469  //return $struct_obj->getTitle();
470  return ilStructureObject::_getPresentationTitle($pred_node["obj_id"],
471  $a_include_numbers).$cnt_str;
472  //return $pred_node["title"].$cnt_str;
473  }
474  else
475  {
476  return $pg_rec["title"];
477  }
478  }
479 
486  function exportXML(&$a_xml_writer, $a_mode = "normal", $a_inst = 0)
487  {
488  global $ilBench;
489 
490  $attrs = array();
491  $a_xml_writer->xmlStartTag("PageObject", $attrs);
492 
493  switch ($a_mode)
494  {
495  case "normal":
496  // MetaData
497  $ilBench->start("ContentObjectExport", "exportPageObject_XML_Meta");
498  $this->exportXMLMetaData($a_xml_writer);
499  $ilBench->stop("ContentObjectExport", "exportPageObject_XML_Meta");
500 
501  // PageContent
502  $ilBench->start("ContentObjectExport", "exportPageObject_XML_PageContent");
503  $this->exportXMLPageContent($a_xml_writer, $a_inst);
504  $ilBench->stop("ContentObjectExport", "exportPageObject_XML_PageContent");
505  break;
506 
507  case "alias":
508  $attrs = array();
509  $attrs["OriginId"] = "il_".$a_inst.
510  "_pg_".$this->getId();
511  $a_xml_writer->xmlElement("PageAlias", $attrs);
512  break;
513  }
514 
515  // Layout
516  // not implemented
517 
518  $a_xml_writer->xmlEndTag("PageObject");
519  }
520 
524  function _exportXMLAlias(&$a_xml_writer, $a_id, $a_inst = 0)
525  {
526  $attrs = array();
527  $a_xml_writer->xmlStartTag("PageObject", $attrs);
528 
529  $attrs = array();
530  $attrs["OriginId"] = "il_".$a_inst.
531  "_pg_".$a_id;
532  $a_xml_writer->xmlElement("PageAlias", $attrs);
533 
534  $a_xml_writer->xmlEndTag("PageObject");
535  }
536 
537 
544  function exportXMLMetaData(&$a_xml_writer)
545  {
546  include_once("Services/MetaData/classes/class.ilMD2XML.php");
547  $md2xml = new ilMD2XML($this->getLMId(), $this->getId(), $this->getType());
548  $md2xml->setExportMode(true);
549  $md2xml->startExport();
550  $a_xml_writer->appendXML($md2xml->getXML());
551  }
552 
553  function modifyExportIdentifier($a_tag, $a_param, $a_value)
554  {
555  if ($a_tag == "Identifier" && $a_param == "Entry")
556  {
557  $a_value = "il_".IL_INST_ID."_pg_".$this->getId();
558  //$a_value = ilUtil::insertInstIntoID($a_value);
559  }
560 
561  return $a_value;
562  }
563 
564 
571  function exportXMLPageContent(&$a_xml_writer, $a_inst = 0)
572  {
573 //echo "exportxmlpagecontent:$a_inst:<br>";
574  $cont_obj =& $this->getContentObject();
575  //$page_obj = new ilPageObject($cont_obj->getType(), $this->getId());
576 
577  $this->page_object->buildDom();
578  $this->page_object->insertInstIntoIDs($a_inst);
579  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
580  $this->files_contained = $this->page_object->collectFileItems();
581 // $this->questions_contained = $this->page_object->getQuestionIds();
582  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
583  $xml = str_replace("&","&amp;", $xml);
584  $a_xml_writer->appendXML($xml);
585 
586  $this->page_object->freeDom();
587  }
588 
594  function getQuestionIds()
595  {
596  return ilPageObject::_getQuestionIdsForPage($this->content_object->getType(),
597  $this->getId());
598  }
599 
605  function getMediaObjectIds()
606  {
607  return $this->mobs_contained;
608  }
609 
615  function getFileItemIds()
616  {
617  return $this->files_contained;
618  }
619 
626  function exportFO(&$a_xml_writer)
627  {
628  global $ilBench;
629 
630  //$attrs = array();
631  //$a_xml_writer->xmlStartTag("PageObject", $attrs);
633  if ($title != "")
634  {
635  $attrs = array();
636  $attrs["font-family"] = "Times";
637  $attrs["font-size"] = "14pt";
638  $a_xml_writer->xmlElement("fo:block", $attrs, $title);
639  }
640 
641  // PageContent
642  $this->page_object->buildDom();
643  $fo = $this->page_object->getFO();
644  $a_xml_writer->appendXML($fo);
645 
646  //$a_xml_writer->xmlEndTag("PageObject");
647  }
648 
655  function queryQuestionsOfLearningModule($a_lm_id, $a_order_field,
656  $a_order_dir, $a_offset, $a_limit)
657  {
658  global $ilDB, $rbacreview;
659 
660 
661  // count query
662  $count_query = "SELECT count(pq.question_id) cnt ";
663 
664  // basic query
665  $query = "SELECT pq.page_id, pq.question_id ";
666 
667  $from = " FROM page_question pq JOIN lm_tree t ON (t.lm_id = ".$ilDB->quote($a_lm_id, "integer").
668  " AND pq.page_id = t.child and pq.page_parent_type = ".$ilDB->quote("lm", "text").") ".
669  " WHERE t.lm_id = ".$ilDB->quote($a_lm_id, "integer");
670  $count_query.= $from;
671  $query.= $from;
672 
673 
674  // count query
675  $set = $ilDB->query($count_query);
676  $cnt = 0;
677  if ($rec = $ilDB->fetchAssoc($set))
678  {
679  $cnt = $rec["cnt"];
680  }
681 
682  $offset = (int) $a_offset;
683  $limit = (int) $a_limit;
684  $ilDB->setLimit($limit, $offset);
685 
686  // set query
687  $set = $ilDB->query($query);
688  $result = array();
689  while($rec = $ilDB->fetchAssoc($set))
690  {
691  $result[] = $rec;
692  }
693  return array("cnt" => $cnt, "set" => $result);
694  }
695 
702  public static function saveExportId($a_lm_id, $a_pg_id, $a_exp_id)
703  {
704  global $ilDB;
705 
706  include_once("Services/MetaData/classes/class.ilMDIdentifier.php");
707 
708  if (trim($a_exp_id) == "")
709  {
710  // delete export ids, if existing
712  $a_lm_id, $a_pg_id, "pg");
713 
714  foreach ($entries as $id => $e)
715  {
716  if ($e["catalog"] == "ILIAS_NID")
717  {
718  $identifier = new ilMDIdentifier();
719  $identifier->setMetaId($id);
720  $identifier->delete();
721  }
722  }
723  }
724  else
725  {
726  // update existing entry
728  $a_lm_id, $a_pg_id, "pg");
729 
730  $updated = false;
731  foreach ($entries as $id => $e)
732  {
733  if ($e["catalog"] == "ILIAS_NID")
734  {
735  $identifier = new ilMDIdentifier();
736  $identifier->setMetaId($id);
737  $identifier->read();
738  $identifier->setEntry($a_exp_id);
739  $identifier->update();
740  $updated = true;
741  }
742  }
743 
744  // nothing updated? create a new one
745  if (!$updated)
746  {
747  include_once("./Services/MetaData/classes/class.ilMD.php");
748  $md = new ilMD($a_lm_id, $a_pg_id, "pg");
749  $md_gen = $md->getGeneral();
750  $identifier = $md_gen->addIdentifier();
751  $identifier->setEntry($a_exp_id);
752  $identifier->setCatalog("ILIAS_NID");
753  $identifier->save();
754  }
755  }
756 
757  }
758 
765  public static function getExportId($a_lm_id, $a_pg_id)
766  {
767  // look for export id
768  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
770  $a_lm_id, $a_pg_id, "pg");
771 
772  foreach ($entries as $e)
773  {
774  if ($e["catalog"] == "ILIAS_NID")
775  {
776  return $e["entry"];
777  }
778  }
779  }
780 
787  function existsExportID($a_lm_id, $a_exp_id)
788  {
789  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
790  return ilMDIdentifier::existsIdInRbacObject($a_lm_id, "pg", "ILIAS_NID", $a_exp_id);
791  }
792 
796  public static function getDuplicateExportIDs($a_lm_id)
797  {
798  include_once("./Services/MetaData/classes/class.ilMDIdentifier.php");
799  $entries = ilMDIdentifier::_getEntriesForRbacObj($a_lm_id, "pg");
800  $res = array();
801  foreach ($entries as $e)
802  {
803  if ($e["catalog"] == "ILIAS_NID")
804  {
805  if (ilLMObject::_exists($e["obj_id"]))
806  {
807  $res[trim($e["entry"])]++;
808  }
809  }
810  }
811  return $res;
812  }
813 }
814 ?>