ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilLMPageObject.php
Go to the documentation of this file.
1 <?php
2 
20 
34 {
35  protected bool $halt_on_error;
36  protected array $files_contained;
37  protected array $mobs_contained;
38  protected bool $contains_int_link;
40 
41  public function __construct(
42  ilObjLearningModule $a_content_obj,
43  int $a_id = 0,
44  bool $a_halt = true
45  ) {
46  parent::__construct($a_content_obj, $a_id);
47  $this->setType("pg");
48  $this->id = $a_id;
49 
50  $this->contains_int_link = false;
51  $this->mobs_contained = array();
52  $this->files_contained = array();
53  $this->halt_on_error = $a_halt;
54 
55  if ($a_id != 0) {
56  $this->read();
57  }
58  }
59 
60  public function read(): void
61  {
62  parent::read();
63  $this->page_object = new ilLMPage($this->id, 0);
64  }
65 
66  public function create(
67  bool $a_upload = false,
68  bool $a_omit_page_object_creation = false,
69  int $a_layout_id = 0
70  ): void {
71  parent::create($a_upload);
72  if ($a_omit_page_object_creation) {
73  return;
74  }
75  if (!is_object($this->page_object)) {
76  $this->page_object = new ilLMPage();
77  }
78  $this->page_object->setId($this->getId());
79  $this->page_object->setParentId($this->getLMId());
80  if ($a_layout_id == 0) {
81  $this->page_object->create(false);
82  } else {
83  $this->page_object->createWithLayoutId($a_layout_id);
84  }
85  }
86 
87  public function delete(bool $a_delete_meta_data = true): void
88  {
89  parent::delete($a_delete_meta_data);
90  $this->page_object->delete();
91  }
92 
93  // copy page
94  public function copy(
95  ilObjLearningModule $a_target_lm
96  ): ilLMPageObject {
97  // copy page
98  $lm_page = new ilLMPageObject($a_target_lm);
99  $lm_page->setTitle($this->getTitle());
100  $lm_page->setShortTitle($this->getShortTitle());
101  $lm_page->setLayout($this->getLayout());
102  $lm_page->setLMId($a_target_lm->getId());
103  $lm_page->setType($this->getType());
104  $lm_page->setDescription($this->getDescription());
105  $lm_page->setImportId("il__pg_" . $this->getId());
106  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
107 
108  // check whether export id already exists in the target lm
109  $del_exp_id = false;
110  $exp_id = ilLMPageObject::getExportId($this->getLMId(), $this->getId());
111  if (trim($exp_id) != "") {
112  if (ilLMPageObject::existsExportID($a_target_lm->getId(), $exp_id)) {
113  $del_exp_id = true;
114  }
115  }
116 
117  // copy meta data
118  $this->lom_services->derive()
119  ->fromObject($this->getLMId(), $this->getId(), $this->getType())
120  ->forObject($a_target_lm->getId(), $lm_page->getId(), $this->getType());
121 
122  // check whether export id already exists in the target lm
123  if ($del_exp_id) {
124  ilLMPageObject::saveExportId($a_target_lm->getId(), $lm_page->getId(), "");
125  } else {
127  $a_target_lm->getId(),
128  $lm_page->getId(),
129  trim($exp_id)
130  );
131  }
132 
133  // copy page content and activation
134  $page = $lm_page->getPageObject();
135  $this->page_object->copy($page->getId(), $page->getParentType(), $page->getParentId());
136  $lm_page->read(); // this gets the updated page object into lm page
137 
138  // copy translations
139  ilLMObjTranslation::copy($this->getId(), $lm_page->getId());
140 
141  return $lm_page;
142  }
143 
147  public function copyToOtherContObject(
148  ilObjLearningModule $a_cont_obj,
149  array &$a_copied_nodes
150  ): ilLMPageObject {
151  // copy page
152  $lm_page = new ilLMPageObject($a_cont_obj);
153  $lm_page->setTitle($this->getTitle());
154  $lm_page->setShortTitle($this->getShortTitle());
155  $lm_page->setLMId($a_cont_obj->getId());
156  $lm_page->setImportId("il__pg_" . $this->getId());
157  $lm_page->setType($this->getType());
158  $lm_page->setDescription($this->getDescription());
159  $lm_page->create(true); // setting "upload" flag to true prevents creating of meta data
160  $a_copied_nodes[$this->getId()] = $lm_page->getId();
161 
162  // copy meta data
163  $this->lom_services->derive()
164  ->fromObject($this->getLMId(), $this->getId(), $this->getType())
165  ->forObject($a_cont_obj->getId(), $lm_page->getId(), $this->getType());
166 
167  // copy page content
168  $page = $lm_page->getPageObject();
169  $page->setXMLContent($this->page_object->getXMLContent());
170  $page->buildDom();
171  $page->update();
172 
173  return $lm_page;
174  }
175 
176 
180  public function assignPageObject(ilLMPage $a_page_obj): void
181  {
182  $this->page_object = $a_page_obj;
183  }
184 
185 
189  public function getPageObject(): ilLMPage
190  {
191  return $this->page_object;
192  }
193 
194  public function setId(int $a_id): void
195  {
196  $this->id = $a_id;
197  }
198 
199  public function getId(): int
200  {
201  return $this->id;
202  }
203 
204  public static function getPageList(int $lm_id): array
205  {
206  return ilLMObject::getObjectList($lm_id, "pg");
207  }
208 
212  public static function getPagesWithLinksList(
213  int $a_lm_id,
214  string $a_par_type
215  ): array {
216  $pages = ilLMPageObject::getPageList($a_lm_id);
217  $linked_pages = ilLMPage::getPagesWithLinks($a_par_type, $a_lm_id);
218  $result = array();
219  foreach ($pages as $page) {
220  if (isset($linked_pages[$page["obj_id"]])) {
221  $result[] = $page;
222  }
223  }
224  return $result;
225  }
226 
232  public static function _getPresentationTitle(
233  int $a_pg_id,
234  string $a_mode = self::CHAPTER_TITLE,
235  bool $a_include_numbers = false,
236  bool $a_time_scheduled_activation = false,
237  bool $a_force_content = false,
238  int $a_lm_id = 0,
239  string $a_lang = "-",
240  bool $a_include_short = false
241  ): string {
242  global $DIC;
243  $ilDB = $DIC->database();
244 
245  if ($a_mode == self::NO_HEADER && !$a_force_content) {
246  return "";
247  }
248 
249  $cur_cnt = 0;
250 
251  if ($a_lm_id == 0) {
252  $a_lm_id = ilLMObject::_lookupContObjID($a_pg_id);
253  }
254 
255  if ($a_lm_id == 0) {
256  return "";
257  }
258 
259  // this is optimized when ilLMObject::preloadDataByLM is invoked (e.g. done in ilLMExplorerGUI)
260  $title = "";
261  if ($a_include_short) {
262  $title = trim(ilLMObject::_lookupShortTitle($a_pg_id));
263  }
264  if ($title == "") {
265  $title = ilLMObject::_lookupTitle($a_pg_id);
266  }
267 
268  $ot = (new TranslationsRepository($ilDB))->getFor($a_lm_id);
269 
270  if ($a_lang != "-" && $ot->getContentTranslationActivated()) {
271  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $a_lang);
272  $trans_title = "";
273  if ($a_include_short) {
274  $trans_title = trim($lmobjtrans->getShortTitle());
275  }
276  if ($trans_title == "") {
277  $trans_title = $lmobjtrans->getTitle();
278  }
279  if ($trans_title == "") {
280  $lmobjtrans = new ilLMObjTranslation($a_pg_id, $ot->getDefaultLanguage());
281  $trans_title = $lmobjtrans->getTitle();
282  }
283  if ($trans_title != "") {
284  $title = $trans_title;
285  }
286  }
287 
288  if ($a_mode == self::PAGE_TITLE) {
289  return $title;
290  }
291 
292  $tree = ilLMTree::getInstance($a_lm_id);
293 
294  if ($tree->isInTree($a_pg_id)) {
295  $pred_node = $tree->fetchPredecessorNode($a_pg_id, "st");
296  $childs = $tree->getChildsByType($pred_node["obj_id"], "pg");
297  $cnt_str = "";
298  if (count($childs) > 1) {
299  $cnt = 0;
300  foreach ($childs as $child) {
302  $child["obj_id"],
303  ilObject::_lookupType($a_lm_id),
304  $a_time_scheduled_activation
305  );
306 
307  if (!$active) {
308  $act_data = ilLMPage::_lookupActivationData((int) $child["obj_id"], ilObject::_lookupType($a_lm_id));
309  if ($act_data["show_activation_info"] &&
310  (ilUtil::now() < $act_data["activation_start"])) {
311  $active = true;
312  }
313  }
314 
315  if ($child["type"] != "pg" || $active) {
316  $cnt++;
317  }
318  if ($child["obj_id"] == $a_pg_id) {
319  $cur_cnt = $cnt;
320  }
321  }
322  if ($cnt > 1) {
323  $cnt_str = " (" . $cur_cnt . "/" . $cnt . ")";
324  }
325  }
327  $pred_node["obj_id"],
328  self::CHAPTER_TITLE,
329  $a_include_numbers,
330  false,
331  false,
332  0,
333  $a_lang,
334  true
335  ) . $cnt_str;
336  } else {
337  return $title;
338  }
339  }
340 
344  public function exportXML(
345  ilXmlWriter $a_xml_writer,
346  string $a_mode = "normal",
347  int $a_inst = 0
348  ): void {
349  $attrs = array();
350  $a_xml_writer->xmlStartTag("PageObject", $attrs);
351 
352  switch ($a_mode) {
353  case "normal":
354  // MetaData
355  $this->exportXMLMetaData($a_xml_writer);
356 
357  // PageContent
358  $this->exportXMLPageContent($a_xml_writer, $a_inst);
359  break;
360 
361  case "alias":
362  $attrs = array();
363  $attrs["OriginId"] = "il_" . $a_inst .
364  "_pg_" . $this->getId();
365  $a_xml_writer->xmlElement("PageAlias", $attrs);
366  break;
367  }
368  $a_xml_writer->xmlEndTag("PageObject");
369  }
370 
374  public static function _exportXMLAlias(
375  ilXmlWriter $a_xml_writer,
376  int $a_id,
377  int $a_inst = 0
378  ): void {
379  $attrs = array();
380  $a_xml_writer->xmlStartTag("PageObject", $attrs);
381 
382  $attrs = array();
383  $attrs["OriginId"] = "il_" . $a_inst .
384  "_pg_" . $a_id;
385  $a_xml_writer->xmlElement("PageAlias", $attrs);
386 
387  $a_xml_writer->xmlEndTag("PageObject");
388  }
389 
390  public function exportXMLMetaData(
391  ilXmlWriter $a_xml_writer
392  ): void {
393  /*
394  * As far as I can tell, this is unused.
395  *
396  * I traced usages of this method up to ilObjContentObjectGUI::export and
397  * ilObjMediaPoolGUI::export (both via ilObjContentObject::exportXML), which have
398  * both been made redundant by the usual export mechanisms.
399  */
400  /*$md2xml = new ilMD2XML($this->getLMId(), $this->getId(), $this->getType());
401  $md2xml->setExportMode(true);
402  $md2xml->startExport();
403  $a_xml_writer->appendXML($md2xml->getXML());*/
404  }
405 
406  public function modifyExportIdentifier(
407  string $a_tag,
408  string $a_param,
409  string $a_value
410  ): string {
411  if ($a_tag == "Identifier" && $a_param == "Entry") {
412  $a_value = "il_" . IL_INST_ID . "_pg_" . $this->getId();
413  }
414 
415  return $a_value;
416  }
417 
418  public function exportXMLPageContent(
419  ilXmlWriter $a_xml_writer,
420  int $a_inst = 0
421  ): void {
422  $this->page_object->buildDom();
423  $this->page_object->insertInstIntoIDs($a_inst);
424  $this->mobs_contained = $this->page_object->collectMediaObjects(false);
425  $this->files_contained = ilPCFileList::collectFileItems($this->page_object, $this->page_object->getDomDoc());
426  $xml = $this->page_object->getXMLFromDom(false, false, false, "", true);
427  $xml = str_replace("&", "&amp;", $xml);
428  $a_xml_writer->appendXML($xml);
429 
430  $this->page_object->freeDom();
431  }
432 
437  public function getQuestionIds(): array
438  {
440  $this->content_object->getType(),
441  $this->getId()
442  );
443  }
444 
449  public function getMediaObjectIds(): array
450  {
451  return $this->mobs_contained;
452  }
453 
458  public function getFileItemIds(): array
459  {
460  return $this->files_contained;
461  }
462 
463 
467  public static function queryQuestionsOfLearningModule(
468  int $a_lm_id,
469  string $a_order_field,
470  string $a_order_dir,
471  int $a_offset,
472  int $a_limit
473  ): array {
474  global $DIC;
475 
476  $ilDB = $DIC->database();
477 
478  // count query
479  $count_query = "SELECT count(pq.question_id) cnt ";
480 
481  // basic query
482  $query = "SELECT pq.page_id, pq.question_id ";
483 
484  $from = " FROM page_question pq JOIN lm_tree t ON (t.lm_id = " . $ilDB->quote($a_lm_id, "integer") .
485  " AND pq.page_id = t.child and pq.page_parent_type = " . $ilDB->quote("lm", "text") . ") " .
486  " WHERE t.lm_id = " . $ilDB->quote($a_lm_id, "integer");
487  $count_query .= $from;
488  $query .= $from;
489 
490  // count query
491  $set = $ilDB->query($count_query);
492  $cnt = 0;
493  if ($rec = $ilDB->fetchAssoc($set)) {
494  $cnt = $rec["cnt"];
495  }
496 
497  $offset = $a_offset;
498  $limit = $a_limit;
499  if ($a_limit > 0) {
500  $ilDB->setLimit($limit, $offset);
501  }
502 
503  // set query
504  $set = $ilDB->query($query);
505  $result = array();
506  while ($rec = $ilDB->fetchAssoc($set)) {
507  $result[] = $rec;
508  }
509  return array("cnt" => $cnt, "set" => $result);
510  }
511 
515  public static function insertPagesFromTemplate(
516  int $lm_id,
517  int $num,
518  int $node_id,
519  bool $first_child,
520  int $layout_id,
521  string $title = ""
522  ): array {
523  global $DIC;
524 
525  $lng = $DIC->language();
526 
527  if ($title == "") {
528  $title = $lng->txt("cont_new_page");
529  }
530  $lm_tree = new ilLMTree($lm_id);
531  $lm = new ilObjLearningModule($lm_id, false);
532  if (!$first_child) { // insert after node id
533  $parent_id = $lm_tree->getParentId($node_id);
534  $target = $node_id;
535  } else { // insert as first child
536  $parent_id = $node_id;
537  $target = ilTree::POS_FIRST_NODE;
538  }
539 
540  $page_ids = array();
541  for ($i = 1; $i <= $num; $i++) {
542  $page = new ilLMPageObject($lm);
543  $page->setTitle($title);
544  $page->setLMId($lm->getId());
545  $page->create(false, false, $layout_id);
546  ilLMObject::putInTree($page, $parent_id, $target);
547  $page_ids[] = $page->getId();
548  }
549  $page_ids = array_reverse($page_ids);
550  return $page_ids;
551  }
552 }
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)
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)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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...
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 insertPagesFromTemplate(int $lm_id, int $num, int $node_id, bool $first_child, int $layout_id, string $title="")
Insert (multiple) pages templates at node.
global $DIC
Definition: shib_login.php:26
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)
global $lng
Definition: privfeed.php:31
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...
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...