ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5
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  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  {
144  $created = ilUtil::now();
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  $query = "DELETE FROM il_blog_posting".
249  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
250  $ilDB->manipulate($query);
251 
252  parent::delete();
253 
254  return true;
255  }
256 
262  static function deleteAllBlogPostings($a_blog_id)
263  {
264  global $ilDB;
265 
266  include_once 'Services/MetaData/classes/class.ilMD.php';
267 
268  $query = "SELECT * FROM il_blog_posting".
269  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer");
270  $set = $ilDB->query($query);
271  while($rec = $ilDB->fetchAssoc($set))
272  {
273  // delete all md keywords
274  $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
275  if(is_object($md_section = $md_obj->getGeneral()))
276  {
277  foreach($md_section->getKeywordIds() as $id)
278  {
279  $md_key = $md_section->getKeyword($id);
280  $md_key->delete();
281  }
282  }
283 
284  $post = new ilBlogPosting($rec["id"]);
285  $post->delete();
286  }
287  }
288 
295  static function lookupBlogId($a_posting_id)
296  {
297  global $ilDB;
298 
299  $query = "SELECT blog_id FROM il_blog_posting".
300  " WHERE id = ".$ilDB->quote($a_posting_id, "integer");
301  $set = $ilDB->query($query);
302  if ($rec = $ilDB->fetchAssoc($set))
303  {
304  return $rec["blog_id"];
305  }
306  return false;
307  }
308 
317  static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
318  {
319  global $ilDB;
320 
321  $pages = parent::getAllPages("blp", $a_blog_id);
322 
323  if($a_limit)
324  {
325  $ilDB->setLimit($a_limit, $a_offset);
326  }
327 
328  $query = "SELECT * FROM il_blog_posting".
329  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
330  " ORDER BY created DESC";
331  $set = $ilDB->query($query);
332  $post = array();
333  while($rec = $ilDB->fetchAssoc($set))
334  {
335  if (isset($pages[$rec["id"]]))
336  {
337  $post[$rec["id"]] = $pages[$rec["id"]];
338  $post[$rec["id"]]["title"] = $rec["title"];
339  $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
340  $post[$rec["id"]]["author"] = $rec["author"];
341  $post[$rec["id"]]["approved"] = (bool)$rec["approved"];
342 
343  foreach(self::getPageContributors("blp", $rec["id"]) as $editor)
344  {
345  if($editor["user_id"] != $rec["author"])
346  {
347  $post[$rec["id"]]["editors"][] = $editor["user_id"];
348  }
349  }
350  }
351  }
352 
353  return $post;
354  }
355 
363  static function exists($a_blog_id, $a_posting_id)
364  {
365  global $ilDB;
366 
367  $query = "SELECT id FROM il_blog_posting".
368  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
369  " AND id = ".$ilDB->quote($a_posting_id, "integer");
370  $set = $ilDB->query($query);
371  if($rec = $ilDB->fetchAssoc($set))
372  {
373  return true;
374  }
375  return false;
376  }
377 
384  static function getLastPost($a_blog_id)
385  {
386  $data = self::getAllPostings($a_blog_id, 1);
387  if($data)
388  {
389  return array_pop(array_keys($data));
390  }
391  }
392 
399  public function setBlogNodeId($a_id, $a_is_in_workspace = false)
400  {
401  $this->blog_node_id = (int)$a_id;
402  $this->blog_node_is_wsp = (bool)$a_is_in_workspace;
403  }
404 
411  public static function searchBlogsByAuthor($a_user_id)
412  {
413  global $ilDB;
414 
415  $ids = array();
416 
417  $sql = "SELECT DISTINCT(blog_id)".
418  " FROM il_blog_posting".
419  " WHERE author = ".$ilDB->quote($a_user_id);
420  $set = $ilDB->query($sql);
421  while($row = $ilDB->fetchAssoc($set))
422  {
423  $ids[] = $row["blog_id"];
424  }
425  return $ids;
426  }
427 
428  public function getNotificationAbstract()
429  {
430  include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
431  $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
432 
433  // making things more readable
434  $snippet = str_replace('<br/>', "\n", $snippet);
435  $snippet = str_replace('<br />', "\n", $snippet);
436  $snippet = str_replace('</p>', "\n", $snippet);
437  $snippet = str_replace('</div>', "\n", $snippet);
438 
439  return trim(strip_tags($snippet));
440  }
441 
442 
443  // keywords
444 
445  public function getMDSection()
446  {
447  // general section available?
448  include_once 'Services/MetaData/classes/class.ilMD.php';
449  $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
450  if(!is_object($md_section = $md_obj->getGeneral()))
451  {
452  $md_section = $md_obj->addGeneral();
453  $md_section->save();
454  }
455 
456  return $md_section;
457  }
458 
459  public function updateKeywords(array $keywords)
460  {
461  global $ilUser;
462 
463  // language is not "used" anywhere
464  $ulang = $ilUser->getLanguage();
465  $keywords = array($ulang=>$keywords);
466 
467  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
468  ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
469  }
470 
471  public static function getKeywords($a_obj_id, $a_posting_id)
472  {
473  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
474  return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
475  }
476 }
477 
478 ?>
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.
create($a_import=false)
Create new blog posting.
setAuthor($a_id)
Set author user id.
static getKeywords($a_obj_id, $a_posting_id)
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.
$data
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.
Class ilPageObject.
getCreated()
Get creation date.
Date and time handling
setBlogNodeId($a_id, $a_is_in_workspace=false)
Set blog node id (needed for notification)
setId($a_id)
set id
getAuthor()
Get author user id.
getParentType()
Get parent type.
global $ilUser
Definition: imgupload.php:15
global $ilDB
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.
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.
setBlogId($a_id)
Set blog object id.