ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilForumPost.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/Forum/classes/class.ilForumTopic.php';
5 
13 {
14  private $id = 0;
15 
16  private $forum_id = 0;
17 
18  private $thread_id = 0;
19 
20  private $user_id = 0;
21 
22  private $user_alias = '';
23 
24  private $subject = '';
25 
26  private $message = '';
27 
28  private $createdate = '0000-00-00 00:00:00';
29 
30  private $changedate = '0000-00-00 00:00:00';
31 
32  private $user_id_update = 0;
33 
34  private $censored = 0;
35 
36  private $censorship_comment = '';
37 
38  private $notification = 0;
39 
40  private $import_name = '';
41 
42  private $status = 1;
43 
44  private $tree_id = 0;
45 
46  private $parent_id = 0;
47 
48  private $lft = 0;
49 
50  private $rgt = 0;
51 
52  private $depth = 0;
53 
54  private $fullname = '';
55 
56  private $loginname = '';
57 
58  private $objThread = null;
59 
60  private $db = null;
61 
62  private $is_moderator = false;
63 
64  private $post_read = false;
65 
66  public function __construct($a_id = 0, $a_is_moderator = false, $preventImplicitRead = false)
67  {
68  global $ilDB;
69 
70  $this->is_moderator = $a_is_moderator;
71  $this->db = $ilDB;
72  $this->id = $a_id;
73 
74  if( !$preventImplicitRead )
75  {
76  $this->read();
77  }
78  }
79 
80  public function __destruct()
81  {
82  unset($this->db);
83  unset($this->objThread);
84  }
85 
86  public function insert()
87  {
88  global $ilDB;
89 
90  if ($this->forum_id && $this->thread_id)
91  {
92  $this->id = $this->db->nextId('frm_posts');
93 
94  $ilDB->insert('frm_posts', array(
95  'pos_pk' => array('integer', $this->id),
96  'pos_top_fk' => array('integer', $this->forum_id),
97  'pos_thr_fk' => array('integer', $this->thread_id),
98  'pos_usr_id' => array('integer', $this->user_id),
99  'pos_usr_alias' => array('text', $this->user_alias),
100  'pos_subject' => array('text', $this->subject),
101  'pos_message' => array('clob', $this->message),
102  'pos_date' => array('timestamp', $this->createdate),
103  'pos_update' => array('timestamp', null),
104  'update_user' => array('integer', $this->user_id_update),
105  'pos_cens' => array('integer', $this->censored),
106  'notify' => array('integer', (int)$this->notification),
107  'import_name' => array('text', (string)$this->import_name),
108  'pos_status' => array('integer', (int)$this->status)
109  ));
110 
111  return true;
112  }
113 
114  return false;
115  }
116 
117  public function update()
118  {
119  global $ilDB;
120 
121  if($this->id)
122  {
123  $ilDB->update('frm_posts',
124  array(
125  'pos_top_fk' => array('integer', $this->forum_id),
126  'pos_thr_fk' => array('integer', $this->thread_id),
127  'pos_subject' => array('text', $this->subject),
128  'pos_message' => array('clob', $this->message),
129  'pos_update' => array('timestamp', $this->changedate),
130  'update_user' => array('integer', $this->user_id_update),
131  'pos_cens' => array('integer', $this->censored),
132  'pos_cens_com' => array('text', $this->censorship_comment),
133  'notify' => array('integer', (int)$this->notification),
134  'pos_status' => array('integer', (int)$this->status)
135  ),
136  array(
137  'pos_pk' => array('integer', (int)$this->id)
138  )
139  );
140 
141  if($this->objThread->getFirstPostId() == $this->id)
142  {
143  $this->objThread->setSubject($this->subject);
144  $this->objThread->update();
145  $this->objThread->reload();
146  }
147 
148  return true;
149  }
150 
151  return false;
152  }
153 
154  public function getDataAsArray()
155  {
156  $data = array(
157  'pos_pk' => $this->id,
158  'pos_top_fk' => $this->forum_id,
159  'pos_thr_fk' => $this->thread_id,
160  'pos_usr_id' => $this->user_id,
161  'pos_usr_alias' => $this->user_alias,
162  'title' => $this->fullname,
163  'loginname' => $this->loginname,
164  'pos_message' => $this->message,
165  'pos_subject' => $this->subject,
166  'pos_cens_com' => $this->censorship_comment,
167  'pos_cens' => $this->censored,
168  'pos_date' => $this->createdate,
169  'pos_update' => $this->changedate,
170  'update_user' => $this->user_id_update,
171  'notify' => $this->notification,
172  'import_name' => $this->import_name,
173  'pos_status' => $this->status
174  );
175 
176  return $data;
177  }
178 
179  public function getDataAsArrayForExplorer()
180  {
181  $data = array(
182  'pos_pk' => $this->id,
183  'child' => $this->id,
184  'author' => $this->user_id,
185  'alias' => $this->user_alias,
186  'title' => $this->fullname,
187  'loginname' => $this->loginname,
188  'type' => 'post',
189  'message' => $this->message,
190  'subject' => $this->subject,
191  'pos_cens_com' => $this->censorship_comment,
192  'pos_cens' => $this->censored,
193  'date' => $this->createdate,
194  'create_date' => $this->createdate,
195  'update' => $this->changedate,
196  'update_user' => $this->user_id_update,
197  'tree' => $this->thread_id,
198  'parent' => $this->parent_id,
199  'lft' => $this->lft,
200  'rgt' => $this->rgt,
201  'depth' => $this->depth,
202  'id' => $this->tree_id,
203  'notify' => $this->notification,
204  'import_name' => $this->import_name,
205  'pos_status' => $this->status
206  );
207 
208  return $data;
209  }
210 
211  private function read()
212  {
213  if ($this->id)
214  {
215  $res = $this->db->queryf('
216  SELECT * FROM frm_posts
217  INNER JOIN frm_posts_tree ON pos_fk = pos_pk
218  WHERE pos_pk = %s',
219  array('integer'), array($this->id));
220  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
221 
222  if (is_object($row))
223  {
224  $this->id = $row->pos_pk;
225  $this->forum_id = $row->pos_top_fk;
226  $this->thread_id = $row->pos_thr_fk;
227  $this->user_id = $row->pos_usr_id;
228  $this->user_alias = $row->pos_usr_alias;
229  $this->subject = $row->pos_subject;
230  $this->message = $row->pos_message;
231  $this->createdate = $row->pos_date;
232  $this->changedate = $row->pos_update;
233  $this->user_id_update = $row->update_user;
234  $this->censored = $row->pos_cens;
235  $this->censorship_comment = $row->pos_cens_com;
236  $this->notification = $row->notify;
237  $this->import_name = $row->import_name ;
238  $this->status = $row->pos_status;
239  $this->tree_id = $row->fpt_pk;
240  $this->parent_id = $row->parent_pos;
241  $this->lft = $row->lft;
242  $this->rgt = $row->rgt;
243  $this->depth = $row->depth;
244 
245  $this->getUserData();
246 
247  $this->objThread = new ilForumTopic($this->thread_id, $this->is_moderator);
248 
249  return true;
250  }
251 
252  return false;
253  }
254 
255  return false;
256  }
257 
258  public function isAnyParentDeactivated()
259  {
260  if ($this->id)
261  {
262  $res = $this->db->queryf('
263  SELECT * FROM frm_posts_tree
264  INNER JOIN frm_posts ON pos_pk = pos_fk
265  WHERE pos_status = %s
266  AND lft < %s AND rgt > %s
267  AND thr_fk = %s',
268  array('integer', 'integer', 'integer', 'integer'),
269  array('0', $this->lft, $this->rgt, $this->thread_id));
270 
271  return $res->numRows();
272  }
273 
274  return false;
275  }
276 
277  protected function buildUserRelatedData($row)
278  {
279  global $lng;
280 
281  if ($row['pos_usr_id'] && $row['pos_pk'])
282  {
283  require_once 'Services/User/classes/class.ilObjUser.php';
284  $tmp_user = new ilObjUser();
285  $tmp_user->setFirstname($row['firstname']);
286  $tmp_user->setLastname($row['lastname']);
287  $tmp_user->setUTitle($row['title']);
288  $tmp_user->setLogin($row['login']);
289 
290  $this->fullname = $tmp_user->getFullname();
291  $this->loginname = $tmp_user->getLogin();
292 
293  $this->fullname = $this->fullname ? $this->fullname : ($this->import_name ? $this->import_name : $lng->txt('unknown'));
294 
295  return true;
296  }
297  }
298 
299  private function getUserData()
300  {
301  global $lng;
302 
303  if ($this->id && $this->user_id)
304  {
305  require_once("Modules/Forum/classes/class.ilObjForumAccess.php");
306  if(($tmp_user = ilObjForumAccess::getCachedUserInstance($this->user_id)))
307  {
308  $this->fullname = $tmp_user->getFullname();
309  $this->loginname = $tmp_user->getLogin();
310  unset($tmp_user);
311  }
312 
313  $this->fullname = $this->fullname ? $this->fullname : ($this->import_name ? $this->import_name : $lng->txt('unknown'));
314 
315  return true;
316  }
317 
318  return false;
319  }
320 
321  public function reload()
322  {
323  return $this->read();
324  }
325 
326  public function setFullname($a_fullname)
327  {
328  $this->fullname = $a_fullname;
329  }
330  public function getFullname()
331  {
332  return $this->fullname;
333  }
334  public function setLoginName($a_loginname)
335  {
336  $this->loginname = $a_loginname;
337  }
338  public function getLoginName()
339  {
340  return $this->loginname;
341  }
342 
343  public function activatePost()
344  {
345  if ($this->id)
346  {
347  $this->db->update('frm_posts',
348  array('pos_status' => array('integer', 1)),
349  array('pos_pk' => array('integer', $this->id)));
350 
351  $this->activateParentPosts();
352 
353  return true;
354  }
355 
356  return false;
357  }
358 
359  public function activatePostAndChildPosts()
360  {
361  if ($this->id)
362  {
363  $query = "SELECT pos_pk FROM frm_posts_tree treea "
364  . "INNER JOIN frm_posts_tree treeb ON treeb.thr_fk = treea.thr_fk "
365  . "AND treeb.lft BETWEEN treea.lft AND treea.rgt "
366  . "INNER JOIN frm_posts ON pos_pk = treeb.pos_fk "
367  . "WHERE treea.pos_fk = %s";
368  $result = $this->db->queryF(
369  $query,
370  array('integer'),
371  array($this->id)
372  );
373 
374  while($row = $this->db->fetchAssoc($result))
375  {
376  $this->db->update('frm_posts',
377  array('pos_status' => array('integer', 1)),
378  array('pos_pk' => array('integer', $row['pos_pk'])));
379  }
380 
381  $this->activateParentPosts();
382 
383  return true;
384  }
385 
386  return false;
387  }
388 
389  public function activateParentPosts()
390  {
391  if ($this->id)
392  {
393  $query = "SELECT pos_pk FROM frm_posts "
394  . "INNER JOIN frm_posts_tree ON pos_fk = pos_pk "
395  . "WHERE lft < %s AND rgt > %s AND thr_fk = %s";
396  $result = $this->db->queryF(
397  $query,
398  array('integer', 'integer', 'integer'),
399  array($this->lft, $this->rgt, $this->thread_id)
400  );
401 
402  while($row = $this->db->fetchAssoc($result))
403  {
404  $this->db->update('frm_posts',
405  array('pos_status' => array('integer', 1)),
406  array('pos_pk' => array('integer', $row['pos_pk'])));
407  }
408 
409  return true;
410  }
411 
412  return false;
413  }
414 
415  public function deactivatePostAndChildPosts()
416  {
417  if ($this->id)
418  {
419  $query = "SELECT pos_pk FROM frm_posts_tree treea "
420  . "INNER JOIN frm_posts_tree treeb ON treeb.thr_fk = treea.thr_fk "
421  . "AND treeb.lft BETWEEN treea.lft AND treea.rgt "
422  . "INNER JOIN frm_posts ON pos_pk = treeb.pos_fk "
423  . "WHERE treea.pos_fk = %s";
424  $result = $this->db->queryF(
425  $query,
426  array('integer'),
427  array($this->id)
428  );
429 
430  while($row = $this->db->fetchAssoc($result))
431  {
432  $this->db->update('frm_posts',
433  array('pos_status' => array('integer', 0)),
434  array('pos_pk' => array('integer', $row['pos_pk'])));
435  }
436 
437  return true;
438  }
439 
440  return false;
441  }
442 
443  public function isPostRead()
444  {
445  return $this->getIsRead();
446  }
447 
448  public function isRead($a_user_id = 0)
449  {
450  if ($a_user_id && $this->id)
451  {
452 
453  $res = $this->db->queryf('
454  SELECT * FROM frm_user_read
455  WHERE usr_id = %s
456  AND post_id = %s',
457  array('integer', 'integer'),
458  array($a_user_id, $this->id));
459 
460  return $res->numRows() ? true : false;
461  }
462 
463  return false;
464  }
465 
466  public function hasReplies()
467  {
468  if ($this->id && $this->rgt && $this->lft)
469  {
470  $res = $this->db->queryf('
471  SELECT * FROM frm_posts_tree
472  WHERE lft > %s AND rgt < %s
473  AND thr_fk = %s',
474  array('integer', 'integer', 'integer'),
475  array($this->lft, $this->rgt, $this->thread_id));
476 
477  return $res->numRows() ? true : false;
478  }
479 
480  return false;
481  }
482 
483  public function isOwner($a_user_id = 0)
484  {
485  if ($this->user_id && $a_user_id)
486  {
487  if ((int) $this->user_id == (int) $a_user_id)
488  {
489  return true;
490  }
491  return false;
492  }
493  return false;
494  }
495 
496  public function setId($a_id)
497  {
498  $this->id = $a_id;
499  }
500  public function getId()
501  {
502  return $this->id;
503  }
504  public function setForumId($a_forum_id)
505  {
506  $this->forum_id = $a_forum_id;
507  }
508  public function getForumId()
509  {
510  return $this->forum_id;
511  }
512  public function setThreadId($a_thread_id)
513  {
514  $this->thread_id = $a_thread_id;
515  }
516  public function getThreadId()
517  {
518  return $this->thread_id;
519  }
520  public function setUserId($a_user_id)
521  {
522  $this->user_id = $a_user_id;
523  }
524  public function getUserId()
525  {
526  return $this->user_id;
527  }
528  public function setUserAlias($a_user_alias)
529  {
530  $this->user_alias = $a_user_alias;
531  }
532  public function getUserAlias()
533  {
534  return $this->user_alias;
535  }
536  public function setSubject($a_subject)
537  {
538  $this->subject = $a_subject;
539  }
540  public function getSubject()
541  {
542  return $this->subject;
543  }
544  public function setMessage($a_message)
545  {
546  $this->message = $a_message;
547  }
548  public function getMessage()
549  {
550  return $this->message;
551  }
552  public function setCreateDate($a_createdate)
553  {
554  $this->createdate = $a_createdate;
555  }
556  public function getCreateDate()
557  {
558  return $this->createdate;
559  }
560  public function setChangeDate($a_changedate)
561  {
562  $this->changedate = $a_changedate;
563  }
564  public function getChangeDate()
565  {
566  return $this->changedate;
567  }
568  public function setUpdateUserId($a_user_id_update)
569  {
570  $this->user_id_update = $a_user_id_update;
571  }
572  public function getUpdateUserId()
573  {
574  return $this->user_id_update;
575  }
576  public function setCensorship($a_censorship)
577  {
578  $this->censored = $a_censorship;
579  }
580  public function isCensored()
581  {
582  return $this->censored == 1 ? true : false;
583  }
584  public function setCensorshipComment($a_comment)
585  {
586  $this->censorship_comment = $a_comment;
587  }
588  public function getCensorshipComment()
589  {
591  }
592  public function setNotification($a_notification)
593  {
594  $this->notification = $a_notification;
595  }
596  public function isNotificationEnabled()
597  {
598  return $this->notification == 1 ? true : false;
599  }
600  public function setImportName($a_import_name)
601  {
602  $this->import_name = $a_import_name;
603  }
604  public function getImportName()
605  {
606  return $this->import_name;
607  }
608  public function setStatus($a_status)
609  {
610  $this->status = $a_status;
611  }
612  public function isActivated()
613  {
614  return $this->status == 1 ? true : false;
615  }
616  public function setTreeId($a_tree_id)
617  {
618  $this->tree_id = $a_tree_id;
619  }
620  public function getTreeId()
621  {
622  return $this->tree_id;
623  }
624  public function setParentId($a_parent_id)
625  {
626  $this->parent_id = $a_parent_id;
627  }
628 
629  public function setIsRead($a_is_read)
630  {
631  $this->post_read = $a_is_read;
632  }
633 
634  public function getIsRead()
635  {
636  return $this->post_read;
637  }
638 
639  public function getParentId()
640  {
641  return $this->parent_id;
642  }
643  public function setLft($a_lft)
644  {
645  $this->lft = $a_lft;
646  }
647  public function getLft()
648  {
649  return $this->lft;
650  }
651  public function setRgt($a_rgt)
652  {
653  $this->rgt = $a_rgt;
654  }
655  public function getRgt()
656  {
657  return $this->rgt;
658  }
659  public function setDepth($a_depth)
660  {
661  $this->depth = $a_depth;
662  }
663  public function getDepth()
664  {
665  return $this->depth;
666  }
667  public function setThread(ilForumTopic $thread)
668  {
669  $this->objThread = $thread;
670  }
671  public function getThread()
672  {
673  return $this->objThread;
674  }
675 
676  public function assignData($row)
677  {
678  $this->setUserAlias($row['pos_usr_alias']);
679  $this->setSubject($row['pos_subject']);
680  $this->setCreateDate($row['pos_date']);
681  $this->setMessage($row['pos_message']);
682  $this->setForumId($row['pos_top_fk']);
683  $this->setThreadId($row['pos_thr_fk']);
684  $this->setChangeDate($row['pos_update']);
685  $this->setUpdateUserId($row['update_user']);
686  $this->setCensorship($row['pos_cens']);
687  $this->setCensorshipComment($row['pos_cens_com']);
688  $this->setNotification($row['notify']);
689  $this->setImportName($row['import_name']);
690  $this->setStatus($row['pos_status']);
691  $this->setTreeId($row['fpt_pk']);
692  $this->setParentId($row['parent_pos']);
693  $this->setLft($row['lft']);
694  $this->setRgt($row['rgt']);
695  $this->setDepth($row['depth']);
696  $this->setIsRead($row['post_read']);
697  $this->setUserId($row['pos_usr_id']);
698  $this->buildUserRelatedData($row);
699  }
700 }
701 ?>