ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBlogPosting.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/COPage/classes/class.ilPageObject.php");
5 
15 {
16  protected $title; // [string]
17  protected $created; // [ilDateTime]
18  protected $blog_node_id; // [int]
19  protected $blog_node_is_wsp; // [bool]
20  protected $author; // [int]
21  protected $approved; // [bool]
22 
28  public function getParentType()
29  {
30  return "blp";
31  }
32 
33 
39  public function setTitle($a_title)
40  {
41  $this->title = $a_title;
42  }
43 
49  public function getTitle()
50  {
51  return $this->title;
52  }
53 
59  public function setBlogId($a_id)
60  {
61  $this->setParentId($a_id);
62  }
63 
69  public function getBlogId()
70  {
71  return $this->getParentId();
72  }
73 
79  public function setCreated(ilDateTime $a_date)
80  {
81  $this->created = $a_date;
82  }
83 
89  public function getCreated()
90  {
91  return $this->created;
92  }
93 
99  public function setAuthor($a_id)
100  {
101  $this->author = (int) $a_id;
102  }
103 
109  public function getAuthor()
110  {
111  return $this->author;
112  }
113 
117  public function setApproved($a_status)
118  {
119  $this->approved = (bool) $a_status;
120  }
121 
127  public function isApproved()
128  {
129  return (bool) $this->approved;
130  }
131 
135  public function create($a_import = false)
136  {
137  $ilDB = $this->db;
138 
139  $id = $ilDB->nextId("il_blog_posting");
140  $this->setId($id);
141 
142  if (!$a_import) {
143  $created = ilUtil::now();
144  } else {
145  $created = $this->getCreated()->get(IL_CAL_DATETIME);
146  }
147 
148  // we are using a separate creation date to enable sorting without JOINs
149 
150  $query = "INSERT INTO il_blog_posting (id, title, blog_id, created, author, approved)" .
151  " VALUES (" .
152  $ilDB->quote($this->getId(), "integer") . "," .
153  $ilDB->quote($this->getTitle(), "text") . "," .
154  $ilDB->quote($this->getBlogId(), "integer") . "," .
155  $ilDB->quote($created, "timestamp") . "," .
156  $ilDB->quote($this->getAuthor(), "integer") . "," .
157  $ilDB->quote($this->isApproved(), "integer") . ")"; // #16526 - import
158  $ilDB->manipulate($query);
159 
160  if (!$a_import) {
161  parent::create();
162  // $this->saveInternalLinks($this->getXMLContent());
163  }
164  }
165 
175  public function update($a_validate = true, $a_no_history = false, $a_notify = true, $a_notify_action = "update")
176  {
177  $ilDB = $this->db;
178 
179  // blog_id, author and created cannot be changed
180 
181  $query = "UPDATE il_blog_posting SET" .
182  " title = " . $ilDB->quote($this->getTitle(), "text") .
183  ",created = " . $ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "text") .
184  ",approved =" . $ilDB->quote($this->isApproved(), "integer") .
185  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
186  $ilDB->manipulate($query);
187 
188  parent::update($a_validate, $a_no_history);
189 
190  if ($a_notify && $this->getActive()) {
191  include_once "Modules/Blog/classes/class.ilObjBlog.php";
192  ilObjBlog::sendNotification($a_notify_action, $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
193  }
194 
195  return true;
196  }
197 
201  public function read()
202  {
203  $ilDB = $this->db;
204 
205  $query = "SELECT * FROM il_blog_posting" .
206  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
207  $set = $ilDB->query($query);
208  $rec = $ilDB->fetchAssoc($set);
209 
210  $this->setTitle($rec["title"]);
211  $this->setBlogId($rec["blog_id"]);
212  $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
213  $this->setAuthor($rec["author"]);
214  if ((bool) $rec["approved"]) {
215  $this->setApproved(true);
216  }
217 
218  // when posting is deactivated it should loose the approval
219  $this->addUpdateListener($this, "checkApproval");
220 
221  parent::read();
222  }
223 
224  public function checkApproval()
225  {
226  if (!$this->getActive() && $this->isApproved()) {
227  $this->approved = false;
228  $this->update();
229  }
230  }
231 
237  public function delete()
238  {
239  $ilDB = $this->db;
240 
241  include_once("./Services/News/classes/class.ilNewsItem.php");
243  $this->getBlogId(),
244  "blog",
245  $this->getId(),
246  $this->getParentType()
247  );
248 
249  $query = "DELETE FROM il_blog_posting" .
250  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
251  $ilDB->manipulate($query);
252 
253  parent::delete();
254 
255  return true;
256  }
257 
261  public function unpublish()
262  {
263  $this->setApproved(false);
264  $this->setActive(false);
265  $this->update(true, false, false);
266 
267  include_once("./Services/News/classes/class.ilNewsItem.php");
269  $this->getBlogId(),
270  "blog",
271  $this->getId(),
272  $this->getParentType()
273  );
274  }
275 
276 
282  public static function deleteAllBlogPostings($a_blog_id)
283  {
284  global $DIC;
285 
286  $ilDB = $DIC->database();
287 
288  include_once 'Services/MetaData/classes/class.ilMD.php';
289 
290  $query = "SELECT * FROM il_blog_posting" .
291  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer");
292  $set = $ilDB->query($query);
293  while ($rec = $ilDB->fetchAssoc($set)) {
294  // delete all md keywords
295  $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
296  if (is_object($md_section = $md_obj->getGeneral())) {
297  foreach ($md_section->getKeywordIds() as $id) {
298  $md_key = $md_section->getKeyword($id);
299  $md_key->delete();
300  }
301  }
302 
303  $post = new ilBlogPosting($rec["id"]);
304  $post->delete();
305  }
306  }
307 
314  public static function lookupBlogId($a_posting_id)
315  {
316  global $DIC;
317 
318  $ilDB = $DIC->database();
319 
320  $query = "SELECT blog_id FROM il_blog_posting" .
321  " WHERE id = " . $ilDB->quote($a_posting_id, "integer");
322  $set = $ilDB->query($query);
323  if ($rec = $ilDB->fetchAssoc($set)) {
324  return $rec["blog_id"];
325  }
326  return false;
327  }
328 
337  public static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
338  {
339  global $DIC;
340 
341  $ilDB = $DIC->database();
342 
343  $pages = parent::getAllPages("blp", $a_blog_id);
344 
345  if ($a_limit) {
346  $ilDB->setLimit($a_limit, $a_offset);
347  }
348 
349  $query = "SELECT * FROM il_blog_posting" .
350  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
351  " ORDER BY created DESC";
352  $set = $ilDB->query($query);
353  $post = array();
354  while ($rec = $ilDB->fetchAssoc($set)) {
355  if (isset($pages[$rec["id"]])) {
356  $post[$rec["id"]] = $pages[$rec["id"]];
357  $post[$rec["id"]]["title"] = $rec["title"];
358  $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
359  $post[$rec["id"]]["author"] = $rec["author"];
360  $post[$rec["id"]]["approved"] = (bool) $rec["approved"];
361 
362  foreach (self::getPageContributors("blp", $rec["id"]) as $editor) {
363  if ($editor["user_id"] != $rec["author"]) {
364  $post[$rec["id"]]["editors"][] = $editor["user_id"];
365  }
366  }
367  }
368  }
369 
370  return $post;
371  }
372 
380  public static function exists($a_blog_id, $a_posting_id)
381  {
382  global $DIC;
383 
384  $ilDB = $DIC->database();
385 
386  $query = "SELECT id FROM il_blog_posting" .
387  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
388  " AND id = " . $ilDB->quote($a_posting_id, "integer");
389  $set = $ilDB->query($query);
390  if ($rec = $ilDB->fetchAssoc($set)) {
391  return true;
392  }
393  return false;
394  }
395 
402  public static function getLastPost($a_blog_id)
403  {
404  $data = self::getAllPostings($a_blog_id, 1);
405  if ($data) {
406  return array_pop(array_keys($data));
407  }
408  }
409 
416  public function setBlogNodeId($a_id, $a_is_in_workspace = false)
417  {
418  $this->blog_node_id = (int) $a_id;
419  $this->blog_node_is_wsp = (bool) $a_is_in_workspace;
420  }
421 
428  public static function searchBlogsByAuthor($a_user_id)
429  {
430  global $DIC;
431 
432  $ilDB = $DIC->database();
433 
434  $ids = array();
435 
436  $sql = "SELECT DISTINCT(blog_id)" .
437  " FROM il_blog_posting" .
438  " WHERE author = " . $ilDB->quote($a_user_id);
439  $set = $ilDB->query($sql);
440  while ($row = $ilDB->fetchAssoc($set)) {
441  $ids[] = $row["blog_id"];
442  }
443  return $ids;
444  }
445 
446  public function getNotificationAbstract()
447  {
448  include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
449  $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
450 
451  // making things more readable
452  $snippet = str_replace('<br/>', "\n", $snippet);
453  $snippet = str_replace('<br />', "\n", $snippet);
454  $snippet = str_replace('</p>', "\n", $snippet);
455  $snippet = str_replace('</div>', "\n", $snippet);
456 
457  return trim(strip_tags($snippet));
458  }
459 
460 
461  // keywords
462 
463  public function getMDSection()
464  {
465  // general section available?
466  include_once 'Services/MetaData/classes/class.ilMD.php';
467  $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
468  if (!is_object($md_section = $md_obj->getGeneral())) {
469  $md_section = $md_obj->addGeneral();
470  $md_section->save();
471  }
472 
473  return $md_section;
474  }
475 
476  public function updateKeywords(array $keywords)
477  {
479 
480  // language is not "used" anywhere
481  $ulang = $ilUser->getLanguage();
482  $keywords = array($ulang => $keywords);
483 
484  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
485  ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
486  }
487 
488  public static function getKeywords($a_obj_id, $a_posting_id)
489  {
490  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
491  return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
492  }
493 
499  public function handleNews($a_update = false)
500  {
501  $lng = $this->lng;
503 
504  // see ilWikiPage::updateNews()
505 
506  if (!$this->getActive()) {
507  return;
508  }
509 
510  include_once("./Services/News/classes/class.ilNewsItem.php");
511  $news_item = null;
512 
513  // try to re-use existing news item
514  if ((bool) $a_update) {
515  // get last news item of the day (if existing)
517  $this->getBlogId(),
518  "blog",
519  $this->getId(),
520  $this->getParentType(),
521  true
522  );
523  if ($news_id > 0) {
524  $news_item = new ilNewsItem($news_id);
525  }
526  }
527 
528  // create new news item
529  if (!$news_item) {
530  $news_set = new ilSetting("news");
531  $default_visibility = $news_set->get("default_visibility", "users");
532 
533  $news_item = new ilNewsItem();
534  $news_item->setContext(
535  $this->getBlogId(),
536  "blog",
537  $this->getId(),
538  $this->getParentType()
539  );
540  $news_item->setPriority(NEWS_NOTICE);
541  $news_item->setVisibility($default_visibility);
542  }
543 
544  // news author
545  $news_item->setUserId($ilUser->getId());
546 
547 
548  // news title/content
549 
550  $news_item->setTitle($this->getTitle());
551 
552  $content = $a_update
553  ? "blog_news_posting_updated"
554  : "blog_news_posting_published";
555 
556  // news "author"
557  include_once "Services/User/classes/class.ilUserUtil.php";
558  $content = sprintf($lng->txt($content), ilUserUtil::getNamePresentation($ilUser->getId()));
559 
560  // posting author[s]
561  $contributors = array();
562  foreach (self::getPageContributors($this->getParentType(), $this->getId()) as $user) {
563  $contributors[] = $user["user_id"];
564  }
565  if (sizeof($contributors) > 1 || !in_array($this->getAuthor(), $contributors)) {
566  // original author should come first?
567  $authors = array(ilUserUtil::getNamePresentation($this->getAuthor()));
568  foreach ($contributors as $user_id) {
569  if ($user_id != $this->getAuthor()) {
570  $authors[] = ilUserUtil::getNamePresentation($user_id);
571  }
572  }
573  $content .= "\n" . sprintf($lng->txt("blog_news_posting_authors"), implode(", ", $authors));
574  }
575 
576  $news_item->setContentTextIsLangVar(false);
577  $news_item->setContent($content);
578 
579  include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
580  $snippet = ilBlogPostingGUI::getSnippet($this->getId());
581  $news_item->setContentLong($snippet);
582 
583  if (!$news_item->getId()) {
584  $news_item->create();
585  } else {
586  $news_item->update(true);
587  }
588  }
589 
597  protected static function lookup($a_field, $a_posting_id)
598  {
599  global $DIC;
600 
601  $db = $DIC->database();
602 
603  $set = $db->query("SELECT $a_field FROM il_blog_posting " .
604  " WHERE id = " . $db->quote($a_posting_id, "integer"));
605  $rec = $db->fetchAssoc($set);
606 
607  return $rec[$a_field];
608  }
609 
616  public static function lookupTitle($a_posting_id)
617  {
618  $t = self::lookup("title", $a_posting_id);
619  return $t;
620  }
621 }
getBlogId()
Get blog object id.
static lookupBlogId($a_posting_id)
Lookup blog id.
Class ilBlogPosting.
const IL_CAL_DATETIME
setCreated(ilDateTime $a_date)
Set creation date.
static getLastPost($a_blog_id)
Get newest posting for blog.
global $DIC
Definition: saml.php:7
setActive($a_active)
set activation
create($a_import=false)
Create new blog posting.
setAuthor($a_id)
Set author user id.
static getKeywords($a_obj_id, $a_posting_id)
handleNews($a_update=false)
Handle news item.
isApproved()
Get approved status.
static now()
Return current timestamp in Y-m-d H:i:s format.
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
Update keywords from input array.
static getAllPostings($a_blog_id, $a_limit=1000, $a_offset=0)
Get all postings of blog.
static searchBlogsByAuthor($a_user_id)
Get all blogs where user has postings.
updateKeywords(array $keywords)
setApproved($a_status)
Toggle approval status.
addUpdateListener(&$a_object, $a_method, $a_parameters="")
static exists($a_blog_id, $a_posting_id)
Checks whether a posting exists.
static getLastNewsIdForContext( $a_context_obj_id, $a_context_obj_type, $a_context_sub_obj_id="", $a_context_sub_obj_type="", $a_only_today=false)
Get last news id of news set related to a certain context.
getActive($a_check_scheduled_activation=false)
get activation
static getSnippet($a_id, $a_truncate=false, $a_truncate_length=500, $a_truncate_sign="...", $a_include_picture=false, $a_picture_width=144, $a_picture_height=144, $a_export_directory=null)
Get first text paragraph of page.
const NEWS_NOTICE
Class ilPageObject.
getCreated()
Get creation date.
$post
Definition: post.php:34
Date and time handling
$ilUser
Definition: imgupload.php:18
$editor
setBlogNodeId($a_id, $a_is_in_workspace=false)
Set blog node id (needed for notification)
$query
static lookup($a_field, $a_posting_id)
Lookup posting property.
static getNamePresentation( $a_user_id, $a_user_image=false, $a_profile_link=false, $a_profile_back_link="", $a_force_first_lastname=false, $a_omit_login=false, $a_sortable=true, $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
setId($a_id)
set id
getAuthor()
Get author user id.
$row
update($pash, $contents, Config $config)
getParentType()
Get parent type.
static deleteNewsOfContext( $a_context_obj_id, $a_context_obj_type, $a_context_sub_obj_id=0, $a_context_sub_obj_type="")
Delete all news of a context.
global $ilDB
static lookupTitle($a_posting_id)
Lookup title.
getTitle()
Get title.
read()
Read blog posting.
update($a_validate=true, $a_no_history=false, $a_notify=true, $a_notify_action="update")
Update blog posting.
setTitle($a_title)
Set title.
unpublish()
Unpublish.
static sendNotification($a_action, $a_in_wsp, $a_blog_node_id, $a_posting_id, $a_comment=null)
static lookupKeywords($a_rbac_id, $a_obj_id, $a_return_ids=false)
Lookup Keywords.
static deleteAllBlogPostings($a_blog_id)
Delete all postings for blog.
$data
Definition: bench.php:6
setBlogId($a_id)
Set blog object id.