ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 afterConstructor(): void
35  {
36  global $DIC;
37  $this->dom_util = $DIC->copage()->internal()->domain()->domUtil();
38  }
39 
40  public function getParentType(): string
41  {
42  return "prtf";
43  }
44 
45  public function setPortfolioId(int $a_val): void
46  {
47  $this->portfolio_id = $a_val;
48  }
49 
50  public function getPortfolioId(): int
51  {
52  return $this->portfolio_id;
53  }
54 
58  public function setType(int $a_val): void
59  {
60  $this->type = $a_val;
61  }
62 
63  public function getType(): int
64  {
65  return $this->type;
66  }
67 
68  public function setTitle(string $a_title): void
69  {
70  $this->title = $a_title;
71  }
72 
73  public function getTitle(): string
74  {
75  $lng = $this->lng;
76 
77  // because of migration of extended user profiles
78  if ($this->title === "###-") {
79  return $lng->txt("profile");
80  }
81 
82  return $this->title;
83  }
84 
85  public function setOrderNr(int $a_val): void
86  {
87  $this->order_nr = $a_val;
88  }
89 
90  public function getOrderNr(): int
91  {
92  return $this->order_nr;
93  }
94 
95  public static function lookupMaxOrderNr(
96  int $a_portfolio_id
97  ): int {
98  global $DIC;
99 
100  $ilDB = $DIC->database();
101 
102  $set = $ilDB->query("SELECT MAX(order_nr) m FROM usr_portfolio_page" .
103  " WHERE portfolio_id = " . $ilDB->quote($a_portfolio_id, "integer"));
104  $rec = $ilDB->fetchAssoc($set);
105  return (int) $rec["m"];
106  }
107 
108  protected function getPropertiesForDB(): array
109  {
110  $fields = array(
111  "portfolio_id" => array("integer", $this->portfolio_id),
112  "type" => array("integer", $this->getType()),
113  "title" => array("text", $this->getTitle()),
114  "order_nr" => array("integer", $this->getOrderNr())
115  );
116 
117  return $fields;
118  }
119 
120  public function create(bool $a_import = false): void
121  {
122  $ilDB = $this->db;
123 
124  if (!$a_import) {
125  $this->setOrderNr(self::lookupMaxOrderNr($this->portfolio_id) + 10);
126  }
127 
128  $id = $ilDB->nextId("usr_portfolio_page");
129  $this->setId($id);
130 
131  $fields = $this->getPropertiesForDB();
132  $fields["id"] = array("integer", $id);
133 
134  $ilDB->insert("usr_portfolio_page", $fields);
135 
136  if (!$a_import) {
137  parent::create($a_import);
138  // $this->saveInternalLinks($this->getDomDoc());
139  }
140  }
141 
142  public function update(
143  bool $a_validate = true,
144  bool $a_no_history = false
145  ) {
146  $ilDB = $this->db;
147 
148  $id = $this->getId();
149  if ($id) {
150  $fields = $this->getPropertiesForDB();
151  $ilDB->update(
152  "usr_portfolio_page",
153  $fields,
154  array("id" => array("integer", $id))
155  );
156 
157  return parent::update($a_validate, $a_no_history);
158  }
159  return false;
160  }
161 
162  public function read(): void
163  {
164  $ilDB = $this->db;
165 
166  $query = "SELECT * FROM usr_portfolio_page" .
167  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
168  $set = $ilDB->query($query);
169  $rec = $ilDB->fetchAssoc($set);
170 
171  $this->setPortfolioId($rec["portfolio_id"]);
172  $this->setType($rec["type"]);
173  $this->setTitle($rec["title"]);
174  $this->setOrderNr($rec["order_nr"]);
175 
176  // get co page
177  parent::read();
178  }
179 
180  public function delete(): void
181  {
182  $ilDB = $this->db;
183 
184  $id = $this->getId();
185  if ($id) {
186  // delete internal links information to this page
188 
189  // delete record of table usr_portfolio_page
190  $query = "DELETE FROM usr_portfolio_page" .
191  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
192  $ilDB->manipulate($query);
193 
194  // delete co page
195  parent::delete();
196  }
197  }
198 
199  protected static function lookupProperty(
200  int $a_id,
201  string $a_prop
202  ): string {
203  global $DIC;
204 
205  $ilDB = $DIC->database();
206 
207  $set = $ilDB->query("SELECT " . $a_prop .
208  " FROM usr_portfolio_page" .
209  " WHERE id = " . $ilDB->quote($a_id, "integer"));
210  $rec = $ilDB->fetchAssoc($set);
211  return (string) ($rec[$a_prop] ?? "");
212  }
213 
214  public static function lookupTitle(int $a_page_id): string
215  {
216  return self::lookupProperty($a_page_id, "title");
217  }
218 
219  public static function lookupType($a_page_id): int
220  {
221  return (int) self::lookupProperty($a_page_id, "type");
222  }
223 
227  public static function getAllPortfolioPages(
228  int $a_portfolio_id
229  ): array {
230  global $DIC;
231 
232  $ilDB = $DIC->database();
233  $lng = $DIC->language();
234 
235  $set = $ilDB->query("SELECT * FROM usr_portfolio_page" .
236  " WHERE portfolio_id = " . $ilDB->quote($a_portfolio_id, "integer") .
237  " ORDER BY order_nr");
238  $pages = array();
239  while ($rec = $ilDB->fetchAssoc($set)) {
240  // because of migration of extended user profiles
241  if ($rec["title"] == "###-") {
242  $rec["title"] = $lng->txt("profile");
243  }
244 
245  $pages[] = $rec;
246  }
247  return $pages;
248  }
249 
250  public static function fixOrdering(
251  int $a_portfolio_id
252  ): void {
253  global $DIC;
254 
255  $ilDB = $DIC->database();
256 
257  $pages = self::getAllPortfolioPages($a_portfolio_id);
258  $cnt = 10;
259  foreach ($pages as $p) {
260  $ilDB->manipulate(
261  "UPDATE usr_portfolio_page SET " .
262  " order_nr = " . $ilDB->quote($cnt, "integer") .
263  " WHERE id = " . $ilDB->quote($p["id"], "integer")
264  );
265  $cnt += 10;
266  }
267  }
268 
272  public static function findPortfolioForPage(int $a_page_id): int
273  {
274  return (int) self::lookupProperty($a_page_id, "portfolio_id");
275  }
276 
280  public static function getGotoForPortfolioPageTarget(
281  int $a_target,
282  bool $a_offline = false
283  ): string {
284  global $DIC;
285 
286  $pid = self::findPortfolioForPage($a_target);
287  $type = ilObject::_lookupType($pid);
288  if ($type === "prtt") {
289  $ctrl = $DIC->ctrl();
290  $ctrl->setParameterByClass("ilobjportfoliotemplategui", "user_page", $a_target);
291  $href = $ctrl->getLinkTargetByClass(array(
292  "ilRepositoryGUI",
293  "ilObjPortfolioTemplateGUI"
294  ), "preview", "", false, true);
295  } else {
296  if (!$a_offline) {
297  $href = "./goto.php?client_id=" . CLIENT_ID . "&amp;target=prtf_" . $pid . "_" . $a_target;
298  } else {
299  $href = "prtf_" . $a_target . ".html";
300  }
301  }
302  return $href;
303  }
304 
308  public static function updateInternalLinks(
309  array $a_copied_nodes,
310  ilObjPortfolioBase $a_target_obj
311  ): void {
312  $all_fixes = array();
313  $tpg = null;
314 
315  foreach ($a_copied_nodes as $original_id => $copied_id) {
316  $pid = self::findPortfolioForPage((int) $copied_id);
317 
318  //
319  // 1. Outgoing links from the copied page.
320  //
321  //$targets = ilInternalLink::_getTargetsOfSource($a_parent_type.":pg", $copied_id);
322  if ($a_target_obj->getType() === "prtf") {
323  $tpg = new ilPortfolioPage($copied_id);
324  }
325  if ($a_target_obj->getType() === "prtt") {
326  $tpg = new ilPortfolioTemplatePage($copied_id);
327  }
328  $tpg->buildDom();
329  $il = $tpg->getInternalLinks();
330  // var_dump($il);
331  $targets = array();
332  foreach ($il as $l) {
333  $targets[] = array(
334  "type" => ilInternalLink::_extractTypeOfTarget($l["Target"]),
335  "id" => ilInternalLink::_extractObjIdOfTarget($l["Target"]),
336  "inst" => (int) ilInternalLink::_extractInstOfTarget($l["Target"])
337  );
338  }
339  $fix = array();
340  foreach ($targets as $target) {
341  if (($target["inst"] == 0 || $target["inst"] = IL_INST_ID) &&
342  ($target["type"] == "ppage")) {
343  // first check, whether target is also within the copied set
344  if ($a_copied_nodes[$target["id"]] > 0) {
345  $fix[$target["id"]] = $a_copied_nodes[$target["id"]];
346  }
347  }
348  }
349  // var_dump($fix);
350  // outgoing links to be fixed
351  if (count($fix) > 0) {
352  $t = ilObject::_lookupType($pid);
353  if (is_array($all_fixes[$t . ":" . $copied_id] ?? false)) {
354  $all_fixes[$t . ":" . $copied_id] += $fix;
355  } else {
356  $all_fixes[$t . ":" . $copied_id] = $fix;
357  }
358  }
359  }
360  // var_dump($all_fixes);
361  foreach ($all_fixes as $pg => $fixes) {
362  $pg = explode(":", $pg);
363  $page = ilPageObjectFactory::getInstance($pg[0], $pg[1]);
364  if ($page->moveIntLinks($fixes)) {
365  $page->update(true, true);
366  }
367  }
368  }
369 
370 
371  public function renameLinksOnTitleChange(
372  array $a_title_changes
373  ): bool {
374  $this->buildDom();
375 
376  $changed = false;
377 
378  // resolve normal internal links
379  $path = "//IntLink";
380  $nodes = $this->dom_util->path($this->getDomDoc(), $path);
381  foreach ($nodes as $node) {
382  $target = $node->getAttribute("Target");
383  $type = $node->getAttribute("Type");
384  $obj_id = ilInternalLink::_extractObjIdOfTarget($target);
385  if (isset($a_title_changes[$obj_id]) && is_int(strpos($target, "__"))) {
386  if ($type == "PortfolioPage") {
387  if ($this->dom_util->getContent($node) == $a_title_changes[$obj_id]["old"]) {
388  $this->dom_util->setContent($node, $a_title_changes[$obj_id]["new"]);
389  $changed = true;
390  }
391  }
392  }
393  }
394 
395  return $changed;
396  }
397 
404  public static function getPagesForBlog(
405  int $a_blog_id
406  ): array {
407  global $DIC;
408 
409  $ilDB = $DIC->database();
410 
411  $set = $ilDB->query("SELECT * FROM usr_portfolio_page" .
412  " WHERE title = " . $ilDB->quote($a_blog_id, "text") .
413  " AND type = " . $ilDB->quote(self::TYPE_BLOG, "integer"));
414  $pages = array();
415  while ($rec = $ilDB->fetchAssoc($set)) {
416  $pages[] = new ilPortfolioPage($rec["id"]);
417  }
418  return $pages;
419  }
420 }
static getAllPortfolioPages(int $a_portfolio_id)
Get pages of portfolio.
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)
getDomDoc()
Get dom doc (DOMDocument)
static lookupType($a_page_id)
$path
Definition: ltiservices.php:32
create(bool $a_import=false)
global $DIC
Definition: feed.php:28
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.
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.