ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBlogPosting.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
28  protected string $title = "";
29  protected ?ilDateTime $created = null;
30  protected int $blog_node_id = 0;
31  protected bool $blog_node_is_wsp = false;
32  protected int $author = 0;
33  protected bool $approved = false;
34  protected ?ilDateTime $withdrawn = null;
35 
36  public function getParentType(): string
37  {
38  return "blp";
39  }
40 
41  public function setTitle(string $a_title): void
42  {
43  $this->title = $a_title;
44  }
45 
46  public function getTitle(): string
47  {
48  return $this->title;
49  }
50 
51  public function setBlogId(int $a_id): void
52  {
53  $this->setParentId($a_id);
54  }
55 
56  public function getBlogId(): int
57  {
58  return $this->getParentId();
59  }
60 
61  public function setCreated(ilDateTime $a_date): void
62  {
63  $this->created = $a_date;
64  }
65 
66  public function getCreated(): ilDateTime
67  {
68  return $this->created;
69  }
70 
71  public function setAuthor(int $a_id): void
72  {
73  $this->author = $a_id;
74  }
75 
76  public function getAuthor(): int
77  {
78  return $this->author;
79  }
80 
81  public function setApproved(bool $a_status): void
82  {
83  $this->approved = $a_status;
84  }
85 
86  public function isApproved(): bool
87  {
88  return $this->approved;
89  }
90 
94  public function setWithdrawn(
95  ilDateTime $a_date
96  ): void {
97  $this->withdrawn = $a_date;
98  }
99 
103  public function getWithdrawn(): ?ilDateTime
104  {
105  return $this->withdrawn;
106  }
107 
111  public function create(
112  bool $a_import = false
113  ): void {
114  $ilDB = $this->db;
115 
116  $id = $ilDB->nextId("il_blog_posting");
117  $this->setId($id);
118 
119  if (!$a_import) {
120  $created = ilUtil::now();
121  } else {
122  $created = $this->getCreated()->get(IL_CAL_DATETIME);
123  }
124 
125  // we are using a separate creation date to enable sorting without JOINs
126  $withdrawn = $this->getWithdrawn()?->get(IL_CAL_DATETIME);
127  $query = "INSERT INTO il_blog_posting (id, title, blog_id, created, author, approved, last_withdrawn)" .
128  " VALUES (" .
129  $ilDB->quote($this->getId(), "integer") . "," .
130  $ilDB->quote($this->getTitle(), "text") . "," .
131  $ilDB->quote($this->getBlogId(), "integer") . "," .
132  $ilDB->quote($created, "timestamp") . "," .
133  $ilDB->quote($this->getAuthor(), "integer") . "," .
134  $ilDB->quote($this->isApproved(), "integer") . "," . // #16526 - import
135  $ilDB->quote($withdrawn, "timestamp") . ")";
136  $ilDB->manipulate($query);
137 
138  if (!$a_import) {
139  parent::create($a_import);
140  // $this->saveInternalLinks($this->getXMLContent());
141  }
142  }
143 
144  public function update(
145  bool $a_validate = true,
146  bool $a_no_history = false,
147  bool $a_notify = true,
148  string $a_notify_action = "update"
149  ) {
150  $ilDB = $this->db;
151 
152  // blog_id, author and created cannot be changed
153  $withdrawn = $this->getWithdrawn()?->get(IL_CAL_DATETIME);
154  $query = "UPDATE il_blog_posting SET" .
155  " title = " . $ilDB->quote($this->getTitle(), "text") .
156  ",created = " . $ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "timestamp") .
157  ",approved =" . $ilDB->quote($this->isApproved(), "integer") .
158  ",last_withdrawn =" . $ilDB->quote($withdrawn, "timestamp") .
159  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
160  $ilDB->manipulate($query);
161 
162  $ret = parent::update($a_validate, $a_no_history);
163 
164  if ($a_notify && $this->getActive()) {
165  ilObjBlog::sendNotification($a_notify_action, $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
166  }
167 
168  return $ret;
169  }
170 
174  public function read(): void
175  {
176  $ilDB = $this->db;
177 
178  $query = "SELECT * FROM il_blog_posting" .
179  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
180  $set = $ilDB->query($query);
181  $rec = $ilDB->fetchAssoc($set);
182 
183  $this->setTitle($rec["title"]);
184  $this->setBlogId($rec["blog_id"]);
185  $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
186  $this->setAuthor($rec["author"]);
187  if ($rec["approved"]) {
188  $this->setApproved(true);
189  }
190  $this->setWithdrawn(new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME));
191 
192  // when posting is deactivated it should loose the approval
193  $this->addUpdateListener($this, "checkApproval");
194 
195  parent::read();
196  }
197 
198  public function checkApproval(): void
199  {
200  if (!$this->getActive() && $this->isApproved()) {
201  $this->approved = false;
202  $this->update();
203  }
204  }
205 
209  public function delete(): void
210  {
211  $ilDB = $this->db;
212 
214  $this->getBlogId(),
215  "blog",
216  $this->getId(),
217  $this->getParentType()
218  );
219 
220  $query = "DELETE FROM il_blog_posting" .
221  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
222  $ilDB->manipulate($query);
223 
224  parent::delete();
225  }
226 
230  public function unpublish(): void
231  {
232  $this->setApproved(false);
233  $this->setActive(false);
235  $this->update(true, false, false);
236 
238  $this->getBlogId(),
239  "blog",
240  $this->getId(),
241  $this->getParentType()
242  );
243  }
244 
245 
249  public static function deleteAllBlogPostings(
250  int $a_blog_id
251  ): void {
252  global $DIC;
253 
254  $ilDB = $DIC->database();
255 
256  $query = "SELECT * FROM il_blog_posting" .
257  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer");
258  $set = $ilDB->query($query);
259  while ($rec = $ilDB->fetchAssoc($set)) {
260  // delete all md keywords
261  $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
262  if (is_object($md_section = $md_obj->getGeneral())) {
263  foreach ($md_section->getKeywordIds() as $id) {
264  $md_key = $md_section->getKeyword($id);
265  $md_key->delete();
266  }
267  }
268 
269  $post = new ilBlogPosting($rec["id"]);
270  $post->delete();
271  }
272  }
273 
274  public static function lookupBlogId(
275  int $a_posting_id
276  ): ?int {
277  global $DIC;
278 
279  $ilDB = $DIC->database();
280 
281  $query = "SELECT blog_id FROM il_blog_posting" .
282  " WHERE id = " . $ilDB->quote($a_posting_id, "integer");
283  $set = $ilDB->query($query);
284  if ($rec = $ilDB->fetchAssoc($set)) {
285  return (int) $rec["blog_id"];
286  }
287  return null;
288  }
289 
293  public static function getAllPostings(
294  int $a_blog_id,
295  int $a_limit = 1000,
296  int $a_offset = 0
297  ): array {
298  global $DIC;
299 
300  $ilDB = $DIC->database();
301 
302  $pages = parent::getAllPages("blp", $a_blog_id);
303 
304  if ($a_limit) {
305  $ilDB->setLimit($a_limit, $a_offset);
306  }
307 
308  $query = "SELECT * FROM il_blog_posting" .
309  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
310  " ORDER BY created DESC";
311  $set = $ilDB->query($query);
312  $post = array();
313  while ($rec = $ilDB->fetchAssoc($set)) {
314  if (isset($pages[$rec["id"]])) {
315  $post[$rec["id"]] = $pages[$rec["id"]];
316  $post[$rec["id"]]["title"] = $rec["title"];
317  $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
318  $post[$rec["id"]]["author"] = $rec["author"];
319  $post[$rec["id"]]["approved"] = (bool) $rec["approved"];
320  $post[$rec["id"]]["last_withdrawn"] = new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME);
321 
322  foreach (self::getPageContributors("blp", $rec["id"]) as $editor) {
323  if ($editor["user_id"] != $rec["author"]) {
324  $post[$rec["id"]]["editors"][] = $editor["user_id"];
325  }
326  }
327  }
328  }
329 
330  return $post;
331  }
332 
336  public static function exists(
337  int $a_blog_id,
338  int $a_posting_id
339  ): bool {
340  global $DIC;
341 
342  $ilDB = $DIC->database();
343 
344  $query = "SELECT id FROM il_blog_posting" .
345  " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
346  " AND id = " . $ilDB->quote($a_posting_id, "integer");
347  $set = $ilDB->query($query);
348  if ($rec = $ilDB->fetchAssoc($set)) {
349  return true;
350  }
351  return false;
352  }
353 
357  public static function getLastPost(
358  int $a_blog_id
359  ): int {
360  $data = self::getAllPostings($a_blog_id, 1);
361  if ($data) {
362  $keys = array_keys($data);
363  return end($keys);
364  }
365  return 0;
366  }
367 
371  public function setBlogNodeId(
372  int $a_id,
373  bool $a_is_in_workspace = false
374  ): void {
375  $this->blog_node_id = $a_id;
376  $this->blog_node_is_wsp = $a_is_in_workspace;
377  }
378 
382  public static function searchBlogsByAuthor(
383  int $a_user_id
384  ): array {
385  global $DIC;
386 
387  $ilDB = $DIC->database();
388 
389  $ids = array();
390 
391  $sql = "SELECT DISTINCT(blog_id)" .
392  " FROM il_blog_posting" .
393  " WHERE author = " . $ilDB->quote($a_user_id);
394  $set = $ilDB->query($sql);
395  while ($row = $ilDB->fetchAssoc($set)) {
396  $ids[] = (int) $row["blog_id"];
397  }
398  return $ids;
399  }
400 
401  public function getNotificationAbstract(): string
402  {
403  $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
404 
405  // making things more readable
406  $snippet = str_replace(array('<br/>', '<br />', '</p>', '</div>'), "\n", $snippet);
407 
408  return trim(strip_tags($snippet));
409  }
410 
411 
412  // keywords
413 
414  public function getMDSection(): ?ilMDGeneral
415  {
416  // general section available?
417  $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
418  if (!is_object($md_section = $md_obj->getGeneral())) {
419  $md_section = $md_obj->addGeneral();
420  $md_section->save();
421  return $md_section;
422  }
423  return $md_section;
424  }
425 
426  public function updateKeywords(
427  array $keywords
428  ): void {
429  $ilUser = $this->user;
430 
431  // language is not "used" anywhere
432  $ulang = $ilUser->getLanguage();
433  $keywords = array($ulang => $keywords);
434 
435  ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
436  }
437 
438  public static function getKeywords(
439  int $a_obj_id,
440  int $a_posting_id
441  ): array {
442  return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
443  }
444 
448  public function handleNews(
449  bool $a_update = false
450  ): void {
451  $lng = $this->lng;
452  $ilUser = $this->user;
453 
454  // see ilWikiPage::updateNews()
455 
456  if (!$this->getActive()) {
457  return;
458  }
459 
460  $news_item = null;
461 
462  // try to re-use existing news item
463  if ($a_update) {
464  // get last news item of the day (if existing)
466  $this->getBlogId(),
467  "blog",
468  $this->getId(),
469  $this->getParentType(),
470  true
471  );
472  if ($news_id > 0) {
473  $news_item = new ilNewsItem($news_id);
474  }
475  }
476 
477  // create new news item
478  if (!$news_item) {
479  $news_set = new ilSetting("news");
480  $default_visibility = $news_set->get("default_visibility", "users");
481 
482  $news_item = new ilNewsItem();
483  $news_item->setContext(
484  $this->getBlogId(),
485  "blog",
486  $this->getId(),
487  $this->getParentType()
488  );
489  $news_item->setPriority(NEWS_NOTICE);
490  $news_item->setVisibility($default_visibility);
491  }
492 
493  // news author
494  $news_item->setUserId($ilUser->getId());
495 
496 
497  // news title/content
498 
499  $news_item->setTitle($this->getTitle());
500 
501  $content = $a_update
502  ? "blog_news_posting_updated"
503  : "blog_news_posting_published";
504 
505  // news "author"
506  $content = sprintf($lng->txt($content), ilUserUtil::getNamePresentation($ilUser->getId()));
507 
508  // posting author[s]
509  $contributors = array();
510  foreach (self::getPageContributors($this->getParentType(), $this->getId()) as $user) {
511  $contributors[] = $user["user_id"];
512  }
513  if (count($contributors) > 1 || !in_array($this->getAuthor(), $contributors)) {
514  // original author should come first?
515  $authors = array(ilUserUtil::getNamePresentation($this->getAuthor()));
516  foreach ($contributors as $user_id) {
517  if ($user_id != $this->getAuthor()) {
518  $authors[] = ilUserUtil::getNamePresentation($user_id);
519  }
520  }
521  $content .= "\n" . sprintf($lng->txt("blog_news_posting_authors"), implode(", ", $authors));
522  }
523 
524  $news_item->setContentTextIsLangVar(false);
525  $news_item->setContent($content);
526 
527  $snippet = ilBlogPostingGUI::getSnippet($this->getId());
528  $news_item->setContentLong($snippet);
529 
530  if (!$news_item->getId()) {
531  $news_item->create();
532  } else {
533  $news_item->update(true);
534  }
535  }
536 
540  protected static function lookup(
541  string $a_field,
542  int $a_posting_id
543  ): ?string {
544  global $DIC;
545 
546  $db = $DIC->database();
547 
548  $set = $db->query("SELECT $a_field FROM il_blog_posting " .
549  " WHERE id = " . $db->quote($a_posting_id, "integer"));
550  $rec = $db->fetchAssoc($set);
551 
552  return $rec[$a_field] ?? null;
553  }
554 
555  public static function lookupTitle(int $a_posting_id): string
556  {
557  return (string) self::lookup("title", $a_posting_id);
558  }
559 }
getActive(bool $a_check_scheduled_activation=false)
static lookupTitle(int $a_posting_id)
static getLastPost(int $a_blog_id)
Get newest posting for blog.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_DATETIME
static deleteNewsOfContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Delete all news of a context.
static searchBlogsByAuthor(int $a_user_id)
Get all blogs where user has postings.
setParentId(int $a_id)
setCreated(ilDateTime $a_date)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static getLastNewsIdForContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="", bool $a_only_today=false)
Get last news id of news set related to a certain context.
static getAllPostings(int $a_blog_id, int $a_limit=1000, int $a_offset=0)
Get all postings of blog.
fetchAssoc(ilDBStatement $statement)
static exists(int $a_blog_id, int $a_posting_id)
Checks whether a posting exists.
update(bool $a_validate=true, bool $a_no_history=false, bool $a_notify=true, string $a_notify_action="update")
setTitle(string $a_title)
quote($value, string $type)
getWithdrawn()
Get last withdrawal date.
static now()
Return current timestamp in Y-m-d H:i:s format.
handleNews(bool $a_update=false)
Handle news item.
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
static deleteAllBlogPostings(int $a_blog_id)
Delete all postings for blog.
global $DIC
Definition: feed.php:28
static lookup(string $a_field, int $a_posting_id)
Lookup posting property.
setApproved(bool $a_status)
updateKeywords(array $keywords)
static sendNotification(string $a_action, bool $a_in_wsp, int $a_blog_node_id, int $a_posting_id, ?string $a_comment=null)
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
const NEWS_NOTICE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
static lookupKeywords(int $a_rbac_id, int $a_obj_id, bool $a_return_ids=false)
query(string $query)
Run a (read-only) Query on the database.
setBlogNodeId(int $a_id, bool $a_is_in_workspace=false)
Set blog node id (needed for notification)
static lookupBlogId(int $a_posting_id)
A news item can be created by different sources.
ilDBInterface $db
addUpdateListener(object $a_object, string $a_method, $a_parameters="")
create(bool $a_import=false)
Create new blog posting.
setWithdrawn(ilDateTime $a_date)
Set last withdrawal date.
static getSnippet(int $a_id, bool $a_truncate=false, int $a_truncate_length=500, string $a_truncate_sign="...", bool $a_include_picture=false, int $a_picture_width=144, int $a_picture_height=144, string $a_export_directory=null)
Get first text paragraph of page.
read()
Read blog posting.
static getKeywords(int $a_obj_id, int $a_posting_id)
unpublish()
Unpublish.
$post
Definition: ltitoken.php:49
setActive(bool $a_active)