ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilBlogPosting.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
11{
12 protected $title; // [string]
13 protected $created; // [ilDateTime]
14 protected $blog_node_id; // [int]
15 protected $blog_node_is_wsp; // [bool]
16 protected $author; // [int]
17 protected $approved; // [bool]
18 protected $withdrawn; // [ilDateTime]
19
25 public function getParentType()
26 {
27 return "blp";
28 }
29
30
36 public function setTitle($a_title)
37 {
38 $this->title = $a_title;
39 }
40
46 public function getTitle()
47 {
48 return $this->title;
49 }
50
56 public function setBlogId($a_id)
57 {
58 $this->setParentId($a_id);
59 }
60
66 public function getBlogId()
67 {
68 return $this->getParentId();
69 }
70
76 public function setCreated(ilDateTime $a_date)
77 {
78 $this->created = $a_date;
79 }
80
86 public function getCreated()
87 {
88 return $this->created;
89 }
90
96 public function setAuthor($a_id)
97 {
98 $this->author = (int) $a_id;
99 }
100
106 public function getAuthor()
107 {
108 return $this->author;
109 }
110
114 public function setApproved($a_status)
115 {
116 $this->approved = (bool) $a_status;
117 }
118
124 public function isApproved()
125 {
126 return (bool) $this->approved;
127 }
128
134 public function setWithdrawn(ilDateTime $a_date)
135 {
136 $this->withdrawn = $a_date;
137 }
138
144 public function getWithdrawn()
145 {
146 return $this->withdrawn;
147 }
148
152 public function create($a_import = false)
153 {
155
156 $id = $ilDB->nextId("il_blog_posting");
157 $this->setId($id);
158
159 if (!$a_import) {
161 } else {
162 $created = $this->getCreated()->get(IL_CAL_DATETIME);
163 }
164
165 // we are using a separate creation date to enable sorting without JOINs
166
167 $query = "INSERT INTO il_blog_posting (id, title, blog_id, created, author, approved, last_withdrawn)" .
168 " VALUES (" .
169 $ilDB->quote($this->getId(), "integer") . "," .
170 $ilDB->quote($this->getTitle(), "text") . "," .
171 $ilDB->quote($this->getBlogId(), "integer") . "," .
172 $ilDB->quote($created, "timestamp") . "," .
173 $ilDB->quote($this->getAuthor(), "integer") . "," .
174 $ilDB->quote($this->isApproved(), "integer") . "," . // #16526 - import
175 $ilDB->quote($this->getWithdrawn(), "timestamp") . ")";
176 $ilDB->manipulate($query);
177
178 if (!$a_import) {
179 parent::create();
180 // $this->saveInternalLinks($this->getXMLContent());
181 }
182 }
183
193 public function update($a_validate = true, $a_no_history = false, $a_notify = true, $a_notify_action = "update")
194 {
196
197 // blog_id, author and created cannot be changed
198
199 $query = "UPDATE il_blog_posting SET" .
200 " title = " . $ilDB->quote($this->getTitle(), "text") .
201 ",created = " . $ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "timestamp") .
202 ",approved =" . $ilDB->quote($this->isApproved(), "integer") .
203 ",last_withdrawn =" . $ilDB->quote($this->getWithdrawn()->get(IL_CAL_DATETIME), "timestamp") .
204 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
205 $ilDB->manipulate($query);
206
207 parent::update($a_validate, $a_no_history);
208
209 if ($a_notify && $this->getActive()) {
210 ilObjBlog::sendNotification($a_notify_action, $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
211 }
212
213 return true;
214 }
215
219 public function read()
220 {
222
223 $query = "SELECT * FROM il_blog_posting" .
224 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
225 $set = $ilDB->query($query);
226 $rec = $ilDB->fetchAssoc($set);
227
228 $this->setTitle($rec["title"]);
229 $this->setBlogId($rec["blog_id"]);
230 $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
231 $this->setAuthor($rec["author"]);
232 if ((bool) $rec["approved"]) {
233 $this->setApproved(true);
234 }
235 $this->setWithdrawn(new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME));
236
237 // when posting is deactivated it should loose the approval
238 $this->addUpdateListener($this, "checkApproval");
239
240 parent::read();
241 }
242
243 public function checkApproval()
244 {
245 if (!$this->getActive() && $this->isApproved()) {
246 $this->approved = false;
247 $this->update();
248 }
249 }
250
256 public function delete()
257 {
259
261 $this->getBlogId(),
262 "blog",
263 $this->getId(),
264 $this->getParentType()
265 );
266
267 $query = "DELETE FROM il_blog_posting" .
268 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
269 $ilDB->manipulate($query);
270
271 parent::delete();
272
273 return true;
274 }
275
279 public function unpublish()
280 {
281 $this->setApproved(false);
282 $this->setActive(false);
284 $this->update(true, false, false);
285
287 $this->getBlogId(),
288 "blog",
289 $this->getId(),
290 $this->getParentType()
291 );
292 }
293
294
300 public static function deleteAllBlogPostings($a_blog_id)
301 {
302 global $DIC;
303
304 $ilDB = $DIC->database();
305
306 $query = "SELECT * FROM il_blog_posting" .
307 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer");
308 $set = $ilDB->query($query);
309 while ($rec = $ilDB->fetchAssoc($set)) {
310 // delete all md keywords
311 $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
312 if (is_object($md_section = $md_obj->getGeneral())) {
313 foreach ($md_section->getKeywordIds() as $id) {
314 $md_key = $md_section->getKeyword($id);
315 $md_key->delete();
316 }
317 }
318
319 $post = new ilBlogPosting($rec["id"]);
320 $post->delete();
321 }
322 }
323
330 public static function lookupBlogId($a_posting_id)
331 {
332 global $DIC;
333
334 $ilDB = $DIC->database();
335
336 $query = "SELECT blog_id FROM il_blog_posting" .
337 " WHERE id = " . $ilDB->quote($a_posting_id, "integer");
338 $set = $ilDB->query($query);
339 if ($rec = $ilDB->fetchAssoc($set)) {
340 return $rec["blog_id"];
341 }
342 return false;
343 }
344
353 public static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
354 {
355 global $DIC;
356
357 $ilDB = $DIC->database();
358
359 $pages = parent::getAllPages("blp", $a_blog_id);
360
361 if ($a_limit) {
362 $ilDB->setLimit($a_limit, $a_offset);
363 }
364
365 $query = "SELECT * FROM il_blog_posting" .
366 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
367 " ORDER BY created DESC";
368 $set = $ilDB->query($query);
369 $post = array();
370 while ($rec = $ilDB->fetchAssoc($set)) {
371 if (isset($pages[$rec["id"]])) {
372 $post[$rec["id"]] = $pages[$rec["id"]];
373 $post[$rec["id"]]["title"] = $rec["title"];
374 $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
375 $post[$rec["id"]]["author"] = $rec["author"];
376 $post[$rec["id"]]["approved"] = (bool) $rec["approved"];
377 $post[$rec["id"]]["last_withdrawn"] = new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME);
378
379 foreach (self::getPageContributors("blp", $rec["id"]) as $editor) {
380 if ($editor["user_id"] != $rec["author"]) {
381 $post[$rec["id"]]["editors"][] = $editor["user_id"];
382 }
383 }
384 }
385 }
386
387 return $post;
388 }
389
397 public static function exists($a_blog_id, $a_posting_id)
398 {
399 global $DIC;
400
401 $ilDB = $DIC->database();
402
403 $query = "SELECT id FROM il_blog_posting" .
404 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
405 " AND id = " . $ilDB->quote($a_posting_id, "integer");
406 $set = $ilDB->query($query);
407 if ($rec = $ilDB->fetchAssoc($set)) {
408 return true;
409 }
410 return false;
411 }
412
419 public static function getLastPost($a_blog_id)
420 {
421 $data = self::getAllPostings($a_blog_id, 1);
422 if ($data) {
423 $keys = array_keys($data);
424 return array_pop($keys);
425 }
426 }
427
434 public function setBlogNodeId($a_id, $a_is_in_workspace = false)
435 {
436 $this->blog_node_id = (int) $a_id;
437 $this->blog_node_is_wsp = (bool) $a_is_in_workspace;
438 }
439
446 public static function searchBlogsByAuthor($a_user_id)
447 {
448 global $DIC;
449
450 $ilDB = $DIC->database();
451
452 $ids = array();
453
454 $sql = "SELECT DISTINCT(blog_id)" .
455 " FROM il_blog_posting" .
456 " WHERE author = " . $ilDB->quote($a_user_id);
457 $set = $ilDB->query($sql);
458 while ($row = $ilDB->fetchAssoc($set)) {
459 $ids[] = $row["blog_id"];
460 }
461 return $ids;
462 }
463
464 public function getNotificationAbstract()
465 {
466 $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
467
468 // making things more readable
469 $snippet = str_replace('<br/>', "\n", $snippet);
470 $snippet = str_replace('<br />', "\n", $snippet);
471 $snippet = str_replace('</p>', "\n", $snippet);
472 $snippet = str_replace('</div>', "\n", $snippet);
473
474 return trim(strip_tags($snippet));
475 }
476
477
478 // keywords
479
480 public function getMDSection()
481 {
482 // general section available?
483 $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
484 if (!is_object($md_section = $md_obj->getGeneral())) {
485 $md_section = $md_obj->addGeneral();
486 $md_section->save();
487 }
488
489 return $md_section;
490 }
491
492 public function updateKeywords(array $keywords)
493 {
495
496 // language is not "used" anywhere
497 $ulang = $ilUser->getLanguage();
498 $keywords = array($ulang => $keywords);
499
500 ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
501 }
502
503 public static function getKeywords($a_obj_id, $a_posting_id)
504 {
505 return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
506 }
507
513 public function handleNews($a_update = false)
514 {
517
518 // see ilWikiPage::updateNews()
519
520 if (!$this->getActive()) {
521 return;
522 }
523
524 $news_item = null;
525
526 // try to re-use existing news item
527 if ((bool) $a_update) {
528 // get last news item of the day (if existing)
530 $this->getBlogId(),
531 "blog",
532 $this->getId(),
533 $this->getParentType(),
534 true
535 );
536 if ($news_id > 0) {
537 $news_item = new ilNewsItem($news_id);
538 }
539 }
540
541 // create new news item
542 if (!$news_item) {
543 $news_set = new ilSetting("news");
544 $default_visibility = $news_set->get("default_visibility", "users");
545
546 $news_item = new ilNewsItem();
547 $news_item->setContext(
548 $this->getBlogId(),
549 "blog",
550 $this->getId(),
551 $this->getParentType()
552 );
553 $news_item->setPriority(NEWS_NOTICE);
554 $news_item->setVisibility($default_visibility);
555 }
556
557 // news author
558 $news_item->setUserId($ilUser->getId());
559
560
561 // news title/content
562
563 $news_item->setTitle($this->getTitle());
564
565 $content = $a_update
566 ? "blog_news_posting_updated"
567 : "blog_news_posting_published";
568
569 // news "author"
570 $content = sprintf($lng->txt($content), ilUserUtil::getNamePresentation($ilUser->getId()));
571
572 // posting author[s]
573 $contributors = array();
574 foreach (self::getPageContributors($this->getParentType(), $this->getId()) as $user) {
575 $contributors[] = $user["user_id"];
576 }
577 if (sizeof($contributors) > 1 || !in_array($this->getAuthor(), $contributors)) {
578 // original author should come first?
579 $authors = array(ilUserUtil::getNamePresentation($this->getAuthor()));
580 foreach ($contributors as $user_id) {
581 if ($user_id != $this->getAuthor()) {
582 $authors[] = ilUserUtil::getNamePresentation($user_id);
583 }
584 }
585 $content .= "\n" . sprintf($lng->txt("blog_news_posting_authors"), implode(", ", $authors));
586 }
587
588 $news_item->setContentTextIsLangVar(false);
589 $news_item->setContent($content);
590
591 $snippet = ilBlogPostingGUI::getSnippet($this->getId());
592 $news_item->setContentLong($snippet);
593
594 if (!$news_item->getId()) {
595 $news_item->create();
596 } else {
597 $news_item->update(true);
598 }
599 }
600
608 protected static function lookup($a_field, $a_posting_id)
609 {
610 global $DIC;
611
612 $db = $DIC->database();
613
614 $set = $db->query("SELECT $a_field FROM il_blog_posting " .
615 " WHERE id = " . $db->quote($a_posting_id, "integer"));
616 $rec = $db->fetchAssoc($set);
617
618 return $rec[$a_field];
619 }
620
627 public static function lookupTitle($a_posting_id)
628 {
629 $t = self::lookup("title", $a_posting_id);
630 return $t;
631 }
632}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATETIME
const NEWS_NOTICE
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.
Class ilBlogPosting.
static lookupTitle($a_posting_id)
Lookup title.
isApproved()
Get approved status.
update($a_validate=true, $a_no_history=false, $a_notify=true, $a_notify_action="update")
Update blog posting.
static lookup($a_field, $a_posting_id)
Lookup posting property.
getCreated()
Get creation date.
handleNews($a_update=false)
Handle news item.
getParentType()
Get parent type.
updateKeywords(array $keywords)
static getLastPost($a_blog_id)
Get newest posting for blog.
static getAllPostings($a_blog_id, $a_limit=1000, $a_offset=0)
Get all postings of blog.
create($a_import=false)
Create new blog posting.
setCreated(ilDateTime $a_date)
Set creation date.
setBlogNodeId($a_id, $a_is_in_workspace=false)
Set blog node id (needed for notification)
static searchBlogsByAuthor($a_user_id)
Get all blogs where user has postings.
setAuthor($a_id)
Set author user id.
static lookupBlogId($a_posting_id)
Lookup blog id.
static exists($a_blog_id, $a_posting_id)
Checks whether a posting exists.
setWithdrawn(ilDateTime $a_date)
Set last withdrawal date.
getBlogId()
Get blog object id.
setTitle($a_title)
Set title.
getTitle()
Get title.
static getKeywords($a_obj_id, $a_posting_id)
getAuthor()
Get author user id.
setApproved($a_status)
Toggle approval status.
getWithdrawn()
Get last withdrawal date.
setBlogId($a_id)
Set blog object id.
read()
Read blog posting.
static deleteAllBlogPostings($a_blog_id)
Delete all postings for blog.
@classDescription Date and time handling
static lookupKeywords($a_rbac_id, $a_obj_id, $a_return_ids=false)
Lookup Keywords.
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
Update keywords from input array.
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.
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.
static sendNotification($a_action, $a_in_wsp, $a_blog_node_id, $a_posting_id, $a_comment=null)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
addUpdateListener(&$a_object, $a_method, $a_parameters="")
setActive($a_active)
set activation
setId($a_id)
set id
getActive($a_check_scheduled_activation=false)
get activation
ILIAS Setting Class.
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:
static now()
Return current timestamp in Y-m-d H:i:s format.
global $DIC
Definition: goto.php:24
$ilUser
Definition: imgupload.php:18
$keys
Definition: metadata.php:187
$query
global $ilDB
$data
Definition: storeScorm.php:23