ILIAS  release_4-3 Revision
 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 
31  function __construct($a_id = 0, $a_old_nr = 0, $a_halt = true)
32  {
33  parent::__construct("blp", $a_id, $a_old_nr, $a_halt);
34  }
35 
41  function setTitle($a_title)
42  {
43  $this->title = $a_title;
44  }
45 
51  function getTitle()
52  {
53  return $this->title;
54  }
55 
61  function setBlogId($a_id)
62  {
63  $this->setParentId($a_id);
64  }
65 
71  function getBlogId()
72  {
73  return $this->getParentId();
74  }
75 
81  function setCreated(ilDateTime $a_date)
82  {
83  $this->created = $a_date;
84  }
85 
91  function getCreated()
92  {
93  return $this->created;
94  }
95 
101  function setAuthor($a_id)
102  {
103  $this->author = (int)$a_id;
104  }
105 
111  function getAuthor()
112  {
113  return $this->author;
114  }
115 
119  function setApproved($a_status)
120  {
121  $this->approved = (bool)$a_status;
122  }
123 
129  function isApproved()
130  {
131  return (bool)$this->approved;
132  }
133 
137  function create($a_import = false)
138  {
139  global $ilDB;
140 
141  $id = $ilDB->nextId("il_blog_posting");
142  $this->setId($id);
143 
144  if(!$a_import)
145  {
146  $created = ilUtil::now();
147  }
148  else
149  {
150  $created = $this->getCreated()->get(IL_CAL_DATETIME);
151  }
152 
153  // we are using a separate creation date to enable sorting without JOINs
154 
155  $query = "INSERT INTO il_blog_posting (id, title, blog_id, created, author, approved)".
156  " VALUES (".
157  $ilDB->quote($this->getId(), "integer").",".
158  $ilDB->quote($this->getTitle(), "text").",".
159  $ilDB->quote($this->getBlogId(), "integer").",".
160  $ilDB->quote($created, "timestamp").",".
161  $ilDB->quote($this->getAuthor(), "integer").",".
162  $ilDB->quote(false, "integer").")";
163  $ilDB->manipulate($query);
164 
165  if(!$a_import)
166  {
167  parent::create();
168  // $this->saveInternalLinks($this->getXMLContent());
169  }
170  }
171 
180  function update($a_validate = true, $a_no_history = false, $a_notify = true)
181  {
182  global $ilDB;
183 
184  // blog_id, author and created cannot be changed
185 
186  $query = "UPDATE il_blog_posting SET".
187  " title = ".$ilDB->quote($this->getTitle(), "text").
188  ",created = ".$ilDB->quote($this->getCreated()->get(IL_CAL_DATETIME), "text").
189  ",approved =".$ilDB->quote($this->isApproved(), "integer").
190  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
191  $ilDB->manipulate($query);
192 
193  parent::update($a_validate, $a_no_history);
194 
195  if($a_notify && $this->getActive())
196  {
197  include_once "Modules/Blog/classes/class.ilObjBlog.php";
198  ilObjBlog::sendNotification("update", $this->blog_node_is_wsp, $this->blog_node_id, $this->getId());
199  }
200 
201  return true;
202  }
203 
207  function read()
208  {
209  global $ilDB;
210 
211  $query = "SELECT * FROM il_blog_posting".
212  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
213  $set = $ilDB->query($query);
214  $rec = $ilDB->fetchAssoc($set);
215 
216  $this->setTitle($rec["title"]);
217  $this->setBlogId($rec["blog_id"]);
218  $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
219  $this->setAuthor($rec["author"]);
220  if((bool)$rec["approved"])
221  {
222  $this->setApproved(true);
223  }
224 
225  // when posting is deactivated it should loose the approval
226  $this->addUpdateListener($this, "checkApproval");
227 
228  parent::read();
229  }
230 
231  function checkApproval()
232  {
233  if(!$this->getActive() && $this->isApproved())
234  {
235  $this->approved = false;
236  $this->update();
237  }
238  }
239 
245  function delete()
246  {
247  global $ilDB;
248 
249  $query = "DELETE FROM il_blog_posting".
250  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
251  $ilDB->manipulate($query);
252 
253  parent::delete();
254 
255  return true;
256  }
257 
263  static function deleteAllBlogPostings($a_blog_id)
264  {
265  global $ilDB;
266 
267  include_once 'Services/MetaData/classes/class.ilMD.php';
268 
269  $query = "SELECT * FROM il_blog_posting".
270  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer");
271  $set = $ilDB->query($query);
272  while($rec = $ilDB->fetchAssoc($set))
273  {
274  // delete all md keywords
275  $md_obj = new ilMD($a_blog_id, $rec["id"], "blp");
276  if(is_object($md_section = $md_obj->getGeneral()))
277  {
278  foreach($md_section->getKeywordIds() as $id)
279  {
280  $md_key = $md_section->getKeyword($id);
281  $md_key->delete();
282  }
283  }
284 
285  $post = new ilBlogPosting($rec["id"]);
286  $post->delete();
287  }
288  }
289 
296  static function lookupBlogId($a_posting_id)
297  {
298  global $ilDB;
299 
300  $query = "SELECT blog_id FROM il_blog_posting".
301  " WHERE id = ".$ilDB->quote($a_posting_id, "integer");
302  $set = $ilDB->query($query);
303  if ($rec = $ilDB->fetchAssoc($set))
304  {
305  return $rec["blog_id"];
306  }
307  return false;
308  }
309 
318  static function getAllPostings($a_blog_id, $a_limit = 1000, $a_offset = 0)
319  {
320  global $ilDB;
321 
322  $pages = parent::getAllPages("blp", $a_blog_id);
323 
324  if($a_limit)
325  {
326  $ilDB->setLimit($a_limit, $a_offset);
327  }
328 
329  $query = "SELECT * FROM il_blog_posting".
330  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
331  " ORDER BY created DESC";
332  $set = $ilDB->query($query);
333  $post = array();
334  while($rec = $ilDB->fetchAssoc($set))
335  {
336  if (isset($pages[$rec["id"]]))
337  {
338  $post[$rec["id"]] = $pages[$rec["id"]];
339  $post[$rec["id"]]["title"] = $rec["title"];
340  $post[$rec["id"]]["created"] = new ilDateTime($rec["created"], IL_CAL_DATETIME);
341  $post[$rec["id"]]["author"] = $rec["author"];
342  $post[$rec["id"]]["approved"] = (bool)$rec["approved"];
343  }
344  }
345 
346  return $post;
347  }
348 
356  static function exists($a_blog_id, $a_posting_id)
357  {
358  global $ilDB;
359 
360  $query = "SELECT id FROM il_blog_posting".
361  " WHERE blog_id = ".$ilDB->quote($a_blog_id, "integer").
362  " AND id = ".$ilDB->quote($a_posting_id, "integer");
363  $set = $ilDB->query($query);
364  if($rec = $ilDB->fetchAssoc($set))
365  {
366  return true;
367  }
368  return false;
369  }
370 
377  static function getLastPost($a_blog_id)
378  {
379  $data = self::getAllPostings($a_blog_id, 1);
380  if($data)
381  {
382  return array_pop(array_keys($data));
383  }
384  }
385 
392  public function setBlogNodeId($a_id, $a_is_in_workspace = false)
393  {
394  $this->blog_node_id = (int)$a_id;
395  $this->blog_node_is_wsp = (bool)$a_is_in_workspace;
396  }
397 
404  public static function searchBlogsByAuthor($a_user_id)
405  {
406  global $ilDB;
407 
408  $ids = array();
409 
410  $sql = "SELECT DISTINCT(blog_id)".
411  " FROM il_blog_posting".
412  " WHERE author = ".$ilDB->quote($a_user_id);
413  $set = $ilDB->query($sql);
414  while($row = $ilDB->fetchAssoc($set))
415  {
416  $ids[] = $row["blog_id"];
417  }
418  return $ids;
419  }
420 }
421 
422 ?>