ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 
172 
176  public function assignPageObject(ilLMPage $a_page_obj): void
177  {
178  $this->page_object = $a_page_obj;
179  }
180 
181 
185  public function getPageObject(): ilLMPage
186  {
187  return $this->page_object;
188  }
189 
190  public function setId(int $a_id): void
191  {
192  $this->id = $a_id;
193  }
194 
195  public function getId(): int
196  {
197  return $this->id;
198  }
199 
200  public static function getPageList(int $lm_id): array
201  {
202  return ilLMObject::getObjectList($lm_id, "pg");
203  }
204 
208  public static function getPagesWithLinksList(
209  int $a_lm_id,
210  string $a_par_type
211  ): array {
212  $pages = ilLMPageObject::getPageList($a_lm_id);
213  $linked_pages = ilLMPage::getPagesWithLinks($a_par_type, $a_lm_id);
214  $result = array();
215  foreach ($pages as $page) {
216  if (isset($linked_pages[$page["obj_id"]])) {
217  $result[] = $page;
218  }
219  }
220  return $result;
221  }
222 
228  public static function _getPresentationTitle(
229  int $a_pg_id,
230  string $a_mode = self::CHAPTER_TITLE,
231  bool $a_include_numbers = false,
232  bool $a_time_scheduled_activation = false,
233  bool $a_force_content = false,
234  int $a_lm_id = 0,
235  string $a_lang = "-",
236  bool $a_include_short = false
237  ): string {
238  if ($a_mode == self::NO_HEADER && !$a_force_content) {
239  return "";
240  }
241 
242  $cur_cnt = 0;
243 
244  if ($a_lm_id == 0) {
245  $a_lm_id = ilLMObject::_lookupContObjID($a_pg_id);
246  }
247 
248  if ($a_lm_id == 0) {
249  return "";
250  }
251 
252  // this is optimized when ilLMObject::preloadDataByLM is invoked (e.g. done in ilLMExplorerGUI)
253  $title = "";
254  if ($a_include_short) {
255  $title = trim(ilLMObject::_lookupShortTitle($a_pg_id));
256  }
257  if ($title == "") {
258  $title = ilLMObject::_lookupTitle($a_pg_id);
259  }
260 
261  // this is also optimized since ilObjectTranslation re-uses instances for one lm
262  $ot = ilObjectTranslation::getInstance($a_lm_id);
263  $languages = $ot->getLanguages();
264 
265  if ($a_lang != "-" && $ot->getContentActivated()) {
266  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $a_lang);
267  $trans_title = "";
268  if ($a_include_short) {
269  $trans_title = trim($lmobjtrans->getShortTitle());
270  }
271  if ($trans_title == "") {
272  $trans_title = $lmobjtrans->getTitle();
273  }
274  if ($trans_title == "") {
275  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $ot->getFallbackLanguage());
276  $trans_title = $lmobjtrans->getTitle();
277  }
278  if ($trans_title != "") {
279  $title = $trans_title;
280  }
281  }
282 
283  if ($a_mode == self::PAGE_TITLE) {
284  return $title;
285  }
286 
287  $tree = ilLMTree::getInstance($a_lm_id);
288 
289  if ($tree->isInTree($a_pg_id)) {
290  $pred_node = $tree->fetchPredecessorNode($a_pg_id, "st");
291  $childs = $tree->getChildsByType($pred_node["obj_id"], "pg");
292  $cnt_str = "";
293  if (count($childs) > 1) {
294  $cnt = 0;
295  foreach ($childs as $child) {
297  $child["obj_id"],
298  ilObject::_lookupType($a_lm_id),
299  $a_time_scheduled_activation
300  );
301 
302  if (!$active) {
303  $act_data = ilLMPage::_lookupActivationData((int) $child["obj_id"], ilObject::_lookupType($a_lm_id));
304  if ($act_data["show_activation_info"] &&
305  (ilUtil::now() < $act_data["activation_start"])) {
306  $active = true;
307  }
308  }
309 
310  if ($child["type"] != "pg" || $active) {
311  $cnt++;
312  }
313  if ($child["obj_id"] == $a_pg_id) {
314  $cur_cnt = $cnt;
315  }
316  }
317  if ($cnt > 1) {
318  $cnt_str = " (" . $cur_cnt . "/" . $cnt . ")";
319  }
320  }
322  $pred_node["obj_id"],
323  self::CHAPTER_TITLE,
324  $a_include_numbers,
325  false,
326  false,
327  0,
328  $a_lang,
329  true
330  ) . $cnt_str;
331  } else {
332  return $title;
333  }
334  }
335 
339  public function exportXML(
340  ilXmlWriter $a_xml_writer,
341  string $a_mode = "normal",
342  int $a_inst = 0
343  ): void {
344  $attrs = array();
345  $a_xml_writer->xmlStartTag("PageObject", $attrs);
346 
347  switch ($a_mode) {
348  case "normal":
349  // MetaData
350  $this->exportXMLMetaData($a_xml_writer);
351 
352  // PageContent
353  $this->exportXMLPageContent($a_xml_writer, $a_inst);
354  break;
355 
356  case "alias":
357  $attrs = array();
358  $attrs["OriginId"] = "il_" . $a_inst .
359  "_pg_" . $this->getId();
360  $a_xml_writer->xmlElement("PageAlias", $attrs);
361  break;
362  }
363  $a_xml_writer->xmlEndTag("PageObject");
364  }
365 
369  public static function _exportXMLAlias(
370  ilXmlWriter $a_xml_writer,
371  int $a_id,
372  int $a_inst = 0
373  ): void {
374  $attrs = array();
375  $a_xml_writer->xmlStartTag("PageObject", $attrs);
376 
377  $attrs = array();
378  $attrs["OriginId"] = "il_" . $a_inst .
379  "_pg_" . $a_id;
380  $a_xml_writer->xmlElement("PageAlias", $attrs);
381 
382  $a_xml_writer->xmlEndTag("PageObject");
383  }
384 
385  public function exportXMLMetaData(
386  ilXmlWriter $a_xml_writer
387  ): void {
388  $md2xml = new ilMD2XML($this->getLMId(), $this->getId(), $this->getType());
389  $md2xml->setExportMode(true);
390  $md2xml->startExport();
391  $a_xml_writer->appendXML($md2xml->getXML());
392  }
393 
394  public function modifyExportIdentifier(
395  string $a_tag,
396  string $a_param,
397  string $a_value
398  ): string {
399  if ($a_tag == "Identifier" && $a_param == "Entry") {
400  $a_value = "il_" . IL_INST_ID . "_pg_" . $this->getId();
401  }
402 
403  return $a_value;
404  }
405 
406  public function exportXMLPageContent(
407  ilXmlWriter $a_xml_writer,
408  int $a_inst = 0
409  ): void {
410  $this->page_object->buildDom();
411  $this->page_object->insertInstIntoIDs($a_inst);
412  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
413  $this->files_contained = ilPCFileList::collectFileItems($this->page_object, $this->page_object->getDomDoc());
414  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
415  $xml = str_replace("&", "&amp;", $xml);
416  $a_xml_writer->appendXML($xml);
417 
418  $this->page_object->freeDom();
419  }
420 
425  public function getQuestionIds(): array
426  {
428  $this->content_object->getType(),
429  $this->getId()
430  );
431  }
432 
437  public function getMediaObjectIds(): array
438  {
439  return $this->mobs_contained;
440  }
441 
446  public function getFileItemIds(): array
447  {
448  return $this->files_contained;
449  }
450 
451 
455  public static function queryQuestionsOfLearningModule(
456  int $a_lm_id,
457  string $a_order_field,
458  string $a_order_dir,
459  int $a_offset,
460  int $a_limit
461  ): array {
462  global $DIC;
463 
464  $ilDB = $DIC->database();
465 
466  // count query
467  $count_query = "SELECT count(pq.question_id) cnt ";
468 
469  // basic query
470  $query = "SELECT pq.page_id, pq.question_id ";
471 
472  $from = " FROM page_question pq JOIN lm_tree t ON (t.lm_id = " . $ilDB->quote($a_lm_id, "integer") .
473  " AND pq.page_id = t.child and pq.page_parent_type = " . $ilDB->quote("lm", "text") . ") " .
474  " WHERE t.lm_id = " . $ilDB->quote($a_lm_id, "integer");
475  $count_query .= $from;
476  $query .= $from;
477 
478  // count query
479  $set = $ilDB->query($count_query);
480  $cnt = 0;
481  if ($rec = $ilDB->fetchAssoc($set)) {
482  $cnt = $rec["cnt"];
483  }
484 
485  $offset = $a_offset;
486  $limit = $a_limit;
487  if ($a_limit > 0) {
488  $ilDB->setLimit($limit, $offset);
489  }
490 
491  // set query
492  $set = $ilDB->query($query);
493  $result = array();
494  while ($rec = $ilDB->fetchAssoc($set)) {
495  $result[] = $rec;
496  }
497  return array("cnt" => $cnt, "set" => $result);
498  }
499 
503  public static function insertPagesFromTemplate(
504  int $lm_id,
505  int $num,
506  int $node_id,
507  bool $first_child,
508  int $layout_id,
509  string $title = ""
510  ): array {
511  global $DIC;
512 
513  $lng = $DIC->language();
514 
515  if ($title == "") {
516  $title = $lng->txt("cont_new_page");
517  }
518  $lm_tree = new ilLMTree($lm_id);
519  $lm = new ilObjLearningModule($lm_id, false);
520  if (!$first_child) { // insert after node id
521  $parent_id = $lm_tree->getParentId($node_id);
522  $target = $node_id;
523  } else { // insert as first child
524  $parent_id = $node_id;
525  $target = ilTree::POS_FIRST_NODE;
526  }
527 
528  $page_ids = array();
529  for ($i = 1; $i <= $num; $i++) {
530  $page = new ilLMPageObject($lm);
531  $page->setTitle($title);
532  $page->setLMId($lm->getId());
533  $page->create(false, false, $layout_id);
534  ilLMObject::putInTree($page, $parent_id, $target);
535  $page_ids[] = $page->getId();
536  }
537  $page_ids = array_reverse($page_ids);
538  return $page_ids;
539  }
540 }
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")
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.
__construct(VocabulariesInterface $vocabularies)
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...
$lng
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.
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)
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 _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...