ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
10abstract class ilObjPortfolioBase extends ilObject2
11{
12
16 public function __construct($a_id = 0, $a_reference = true)
17 {
18 global $DIC;
19 parent::__construct($a_id, $a_reference);
20
21 $this->db = $DIC->database();
22 }
23
24 protected $online; // [bool]
25 protected $comments; // [bool]
26 protected $bg_color; // [string]
27 protected $font_color; // [string]
28 protected $img; // [string]
29 protected $ppic; // [string]
30 protected $style; // [bool]
31
32
33 //
34 // PROPERTIES
35 //
36
42 public function setOnline($a_value)
43 {
44 $this->online = (bool) $a_value;
45 }
46
52 public function isOnline()
53 {
54 return $this->online;
55 }
56
62 public static function lookupOnline($a_id)
63 {
64 global $DIC;
65
66 $ilDB = $DIC->database();
67
68 $set = $ilDB->query("SELECT is_online" .
69 " FROM usr_portfolio" .
70 " WHERE id = " . $ilDB->quote($a_id, "integer"));
71 $row = $ilDB->fetchAssoc($set);
72 return (bool) $row["is_online"];
73 }
74
80 public function setPublicComments($a_value)
81 {
82 $this->comments = (bool) $a_value;
83 }
84
90 public function hasPublicComments()
91 {
92 return $this->comments;
93 }
94
100 public function hasProfilePicture()
101 {
102 return $this->ppic;
103 }
104
110 public function setProfilePicture($a_status)
111 {
112 $this->ppic = (bool) $a_status;
113 }
114
120 public function getBackgroundColor()
121 {
122 if (!$this->bg_color) {
123 $this->bg_color = "ffffff";
124 }
125 return $this->bg_color;
126 }
127
133 public function setBackgroundColor($a_value)
134 {
135 $this->bg_color = (string) $a_value;
136 }
137
143 public function getFontColor()
144 {
145 if (!$this->font_color) {
146 $this->font_color = "505050";
147 }
148 return $this->font_color;
149 }
150
156 public function setFontColor($a_value)
157 {
158 $this->font_color = (string) $a_value;
159 }
160
166 public function getImage()
167 {
168 return $this->img;
169 }
170
176 public function setImage($a_value)
177 {
178 $this->img = (string) $a_value;
179 }
180
186 public function getStyleSheetId()
187 {
188 return (int) $this->style;
189 }
190
196 public function setStyleSheetId($a_style)
197 {
198 $this->style = (int) $a_style;
199 }
200
201
202 //
203 // CRUD
204 //
205
206 protected function doRead()
207 {
209
210 $set = $ilDB->query("SELECT * FROM usr_portfolio" .
211 " WHERE id = " . $ilDB->quote($this->id, "integer"));
212 $row = $ilDB->fetchAssoc($set);
213
214 $this->setOnline((bool) $row["is_online"]);
215 $this->setProfilePicture((bool) $row["ppic"]);
216 $this->setBackgroundColor($row["bg_color"]);
217 $this->setFontColor($row["font_color"]);
218 $this->setImage($row["img"]);
219
220 // #14661
221 $this->setPublicComments(ilNote::commentsActivated($this->id, 0, $this->getType()));
222
224
225 $this->doReadCustom($row);
226 }
227
228 protected function doReadCustom(array $a_row)
229 {
230 }
231
232 protected function doCreate()
233 {
235
236 $ilDB->manipulate("INSERT INTO usr_portfolio (id,is_online)" .
237 " VALUES (" . $ilDB->quote($this->id, "integer") . "," .
238 $ilDB->quote(0, "integer") . ")");
239 }
240
241 protected function doUpdate()
242 {
244
245 $fields = array(
246 "is_online" => array("integer", $this->isOnline()),
247 "ppic" => array("integer", $this->hasProfilePicture()),
248 "bg_color" => array("text", $this->getBackgroundColor()),
249 "font_color" => array("text", $this->getFontcolor()),
250 "img" => array("text", $this->getImage())
251 );
252 $this->doUpdateCustom($fields);
253
254 // #14661
255 ilNote::activateComments($this->id, 0, $this->getType(), $this->hasPublicComments());
256
258
259 $ilDB->update(
260 "usr_portfolio",
261 $fields,
262 array("id" => array("integer", $this->id))
263 );
264 }
265
266 protected function doUpdateCustom(array &$a_fields)
267 {
268 }
269
270 protected function doDelete()
271 {
273
274 $this->deleteAllPages();
275 $this->deleteImage();
276
277 $ilDB->manipulate("DELETE FROM usr_portfolio" .
278 " WHERE id = " . $ilDB->quote($this->id, "integer"));
279 }
280
281 abstract protected function deleteAllPages();
282
283
284 //
285 // IMAGES
286 //
287
293 public function getImageFullPath($a_as_thumb = false)
294 {
295 if ($this->img) {
296 $path = $this->initStorage($this->id);
297 if (!$a_as_thumb) {
298 return $path . $this->img;
299 } else {
300 return $path . "thb_" . $this->img;
301 }
302 }
303 }
304
308 public function deleteImage()
309 {
310 if ($this->id) {
311 $storage = new ilFSStoragePortfolio($this->id);
312 $storage->delete();
313
314 $this->setImage(null);
315 }
316 }
317
325 public static function initStorage($a_id, $a_subdir = null)
326 {
327 $storage = new ilFSStoragePortfolio($a_id);
328 $storage->create();
329
330 $path = $storage->getAbsolutePath() . "/";
331
332 if ($a_subdir) {
333 $path .= $a_subdir . "/";
334
335 if (!is_dir($path)) {
336 mkdir($path);
337 }
338 }
339
340 return $path;
341 }
342
349 public function uploadImage(array $a_upload)
350 {
351 if (!$this->id) {
352 return false;
353 }
354
355 $this->deleteImage();
356
357 // #10074
358 $clean_name = preg_replace("/[^a-zA-Z0-9\_\.\-]/", "", $a_upload["name"]);
359
360 $path = $this->initStorage($this->id);
361 $original = "org_" . $this->id . "_" . $clean_name;
362 $thumb = "thb_" . $this->id . "_" . $clean_name;
363 $processed = $this->id . "_" . $clean_name;
364
365 if (ilUtil::moveUploadedFile($a_upload["tmp_name"], $original, $path . $original)) {
366 chmod($path . $original, 0770);
367
368 $prfa_set = new ilSetting("prfa");
369 /* as banner height should overflow, we only handle width
370 $dimensions = $prfa_set->get("banner_width")."x".
371 $prfa_set->get("banner_height");
372 */
373 $dimensions = $prfa_set->get("banner_width");
374
375 // take quality 100 to avoid jpeg artefacts when uploading jpeg files
376 // taking only frame [0] to avoid problems with animated gifs
377 $original_file = ilUtil::escapeShellArg($path . $original);
378 $thumb_file = ilUtil::escapeShellArg($path . $thumb);
379 $processed_file = ilUtil::escapeShellArg($path . $processed);
380 ilUtil::execConvert($original_file . "[0] -geometry 100x100 -quality 100 JPEG:" . $thumb_file);
381 ilUtil::execConvert($original_file . "[0] -geometry " . $dimensions . " -quality 100 JPEG:" . $processed_file);
382
383 $this->setImage($processed);
384
385 return true;
386 }
387 return false;
388 }
389
390
391 //
392 // TRANSMOGRIFIER
393 //
394
401 protected static function cloneBasics(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target)
402 {
403 // copy portfolio properties
404 $a_target->setPublicComments($a_source->hasPublicComments());
405 $a_target->setProfilePicture($a_source->hasProfilePicture());
406 $a_target->setFontColor($a_source->getFontColor());
407 $a_target->setBackgroundColor($a_source->getBackgroundColor());
408 $a_target->setImage($a_source->getImage());
409 $a_target->update();
410
411 // banner/images
412 $source_dir = $a_source->initStorage($a_source->getId());
413 $target_dir = $a_target->initStorage($a_target->getId());
414 ilFSStoragePortfolio::_copyDirectory($source_dir, $target_dir);
415
416 // set/copy stylesheet
417 $style_id = $a_source->getStyleSheetId();
418 if ($style_id > 0 && !ilObjStyleSheet::_lookupStandard($style_id)) {
419 $style_obj = ilObjectFactory::getInstanceByObjId($style_id);
420 $new_id = $style_obj->ilClone();
421 $a_target->setStyleSheetId($new_id);
422 $a_target->update();
423 }
424 }
425
433 public static function clonePagesAndSettings(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target, array $a_recipe = null, $copy_all = false)
434 {
435 global $DIC;
436
437 $lng = $DIC->language();
438 $ilUser = $DIC->user();
439
440 $source_id = $a_source->getId();
441 $target_id = $a_target->getId();
442
443 if ($a_source instanceof ilObjPortfolioTemplate &&
444 $a_target instanceof ilObjPortfolio) {
445 $direction = "t2p";
446 } elseif ($a_source instanceof ilObjPortfolio &&
447 $a_target instanceof ilObjPortfolioTemplate) {
448 $direction = "p2t";
449 } else {
450 return;
451 }
452
453 self::cloneBasics($a_source, $a_target);
454
455 // personal skills
456 $pskills = array_keys(ilPersonalSkill::getSelectedUserSkills($ilUser->getId()));
457
458 // copy pages
459 $blog_count = 0;
460 $page_map = array();
461 foreach (ilPortfolioPage::getAllPortfolioPages($source_id) as $page) {
462 $page_id = $page["id"];
463
464 if ($direction == "t2p") {
465 $source_page = new ilPortfolioTemplatePage($page_id);
466 $target_page = new ilPortfolioPage();
467 } else {
468 $source_page = new ilPortfolioPage($page_id);
469 $target_page = new ilPortfolioTemplatePage();
470 }
471 $source_page->setPortfolioId($source_id);
472 $target_page->setPortfolioId($target_id);
473
474 $page_type = $source_page->getType();
475 $page_title = $source_page->getTitle();
476
477
478
479
480 $page_recipe = null;
481 if (is_array($a_recipe)) {
482 $page_recipe = $a_recipe[$page_id];
483 }
484
485 $valid = false;
486 switch ($page_type) {
487 // blog => blog template
489 if ($direction == "p2t") {
491 $page_title = $lng->txt("obj_blog") . " " . (++$blog_count);
492 $valid = true;
493 }
494 break;
495
496 // blog template => blog (needs recipe)
498 if ($direction == "t2p" && (is_array($page_recipe) || $copy_all)) {
499 $page_type = ilPortfolioPage::TYPE_BLOG;
500 if ($copy_all) {
501 $page_title = self::createBlogInPersonalWorkspace($page_title);
502 $valid = true;
503 } else {
504 if ($page_recipe[0] == "blog") {
505 switch ($page_recipe[1]) {
506 case "create":
507 $page_title = self::createBlogInPersonalWorkspace($page_recipe[2]);
508 $valid = true;
509 break;
510
511 case "reuse":
512 $page_title = $page_recipe[2];
513 $valid = true;
514 break;
515
516 case "ignore":
517 // do nothing
518 break;
519 }
520 }
521 }
522 }
523 break;
524
525 // page editor
526 default:
527 $target_page->setXMLContent($source_page->copyXmlContent(true)); // copy mobs
528 $target_page->buildDom(true);
529
530
531 // parse content / blocks
532
533 if ($direction == "t2p") {
534 $dom = $target_page->getDom();
535 if ($dom instanceof php4DOMDocument) {
536 $dom = $dom->myDOMDocument;
537 }
538
539 // update profile/consultation hours user id
540 self::updateDomNodes($dom, "//PageContent/Profile", "User", $ilUser->getId());
541 self::updateDomNodes($dom, "//PageContent/ConsultationHours", "User", $ilUser->getId());
542 self::updateDomNodes($dom, "//PageContent/MyCourses", "User", $ilUser->getId());
543
544 // skills
545 $xpath = new DOMXPath($dom);
546 $nodes = $xpath->query("//PageContent/Skills");
547 foreach ($nodes as $node) {
548 $skill_id = $node->getAttribute("Id");
549
550 // existing personal skills
551 if (in_array($skill_id, $pskills)) {
552 $node->setAttribute("User", $ilUser->getId());
553 }
554 // new skill
555 elseif ($copy_all || in_array($skill_id, $a_recipe["skills"])) {
556 ilPersonalSkill::addPersonalSkill($ilUser->getId(), $skill_id);
557
558 $node->setAttribute("User", $ilUser->getId());
559 }
560 // remove skill
561 else {
562 $page_element = $node->parentNode;
563 $page_element->parentNode->removeChild($page_element);
564 }
565 }
566 }
567
568 $valid = true;
569 break;
570 }
571
572 if ($valid) {
573 // #12038 - update xml from dom
574 $target_page->setXMLContent($target_page->getXMLFromDom());
575
576 $target_page->setType($page_type);
577 $target_page->setTitle($page_title);
578 $target_page->create();
579
580 if ($page_type == ilPortfolioPage::TYPE_PAGE) {
581 $target_page->update(); // handle mob usages!
582 }
583 $page_map[$source_page->getId()] = $target_page->getId();
584 }
585 }
586
587 ilPortfolioPage::updateInternalLinks($page_map, $a_target);
588 }
589
590 protected static function updateDomNodes($a_dom, $a_xpath, $a_attr_id, $a_attr_value)
591 {
592 $xpath_temp = new DOMXPath($a_dom);
593 $nodes = $xpath_temp->query($a_xpath);
594 foreach ($nodes as $node) {
595 $node->setAttribute($a_attr_id, $a_attr_value);
596 }
597 }
598
599 protected static function createBlogInPersonalWorkspace($a_title)
600 {
601 global $DIC;
602
603 $ilUser = $DIC->user();
604
605 static $ws_access = null;
606
607 $blog = new ilObjBlog();
608 $blog->setType("blog");
609 $blog->setTitle($a_title);
610 $blog->create();
611
612 if (!$ws_access) {
613 $tree = new ilWorkspaceTree($ilUser->getId());
614
615 // #13235
616 if (!$tree->getRootId()) {
617 $tree->createTreeForUser($ilUser->getId());
618 }
619
620 $ws_access = new ilWorkspaceAccessHandler($tree);
621 }
622
623 $tree = $ws_access->getTree();
624 $node_id = $tree->insertObject($tree->getRootId(), $blog->getId());
625 $ws_access->setPermissions($tree->getRootId(), $node_id);
626
627 return $blog->getId();
628 }
629
635 public function fixLinksOnTitleChange($a_title_changes)
636 {
637 foreach (ilPortfolioPage::getAllPortfolioPages($this->getId()) as $port_page) {
638 if ($this->getType() == "prtt") {
639 $page = new ilPortfolioTemplatePage($port_page["id"]);
640 } else {
641 $page = new ilPortfolioPage($port_page["id"]);
642 }
643 if ($page->renameLinksOnTitleChange($a_title_changes)) {
644 $page->update(true, true);
645 }
646 }
647 }
648}
An exception for terminatinating execution or to throw for unit testing.
static _copyDirectory($a_source, $a_target)
Copy directory and all contents.
static commentsActivated($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_news_id=0)
Are comments activated for object?
static activateComments($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_activate=true)
Activate notes feature.
Class ilObjBlog.
deleteImage()
remove existing file
setPublicComments($a_value)
Set public comments status.
doUpdateCustom(array &$a_fields)
getStyleSheetId()
Get style sheet id.
static clonePagesAndSettings(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target, array $a_recipe=null, $copy_all=false)
Build template from portfolio and vice versa.
setOnline($a_value)
Set online status.
static cloneBasics(ilObjPortfolioBase $a_source, ilObjPortfolioBase $a_target)
Clone basic settings.
getBackgroundColor()
Get background color.
hasProfilePicture()
Get profile picture status.
uploadImage(array $a_upload)
Upload new image file.
setBackgroundColor($a_value)
Set background color.
static createBlogInPersonalWorkspace($a_title)
__construct($a_id=0, $a_reference=true)
Constructor.
setStyleSheetId($a_style)
Set style sheet id.
static updateDomNodes($a_dom, $a_xpath, $a_attr_id, $a_attr_value)
static lookupOnline($a_id)
Is online?
hasPublicComments()
Active public comments?
setImage($a_value)
Set banner image.
setProfilePicture($a_status)
Toggle profile picture status.
static initStorage($a_id, $a_subdir=null)
Init file system storage.
fixLinksOnTitleChange($a_title_changes)
Fix internal portfolio links.
setFontColor($a_value)
Set font color.
getImageFullPath($a_as_thumb=false)
Get banner image incl.
static _lookupStandard($a_id)
Lookup standard flag.
static lookupObjectStyle($a_obj_id)
Lookup object style.
static writeStyleUsage($a_obj_id, $a_style_id)
Write style usage.
Class ilObject2 This is an intermediate progress of ilObject class.
getType()
get object type @access public
update()
update object in db
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
getId()
get object id @access public
static getSelectedUserSkills($a_user_id)
Get personal selected user skills.
static addPersonalSkill($a_user_id, $a_skill_node_id)
Add personal skill.
Page for user portfolio.
static getAllPortfolioPages($a_portfolio_id)
Get pages of portfolio.
static updateInternalLinks($a_copied_nodes, ilObjPortfolioBase $a_target_obj)
Update internal links, after multiple pages have been copied.
Page for portfolio template.
ILIAS Setting Class.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static escapeShellArg($a_arg)
static execConvert($args)
execute convert command
Access handler for personal workspace.
Tree handler for personal workspace.
$valid
$target_id
Definition: goto.php:49
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $ilDB
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46