ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjBlog.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once "Services/Object/classes/class.ilObject2.php";
6 
15 class ilObjBlog extends ilObject2
16 {
17  protected $notes; // [bool]
18  protected $bg_color; // [string]
19  protected $font_color; // [string]
20  protected $img; // [string]
21  protected $ppic; // [string]
22  protected $rss; // [bool]
23  protected $approval; // [bool]
24 
25  function initType()
26  {
27  $this->type = "blog";
28  }
29 
30  protected function doRead()
31  {
32  global $ilDB;
33 
34  $set = $ilDB->query("SELECT * FROM il_blog".
35  " WHERE id = ".$ilDB->quote($this->id, "integer"));
36  $row = $ilDB->fetchAssoc($set);
37  $this->setNotesStatus((bool)$row["notes"]);
38  $this->setProfilePicture((bool)$row["ppic"]);
39  $this->setBackgroundColor($row["bg_color"]);
40  $this->setFontColor($row["font_color"]);
41  $this->setImage($row["img"]);
42  $this->setRSS($row["rss_active"]);
43  $this->setApproval($row["approval"]);
44  }
45 
46  protected function doCreate()
47  {
48  global $ilDB;
49 
50  $ilDB->manipulate("INSERT INTO il_blog (id,notes,ppic,rss_active,approval) VALUES (".
51  $ilDB->quote($this->id, "integer").",".
52  $ilDB->quote(true, "integer").",".
53  $ilDB->quote(true, "integer").",".
54  $ilDB->quote(true, "integer").",".
55  $ilDB->quote(false, "integer").")");
56  }
57 
58  protected function doDelete()
59  {
60  global $ilDB;
61 
62  $this->deleteImage();
63 
64  include_once "Modules/Blog/classes/class.ilBlogPosting.php";
66 
67  // remove all notifications
68  include_once "./Services/Notification/classes/class.ilNotification.php";
70 
71  $ilDB->manipulate("DELETE FROM il_blog".
72  " WHERE id = ".$ilDB->quote($this->id, "integer"));
73  }
74 
75  protected function doUpdate()
76  {
77  global $ilDB;
78 
79  if($this->id)
80  {
81  $ilDB->manipulate("UPDATE il_blog".
82  " SET notes = ".$ilDB->quote($this->getNotesStatus(), "integer").
83  ",ppic = ".$ilDB->quote($this->hasProfilePicture(), "integer").
84  ",bg_color = ".$ilDB->quote($this->getBackgroundColor(), "text").
85  ",font_color = ".$ilDB->quote($this->getFontcolor(), "text").
86  ",img = ".$ilDB->quote($this->getImage(), "text").
87  ",rss_active = ".$ilDB->quote($this->hasRSS(), "integer").
88  ",approval = ".$ilDB->quote($this->hasApproval(), "integer").
89  " WHERE id = ".$ilDB->quote($this->id, "integer"));
90  }
91  }
92 
98  function getNotesStatus()
99  {
100  return $this->notes;
101  }
102 
108  function setNotesStatus($a_status)
109  {
110  $this->notes = (bool)$a_status;
111  }
112 
118  function hasProfilePicture()
119  {
120  return $this->ppic;
121  }
122 
128  function setProfilePicture($a_status)
129  {
130  $this->ppic = (bool)$a_status;
131  }
132 
139  {
140  if(!$this->bg_color)
141  {
142  $this->bg_color = "ffffff";
143  }
144  return $this->bg_color;
145  }
146 
152  function setBackgroundColor($a_value)
153  {
154  $this->bg_color = (string)$a_value;
155  }
156 
162  function getFontColor()
163  {
164  if(!$this->font_color)
165  {
166  $this->font_color = "505050";
167  }
168  return $this->font_color;
169  }
170 
176  function setFontColor($a_value)
177  {
178  $this->font_color = (string)$a_value;
179  }
180 
186  function getImage()
187  {
188  return $this->img;
189  }
190 
196  function setImage($a_value)
197  {
198  $this->img = (string)$a_value;
199  }
200 
206  function getImageFullPath($a_as_thumb = false)
207  {
208  if($this->img)
209  {
210  $path = $this->initStorage($this->id);
211  if(!$a_as_thumb)
212  {
213  return $path.$this->img;
214  }
215  else
216  {
217  return $path."thb_".$this->img;
218  }
219  }
220  }
221 
225  public function deleteImage()
226  {
227  if($this->id)
228  {
229  include_once "Modules/Blog/classes/class.ilFSStorageBlog.php";
230  $storage = new ilFSStorageBlog($this->id);
231  $storage->delete();
232 
233  $this->setImage(null);
234  }
235  }
236 
244  public static function initStorage($a_id, $a_subdir = null)
245  {
246  include_once "Modules/Blog/classes/class.ilFSStorageBlog.php";
247  $storage = new ilFSStorageBlog($a_id);
248  $storage->create();
249 
250  $path = $storage->getAbsolutePath()."/";
251 
252  if($a_subdir)
253  {
254  $path .= $a_subdir."/";
255 
256  if(!is_dir($path))
257  {
258  mkdir($path);
259  }
260  }
261 
262  return $path;
263  }
264 
271  function uploadImage(array $a_upload)
272  {
273  if(!$this->id)
274  {
275  return false;
276  }
277 
278  $this->deleteImage();
279 
280  // #10074
281  $clean_name = preg_replace("/[^a-zA-Z0-9\_\.\-]/", "", $a_upload["name"]);
282 
283  $path = $this->initStorage($this->id);
284  $original = "org_".$this->id."_".$clean_name;
285  $thumb = "thb_".$this->id."_".$clean_name;
286  $processed = $this->id."_".$clean_name;
287 
288  if(@move_uploaded_file($a_upload["tmp_name"], $path.$original))
289  {
290  chmod($path.$original, 0770);
291 
292  $blga_set = new ilSetting("blga");
293  $dimensions = $blga_set->get("banner_width")."x".
294  $blga_set->get("banner_height");
295 
296  // take quality 100 to avoid jpeg artefacts when uploading jpeg files
297  // taking only frame [0] to avoid problems with animated gifs
298  $original_file = ilUtil::escapeShellArg($path.$original);
299  $thumb_file = ilUtil::escapeShellArg($path.$thumb);
300  $processed_file = ilUtil::escapeShellArg($path.$processed);
301  ilUtil::execConvert($original_file."[0] -geometry 100x100 -quality 100 JPEG:".$thumb_file);
302  ilUtil::execConvert($original_file."[0] -geometry ".$dimensions."! -quality 100 JPEG:".$processed_file);
303 
304  $this->setImage($processed);
305  return true;
306  }
307  return false;
308  }
309 
315  function hasRSS()
316  {
317  return $this->rss;
318  }
319 
325  function setRSS($a_status)
326  {
327  $this->rss = (bool)$a_status;
328  }
329 
335  function hasApproval()
336  {
337  return (bool)$this->approval;
338  }
339 
345  function setApproval($a_status)
346  {
347  $this->approval = (bool)$a_status;
348  }
349 
350  static function sendNotification($a_action, $a_in_wsp, $a_blog_node_id, $a_posting_id, $a_comment = null)
351  {
352  global $ilUser, $ilAccess;
353 
354  // get blog object id (repository or workspace)
355  if($a_in_wsp)
356  {
357  include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
358  include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
359  $tree = new ilWorkspaceTree($ilUser->getId()); // owner of tree is irrelevant
360  $blog_obj_id = $tree->lookupObjectId($a_blog_node_id);
361  $access_handler = new ilWorkspaceAccessHandler($tree);
362 
363  $link = ilWorkspaceAccessHandler::getGotoLink($a_blog_node_id, $blog_obj_id, "_".$a_posting_id);
364  }
365  else
366  {
367  $blog_obj_id = ilObject::_lookupObjId($a_blog_node_id);
368  $access_handler = null;
369 
370  include_once "Services/Link/classes/class.ilLink.php";
371  $link = ilLink::_getStaticLink($a_blog_node_id, "blog", true , "_".$a_posting_id);
372  }
373  if(!$blog_obj_id)
374  {
375  return;
376  }
377 
378  include_once "./Modules/Blog/classes/class.ilBlogPosting.php";
379  $posting = new ilBlogPosting($a_posting_id);
380 
381  // #11138
382  $ignore_threshold = ($a_action == "comment");
383 
384  // approval handling
385  $admin_only = false;
386  if(!$posting->isApproved())
387  {
388  $blog = new self($blog_obj_id, false);
389  if($blog->hasApproval())
390  {
391  switch($a_action)
392  {
393  case "update":
394  // un-approved posting was updated - no notifications
395  return;
396 
397  case "new":
398  // un-approved posting was activated - admin-only notification
399  $admin_only = true;
400  $ignore_threshold = true;
401  break;
402  }
403  }
404  }
405 
406  // recipients
407  include_once "./Services/Notification/classes/class.ilNotification.php";
409  $blog_obj_id, $a_posting_id, $ignore_threshold);
410  if(!sizeof($users))
411  {
412  return;
413  }
414 
415 
416  // send mails
417 
418  include_once "./Services/Mail/classes/class.ilMail.php";
419  include_once "./Services/User/classes/class.ilObjUser.php";
420  include_once "./Services/Language/classes/class.ilLanguageFactory.php";
421  include_once("./Services/User/classes/class.ilUserUtil.php");
422 
423  $posting_title = $posting->getTitle();
424  $blog_title = ilObject::_lookupTitle($blog_obj_id);
425  $author = $posting->getAuthor();
426 
427  $notified = array();
428  foreach(array_unique($users) as $idx => $user_id)
429  {
430  // the user responsible for the action should not be notified
431  if($user_id == $ilUser->getId())
432  {
433  continue;
434  }
435 
436  // workspace
437  if($access_handler)
438  {
439  if($admin_only &&
440  !$access_handler->checkAccessOfUser($tree, $user_id, 'write', '', $a_blog_node_id))
441  {
442  continue;
443  }
444  if(!$access_handler->checkAccessOfUser($tree, $user_id, 'read', '', $a_blog_node_id))
445  {
446  continue;
447  }
448  }
449  // repository
450  else
451  {
452  if($admin_only &&
453  !$ilAccess->checkAccessOfUser($user_id, 'write', '', $a_blog_node_id))
454  {
455  continue;
456  }
457  if(!$ilAccess->checkAccessOfUser($user_id, 'read', '', $a_blog_node_id))
458  {
459  continue;
460  }
461  }
462 
463  // use language of recipient to compose message
464  $ulng = ilLanguageFactory::_getLanguageOfUser($user_id);
465  $ulng->loadLanguageModule('blog');
466 
467  $subject = sprintf($ulng->txt('blog_change_notification_subject'), $blog_title);
468  $message = sprintf($ulng->txt('blog_change_notification_salutation'), ilObjUser::_lookupFullname($user_id))."\n\n";
469 
470  $message .= $ulng->txt('blog_change_notification_body_'.$a_action).":\n\n";
471  $message .= $ulng->txt('obj_blog').": ".$blog_title."\n";
472  $message .= $ulng->txt('blog_posting').": ".$posting_title."\n";
473  $message .= $ulng->txt('blog_changed_by').": ".ilUserUtil::getNamePresentation($ilUser->getId())."\n";
474  if($a_comment)
475  {
476  $message .= "\n".$ulng->txt('comment').":\n\"".trim($a_comment)."\"\n";
477  }
478  $message .= "\n".$ulng->txt('blog_change_notification_link').": ".$link;
479 
480  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
481  $mail_obj->appendInstallationSignature(true);
482  $mail_obj->sendMail(ilObjUser::_lookupLogin($user_id),
483  "", "", $subject, $message, array(), array("system"));
484 
485  $notified[] = $user_id;
486  }
487 
488  if(sizeof($notified))
489  {
490  // #14387
491  ilNotification::updateNotificationTime(ilNotification::TYPE_BLOG, $blog_obj_id, $notified, $a_posting_id);
492  }
493  }
494 
500  static function deliverRSS($a_wsp_id)
501  {
502  global $tpl, $ilSetting;
503 
504  if(!$ilSetting->get('enable_global_profiles'))
505  {
506  return;
507  }
508 
509  // #10827
510  if(substr($a_wsp_id, -4) != "_cll")
511  {
512  include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
513  $wsp_id = new ilWorkspaceTree(0);
514  $obj_id = $wsp_id->lookupObjectId($a_wsp_id);
515  $is_wsp = "_wsp";
516  }
517  else
518  {
519  $a_wsp_id = substr($a_wsp_id, 0, -4);
520  $obj_id = ilObject::_lookupObjId($a_wsp_id);
521  $is_wsp = null;
522  }
523  if(!$obj_id)
524  {
525  return;
526  }
527 
528  $blog = new self($obj_id, false);
529  if(!$blog->hasRSS())
530  {
531  return;
532  }
533 
534  include_once "Services/Feeds/classes/class.ilFeedWriter.php";
535  $feed = new ilFeedWriter();
536 
537  include_once "Services/Link/classes/class.ilLink.php";
538  $url = ilLink::_getStaticLink($a_wsp_id, "blog", true, $is_wsp);
539  $url = str_replace("&", "&amp;", $url);
540 
541  // #11870
542  $feed->setChannelTitle(str_replace("&", "&amp;", $blog->getTitle()));
543  $feed->setChannelDescription(str_replace("&", "&amp;", $blog->getDescription()));
544  $feed->setChannelLink($url);
545 
546  // needed for blogpostinggui / pagegui
547  $tpl = new ilTemplate("tpl.main.html", true, true);
548 
549  include_once("./Modules/Blog/classes/class.ilBlogPosting.php");
550  include_once("./Modules/Blog/classes/class.ilBlogPostingGUI.php");
551  foreach(ilBlogPosting::getAllPostings($obj_id) as $item)
552  {
553  $id = $item["id"];
554 
555  // only published items
556  $is_active = ilBlogPosting::_lookupActive($id, "blp");
557  if(!$is_active)
558  {
559  continue;
560  }
561 
562  $snippet = strip_tags(ilBlogPostingGUI::getSnippet($id));
563  $snippet = str_replace("&", "&amp;", $snippet);
564 
565  $url = ilLink::_getStaticLink($a_wsp_id, "blog", true, "_".$id.$is_wsp);
566  $url = str_replace("&", "&amp;", $url);
567 
568  $feed_item = new ilFeedItem();
569  $feed_item->setTitle($item["title"]);
570  $feed_item->setDate($item["created"]->get(IL_CAL_DATETIME));
571  $feed_item->setDescription($snippet);
572  $feed_item->setLink($url);
573  $feed_item->setAbout($url);
574  $feed->addItem($feed_item);
575  }
576 
577  $feed->showFeed();
578  exit();
579  }
580 
587  function getParentContainerId($a_node_id)
588  {
589  global $tree;
590 
591  $crs_id = $tree->checkForParentType($a_node_id, "crs");
592  if($crs_id)
593  {
594  return $crs_id;
595  }
596 
597  $grp_id = $tree->checkForParentType($a_node_id, "grp");
598  if($grp_id)
599  {
600  return $grp_id;
601  }
602  }
603 
610  function getParentMemberIds($a_node_id)
611  {
612  $container_id = $this->getParentContainerId($a_node_id);
613  if($container_id)
614  {
615  $members = null;
616 
617  if(ilObject::_lookupType($container_id) == "crs")
618  {
619  include_once "Modules/Course/classes/class.ilCourseParticipants.php";
620  $members = new ilCourseParticipants(ilObject::_lookupObjId($container_id));
621  }
622  else
623  {
624  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
625  $members = new ilGroupParticipants(ilObject::_lookupObjId($container_id));
626  }
627 
628  // :TODO: review limit, members vs. participants
629  if($members && $members->getCountParticipants() < 100)
630  {
631  return $members->getParticipants();
632  }
633  }
634  }
635 
636  function initDefaultRoles()
637  {
638  global $rbacadmin, $rbacreview, $ilDB;
639 
640  // SET PERMISSION TEMPLATE OF NEW LOCAL CONTRIBUTOR ROLE
641  $set = $ilDB->query("SELECT obj_id FROM object_data ".
642  " WHERE type=".$ilDB->quote("rolt", "text").
643  " AND title=".$ilDB->quote("il_blog_contributor", "text"));
644  $res = $ilDB->fetchAssoc($set);
645  if($res["obj_id"])
646  {
647  $rolf_obj = $this->createRoleFolder();
648 
649  // CREATE ADMIN ROLE
650  $role_obj = $rolf_obj->createRole("il_blog_contributor_".$this->getRefId(),
651  "Contributor of blog obj_no.".$this->getId());
652 
653  $rbacadmin->copyRoleTemplatePermissions($res["obj_id"], ROLE_FOLDER_ID,
654  $rolf_obj->getRefId(), $role_obj->getId());
655 
656  // SET OBJECT PERMISSIONS OF BLOG OBJECT
657  $ops = $rbacreview->getOperationsOfRole($role_obj->getId(), "blog", $rolf_obj->getRefId());
658  $rbacadmin->grantPermission($role_obj->getId(), $ops, $this->getRefId());
659 
660  return true;
661  }
662 
663  return false;
664  }
665 
672  function getLocalContributorRole($a_node_id)
673  {
674  global $rbacreview;
675 
676  foreach($rbacreview->getLocalRoles($a_node_id) as $role_id)
677  {
678  if(substr(ilObject::_lookupTitle($role_id), 0, 19) == "il_blog_contributor")
679  {
680  return $role_id;
681  }
682  }
683  }
684 
685  function getRolesWithContribute($a_node_id)
686  {
687  global $rbacreview;
688 
689  include_once "Services/AccessControl/classes/class.ilObjRole.php";
690 
691  $contr_op_id = ilRbacReview::_getOperationIdByName("contribute");
692  $contr_role_id = $this->getLocalContributorRole($a_node_id);
693 
694  $res = array();
695  foreach($rbacreview->getParentRoleIds($a_node_id) as $role_id => $role)
696  {
697  if($role_id != $contr_role_id &&
698  in_array($contr_op_id, $rbacreview->getActiveOperationsOfRole($a_node_id, $role_id)))
699  {
700  $res[$role_id] = ilObjRole:: _getTranslation($role["title"]);
701  }
702  }
703 
704  return $res;
705  }
706 }
707 
708 ?>