ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjPortfolioBase.php
Go to the documentation of this file.
1 <?php
2 
24 abstract class ilObjPortfolioBase extends ilObject2
25 {
26  protected \ILIAS\Notes\Service $notes;
27  protected ilSetting $setting;
28  protected bool $online = false;
29  protected bool $comments = false;
30  protected string $bg_color = "";
31  protected string $font_color = "";
32  protected string $img = "";
33  protected string $ppic = "";
34  protected bool $style = false;
35 
36  public function __construct(
37  int $a_id = 0,
38  bool $a_reference = true
39  ) {
40  global $DIC;
41 
42  $this->notes = $DIC->notes();
43 
44  parent::__construct($a_id, $a_reference);
45 
46  $this->setting = $DIC->settings();
47 
48  $this->db = $DIC->database();
49  }
50 
51 
52  //
53  // PROPERTIES
54  //
55 
56  public function setOnline(bool $a_value): void
57  {
58  $this->online = $a_value;
59  }
60 
61  public function isOnline(): bool
62  {
63  return $this->online;
64  }
65 
66  public static function lookupOnline(int $a_id): bool
67  {
68  global $DIC;
69 
70  $ilDB = $DIC->database();
71 
72  $set = $ilDB->query("SELECT is_online" .
73  " FROM usr_portfolio" .
74  " WHERE id = " . $ilDB->quote($a_id, "integer"));
75  $row = $ilDB->fetchAssoc($set);
76  return (bool) $row["is_online"];
77  }
78 
79  public function setPublicComments(bool $a_value): void
80  {
81  $this->comments = $a_value;
82  }
83 
84  public function hasPublicComments(): bool
85  {
86  return $this->comments;
87  }
88 
89  public function hasProfilePicture(): bool
90  {
91  return $this->ppic;
92  }
93 
94  public function setProfilePicture(bool $a_status): void
95  {
96  $this->ppic = $a_status;
97  }
98 
99  public function getBackgroundColor(): string
100  {
101  if (!$this->bg_color) {
102  $this->bg_color = "ffffff";
103  }
104  return $this->bg_color;
105  }
106 
110  public function setBackgroundColor(string $a_value): void
111  {
112  $this->bg_color = $a_value;
113  }
114 
115  public function getFontColor(): string
116  {
117  if (!$this->font_color) {
118  $this->font_color = "505050";
119  }
120  return $this->font_color;
121  }
122 
123  public function setFontColor(string $a_value): void
124  {
125  $this->font_color = $a_value;
126  }
127 
128  //
129  // CRUD
130  //
131 
132  protected function doRead(): void
133  {
134  $ilDB = $this->db;
135 
136  $set = $ilDB->query("SELECT * FROM usr_portfolio" .
137  " WHERE id = " . $ilDB->quote($this->id, "integer"));
138  $row = $ilDB->fetchAssoc($set);
139 
140  $this->setOnline((bool) $row["is_online"]);
141  $this->setProfilePicture((bool) $row["ppic"]);
142  $this->setBackgroundColor((string) $row["bg_color"]);
143  $this->setFontColor((string) $row["font_color"]);
144 
145  // #14661
146  $this->setPublicComments($this->notes->domain()->commentsActive($this->id));
147 
148  $this->doReadCustom($row);
149  }
150 
154  protected function doReadCustom(array $a_row): void
155  {
156  }
157 
158  protected function doCreate(bool $clone_mode = false): void
159  {
160  $ilDB = $this->db;
161 
162  $ilDB->manipulate("INSERT INTO usr_portfolio (id,is_online)" .
163  " VALUES (" . $ilDB->quote($this->id, "integer") . "," .
164  $ilDB->quote(0, "integer") . ")");
165  }
166 
167  protected function doUpdate(): void
168  {
169  $ilDB = $this->db;
170 
171  $fields = array(
172  "is_online" => array("integer", $this->isOnline()),
173  "ppic" => array("integer", $this->hasProfilePicture()),
174  "bg_color" => array("text", $this->getBackgroundColor()),
175  "font_color" => array("text", $this->getFontColor())
176  );
177  $this->doUpdateCustom($fields);
178 
179  // #14661
180  $this->notes->domain()->activateComments($this->id, $this->hasPublicComments());
181 
182  $ilDB->update(
183  "usr_portfolio",
184  $fields,
185  array("id" => array("integer", $this->id))
186  );
187  }
188 
192  protected function doUpdateCustom(array &$a_fields): void
193  {
194  }
195 
196  protected function doDelete(): void
197  {
198  $ilDB = $this->db;
199 
200  $this->deleteAllPages();
201 
202  $ilDB->manipulate("DELETE FROM usr_portfolio" .
203  " WHERE id = " . $ilDB->quote($this->id, "integer"));
204  }
205 
206  abstract protected function deleteAllPages(): void;
207 
208 
209  //
210  // TRANSMOGRIFIER
211  //
212 
219  protected static function cloneBasics(
220  ilObjPortfolioBase $a_source,
221  ilObjPortfolioBase $a_target
222  ): void {
223  global $DIC;
224 
225  // copy portfolio properties
226  $a_target->setPublicComments($a_source->hasPublicComments());
227  $a_target->setProfilePicture($a_source->hasProfilePicture());
228  $a_target->setFontColor($a_source->getFontColor());
229  $a_target->setBackgroundColor($a_source->getBackgroundColor());
230  $a_target->update();
231 
232  // container settings
233  foreach (ilContainer::_getContainerSettings($a_source->getId()) as $keyword => $value) {
234  ilContainer::_writeContainerSetting($a_target->getId(), $keyword, $value);
235  }
236 
237  // style
238  $content_style_domain = $DIC
239  ->contentStyle()
240  ->domain()
241  ->styleForObjId($a_source->getId());
242  $content_style_domain->cloneTo($a_target->getId());
243  }
244 
248  public static function clonePagesAndSettings(
249  ilObjPortfolioBase $a_source,
250  ilObjPortfolioBase $a_target,
251  ?array $a_recipe = null,
252  bool $copy_all = false
253  ): void {
254  global $DIC;
255 
256  $lng = $DIC->language();
257  $ilUser = $DIC->user();
258  $skill_personal_service = $DIC->skills()->personal();
259 
260  $source_id = $a_source->getId();
261  $target_id = $a_target->getId();
262 
263  if ($a_source instanceof ilObjPortfolioTemplate &&
264  $a_target instanceof ilObjPortfolio) {
265  $direction = "t2p";
266  } elseif ($a_source instanceof ilObjPortfolio &&
267  $a_target instanceof ilObjPortfolioTemplate) {
268  $direction = "p2t";
269  } else {
270  return;
271  }
272 
273  self::cloneBasics($a_source, $a_target);
274 
275  // copy advanced metadata
277  ilAdvancedMDValues::_cloneValues($copy_id, $a_source->getId(), $a_target->getId());
278 
279  // copy selection of global optional sets
281  $a_target->getId(),
282  'pfpg',
283  ilAdvancedMDRecord::getObjRecSelection($a_source->getId(), 'pfpg')
284  );
285 
286  // fix metadata record type assignment
287  // e.g. if portfolio is created from template
288  // we need to change this from prtt to prtf
290  ilObject::_lookupType($a_source->getId()),
291  $a_target->getId(),
292  "pfpg",
293  false
294  ) as $rec) {
295  /*
296  * BT 35494: reset assignement of the newly cloned local records,
297  * and only append what's needed to global ones
298  */
299  $target_type = ilObject::_lookupType($a_target->getId());
300  if ($rec->getParentObject() == $a_target->getId()) {
301  $rec->setAssignedObjectTypes(
302  [[
303  "obj_type" => $target_type,
304  "sub_type" => "pfpg",
305  "optional" => 0
306  ]
307  ]
308  );
309  } elseif (!$rec->isAssignedObjectType($target_type, 'pfpg')) {
310  $rec->appendAssignedObjectType(
311  $target_type,
312  "pfpg"
313  );
314  }
315  $rec->update();
316  }
317 
318  // personal skills
319  $pskills = array_keys($skill_personal_service->getSelectedUserSkills($ilUser->getId()));
320 
321  // copy pages
322  $page_map = array();
323  foreach (ilPortfolioPage::getAllPortfolioPages($source_id) as $page) {
324  $page_id = $page["id"];
325 
326  if ($direction === "t2p") {
327  $source_page = new ilPortfolioTemplatePage($page_id);
328  $target_page = new ilPortfolioPage();
329  } else {
330  $source_page = new ilPortfolioPage($page_id);
331  $target_page = new ilPortfolioTemplatePage();
332  }
333  $source_page->setPortfolioId($source_id);
334  $target_page->setPortfolioId($target_id);
335 
336  $page_type = $source_page->getType();
337  $page_title = $source_page->getTitle();
338 
339 
340 
341 
342  $page_recipe = null;
343  if (isset($a_recipe)) {
344  $page_recipe = $a_recipe[$page_id] ?? null;
345  }
346 
347  $valid = false;
348  switch ($page_type) {
349  // page editor
350  default:
351  $target_page->setXMLContent(
352  $source_page->copyXmlContent(
353  true,
354  $a_target->getId(),
355  $copy_id
356  )
357  );
358  $target_page->buildDom(true);
359 
360  // parse content / blocks
361 
362  if ($direction === "t2p") {
363  $dom = $target_page->getDomDoc();
364 
365  // update profile/consultation hours user id
366  self::updateDomNodes($dom, "//PageContent/Profile", "User", $ilUser->getId());
367  self::updateDomNodes($dom, "//PageContent/ConsultationHours", "User", $ilUser->getId());
368  self::updateDomNodes($dom, "//PageContent/MyCourses", "User", $ilUser->getId());
369 
370  // skills
371  $xpath = new DOMXPath($dom);
372  $nodes = $xpath->query("//PageContent/Skills");
373  foreach ($nodes as $node) {
374  $skill_id = $node->getAttribute("Id");
375 
376  // existing personal skills
377  if (in_array($skill_id, $pskills)) {
378  $node->setAttribute("User", $ilUser->getId());
379  }
380  // new skill
381  elseif ($copy_all || in_array($skill_id, $a_recipe["skills"])) {
382  $skill_personal_service->addPersonalSkill($ilUser->getId(), $skill_id);
383 
384  $node->setAttribute("User", $ilUser->getId());
385  }
386  // remove skill
387  else {
388  $page_element = $node->parentNode;
389  $page_element->parentNode->removeChild($page_element);
390  }
391  }
392  }
393 
394  $valid = true;
395  break;
396  }
397 
398  if ($valid) {
399  // #12038 - update xml from dom
400  $target_page->setXMLContent($target_page->getXMLFromDom());
401 
402  $target_page->setType($page_type);
403  $target_page->setTitle($page_title);
404  $target_page->create(false);
405 
406  if ($page_type === ilPortfolioPage::TYPE_PAGE) {
407  $target_page->update(); // handle mob usages!
408  }
409  $page_map[$source_page->getId()] = $target_page->getId();
410  }
411  }
412  ilPortfolioPage::updateInternalLinks($page_map, $a_target);
413  }
414 
415  protected static function updateDomNodes(
416  DOMDocument $a_dom,
417  string $a_xpath,
418  string $a_attr_id,
419  string $a_attr_value
420  ): void {
421  $xpath_temp = new DOMXPath($a_dom);
422  $nodes = $xpath_temp->query($a_xpath);
423  foreach ($nodes as $node) {
424  $node->setAttribute($a_attr_id, $a_attr_value);
425  }
426  }
427 
431  public function fixLinksOnTitleChange(array $a_title_changes): void
432  {
433  foreach (ilPortfolioPage::getAllPortfolioPages($this->getId()) as $port_page) {
434  if ($this->getType() === "prtt") {
435  $page = new ilPortfolioTemplatePage($port_page["id"]);
436  } else {
437  $page = new ilPortfolioPage($port_page["id"]);
438  }
439  if ($page->renameLinksOnTitleChange($a_title_changes)) {
440  $page->update(true, true);
441  }
442  }
443  }
444 }
static getAllPortfolioPages(int $a_portfolio_id)
Get pages of portfolio.
setProfilePicture(bool $a_status)
ILIAS Notes Service $notes
$valid
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setBackgroundColor(string $a_value)
Set background color, e.g.
static cloneBasics(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target)
Clone basic settings.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getObjRecSelection(int $a_obj_id, string $a_sub_type="")
Get repository object record selection.
static clonePagesAndSettings(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target, ?array $a_recipe=null, bool $copy_all=false)
Build template from portfolio and vice versa.
static lookupOnline(int $a_id)
ilLanguage $lng
static updateInternalLinks(array $a_copied_nodes, ilObjPortfolioBase $a_target_obj)
Update internal links, after multiple pages have been copied.
ilDBInterface $db
global $DIC
Definition: shib_login.php:22
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 _allocateCopyId()
Allocate a copy for further entries.
static updateDomNodes(DOMDocument $a_dom, string $a_xpath, string $a_attr_id, string $a_attr_value)
__construct(int $a_id=0, bool $a_reference=true)
doReadCustom(array $a_row)
May be overwritten by derived classes.
static saveObjRecSelection(int $a_obj_id, string $a_sub_type="", ?array $a_records=null, bool $a_delete_before=true)
Save repository object record selection.
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
static _cloneValues(int $copy_id, int $a_source_id, int $a_target_id, ?string $a_sub_type=null, ?int $a_source_sub_id=null, ?int $a_target_sub_id=null)
Clone Advanced Meta Data.
__construct(Container $dic, ilPlugin $plugin)
fixLinksOnTitleChange(array $a_title_changes)
Update internal portfolio links on title change.
doCreate(bool $clone_mode=false)
doUpdateCustom(array &$a_fields)
May be overwritte by derived classes.
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 _getContainerSettings(int $a_id)