ILIAS  release_8 Revision v8.23
class.ilLMPageObject.php
Go to the documentation of this file.
1 <?php
2 
32 {
33  protected bool $halt_on_error;
34  protected array $files_contained;
35  protected array $mobs_contained;
36  protected bool $contains_int_link;
37  public ?ilLMPage $page_object = null;
38 
39  public function __construct(
40  ilObjLearningModule $a_content_obj,
41  int $a_id = 0,
42  bool $a_halt = true
43  ) {
44  parent::__construct($a_content_obj, $a_id);
45  $this->setType("pg");
46  $this->id = $a_id;
47 
48  $this->contains_int_link = false;
49  $this->mobs_contained = array();
50  $this->files_contained = array();
51  $this->halt_on_error = $a_halt;
52 
53  if ($a_id != 0) {
54  $this->read();
55  }
56  }
57 
58  public function read(): void
59  {
60  parent::read();
61  $this->page_object = new ilLMPage($this->id, 0);
62  }
63 
64  public function create(
65  bool $a_upload = false,
66  bool $a_omit_page_object_creation = false,
67  int $a_layout_id = 0
68  ): void {
69  parent::create($a_upload);
70  if ($a_omit_page_object_creation) {
71  return;
72  }
73  if (!is_object($this->page_object)) {
74  $this->page_object = new ilLMPage();
75  }
76  $this->page_object->setId($this->getId());
77  $this->page_object->setParentId($this->getLMId());
78  if ($a_layout_id == 0) {
79  $this->page_object->create(false);
80  } else {
81  $this->page_object->createWithLayoutId($a_layout_id);
82  }
83  }
84 
85  public function delete(bool $a_delete_meta_data = true): void
86  {
87  parent::delete($a_delete_meta_data);
88  $this->page_object->delete();
89  }
90 
91  // copy page
92  public function copy(
93  ilObjLearningModule $a_target_lm
94  ): ilLMPageObject {
95  // copy page
96  $lm_page = new ilLMPageObject($a_target_lm);
97  $lm_page->setTitle($this->getTitle());
98  $lm_page->setShortTitle($this->getShortTitle());
99  $lm_page->setLayout($this->getLayout());
100  $lm_page->setLMId($a_target_lm->getId());
101  $lm_page->setType($this->getType());
102  $lm_page->setDescription($this->getDescription());
103  $lm_page->setImportId("il__pg_" . $this->getId());
104  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
105 
106  // check whether export id already exists in the target lm
107  $del_exp_id = false;
108  $exp_id = ilLMPageObject::getExportId($this->getLMId(), $this->getId());
109  if (trim($exp_id) != "") {
110  if (ilLMPageObject::existsExportID($a_target_lm->getId(), $exp_id)) {
111  $del_exp_id = true;
112  }
113  }
114 
115  // copy meta data
116  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
117  $new_md = $md->cloneMD($a_target_lm->getId(), $lm_page->getId(), $this->getType());
118 
119  // check whether export id already exists in the target lm
120  if ($del_exp_id) {
121  ilLMPageObject::saveExportId($a_target_lm->getId(), $lm_page->getId(), "");
122  } else {
124  $a_target_lm->getId(),
125  $lm_page->getId(),
126  trim($exp_id)
127  );
128  }
129 
130  // copy page content and activation
131  $page = $lm_page->getPageObject();
132  $this->page_object->copy($page->getId(), $page->getParentType(), $page->getParentId());
133  $lm_page->read(); // this gets the updated page object into lm page
134 
135  // copy translations
136  ilLMObjTranslation::copy($this->getId(), $lm_page->getId());
137 
138  return $lm_page;
139  }
140 
144  public function copyToOtherContObject(
145  ilObjLearningModule $a_cont_obj,
146  array &$a_copied_nodes
147  ): ilLMPageObject {
148  // copy page
149  $lm_page = new ilLMPageObject($a_cont_obj);
150  $lm_page->setTitle($this->getTitle());
151  $lm_page->setShortTitle($this->getShortTitle());
152  $lm_page->setLMId($a_cont_obj->getId());
153  $lm_page->setImportId("il__pg_" . $this->getId());
154  $lm_page->setType($this->getType());
155  $lm_page->setDescription($this->getDescription());
156  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
157  $a_copied_nodes[$this->getId()] = $lm_page->getId();
158 
159  // copy meta data
160  $md = new ilMD($this->getLMId(), $this->getId(), $this->getType());
161  $new_md = $md->cloneMD($a_cont_obj->getId(), $lm_page->getId(), $this->getType());
162 
163  // copy page content
164  $page = $lm_page->getPageObject();
165  $page->setXMLContent($this->page_object->getXMLContent());
166  $page->buildDom();
167  $page->update();
168 
169  return $lm_page;
170  }
171 
178  public static function _splitPage(
179  int $a_page_id,
180  string $a_pg_parent_type,
181  string $a_hier_id
182  ): ilLMPageObject {
183  // get content object (learning module / digilib book)
184  $lm_id = ilLMObject::_lookupContObjID($a_page_id);
186  $cont_obj = new ilObjLearningModule($lm_id, false);
187  $source_lm_page = new ilLMPageObject($cont_obj, $a_page_id);
188 
189  // create new page
190  $lm_page = new ilLMPageObject($cont_obj);
191  $lm_page->setTitle($source_lm_page->getTitle());
192  $lm_page->setLMId($source_lm_page->getLMId());
193  $lm_page->setType($source_lm_page->getType());
194  $lm_page->setDescription($source_lm_page->getDescription());
195  $lm_page->create(true);
196 
197 
198  // copy complete content of source page to new page
199  $source_page = $source_lm_page->getPageObject();
200  $page = $lm_page->getPageObject();
201  $page->setXMLContent($source_page->copyXmlContent());
202  $page->buildDom(true);
203  $page->update();
204 
205  // copy meta data
206  $md = new ilMD($source_lm_page->getLMId(), $a_page_id, $source_lm_page->getType());
207  $md->cloneMD($source_lm_page->getLMId(), $lm_page->getId(), $source_lm_page->getType());
208 
209  // insert new page in tree (after original page)
210  $tree = new ilTree($cont_obj->getId());
211  $tree->setTableNames('lm_tree', 'lm_data');
212  $tree->setTreeTablePK("lm_id");
213  if ($tree->isInTree($source_lm_page->getId())) {
214  $parent_node = $tree->getParentNodeData($source_lm_page->getId());
215  $tree->insertNode($lm_page->getId(), $parent_node["child"], $source_lm_page->getId());
216  }
217 
218  // remove all nodes < hierarchical id from new page (incl. update)
219  $page->addHierIDs();
220  $page->deleteContentBeforeHierId($a_hier_id);
221  // $page->update();
222 
223  // remove all nodes >= hierarchical id from source page
224  $source_page->buildDom();
225  $source_page->addHierIDs();
226  $source_page->deleteContentFromHierId($a_hier_id);
227 
228  return $lm_page;
229  }
230 
238  public static function _splitPageNext(
239  int $a_page_id,
240  string $a_pg_parent_type,
241  string $a_hier_id
242  ): int {
243  // get content object (learning module / digilib book)
244  $lm_id = ilLMObject::_lookupContObjID($a_page_id);
246  $cont_obj = new ilObjLearningModule($lm_id, false);
247  $tree = new ilTree($cont_obj->getId());
248  $tree->setTableNames('lm_tree', 'lm_data');
249  $tree->setTreeTablePK("lm_id");
250 
251  $source_lm_page = new ilLMPageObject($cont_obj, $a_page_id);
252  $source_page = $source_lm_page->getPageObject();
253 
254  // get next page
255  $succ = $tree->fetchSuccessorNode($a_page_id, "pg");
256  if ($succ["child"] > 0) {
257  $target_lm_page = new ilLMPageObject($cont_obj, $succ["child"]);
258  $target_page = $target_lm_page->getPageObject();
259  $target_page->buildDom();
260  $target_page->addHierIDs();
261 
262  // move nodes to target page
263  $source_page->buildDom();
264  $source_page->addHierIDs();
265  ilLMPage::_moveContentAfterHierId($source_page, $target_page, $a_hier_id);
266  //$source_page->deleteContentFromHierId($a_hier_id);
267 
268  return (int) $succ["child"];
269  }
270  return 0;
271  }
272 
273 
277  public function assignPageObject(ilLMPage $a_page_obj): void
278  {
279  $this->page_object = $a_page_obj;
280  }
281 
282 
286  public function getPageObject(): ilLMPage
287  {
288  return $this->page_object;
289  }
290 
291  public function setId(int $a_id): void
292  {
293  $this->id = $a_id;
294  }
295 
296  public function getId(): int
297  {
298  return $this->id;
299  }
300 
301  public static function getPageList(int $lm_id): array
302  {
303  return ilLMObject::getObjectList($lm_id, "pg");
304  }
305 
309  public static function getPagesWithLinksList(
310  int $a_lm_id,
311  string $a_par_type
312  ): array {
313  $pages = ilLMPageObject::getPageList($a_lm_id);
314  $linked_pages = ilLMPage::getPagesWithLinks($a_par_type, $a_lm_id);
315  $result = array();
316  foreach ($pages as $page) {
317  if (isset($linked_pages[$page["obj_id"]])) {
318  $result[] = $page;
319  }
320  }
321  return $result;
322  }
323 
329  public static function _getPresentationTitle(
330  int $a_pg_id,
331  string $a_mode = self::CHAPTER_TITLE,
332  bool $a_include_numbers = false,
333  bool $a_time_scheduled_activation = false,
334  bool $a_force_content = false,
335  int $a_lm_id = 0,
336  string $a_lang = "-",
337  bool $a_include_short = false
338  ): string {
339  if ($a_mode == self::NO_HEADER && !$a_force_content) {
340  return "";
341  }
342 
343  $cur_cnt = 0;
344 
345  if ($a_lm_id == 0) {
346  $a_lm_id = ilLMObject::_lookupContObjID($a_pg_id);
347  }
348 
349  if ($a_lm_id == 0) {
350  return "";
351  }
352 
353  // this is optimized when ilLMObject::preloadDataByLM is invoked (e.g. done in ilLMExplorerGUI)
354  $title = "";
355  if ($a_include_short) {
356  $title = trim(ilLMObject::_lookupShortTitle($a_pg_id));
357  }
358  if ($title == "") {
359  $title = ilLMObject::_lookupTitle($a_pg_id);
360  }
361 
362  // this is also optimized since ilObjectTranslation re-uses instances for one lm
363  $ot = ilObjectTranslation::getInstance($a_lm_id);
364  $languages = $ot->getLanguages();
365 
366  if ($a_lang != "-" && $ot->getContentActivated()) {
367  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $a_lang);
368  $trans_title = "";
369  if ($a_include_short) {
370  $trans_title = trim($lmobjtrans->getShortTitle());
371  }
372  if ($trans_title == "") {
373  $trans_title = $lmobjtrans->getTitle();
374  }
375  if ($trans_title == "") {
376  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $ot->getFallbackLanguage());
377  $trans_title = $lmobjtrans->getTitle();
378  }
379  if ($trans_title != "") {
380  $title = $trans_title;
381  }
382  }
383 
384  if ($a_mode == self::PAGE_TITLE) {
385  return $title;
386  }
387 
388  $tree = ilLMTree::getInstance($a_lm_id);
389 
390  if ($tree->isInTree($a_pg_id)) {
391  $pred_node = $tree->fetchPredecessorNode($a_pg_id, "st");
392  $childs = $tree->getChildsByType($pred_node["obj_id"], "pg");
393  $cnt_str = "";
394  if (count($childs) > 1) {
395  $cnt = 0;
396  foreach ($childs as $child) {
398  $child["obj_id"],
399  ilObject::_lookupType($a_lm_id),
400  $a_time_scheduled_activation
401  );
402 
403  if (!$active) {
404  $act_data = ilLMPage::_lookupActivationData((int) $child["obj_id"], ilObject::_lookupType($a_lm_id));
405  if ($act_data["show_activation_info"] &&
406  (ilUtil::now() < $act_data["activation_start"])) {
407  $active = true;
408  }
409  }
410 
411  if ($child["type"] != "pg" || $active) {
412  $cnt++;
413  }
414  if ($child["obj_id"] == $a_pg_id) {
415  $cur_cnt = $cnt;
416  }
417  }
418  if ($cnt > 1) {
419  $cnt_str = " (" . $cur_cnt . "/" . $cnt . ")";
420  }
421  }
423  $pred_node["obj_id"],
424  self::CHAPTER_TITLE,
425  $a_include_numbers,
426  false,
427  false,
428  0,
429  $a_lang,
430  true
431  ) . $cnt_str;
432  } else {
433  return $title;
434  }
435  }
436 
440  public function exportXML(
441  ilXmlWriter $a_xml_writer,
442  string $a_mode = "normal",
443  int $a_inst = 0
444  ): void {
445  $attrs = array();
446  $a_xml_writer->xmlStartTag("PageObject", $attrs);
447 
448  switch ($a_mode) {
449  case "normal":
450  // MetaData
451  $this->exportXMLMetaData($a_xml_writer);
452 
453  // PageContent
454  $this->exportXMLPageContent($a_xml_writer, $a_inst);
455  break;
456 
457  case "alias":
458  $attrs = array();
459  $attrs["OriginId"] = "il_" . $a_inst .
460  "_pg_" . $this->getId();
461  $a_xml_writer->xmlElement("PageAlias", $attrs);
462  break;
463  }
464  $a_xml_writer->xmlEndTag("PageObject");
465  }
466 
470  public static function _exportXMLAlias(
471  ilXmlWriter $a_xml_writer,
472  int $a_id,
473  int $a_inst = 0
474  ): void {
475  $attrs = array();
476  $a_xml_writer->xmlStartTag("PageObject", $attrs);
477 
478  $attrs = array();
479  $attrs["OriginId"] = "il_" . $a_inst .
480  "_pg_" . $a_id;
481  $a_xml_writer->xmlElement("PageAlias", $attrs);
482 
483  $a_xml_writer->xmlEndTag("PageObject");
484  }
485 
486  public function exportXMLMetaData(
487  ilXmlWriter $a_xml_writer
488  ): void {
489  $md2xml = new ilMD2XML($this->getLMId(), $this->getId(), $this->getType());
490  $md2xml->setExportMode(true);
491  $md2xml->startExport();
492  $a_xml_writer->appendXML($md2xml->getXML());
493  }
494 
495  public function modifyExportIdentifier(
496  string $a_tag,
497  string $a_param,
498  string $a_value
499  ): string {
500  if ($a_tag == "Identifier" && $a_param == "Entry") {
501  $a_value = "il_" . IL_INST_ID . "_pg_" . $this->getId();
502  }
503 
504  return $a_value;
505  }
506 
507  public function exportXMLPageContent(
508  ilXmlWriter $a_xml_writer,
509  int $a_inst = 0
510  ): void {
511  $this->page_object->buildDom();
512  $this->page_object->insertInstIntoIDs($a_inst);
513  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
514  $this->files_contained = ilPCFileList::collectFileItems($this->page_object, $this->page_object->getDomDoc());
515  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
516  $xml = str_replace("&", "&amp;", $xml);
517  $a_xml_writer->appendXML($xml);
518 
519  $this->page_object->freeDom();
520  }
521 
526  public function getQuestionIds(): array
527  {
529  $this->content_object->getType(),
530  $this->getId()
531  );
532  }
533 
538  public function getMediaObjectIds(): array
539  {
540  return $this->mobs_contained;
541  }
542 
547  public function getFileItemIds(): array
548  {
549  return $this->files_contained;
550  }
551 
555  public function exportFO(
556  ilXmlWriter $a_xml_writer
557  ): void {
559  if ($title != "") {
560  $attrs = array();
561  $attrs["font-family"] = "Times";
562  $attrs["font-size"] = "14pt";
563  $a_xml_writer->xmlElement("fo:block", $attrs, $title);
564  }
565 
566  // PageContent
567  $this->page_object->buildDom();
568  $fo = $this->page_object->getFO();
569  $a_xml_writer->appendXML($fo);
570  }
571 
575  public static function queryQuestionsOfLearningModule(
576  int $a_lm_id,
577  string $a_order_field,
578  string $a_order_dir,
579  int $a_offset,
580  int $a_limit
581  ): array {
582  global $DIC;
583 
584  $ilDB = $DIC->database();
585 
586  // count query
587  $count_query = "SELECT count(pq.question_id) cnt ";
588 
589  // basic query
590  $query = "SELECT pq.page_id, pq.question_id ";
591 
592  $from = " FROM page_question pq JOIN lm_tree t ON (t.lm_id = " . $ilDB->quote($a_lm_id, "integer") .
593  " AND pq.page_id = t.child and pq.page_parent_type = " . $ilDB->quote("lm", "text") . ") " .
594  " WHERE t.lm_id = " . $ilDB->quote($a_lm_id, "integer");
595  $count_query .= $from;
596  $query .= $from;
597 
598  // count query
599  $set = $ilDB->query($count_query);
600  $cnt = 0;
601  if ($rec = $ilDB->fetchAssoc($set)) {
602  $cnt = $rec["cnt"];
603  }
604 
605  $offset = $a_offset;
606  $limit = $a_limit;
607  if ($a_limit > 0) {
608  $ilDB->setLimit($limit, $offset);
609  }
610 
611  // set query
612  $set = $ilDB->query($query);
613  $result = array();
614  while ($rec = $ilDB->fetchAssoc($set)) {
615  $result[] = $rec;
616  }
617  return array("cnt" => $cnt, "set" => $result);
618  }
619 
623  public static function insertPagesFromTemplate(
624  int $lm_id,
625  int $num,
626  int $node_id,
627  bool $first_child,
628  int $layout_id,
629  string $title = ""
630  ): array {
631  global $DIC;
632 
633  $lng = $DIC->language();
634 
635  if ($title == "") {
636  $title = $lng->txt("cont_new_page");
637  }
638  $lm_tree = new ilLMTree($lm_id);
639  $lm = new ilObjLearningModule($lm_id, false);
640  if (!$first_child) { // insert after node id
641  $parent_id = $lm_tree->getParentId($node_id);
642  $target = $node_id;
643  } else { // insert as first child
644  $parent_id = $node_id;
645  $target = ilTree::POS_FIRST_NODE;
646  }
647 
648  $page_ids = array();
649  for ($i = 1; $i <= $num; $i++) {
650  $page = new ilLMPageObject($lm);
651  $page->setTitle($title);
652  $page->setLMId($lm->getId());
653  $page->create(false, false, $layout_id);
654  ilLMObject::putInTree($page, $parent_id, $target);
655  $page_ids[] = $page->getId();
656  }
657  $page_ids = array_reverse($page_ids);
658  return $page_ids;
659  }
660 }
static _lookupActive(int $a_id, string $a_parent_type, bool $a_check_scheduled_activation=false, string $a_lang="-")
lookup activation status
static queryQuestionsOfLearningModule(int $a_lm_id, string $a_order_field, string $a_order_dir, int $a_offset, int $a_limit)
Get questions of learning module.
static _getPresentationTitle(int $a_st_id, string $a_mode=self::CHAPTER_TITLE, bool $a_include_numbers=false, bool $a_time_scheduled_activation=false, bool $a_force_content=false, int $a_lm_id=0, string $a_lang="-", bool $a_include_short=false)
static _exportXMLAlias(ilXmlWriter $a_xml_writer, int $a_id, int $a_inst=0)
export page alias to xml
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_INST_ID
Definition: constants.php:40
static getPagesWithLinks(string $a_parent_type, int $a_parent_id, string $a_lang="-")
Get all pages for parent object that contain internal links.
getPageObject()
get assigned page object
static getExportId(int $a_lm_id, int $a_lmobj_id, string $a_type="pg")
$lng
getQuestionIds()
Get question ids note: this method must be called afer exportXMLPageContent.
setType(string $a_type)
exportXMLMetaData(ilXmlWriter $a_xml_writer)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupShortTitle(int $a_obj_id)
static getObjectList(int $lm_id, string $type="")
static _lookupActivationData(int $a_id, string $a_parent_type, string $a_lang="-")
Lookup activation data.
appendXML(string $a_str)
append xml string to document
static now()
Return current timestamp in Y-m-d H:i:s format.
static getInstance(int $a_tree_id)
modifyExportIdentifier(string $a_tag, string $a_param, string $a_value)
create(bool $a_upload=false, bool $a_omit_page_object_creation=false, int $a_layout_id=0)
static putInTree(ilLMObject $a_obj, int $a_parent_id=0, int $a_target_node_id=0)
put this object into content object tree
xmlEndTag(string $tag)
Writes an endtag.
static _lookupTitle(int $a_obj_id)
global $DIC
Definition: feed.php:28
const POS_FIRST_NODE
static collectFileItems(ilPageObject $a_page, DOMDocument $a_domdoc)
Get all file items that are used within the page.
static copy(string $a_source_id, string $a_target_id)
Copy all translations of an object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _splitPage(int $a_page_id, string $a_pg_parent_type, string $a_hier_id)
split page at hierarchical id the main reason for this method being static is that a lm page object i...
getFileItemIds()
get ids of all file items within the page note: this method must be called afer exportXMLPageContent ...
__construct(ilObjLearningModule $a_content_obj, int $a_id=0, bool $a_halt=true)
static getInstance(int $obj_id)
static insertPagesFromTemplate(int $lm_id, int $num, int $node_id, bool $first_child, int $layout_id, string $title="")
Insert (multiple) pages templates at node.
$xml
Definition: metadata.php:351
exportFO(ilXmlWriter $a_xml_writer)
export page object to fo
$query
static getPagesWithLinksList(int $a_lm_id, string $a_par_type)
Get all pages of lm that contain any internal links.
existsExportID(int $a_lm_id, int $a_exp_id, string $a_type="pg")
Does export ID exist in lm?
assignPageObject(ilLMPage $a_page_obj)
assign page object
getMediaObjectIds()
get ids of all media objects within the page note: this method must be called afer exportXMLPageConte...
copy(ilObjLearningModule $a_target_lm)
exportXML(ilXmlWriter $a_xml_writer, string $a_mode="normal", int $a_inst=0)
export page object to xml (see ilias_co.dtd)
static saveExportId(int $a_lm_id, int $a_lmobj_id, string $a_exp_id, string $a_type="pg")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getPresentationTitle(int $a_pg_id, string $a_mode=self::CHAPTER_TITLE, bool $a_include_numbers=false, bool $a_time_scheduled_activation=false, bool $a_force_content=false, int $a_lm_id=0, string $a_lang="-", bool $a_include_short=false)
presentation title doesn&#39;t have to be page title, it may be chapter title + page title or chapter tit...
static getPageList(int $lm_id)
__construct(Container $dic, ilPlugin $plugin)
static _lookupContObjID(int $a_id)
get learning module id for lm object
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
static _lookupType(int $id, bool $reference=false)
exportXMLPageContent(ilXmlWriter $a_xml_writer, int $a_inst=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _splitPageNext(int $a_page_id, string $a_pg_parent_type, string $a_hier_id)
split page to next page at hierarchical id
static _getQuestionIdsForPage(string $a_parent_type, int $a_page_id, string $a_lang="-")
copyToOtherContObject(ilObjLearningModule $a_cont_obj, array &$a_copied_nodes)
copy a page to another content object (learning module / dlib book)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
cloneMD(int $a_rbac_id, int $a_obj_id, string $a_obj_type)
Definition: class.ilMD.php:288
$i
Definition: metadata.php:41
static _moveContentAfterHierId(ilPageObject $a_source_page, ilPageObject $a_target_page, string $a_hid)
move content of hierarchical id >= $a_hid to other page