ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjPortfolioBase.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
10 abstract class ilObjPortfolioBase extends ilObject2
11 {
15  protected $setting;
16 
20  public function __construct($a_id = 0, $a_reference = true)
21  {
22  global $DIC;
23  parent::__construct($a_id, $a_reference);
24 
25  $this->setting = $DIC->settings();
26 
27  $this->db = $DIC->database();
28  }
29 
30  protected $online; // [bool]
31  protected $comments; // [bool]
32  protected $bg_color; // [string]
33  protected $font_color; // [string]
34  protected $img; // [string]
35  protected $ppic; // [string]
36  protected $style; // [bool]
37 
38 
39  //
40  // PROPERTIES
41  //
42 
48  public function setOnline($a_value)
49  {
50  $this->online = (bool) $a_value;
51  }
52 
58  public function isOnline()
59  {
60  return $this->online;
61  }
62 
68  public static function lookupOnline($a_id)
69  {
70  global $DIC;
71 
72  $ilDB = $DIC->database();
73 
74  $set = $ilDB->query("SELECT is_online" .
75  " FROM usr_portfolio" .
76  " WHERE id = " . $ilDB->quote($a_id, "integer"));
77  $row = $ilDB->fetchAssoc($set);
78  return (bool) $row["is_online"];
79  }
80 
86  public function setPublicComments($a_value)
87  {
88  $this->comments = (bool) $a_value;
89  }
90 
96  public function hasPublicComments()
97  {
98  return $this->comments;
99  }
100 
106  public function hasProfilePicture()
107  {
108  return $this->ppic;
109  }
110 
116  public function setProfilePicture($a_status)
117  {
118  $this->ppic = (bool) $a_status;
119  }
120 
126  public function getBackgroundColor()
127  {
128  if (!$this->bg_color) {
129  $this->bg_color = "ffffff";
130  }
131  return $this->bg_color;
132  }
133 
139  public function setBackgroundColor($a_value)
140  {
141  $this->bg_color = (string) $a_value;
142  }
143 
149  public function getFontColor()
150  {
151  if (!$this->font_color) {
152  $this->font_color = "505050";
153  }
154  return $this->font_color;
155  }
156 
162  public function setFontColor($a_value)
163  {
164  $this->font_color = (string) $a_value;
165  }
166 
172  public function getImage()
173  {
174  return $this->img;
175  }
176 
182  public function setImage($a_value)
183  {
184  $this->img = (string) $a_value;
185  }
186 
192  public function getStyleSheetId()
193  {
194  return (int) $this->style;
195  }
196 
202  public function setStyleSheetId($a_style)
203  {
204  $this->style = (int) $a_style;
205  }
206 
207 
208  //
209  // CRUD
210  //
211 
212  protected function doRead()
213  {
214  $ilDB = $this->db;
215 
216  $set = $ilDB->query("SELECT * FROM usr_portfolio" .
217  " WHERE id = " . $ilDB->quote($this->id, "integer"));
218  $row = $ilDB->fetchAssoc($set);
219 
220  $this->setOnline((bool) $row["is_online"]);
221  $this->setProfilePicture((bool) $row["ppic"]);
222  $this->setBackgroundColor($row["bg_color"]);
223  $this->setFontColor($row["font_color"]);
224  $this->setImage($row["img"]);
225 
226  // #14661
227  $this->setPublicComments(ilNote::commentsActivated($this->id, 0, $this->getType()));
228 
230 
231  $this->doReadCustom($row);
232  }
233 
234  protected function doReadCustom(array $a_row)
235  {
236  }
237 
238  protected function doCreate()
239  {
240  $ilDB = $this->db;
241 
242  $ilDB->manipulate("INSERT INTO usr_portfolio (id,is_online)" .
243  " VALUES (" . $ilDB->quote($this->id, "integer") . "," .
244  $ilDB->quote(0, "integer") . ")");
245  }
246 
247  protected function doUpdate()
248  {
249  $ilDB = $this->db;
250 
251  $fields = array(
252  "is_online" => array("integer", $this->isOnline()),
253  "ppic" => array("integer", $this->hasProfilePicture()),
254  "bg_color" => array("text", $this->getBackgroundColor()),
255  "font_color" => array("text", $this->getFontcolor()),
256  "img" => array("text", $this->getImage())
257  );
258  $this->doUpdateCustom($fields);
259 
260  // #14661
261  ilNote::activateComments($this->id, 0, $this->getType(), $this->hasPublicComments());
262 
264 
265  $ilDB->update(
266  "usr_portfolio",
267  $fields,
268  array("id" => array("integer", $this->id))
269  );
270  }
271 
272  protected function doUpdateCustom(array &$a_fields)
273  {
274  }
275 
276  protected function doDelete()
277  {
278  $ilDB = $this->db;
279 
280  $this->deleteAllPages();
281  $this->deleteImage();
282 
283  $ilDB->manipulate("DELETE FROM usr_portfolio" .
284  " WHERE id = " . $ilDB->quote($this->id, "integer"));
285  }
286 
287  abstract protected function deleteAllPages();
288 
289 
290  //
291  // IMAGES
292  //
293 
299  public function getImageFullPath($a_as_thumb = false)
300  {
301  if ($this->img) {
302  $path = $this->initStorage($this->id);
303  if (!$a_as_thumb) {
304  return $path . $this->img;
305  } else {
306  return $path . "thb_" . $this->img;
307  }
308  }
309  }
310 
314  public function deleteImage()
315  {
316  if ($this->id) {
317  $storage = new ilFSStoragePortfolio($this->id);
318  $storage->delete();
319 
320  $this->setImage(null);
321  }
322  }
323 
331  public static function initStorage($a_id, $a_subdir = null)
332  {
333  $storage = new ilFSStoragePortfolio($a_id);
334  $storage->create();
335 
336  $path = $storage->getAbsolutePath() . "/";
337 
338  if ($a_subdir) {
339  $path .= $a_subdir . "/";
340 
341  if (!is_dir($path)) {
342  mkdir($path);
343  }
344  }
345 
346  return $path;
347  }
348 
355  public function uploadImage(array $a_upload)
356  {
357  if (!$this->id) {
358  return false;
359  }
360 
361  $this->deleteImage();
362 
363  // #10074
364  $clean_name = preg_replace("/[^a-zA-Z0-9\_\.\-]/", "", $a_upload["name"]);
365 
366  $path = $this->initStorage($this->id);
367  $original = "org_" . $this->id . "_" . $clean_name;
368  $thumb = "thb_" . $this->id . "_" . $clean_name;
369  $processed = $this->id . "_" . $clean_name;
370 
371  if (ilUtil::moveUploadedFile($a_upload["tmp_name"], $original, $path . $original)) {
372  chmod($path . $original, 0770);
373 
374  $prfa_set = new ilSetting("prfa");
375  /* as banner height should overflow, we only handle width
376  $dimensions = $prfa_set->get("banner_width")."x".
377  $prfa_set->get("banner_height");
378  */
379  $dimensions = $prfa_set->get("banner_width");
380 
381  // take quality 100 to avoid jpeg artefacts when uploading jpeg files
382  // taking only frame [0] to avoid problems with animated gifs
383  $original_file = ilUtil::escapeShellArg($path . $original);
384  $thumb_file = ilUtil::escapeShellArg($path . $thumb);
385  $processed_file = ilUtil::escapeShellArg($path . $processed);
386  ilUtil::execConvert($original_file . "[0] -geometry 100x100 -quality 100 JPEG:" . $thumb_file);
387  ilUtil::execConvert($original_file . "[0] -geometry " . $dimensions . " -quality 100 JPEG:" . $processed_file);
388 
389  $this->setImage($processed);
390 
391  return true;
392  }
393  return false;
394  }
395 
396 
397  //
398  // TRANSMOGRIFIER
399  //
400 
407  protected static function cloneBasics(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target)
408  {
409  // copy portfolio properties
410  $a_target->setPublicComments($a_source->hasPublicComments());
411  $a_target->setProfilePicture($a_source->hasProfilePicture());
412  $a_target->setFontColor($a_source->getFontColor());
413  $a_target->setBackgroundColor($a_source->getBackgroundColor());
414  $a_target->setImage($a_source->getImage());
415  $a_target->update();
416 
417  // banner/images
418  $source_dir = $a_source->initStorage($a_source->getId());
419  $target_dir = $a_target->initStorage($a_target->getId());
420  ilFSStoragePortfolio::_copyDirectory($source_dir, $target_dir);
421 
422  // set/copy stylesheet
423  $style_id = $a_source->getStyleSheetId();
424  if ($style_id > 0 && !ilObjStyleSheet::_lookupStandard($style_id)) {
425  $style_obj = ilObjectFactory::getInstanceByObjId($style_id);
426  $new_id = $style_obj->ilClone();
427  $a_target->setStyleSheetId($new_id);
428  $a_target->update();
429  }
430  }
431 
439  public static function clonePagesAndSettings(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target, array $a_recipe = null, $copy_all = false)
440  {
441  global $DIC;
442 
443  $lng = $DIC->language();
444  $ilUser = $DIC->user();
445 
446  $source_id = $a_source->getId();
447  $target_id = $a_target->getId();
448 
449  if ($a_source instanceof ilObjPortfolioTemplate &&
450  $a_target instanceof ilObjPortfolio) {
451  $direction = "t2p";
452  } elseif ($a_source instanceof ilObjPortfolio &&
453  $a_target instanceof ilObjPortfolioTemplate) {
454  $direction = "p2t";
455  } else {
456  return;
457  }
458 
459  self::cloneBasics($a_source, $a_target);
460 
461  // personal skills
462  $pskills = array_keys(ilPersonalSkill::getSelectedUserSkills($ilUser->getId()));
463 
464  // copy pages
465  $blog_count = 0;
466  $page_map = array();
467  foreach (ilPortfolioPage::getAllPortfolioPages($source_id) as $page) {
468  $page_id = $page["id"];
469 
470  if ($direction == "t2p") {
471  $source_page = new ilPortfolioTemplatePage($page_id);
472  $target_page = new ilPortfolioPage();
473  } else {
474  $source_page = new ilPortfolioPage($page_id);
475  $target_page = new ilPortfolioTemplatePage();
476  }
477  $source_page->setPortfolioId($source_id);
478  $target_page->setPortfolioId($target_id);
479 
480  $page_type = $source_page->getType();
481  $page_title = $source_page->getTitle();
482 
483 
484 
485 
486  $page_recipe = null;
487  if (is_array($a_recipe)) {
488  $page_recipe = $a_recipe[$page_id];
489  }
490 
491  $valid = false;
492  switch ($page_type) {
493  // blog => blog template
495  if ($direction == "p2t") {
497  $page_title = $lng->txt("obj_blog") . " " . (++$blog_count);
498  $valid = true;
499  }
500  break;
501 
502  // blog template => blog (needs recipe)
504  if ($direction == "t2p" && (is_array($page_recipe) || $copy_all)) {
505  $page_type = ilPortfolioPage::TYPE_BLOG;
506  if ($copy_all) {
507  $page_title = self::createBlogInPersonalWorkspace($page_title);
508  $valid = true;
509  } else {
510  if ($page_recipe[0] == "blog") {
511  switch ($page_recipe[1]) {
512  case "create":
513  $page_title = self::createBlogInPersonalWorkspace($page_recipe[2]);
514  $valid = true;
515  break;
516 
517  case "reuse":
518  $page_title = $page_recipe[2];
519  $valid = true;
520  break;
521 
522  case "ignore":
523  // do nothing
524  break;
525  }
526  }
527  }
528  }
529  break;
530 
531  // page editor
532  default:
533  $target_page->setXMLContent($source_page->copyXmlContent(true)); // copy mobs
534  $target_page->buildDom(true);
535 
536 
537  // parse content / blocks
538 
539  if ($direction == "t2p") {
540  $dom = $target_page->getDom();
541  if ($dom instanceof php4DOMDocument) {
542  $dom = $dom->myDOMDocument;
543  }
544 
545  // update profile/consultation hours user id
546  self::updateDomNodes($dom, "//PageContent/Profile", "User", $ilUser->getId());
547  self::updateDomNodes($dom, "//PageContent/ConsultationHours", "User", $ilUser->getId());
548  self::updateDomNodes($dom, "//PageContent/MyCourses", "User", $ilUser->getId());
549 
550  // skills
551  $xpath = new DOMXPath($dom);
552  $nodes = $xpath->query("//PageContent/Skills");
553  foreach ($nodes as $node) {
554  $skill_id = $node->getAttribute("Id");
555 
556  // existing personal skills
557  if (in_array($skill_id, $pskills)) {
558  $node->setAttribute("User", $ilUser->getId());
559  }
560  // new skill
561  elseif ($copy_all || in_array($skill_id, $a_recipe["skills"])) {
562  ilPersonalSkill::addPersonalSkill($ilUser->getId(), $skill_id);
563 
564  $node->setAttribute("User", $ilUser->getId());
565  }
566  // remove skill
567  else {
568  $page_element = $node->parentNode;
569  $page_element->parentNode->removeChild($page_element);
570  }
571  }
572  }
573 
574  $valid = true;
575  break;
576  }
577 
578  if ($valid) {
579  // #12038 - update xml from dom
580  $target_page->setXMLContent($target_page->getXMLFromDom());
581 
582  $target_page->setType($page_type);
583  $target_page->setTitle($page_title);
584  $target_page->create();
585 
586  if ($page_type == ilPortfolioPage::TYPE_PAGE) {
587  $target_page->update(); // handle mob usages!
588  }
589  $page_map[$source_page->getId()] = $target_page->getId();
590  }
591  }
592 
593  ilPortfolioPage::updateInternalLinks($page_map, $a_target);
594  }
595 
596  protected static function updateDomNodes($a_dom, $a_xpath, $a_attr_id, $a_attr_value)
597  {
598  $xpath_temp = new DOMXPath($a_dom);
599  $nodes = $xpath_temp->query($a_xpath);
600  foreach ($nodes as $node) {
601  $node->setAttribute($a_attr_id, $a_attr_value);
602  }
603  }
604 
605  protected static function createBlogInPersonalWorkspace($a_title)
606  {
607  global $DIC;
608 
609  $ilUser = $DIC->user();
610 
611  static $ws_access = null;
612 
613  $blog = new ilObjBlog();
614  $blog->setType("blog");
615  $blog->setTitle($a_title);
616  $blog->create();
617 
618  if (!$ws_access) {
619  $tree = new ilWorkspaceTree($ilUser->getId());
620 
621  // #13235
622  if (!$tree->getRootId()) {
623  $tree->createTreeForUser($ilUser->getId());
624  }
625 
626  $ws_access = new ilWorkspaceAccessHandler($tree);
627  }
628 
629  $tree = $ws_access->getTree();
630  $node_id = $tree->insertObject($tree->getRootId(), $blog->getId());
631  $ws_access->setPermissions($tree->getRootId(), $node_id);
632 
633  return $blog->getId();
634  }
635 
641  public function fixLinksOnTitleChange($a_title_changes)
642  {
643  foreach (ilPortfolioPage::getAllPortfolioPages($this->getId()) as $port_page) {
644  if ($this->getType() == "prtt") {
645  $page = new ilPortfolioTemplatePage($port_page["id"]);
646  } else {
647  $page = new ilPortfolioPage($port_page["id"]);
648  }
649  if ($page->renameLinksOnTitleChange($a_title_changes)) {
650  $page->update(true, true);
651  }
652  }
653  }
654 }
static getAllPortfolioPages($a_portfolio_id)
Get pages of portfolio.
static updateDomNodes($a_dom, $a_xpath, $a_attr_id, $a_attr_value)
static commentsActivated($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_news_id=0)
Are comments activated for object?
getStyleSheetId()
Get style sheet id.
setPublicComments($a_value)
Set public comments status.
getImageFullPath($a_as_thumb=false)
Get banner image incl.
deleteImage()
remove existing file
$valid
static initStorage($a_id, $a_subdir=null)
Init file system storage.
setFontColor($a_value)
Set font color.
Access handler for personal workspace.
$target_id
Definition: goto.php:51
static createBlogInPersonalWorkspace($a_title)
static getSelectedUserSkills($a_user_id)
Get personal selected user skills.
hasProfilePicture()
Get profile picture status.
static clonePagesAndSettings(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target, array $a_recipe=null, $copy_all=false)
Build template from portfolio and vice versa.
getBackgroundColor()
Get background color.
__construct($a_id=0, $a_reference=true)
Constructor.
getFontColor()
Get font color.
static lookupObjectStyle($a_obj_id)
Lookup object style.
Tree handler for personal workspace.
static updateInternalLinks($a_copied_nodes, ilObjPortfolioBase $a_target_obj)
Update internal links, after multiple pages have been copied.
setOnline($a_value)
Set online status.
static cloneBasics(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target)
Clone basic settings.
static _lookupStandard($a_id)
Lookup standard flag.
static _copyDirectory($a_source, $a_target)
Copy directory and all contents.
static writeStyleUsage($a_obj_id, $a_style_id)
Write style usage.
uploadImage(array $a_upload)
Upload new image file.
static lookupOnline($a_id)
Is online?
static execConvert($args)
execute convert command
getId()
get object id public
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
global $DIC
Definition: goto.php:24
Class ilObjBlog.
static addPersonalSkill($a_user_id, $a_skill_node_id)
Add personal skill.
Page for user portfolio.
Page for portfolio template.
hasPublicComments()
Active public comments?
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
getImage()
Get banner image.
setStyleSheetId($a_style)
Set style sheet id.
static activateComments($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_activate=true)
Activate notes feature.
static escapeShellArg($a_arg)
__construct(Container $dic, ilPlugin $plugin)
global $ilDB
setBackgroundColor($a_value)
Set background color.
$ilUser
Definition: imgupload.php:18
doUpdateCustom(array &$a_fields)
fixLinksOnTitleChange($a_title_changes)
Fix internal portfolio links.
setImage($a_value)
Set banner image.
setProfilePicture($a_status)
Toggle profile picture status.