ILIAS  release_8 Revision v8.24
class.ilBlogPosting.php
Go to the documentation of this file.
1<?php
2
25{
26 protected string $title = "";
27 protected ?ilDateTime $created = null;
28 protected int $blog_node_id = 0;
29 protected bool $blog_node_is_wsp = false;
30 protected int $author = 0;
31 protected bool $approved = false;
32 protected ?ilDateTime $withdrawn = null;
33
34 public function getParentType(): string
35 {
36 return "blp";
37 }
38
39 public function setTitle(string $a_title): void
40 {
41 $this->title = $a_title;
42 }
43
44 public function getTitle(): string
45 {
46 return $this->title;
47 }
48
49 public function setBlogId(int $a_id): void
50 {
51 $this->setParentId($a_id);
52 }
53
54 public function getBlogId(): int
55 {
56 return $this->getParentId();
57 }
58
59 public function setCreated(ilDateTime $a_date): void
60 {
61 $this->created = $a_date;
62 }
63
64 public function getCreated(): ilDateTime
65 {
66 return $this->created;
67 }
68
69 public function setAuthor(int $a_id): void
70 {
71 $this->author = $a_id;
72 }
73
74 public function getAuthor(): int
75 {
76 return $this->author;
77 }
78
79 public function setApproved(bool $a_status): void
80 {
81 $this->approved = $a_status;
82 }
83
84 public function isApproved(): bool
85 {
86 return $this->approved;
87 }
88
92 public function setWithdrawn(
93 ilDateTime $a_date
94 ): void {
95 $this->withdrawn = $a_date;
96 }
97
101 public function getWithdrawn(): ?ilDateTime
102 {
103 return $this->withdrawn;
104 }
105
109 public function create(
110 bool $a_import = false
111 ): void {
112 $ilDB = $this->db;
113
114 $id = $ilDB->nextId("il_blog_posting");
115 $this->setId($id);
116
117 if (!$a_import) {
119 } else {
120 $created = $this->getCreated()->get(IL_CAL_DATETIME);
121 }
122
123 // we are using a separate creation date to enable sorting without JOINs
124 $withdrawn = $this->getWithdrawn()
125 ? $this->getWithdrawn()->get(IL_CAL_DATETIME)
126 : null;
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()
154 ? $this->getWithdrawn()->get(IL_CAL_DATETIME)
155 : null;
156 $query = "UPDATE il_blog_posting SET" .
157 " title = " . $ilDB->quote($this->getTitle(), "text") .
158 ",created = " . $ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "timestamp") .
159 ",approved =" . $ilDB->quote($this->isApproved(), "integer") .
160 ",last_withdrawn =" . $ilDB->quote($withdrawn, "timestamp") .
161 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
162 $ilDB->manipulate($query);
163
164 $ret = parent::update($a_validate, $a_no_history);
165
166 if ($a_notify && $this->getActive()) {
167 ilObjBlog::sendNotification($a_notify_action, $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
168 }
169
170 return $ret;
171 }
172
176 public function read(): void
177 {
178 $ilDB = $this->db;
179
180 $query = "SELECT * FROM il_blog_posting" .
181 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
182 $set = $ilDB->query($query);
183 $rec = $ilDB->fetchAssoc($set);
184
185 $this->setTitle($rec["title"]);
186 $this->setBlogId($rec["blog_id"]);
187 $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
188 $this->setAuthor($rec["author"]);
189 if ($rec["approved"]) {
190 $this->setApproved(true);
191 }
192 $this->setWithdrawn(new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME));
193
194 // when posting is deactivated it should loose the approval
195 $this->addUpdateListener($this, "checkApproval");
196
197 parent::read();
198 }
199
200 public function checkApproval(): void
201 {
202 if (!$this->getActive() && $this->isApproved()) {
203 $this->approved = false;
204 $this->update();
205 }
206 }
207
211 public function delete(): void
212 {
213 $ilDB = $this->db;
214
216 $this->getBlogId(),
217 "blog",
218 $this->getId(),
219 $this->getParentType()
220 );
221
222 $query = "DELETE FROM il_blog_posting" .
223 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
224 $ilDB->manipulate($query);
225
226 parent::delete();
227 }
228
232 public function unpublish(): void
233 {
234 $this->setApproved(false);
235 $this->setActive(false);
236 $this->setWithdrawn(new ilDateTime(ilUtil::now(), IL_CAL_DATETIME));
237 $this->update(true, false, false);
238
240 $this->getBlogId(),
241 "blog",
242 $this->getId(),
243 $this->getParentType()
244 );
245 }
246
247
251 public static function deleteAllBlogPostings(
252 int $a_blog_id
253 ): void {
254 global $DIC;
255
256 $ilDB = $DIC->database();
257
258 $query = "SELECT * FROM il_blog_posting" .
259 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer");
260 $set = $ilDB->query($query);
261 while ($rec = $ilDB->fetchAssoc($set)) {
262 // delete all md keywords
263 $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
264 if (is_object($md_section = $md_obj->getGeneral())) {
265 foreach ($md_section->getKeywordIds() as $id) {
266 $md_key = $md_section->getKeyword($id);
267 $md_key->delete();
268 }
269 }
270
271 $post = new ilBlogPosting($rec["id"]);
272 $post->delete();
273 }
274 }
275
276 public static function lookupBlogId(
277 int $a_posting_id
278 ): ?int {
279 global $DIC;
280
281 $ilDB = $DIC->database();
282
283 $query = "SELECT blog_id FROM il_blog_posting" .
284 " WHERE id = " . $ilDB->quote($a_posting_id, "integer");
285 $set = $ilDB->query($query);
286 if ($rec = $ilDB->fetchAssoc($set)) {
287 return (int) $rec["blog_id"];
288 }
289 return null;
290 }
291
295 public static function getAllPostings(
296 int $a_blog_id,
297 int $a_limit = 1000,
298 int $a_offset = 0
299 ): array {
300 global $DIC;
301
302 $ilDB = $DIC->database();
303
304 $pages = parent::getAllPages("blp", $a_blog_id);
305
306 if ($a_limit) {
307 $ilDB->setLimit($a_limit, $a_offset);
308 }
309
310 $query = "SELECT * FROM il_blog_posting" .
311 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
312 " ORDER BY created DESC";
313 $set = $ilDB->query($query);
314 $post = array();
315 while ($rec = $ilDB->fetchAssoc($set)) {
316 if (isset($pages[$rec["id"]])) {
317 $post[$rec["id"]] = $pages[$rec["id"]];
318 $post[$rec["id"]]["title"] = $rec["title"];
319 $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
320 $post[$rec["id"]]["author"] = $rec["author"];
321 $post[$rec["id"]]["approved"] = (bool) $rec["approved"];
322 $post[$rec["id"]]["last_withdrawn"] = new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME);
323
324 foreach (self::getPageContributors("blp", $rec["id"]) as $editor) {
325 if ($editor["user_id"] != $rec["author"]) {
326 $post[$rec["id"]]["editors"][] = $editor["user_id"];
327 }
328 }
329 }
330 }
331
332 return $post;
333 }
334
338 public static function exists(
339 int $a_blog_id,
340 int $a_posting_id
341 ): bool {
342 global $DIC;
343
344 $ilDB = $DIC->database();
345
346 $query = "SELECT id FROM il_blog_posting" .
347 " WHERE blog_id = " . $ilDB->quote($a_blog_id, "integer") .
348 " AND id = " . $ilDB->quote($a_posting_id, "integer");
349 $set = $ilDB->query($query);
350 if ($rec = $ilDB->fetchAssoc($set)) {
351 return true;
352 }
353 return false;
354 }
355
359 public static function getLastPost(
360 int $a_blog_id
361 ): int {
362 $data = self::getAllPostings($a_blog_id, 1);
363 if ($data) {
364 $keys = array_keys($data);
365 return end($keys);
366 }
367 return 0;
368 }
369
373 public function setBlogNodeId(
374 int $a_id,
375 bool $a_is_in_workspace = false
376 ): void {
377 $this->blog_node_id = $a_id;
378 $this->blog_node_is_wsp = $a_is_in_workspace;
379 }
380
384 public static function searchBlogsByAuthor(
385 int $a_user_id
386 ): array {
387 global $DIC;
388
389 $ilDB = $DIC->database();
390
391 $ids = array();
392
393 $sql = "SELECT DISTINCT(blog_id)" .
394 " FROM il_blog_posting" .
395 " WHERE author = " . $ilDB->quote($a_user_id);
396 $set = $ilDB->query($sql);
397 while ($row = $ilDB->fetchAssoc($set)) {
398 $ids[] = (int) $row["blog_id"];
399 }
400 return $ids;
401 }
402
403 public function getNotificationAbstract(): string
404 {
405 $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
406
407 // making things more readable
408 $snippet = str_replace(array('<br/>', '<br />', '</p>', '</div>'), "\n", $snippet);
409
410 return trim(strip_tags($snippet));
411 }
412
413
414 // keywords
415
416 public function getMDSection(): ?ilMDGeneral
417 {
418 // general section available?
419 $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
420 if (!is_object($md_section = $md_obj->getGeneral())) {
421 $md_section = $md_obj->addGeneral();
422 $md_section->save();
423 return $md_section;
424 }
425 return $md_section;
426 }
427
428 public function updateKeywords(
429 array $keywords
430 ): void {
431 $ilUser = $this->user;
432
433 // language is not "used" anywhere
434 $ulang = $ilUser->getLanguage();
435 $keywords = array($ulang => $keywords);
436
437 ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
438 }
439
440 public static function getKeywords(
441 int $a_obj_id,
442 int $a_posting_id
443 ): array {
444 return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
445 }
446
450 public function handleNews(
451 bool $a_update = false
452 ): void {
453 $lng = $this->lng;
454 $ilUser = $this->user;
455
456 // see ilWikiPage::updateNews()
457
458 if (!$this->getActive()) {
459 return;
460 }
461
462 $news_item = null;
463
464 // try to re-use existing news item
465 if ($a_update) {
466 // get last news item of the day (if existing)
468 $this->getBlogId(),
469 "blog",
470 $this->getId(),
471 $this->getParentType(),
472 true
473 );
474 if ($news_id > 0) {
475 $news_item = new ilNewsItem($news_id);
476 }
477 }
478
479 // create new news item
480 if (!$news_item) {
481 $news_set = new ilSetting("news");
482 $default_visibility = $news_set->get("default_visibility", "users");
483
484 $news_item = new ilNewsItem();
485 $news_item->setContext(
486 $this->getBlogId(),
487 "blog",
488 $this->getId(),
489 $this->getParentType()
490 );
491 $news_item->setPriority(NEWS_NOTICE);
492 $news_item->setVisibility($default_visibility);
493 }
494
495 // news author
496 $news_item->setUserId($ilUser->getId());
497
498
499 // news title/content
500
501 $news_item->setTitle($this->getTitle());
502
503 $content = $a_update
504 ? "blog_news_posting_updated"
505 : "blog_news_posting_published";
506
507 // news "author"
508 $content = sprintf($lng->txt($content), ilUserUtil::getNamePresentation($ilUser->getId()));
509
510 // posting author[s]
511 $contributors = array();
512 foreach (self::getPageContributors($this->getParentType(), $this->getId()) as $user) {
513 $contributors[] = $user["user_id"];
514 }
515 if (count($contributors) > 1 || !in_array($this->getAuthor(), $contributors)) {
516 // original author should come first?
517 $authors = array(ilUserUtil::getNamePresentation($this->getAuthor()));
518 foreach ($contributors as $user_id) {
519 if ($user_id != $this->getAuthor()) {
520 $authors[] = ilUserUtil::getNamePresentation($user_id);
521 }
522 }
523 $content .= "\n" . sprintf($lng->txt("blog_news_posting_authors"), implode(", ", $authors));
524 }
525
526 $news_item->setContentTextIsLangVar(false);
527 $news_item->setContent($content);
528
529 $snippet = ilBlogPostingGUI::getSnippet($this->getId());
530 $news_item->setContentLong($snippet);
531
532 if (!$news_item->getId()) {
533 $news_item->create();
534 } else {
535 $news_item->update(true);
536 }
537 }
538
542 protected static function lookup(
543 string $a_field,
544 string $a_posting_id
545 ): ?string {
546 global $DIC;
547
548 $db = $DIC->database();
549
550 $set = $db->query("SELECT $a_field FROM il_blog_posting " .
551 " WHERE id = " . $db->quote($a_posting_id, "integer"));
552 $rec = $db->fetchAssoc($set);
553
554 return $rec[$a_field] ?? null;
555 }
556
557 public static function lookupTitle(int $a_posting_id): string
558 {
559 return (string) self::lookup("title", $a_posting_id);
560 }
561}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_DATETIME
const NEWS_NOTICE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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")
create(bool $a_import=false)
Create new blog posting.
static lookupTitle(int $a_posting_id)
setApproved(bool $a_status)
handleNews(bool $a_update=false)
Handle news item.
static searchBlogsByAuthor(int $a_user_id)
Get all blogs where user has postings.
setBlogNodeId(int $a_id, bool $a_is_in_workspace=false)
Set blog node id (needed for notification)
static getAllPostings(int $a_blog_id, int $a_limit=1000, int $a_offset=0)
Get all postings of blog.
updateKeywords(array $keywords)
static lookupBlogId(int $a_posting_id)
static getKeywords(int $a_obj_id, int $a_posting_id)
static getLastPost(int $a_blog_id)
Get newest posting for blog.
setCreated(ilDateTime $a_date)
setWithdrawn(ilDateTime $a_date)
Set last withdrawal date.
static lookup(string $a_field, string $a_posting_id)
Lookup posting property.
static deleteAllBlogPostings(int $a_blog_id)
Delete all postings for blog.
setTitle(string $a_title)
getWithdrawn()
Get last withdrawal date.
read()
Read blog posting.
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
A news item can be created by different sources.
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 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 sendNotification(string $a_action, bool $a_in_wsp, int $a_blog_node_id, int $a_posting_id, ?string $a_comment=null)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
setParentId(int $a_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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:
static now()
Return current timestamp in Y-m-d H:i:s format.
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
$post
Definition: ltitoken.php:49
$keys
Definition: metadata.php:204
int $created
Timestamp for when the object was created.
Definition: System.php:151
$query
$lng