ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
4include_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 function getParentType()
29 {
30 return "blp";
31 }
32
33
39 function setTitle($a_title)
40 {
41 $this->title = $a_title;
42 }
43
49 function getTitle()
50 {
51 return $this->title;
52 }
53
59 function setBlogId($a_id)
60 {
61 $this->setParentId($a_id);
62 }
63
69 function getBlogId()
70 {
71 return $this->getParentId();
72 }
73
79 function setCreated(ilDateTime $a_date)
80 {
81 $this->created = $a_date;
82 }
83
89 function getCreated()
90 {
91 return $this->created;
92 }
93
99 function setAuthor($a_id)
100 {
101 $this->author = (int)$a_id;
102 }
103
109 function getAuthor()
110 {
111 return $this->author;
112 }
113
117 function setApproved($a_status)
118 {
119 $this->approved = (bool)$a_status;
120 }
121
127 function isApproved()
128 {
129 return (bool)$this->approved;
130 }
131
135 function create($a_import = false)
136 {
137 global $ilDB;
138
139 $id = $ilDB->nextId("il_blog_posting");
140 $this->setId($id);
141
142 if(!$a_import)
143 {
145 }
146 else
147 {
148 $created = $this->getCreated()->get(IL_CAL_DATETIME);
149 }
150
151 // we are using a separate creation date to enable sorting without JOINs
152
153 $query = "INSERT INTO il_blog_posting (id, title, blog_id, created, author, approved)".
154 " VALUES (".
155 $ilDB->quote($this->getId(), "integer").",".
156 $ilDB->quote($this->getTitle(), "text").",".
157 $ilDB->quote($this->getBlogId(), "integer").",".
158 $ilDB->quote($created, "timestamp").",".
159 $ilDB->quote($this->getAuthor(), "integer").",".
160 $ilDB->quote($this->isApproved(), "integer").")"; // #16526 - import
161 $ilDB->manipulate($query);
162
163 if(!$a_import)
164 {
165 parent::create();
166 // $this->saveInternalLinks($this->getXMLContent());
167 }
168 }
169
179 function update($a_validate = true, $a_no_history = false, $a_notify = true, $a_notify_action = "update")
180 {
181 global $ilDB;
182
183 // blog_id, author and created cannot be changed
184
185 $query = "UPDATE il_blog_posting SET".
186 " title = ".$ilDB->quote($this->getTitle(), "text").
187 ",created = ".$ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "text").
188 ",approved =".$ilDB->quote($this->isApproved(), "integer").
189 " WHERE id = ".$ilDB->quote($this->getId(), "integer");
190 $ilDB->manipulate($query);
191
192 parent::update($a_validate, $a_no_history);
193
194 if($a_notify && $this->getActive())
195 {
196 include_once "Modules/Blog/classes/class.ilObjBlog.php";
197 ilObjBlog::sendNotification($a_notify_action, $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
198 }
199
200 return true;
201 }
202
206 function read()
207 {
208 global $ilDB;
209
210 $query = "SELECT * FROM il_blog_posting".
211 " WHERE id = ".$ilDB->quote($this->getId(), "integer");
212 $set = $ilDB->query($query);
213 $rec = $ilDB->fetchAssoc($set);
214
215 $this->setTitle($rec["title"]);
216 $this->setBlogId($rec["blog_id"]);
217 $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
218 $this->setAuthor($rec["author"]);
219 if((bool)$rec["approved"])
220 {
221 $this->setApproved(true);
222 }
223
224 // when posting is deactivated it should loose the approval
225 $this->addUpdateListener($this, "checkApproval");
226
227 parent::read();
228 }
229
230 function checkApproval()
231 {
232 if(!$this->getActive() && $this->isApproved())
233 {
234 $this->approved = false;
235 $this->update();
236 }
237 }
238
244 function delete()
245 {
246 global $ilDB;
247
248 include_once("./Services/News/classes/class.ilNewsItem.php");
250 "blog", $this->getId(), $this->getParentType());
251
252 $query = "DELETE FROM il_blog_posting".
253 " WHERE id = ".$ilDB->quote($this->getId(), "integer");
254 $ilDB->manipulate($query);
255
256 parent::delete();
257
258 return true;
259 }
260
264 public function unpublish()
265 {
266 $this->setApproved(false);
267 $this->setActive(false);
268 $this->update(true, false, false);
269
270 include_once("./Services/News/classes/class.ilNewsItem.php");
272 "blog", $this->getId(), $this->getParentType());
273 }
274
275
281 static function deleteAllBlogPostings($a_blog_id)
282 {
283 global $ilDB;
284
285 include_once 'Services/MetaData/classes/class.ilMD.php';
286
287 $query = "SELECT * FROM il_blog_posting".
288 " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer");
289 $set = $ilDB->query($query);
290 while($rec = $ilDB->fetchAssoc($set))
291 {
292 // delete all md keywords
293 $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
294 if(is_object($md_section = $md_obj->getGeneral()))
295 {
296 foreach($md_section->getKeywordIds() as $id)
297 {
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 static function lookupBlogId($a_posting_id)
315 {
316 global $ilDB;
317
318 $query = "SELECT blog_id FROM il_blog_posting".
319 " WHERE id = ".$ilDB->quote($a_posting_id, "integer");
320 $set = $ilDB->query($query);
321 if ($rec = $ilDB->fetchAssoc($set))
322 {
323 return $rec["blog_id"];
324 }
325 return false;
326 }
327
336 static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
337 {
338 global $ilDB;
339
340 $pages = parent::getAllPages("blp", $a_blog_id);
341
342 if($a_limit)
343 {
344 $ilDB->setLimit($a_limit, $a_offset);
345 }
346
347 $query = "SELECT * FROM il_blog_posting".
348 " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
349 " ORDER BY created DESC";
350 $set = $ilDB->query($query);
351 $post = array();
352 while($rec = $ilDB->fetchAssoc($set))
353 {
354 if (isset($pages[$rec["id"]]))
355 {
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 {
364 if($editor["user_id"] != $rec["author"])
365 {
366 $post[$rec["id"]]["editors"][] = $editor["user_id"];
367 }
368 }
369 }
370 }
371
372 return $post;
373 }
374
382 static function exists($a_blog_id, $a_posting_id)
383 {
384 global $ilDB;
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 {
392 return true;
393 }
394 return false;
395 }
396
403 static function getLastPost($a_blog_id)
404 {
405 $data = self::getAllPostings($a_blog_id, 1);
406 if($data)
407 {
408 return array_pop(array_keys($data));
409 }
410 }
411
418 public function setBlogNodeId($a_id, $a_is_in_workspace = false)
419 {
420 $this->blog_node_id = (int)$a_id;
421 $this->blog_node_is_wsp = (bool)$a_is_in_workspace;
422 }
423
430 public static function searchBlogsByAuthor($a_user_id)
431 {
432 global $ilDB;
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 {
442 $ids[] = $row["blog_id"];
443 }
444 return $ids;
445 }
446
447 public function getNotificationAbstract()
448 {
449 include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
450 $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
451
452 // making things more readable
453 $snippet = str_replace('<br/>', "\n", $snippet);
454 $snippet = str_replace('<br />', "\n", $snippet);
455 $snippet = str_replace('</p>', "\n", $snippet);
456 $snippet = str_replace('</div>', "\n", $snippet);
457
458 return trim(strip_tags($snippet));
459 }
460
461
462 // keywords
463
464 public function getMDSection()
465 {
466 // general section available?
467 include_once 'Services/MetaData/classes/class.ilMD.php';
468 $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
469 if(!is_object($md_section = $md_obj->getGeneral()))
470 {
471 $md_section = $md_obj->addGeneral();
472 $md_section->save();
473 }
474
475 return $md_section;
476 }
477
478 public function updateKeywords(array $keywords)
479 {
480 global $ilUser;
481
482 // language is not "used" anywhere
483 $ulang = $ilUser->getLanguage();
484 $keywords = array($ulang=>$keywords);
485
486 include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
487 ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
488 }
489
490 public static function getKeywords($a_obj_id, $a_posting_id)
491 {
492 include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
493 return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
494 }
495
501 public function handleNews($a_update = false)
502 {
503 global $lng, $ilUser;
504
505 // see ilWikiPage::updateNews()
506
507 include_once("./Services/News/classes/class.ilNewsItem.php");
508 $news_item = null;
509
510 // try to re-use existing news item
511 if((bool)$a_update)
512 {
513 // get last news item of the day (if existing)
515 $this->getBlogId(),
516 "blog",
517 $this->getId(),
518 $this->getParentType(),
519 true
520 );
521 if($news_id > 0)
522 {
523 $news_item = new ilNewsItem($news_id);
524 }
525 }
526
527 // create new news item
528 if(!$news_item)
529 {
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 {
564 $contributors[] = $user["user_id"];
565 }
566 if(sizeof($contributors) > 1 || !in_array($this->getAuthor(), $contributors))
567 {
568 // original author should come first?
569 $authors = array(ilUserUtil::getNamePresentation($this->getAuthor()));
570 foreach($contributors as $user_id)
571 {
572 if($user_id != $this->getAuthor())
573 {
574 $authors[] = ilUserUtil::getNamePresentation($user_id);
575 }
576 }
577 $content .= "\n".sprintf($lng->txt("blog_news_posting_authors"), implode(", ", $authors));
578 }
579
580 $news_item->setContentTextIsLangVar(false);
581 $news_item->setContent($content);
582
583 include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
584 $snippet = ilBlogPostingGUI::getSnippet($this->getId());
585 $news_item->setContentLong($snippet);
586
587 if(!$news_item->getId())
588 {
589 $news_item->create();
590 }
591 else
592 {
593 $news_item->update(true);
594 }
595 }
596}
597
598?>
sprintf('%.4f', $callTime)
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.
isApproved()
Get approved status.
update($a_validate=true, $a_no_history=false, $a_notify=true, $a_notify_action="update")
Update blog posting.
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.
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.
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 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 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 sendNotification($a_action, $a_in_wsp, $a_blog_node_id, $a_posting_id, $a_comment=null)
Class ilPageObject.
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)
Default behaviour is:
static now()
Return current timestamp in Y-m-d H:i:s format.
global $lng
Definition: privfeed.php:17
global $ilDB
$ilUser
Definition: imgupload.php:18