ILIAS  Release_4_4_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 
178  function update($a_validate = true, $a_no_history = false, $a_notify = true)
179  {
180  global $ilDB;
181 
182  // blog_id, author and created cannot be changed
183 
184  $query = "UPDATE il_blog_posting SET".
185  " title = ".$ilDB->quote($this->getTitle(), "text").
186  ",created = ".$ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "text").
187  ",approved =".$ilDB->quote($this->isApproved(), "integer").
188  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
189  $ilDB->manipulate($query);
190 
191  parent::update($a_validate, $a_no_history);
192 
193  if($a_notify && $this->getActive())
194  {
195  include_once "Modules/Blog/classes/class.ilObjBlog.php";
196  ilObjBlog::sendNotification("update", $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
197  }
198 
199  return true;
200  }
201 
205  function read()
206  {
207  global $ilDB;
208 
209  $query = "SELECT * FROM il_blog_posting".
210  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
211  $set = $ilDB->query($query);
212  $rec = $ilDB->fetchAssoc($set);
213 
214  $this->setTitle($rec["title"]);
215  $this->setBlogId($rec["blog_id"]);
216  $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
217  $this->setAuthor($rec["author"]);
218  if((bool)$rec["approved"])
219  {
220  $this->setApproved(true);
221  }
222 
223  // when posting is deactivated it should loose the approval
224  $this->addUpdateListener($this, "checkApproval");
225 
226  parent::read();
227  }
228 
229  function checkApproval()
230  {
231  if(!$this->getActive() && $this->isApproved())
232  {
233  $this->approved = false;
234  $this->update();
235  }
236  }
237 
243  function delete()
244  {
245  global $ilDB;
246 
247  $query = "DELETE FROM il_blog_posting".
248  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
249  $ilDB->manipulate($query);
250 
251  parent::delete();
252 
253  return true;
254  }
255 
261  static function deleteAllBlogPostings($a_blog_id)
262  {
263  global $ilDB;
264 
265  include_once 'Services/MetaData/classes/class.ilMD.php';
266 
267  $query = "SELECT * FROM il_blog_posting".
268  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer");
269  $set = $ilDB->query($query);
270  while($rec = $ilDB->fetchAssoc($set))
271  {
272  // delete all md keywords
273  $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
274  if(is_object($md_section = $md_obj->getGeneral()))
275  {
276  foreach($md_section->getKeywordIds() as $id)
277  {
278  $md_key = $md_section->getKeyword($id);
279  $md_key->delete();
280  }
281  }
282 
283  $post = new ilBlogPosting($rec["id"]);
284  $post->delete();
285  }
286  }
287 
294  static function lookupBlogId($a_posting_id)
295  {
296  global $ilDB;
297 
298  $query = "SELECT blog_id FROM il_blog_posting".
299  " WHERE id = ".$ilDB->quote($a_posting_id, "integer");
300  $set = $ilDB->query($query);
301  if ($rec = $ilDB->fetchAssoc($set))
302  {
303  return $rec["blog_id"];
304  }
305  return false;
306  }
307 
316  static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
317  {
318  global $ilDB;
319 
320  $pages = parent::getAllPages("blp", $a_blog_id);
321 
322  if($a_limit)
323  {
324  $ilDB->setLimit($a_limit, $a_offset);
325  }
326 
327  $query = "SELECT * FROM il_blog_posting".
328  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
329  " ORDER BY created DESC";
330  $set = $ilDB->query($query);
331  $post = array();
332  while($rec = $ilDB->fetchAssoc($set))
333  {
334  if (isset($pages[$rec["id"]]))
335  {
336  $post[$rec["id"]] = $pages[$rec["id"]];
337  $post[$rec["id"]]["title"] = $rec["title"];
338  $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
339  $post[$rec["id"]]["author"] = $rec["author"];
340  $post[$rec["id"]]["approved"] = (bool)$rec["approved"];
341  }
342  }
343 
344  return $post;
345  }
346 
354  static function exists($a_blog_id, $a_posting_id)
355  {
356  global $ilDB;
357 
358  $query = "SELECT id FROM il_blog_posting".
359  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
360  " AND id = ".$ilDB->quote($a_posting_id, "integer");
361  $set = $ilDB->query($query);
362  if($rec = $ilDB->fetchAssoc($set))
363  {
364  return true;
365  }
366  return false;
367  }
368 
375  static function getLastPost($a_blog_id)
376  {
377  $data = self::getAllPostings($a_blog_id, 1);
378  if($data)
379  {
380  return array_pop(array_keys($data));
381  }
382  }
383 
390  public function setBlogNodeId($a_id, $a_is_in_workspace = false)
391  {
392  $this->blog_node_id = (int)$a_id;
393  $this->blog_node_is_wsp = (bool)$a_is_in_workspace;
394  }
395 
402  public static function searchBlogsByAuthor($a_user_id)
403  {
404  global $ilDB;
405 
406  $ids = array();
407 
408  $sql = "SELECT DISTINCT(blog_id)".
409  " FROM il_blog_posting".
410  " WHERE author = ".$ilDB->quote($a_user_id);
411  $set = $ilDB->query($sql);
412  while($row = $ilDB->fetchAssoc($set))
413  {
414  $ids[] = $row["blog_id"];
415  }
416  return $ids;
417  }
418 }
419 
420 ?>