ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPortfolioPage.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  public const TYPE_PAGE = 1;
27  public const TYPE_BLOG = 2;
28 
29  protected int $portfolio_id;
30  protected int $type = 1;
31  protected string $title;
32  protected int $order_nr;
33 
34  public function getParentType(): string
35  {
36  return "prtf";
37  }
38 
39  public function setPortfolioId(int $a_val): void
40  {
41  $this->portfolio_id = $a_val;
42  }
43 
44  public function getPortfolioId(): int
45  {
46  return $this->portfolio_id;
47  }
48 
52  public function setType(int $a_val): void
53  {
54  $this->type = $a_val;
55  }
56 
57  public function getType(): int
58  {
59  return $this->type;
60  }
61 
62  public function setTitle(string $a_title): void
63  {
64  $this->title = $a_title;
65  }
66 
67  public function getTitle(): string
68  {
69  $lng = $this->lng;
70 
71  // because of migration of extended user profiles
72  if ($this->title === "###-") {
73  return $lng->txt("profile");
74  }
75 
76  return $this->title;
77  }
78 
79  public function setOrderNr(int $a_val): void
80  {
81  $this->order_nr = $a_val;
82  }
83 
84  public function getOrderNr(): int
85  {
86  return $this->order_nr;
87  }
88 
89  public static function lookupMaxOrderNr(
90  int $a_portfolio_id
91  ): int {
92  global $DIC;
93 
94  $ilDB = $DIC->database();
95 
96  $set = $ilDB->query("SELECT MAX(order_nr) m FROM usr_portfolio_page" .
97  " WHERE portfolio_id = " . $ilDB->quote($a_portfolio_id, "integer"));
98  $rec = $ilDB->fetchAssoc($set);
99  return (int) $rec["m"];
100  }
101 
102  protected function getPropertiesForDB(): array
103  {
104  $fields = array(
105  "portfolio_id" => array("integer", $this->portfolio_id),
106  "type" => array("integer", $this->getType()),
107  "title" => array("text", $this->getTitle()),
108  "order_nr" => array("integer", $this->getOrderNr())
109  );
110 
111  return $fields;
112  }
113 
114  public function create(bool $a_import = false): void
115  {
116  $ilDB = $this->db;
117 
118  if (!$a_import) {
119  $this->setOrderNr(self::lookupMaxOrderNr($this->portfolio_id) + 10);
120  }
121 
122  $id = $ilDB->nextId("usr_portfolio_page");
123  $this->setId($id);
124 
125  $fields = $this->getPropertiesForDB();
126  $fields["id"] = array("integer", $id);
127 
128  $ilDB->insert("usr_portfolio_page", $fields);
129 
130  if (!$a_import) {
131  parent::create($a_import);
132  // $this->saveInternalLinks($this->getDomDoc());
133  }
134  }
135 
136  public function update(
137  bool $a_validate = true,
138  bool $a_no_history = false
139  ) {
140  $ilDB = $this->db;
141 
142  $id = $this->getId();
143  if ($id) {
144  $fields = $this->getPropertiesForDB();
145  $ilDB->update(
146  "usr_portfolio_page",
147  $fields,
148  array("id" => array("integer", $id))
149  );
150 
151  return parent::update($a_validate, $a_no_history);
152  }
153  return false;
154  }
155 
156  public function read(): void
157  {
158  $ilDB = $this->db;
159 
160  $query = "SELECT * FROM usr_portfolio_page" .
161  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
162  $set = $ilDB->query($query);
163  $rec = $ilDB->fetchAssoc($set);
164 
165  $this->setPortfolioId($rec["portfolio_id"]);
166  $this->setType($rec["type"]);
167  $this->setTitle($rec["title"]);
168  $this->setOrderNr($rec["order_nr"]);
169 
170  // get co page
171  parent::read();
172  }
173 
174  public function delete(): void
175  {
176  $ilDB = $this->db;
177 
178  $id = $this->getId();
179  if ($id) {
180  // delete internal links information to this page
182 
183  // delete record of table usr_portfolio_page
184  $query = "DELETE FROM usr_portfolio_page" .
185  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
186  $ilDB->manipulate($query);
187 
188  // delete co page
189  parent::delete();
190  }
191  }
192 
193  protected static function lookupProperty(
194  int $a_id,
195  string $a_prop
196  ): string {
197  global $DIC;
198 
199  $ilDB = $DIC->database();
200 
201  $set = $ilDB->query("SELECT " . $a_prop .
202  " FROM usr_portfolio_page" .
203  " WHERE id = " . $ilDB->quote($a_id, "integer"));
204  $rec = $ilDB->fetchAssoc($set);
205  return (string) ($rec[$a_prop] ?? "");
206  }
207 
208  public static function lookupTitle(int $a_page_id): string
209  {
210  return self::lookupProperty($a_page_id, "title");
211  }
212 
213  public static function lookupType($a_page_id): int
214  {
215  return (int) self::lookupProperty($a_page_id, "type");
216  }
217 
221  public static function getAllPortfolioPages(
222  int $a_portfolio_id
223  ): array {
224  global $DIC;
225 
226  $ilDB = $DIC->database();
227  $lng = $DIC->language();
228 
229  $set = $ilDB->query("SELECT * FROM usr_portfolio_page" .
230  " WHERE portfolio_id = " . $ilDB->quote($a_portfolio_id, "integer") .
231  " ORDER BY order_nr");
232  $pages = array();
233  while ($rec = $ilDB->fetchAssoc($set)) {
234  // because of migration of extended user profiles
235  if ($rec["title"] == "###-") {
236  $rec["title"] = $lng->txt("profile");
237  }
238 
239  $pages[] = $rec;
240  }
241  return $pages;
242  }
243 
244  public static function fixOrdering(
245  int $a_portfolio_id
246  ): void {
247  global $DIC;
248 
249  $ilDB = $DIC->database();
250 
251  $pages = self::getAllPortfolioPages($a_portfolio_id);
252  $cnt = 10;
253  foreach ($pages as $p) {
254  $ilDB->manipulate(
255  "UPDATE usr_portfolio_page SET " .
256  " order_nr = " . $ilDB->quote($cnt, "integer") .
257  " WHERE id = " . $ilDB->quote($p["id"], "integer")
258  );
259  $cnt += 10;
260  }
261  }
262 
266  public static function findPortfolioForPage(int $a_page_id): int
267  {
268  return (int) self::lookupProperty($a_page_id, "portfolio_id");
269  }
270 
274  public static function getGotoForPortfolioPageTarget(
275  int $a_target,
276  bool $a_offline = false
277  ): string {
278  global $DIC;
279 
280  $pid = self::findPortfolioForPage($a_target);
281  $type = ilObject::_lookupType($pid);
282  if ($type === "prtt") {
283  $ctrl = $DIC->ctrl();
284  $ctrl->setParameterByClass("ilobjportfoliotemplategui", "user_page", $a_target);
285  $href = $ctrl->getLinkTargetByClass(array(
286  "ilRepositoryGUI",
287  "ilObjPortfolioTemplateGUI"
288  ), "preview", "", false, true);
289  } else {
290  if (!$a_offline) {
291  $href = "./goto.php?client_id=" . CLIENT_ID . "&amp;target=prtf_" . $pid . "_" . $a_target;
292  } else {
293  $href = "prtf_" . $a_target . ".html";
294  }
295  }
296  return $href;
297  }
298 
302  public static function updateInternalLinks(
303  array $a_copied_nodes,
304  ilObjPortfolioBase $a_target_obj
305  ): void {
306  $all_fixes = array();
307  $tpg = null;
308 
309  foreach ($a_copied_nodes as $original_id => $copied_id) {
310  $pid = self::findPortfolioForPage((int) $copied_id);
311 
312  //
313  // 1. Outgoing links from the copied page.
314  //
315  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
316  if ($a_target_obj->getType() === "prtf") {
317  $tpg = new ilPortfolioPage($copied_id);
318  }
319  if ($a_target_obj->getType() === "prtt") {
320  $tpg = new ilPortfolioTemplatePage($copied_id);
321  }
322  $tpg->buildDom();
323  $il = $tpg->getInternalLinks();
324  // var_dump($il);
325  $targets = array();
326  foreach ($il as $l) {
327  $targets[] = array(
328  "type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
329  "id" => ilInternalLink::_extractObjIdOfTarget($l["Target"]),
330  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"])
331  );
332  }
333  $fix = array();
334  foreach ($targets as $target) {
335  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
336  ($target["type"] == "ppage")) {
337  // first check, whether target is also within the copied set
338  if ($a_copied_nodes[$target["id"]] > 0) {
339  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
340  }
341  }
342  }
343  // var_dump($fix);
344  // outgoing links to be fixed
345  if (count($fix) > 0) {
346  $t = ilObject::_lookupType($pid);
347  if (is_array($all_fixes[$t . ":" . $copied_id] ?? false)) {
348  $all_fixes[$t . ":" . $copied_id] += $fix;
349  } else {
350  $all_fixes[$t . ":" . $copied_id] = $fix;
351  }
352  }
353  }
354  // var_dump($all_fixes);
355  foreach ($all_fixes as $pg => $fixes) {
356  $pg = explode(":", $pg);
357  $page = ilPageObjectFactory::getInstance($pg[0], $pg[1]);
358  if ($page->moveIntLinks($fixes)) {
359  $page->update(true, true);
360  }
361  }
362  }
363 
364 
365  public function renameLinksOnTitleChange(
366  array $a_title_changes
367  ): bool {
368  $this->buildDom();
369 
370  $changed = false;
371 
372  // resolve normal internal links
373  $xpc = xpath_new_context($this->dom);
374  $path = "//IntLink";
375  $res = xpath_eval($xpc, $path);
376  for ($i = 0, $iMax = count($res->nodeset); $i < $iMax; $i++) {
377  $target = $res->nodeset[$i]->get_attribute("Target");
378  $type = $res->nodeset[$i]->get_attribute("Type");
379  $obj_id = ilInternalLink::_extractObjIdOfTarget($target);
380  if (isset($a_title_changes[$obj_id]) && is_int(strpos($target, "__"))) {
381  if ($type == "PortfolioPage") {
382  if ($res->nodeset[$i]->get_content() == $a_title_changes[$obj_id]["old"]) {
383  $res->nodeset[$i]->set_content($a_title_changes[$obj_id]["new"]);
384  $changed = true;
385  }
386  }
387  }
388  }
389  unset($xpc);
390 
391  return $changed;
392  }
393 
400  public static function getPagesForBlog(
401  int $a_blog_id
402  ): array {
403  global $DIC;
404 
405  $ilDB = $DIC->database();
406 
407  $set = $ilDB->query("SELECT * FROM usr_portfolio_page" .
408  " WHERE title = " . $ilDB->quote($a_blog_id, "text") .
409  " AND type = " . $ilDB->quote(self::TYPE_BLOG, "integer"));
410  $pages = array();
411  while ($rec = $ilDB->fetchAssoc($set)) {
412  $pages[] = new ilPortfolioPage($rec["id"]);
413  }
414  return $pages;
415  }
416 }
xpath_eval(php4DOMXPath $xpath_context, string $eval_str, $contextnode=null)
static getAllPortfolioPages(int $a_portfolio_id)
Get pages of portfolio.
$res
Definition: ltiservices.php:69
static lookupProperty(int $a_id, string $a_prop)
buildDom(bool $a_force=false)
const IL_INST_ID
Definition: constants.php:40
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
update(bool $a_validate=true, bool $a_no_history=false)
static lookupType($a_page_id)
$path
Definition: ltiservices.php:32
create(bool $a_import=false)
global $DIC
Definition: feed.php:28
xpath_new_context($dom_document)
renameLinksOnTitleChange(array $a_title_changes)
static lookupMaxOrderNr(int $a_portfolio_id)
static updateInternalLinks(array $a_copied_nodes, ilObjPortfolioBase $a_target_obj)
Update internal links, after multiple pages have been copied.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
const CLIENT_ID
Definition: constants.php:41
static getPagesForBlog(int $a_blog_id)
Get portfolio pages for blog.
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 getGotoForPortfolioPageTarget(int $a_target, bool $a_offline=false)
Get goto href for portfolio page id.
$query
static lookupTitle(int $a_page_id)
ilDBInterface $db
static findPortfolioForPage(int $a_page_id)
Get portfolio id of page id.
setTitle(string $a_title)
static fixOrdering(int $a_portfolio_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
static getInstance(string $a_parent_type, int $a_id=0, int $a_old_nr=0, string $a_lang="-")
Get page object instance.
$i
Definition: metadata.php:41