ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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(false, "integer").")";
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  }
344 
345  return $post;
346  }
347 
355  static function exists($a_blog_id, $a_posting_id)
356  {
357  global $ilDB;
358 
359  $query = "SELECT id FROM il_blog_posting".
360  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
361  " AND id = ".$ilDB->quote($a_posting_id, "integer");
362  $set = $ilDB->query($query);
363  if($rec = $ilDB->fetchAssoc($set))
364  {
365  return true;
366  }
367  return false;
368  }
369 
376  static function getLastPost($a_blog_id)
377  {
378  $data = self::getAllPostings($a_blog_id, 1);
379  if($data)
380  {
381  return array_pop(array_keys($data));
382  }
383  }
384 
391  public function setBlogNodeId($a_id, $a_is_in_workspace = false)
392  {
393  $this->blog_node_id = (int)$a_id;
394  $this->blog_node_is_wsp = (bool)$a_is_in_workspace;
395  }
396 
403  public static function searchBlogsByAuthor($a_user_id)
404  {
405  global $ilDB;
406 
407  $ids = array();
408 
409  $sql = "SELECT DISTINCT(blog_id)".
410  " FROM il_blog_posting".
411  " WHERE author = ".$ilDB->quote($a_user_id);
412  $set = $ilDB->query($sql);
413  while($row = $ilDB->fetchAssoc($set))
414  {
415  $ids[] = $row["blog_id"];
416  }
417  return $ids;
418  }
419 
420  public function getNotificationAbstract()
421  {
422  include_once "Modules/Blog/classes/class.ilBlogPostingGUI.php";
423  $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
424 
425  // making things more readable
426  $snippet = str_replace('<br/>', "\n", $snippet);
427  $snippet = str_replace('<br />', "\n", $snippet);
428  $snippet = str_replace('</p>', "\n", $snippet);
429  $snippet = str_replace('</div>', "\n", $snippet);
430 
431  return trim(strip_tags($snippet));
432  }
433 
434 
435  // keywords
436 
437  public function getMDSection()
438  {
439  // general section available?
440  include_once 'Services/MetaData/classes/class.ilMD.php';
441  $md_obj = new ilMD($this->getBlogId(), $this->getId(), "blp");
442  if(!is_object($md_section = $md_obj->getGeneral()))
443  {
444  $md_section = $md_obj->addGeneral();
445  $md_section->save();
446  }
447 
448  return $md_section;
449  }
450 
451  public function updateKeywords(array $keywords)
452  {
453  global $ilUser;
454 
455  // language is not "used" anywhere
456  $ulang = $ilUser->getLanguage();
457  $keywords = array($ulang=>$keywords);
458 
459  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
460  ilMDKeyword::updateKeywords($this->getMDSection(), $keywords);
461  }
462 
463  public static function getKeywords($a_obj_id, $a_posting_id)
464  {
465  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
466  return ilMDKeyword::lookupKeywords($a_obj_id, $a_posting_id);
467  }
468 }
469 
470 ?>