ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilForum.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.ilForumProperties.php';
5 require_once './Modules/Forum/classes/class.ilObjForum.php';
6 require_once './Modules/Forum/classes/class.ilForumTopic.php';
7 require_once './Modules/Forum/classes/class.ilForumPost.php';
8 
18 class ilForum
19 {
20  const SORT_TITLE = 1;
21  const SORT_DATE = 2;
22 
28  public $ilias;
29  public $lng;
30 
37  private $dbTable;
38 
44  private $className="ilForum";
45 
52  private $orderField;
53 
54  private $mdb2Query;
55  private $mdb2DataValue;
56  private $mdb2DataType;
57 
58  private $txtQuote1 = "[quote]";
59  private $txtQuote2 = "[/quote]";
60  private $replQuote1 = '<blockquote class="ilForumQuote">';
61  private $replQuote2 = '</blockquote>';
62 
63  // max. datasets per page
64  private $pageHits = 30;
65 
66  // object id
67  private $id;
68 
73  public function __construct()
74  {
75  global $ilias,$lng;
76 
77  $this->ilias = $ilias;
78  $this->lng = $lng;
79  }
80 
81  public function setLanguage($lng)
82  {
83  $this->lng = $lng;
84  }
85 
96  public static function _getLanguageInstanceByUsrId($usr_id)
97  {
98  static $lngCache = array();
99 
100  $languageShorthandle = ilObjUser::_lookupLanguage($usr_id);
101 
102  // lookup in cache array
103  if(!isset($lngCache[$languageShorthandle]))
104  {
105  $lngCache[$languageShorthandle] = new ilLanguage($languageShorthandle);
106  $lngCache[$languageShorthandle]->loadLanguageModule('forum');
107  }
108 
109  return $lngCache[$languageShorthandle];
110  }
111 
117  public function setForumId($a_obj_id)
118  {
119 
120  if (!isset($a_obj_id))
121  {
122  $message = get_class($this)."::setForumId(): No obj_id given!";
123  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
124  }
125 
126  $this->id = $a_obj_id;
127  }
128 
134  public function setForumRefId($a_ref_id)
135  {
136  if (!isset($a_ref_id))
137  {
138  $message = get_class($this)."::setForumRefId(): No ref_id given!";
139  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
140  }
141 
142  $this->ref_id = $a_ref_id;
143  }
144 
151  public function getForumId()
152  {
153  return $this->id;
154  }
155 
161  public function getForumRefId()
162  {
163  return $this->ref_id;
164  }
165 
172  private function setOrderField($orderField)
173  {
174  if ($orderField == "")
175  {
176  die($this->className . "::setOrderField(): No orderField given.");
177  }
178  else
179  {
180  $this->orderField = $orderField;
181  }
182  }
183 
190  public function getOrderField()
191  {
192  return $this->orderField;
193  }
194 
201  public function setDbTable($dbTable)
202  {
203  if ($dbTable == "")
204  {
205  die($this->className . "::setDbTable(): No database table given.");
206  }
207  else
208  {
209  $this->dbTable = $dbTable;
210  }
211  }
212 
219  public function getDbTable()
220  {
221  return $this->dbTable;
222  }
223 
224 
234  public function setMDB2WhereCondition($query_string, $data_type, $data_value)
235  {
236  $this->mdb2Query = $query_string;
237  $this->mdb2DataValue = $data_value;
238  $this->mdb2DataType = $data_type;
239 
240  return true;
241  }
242 
243 
249  public function getMDB2Query()
250  {
251  if($this->mdb2Query != '')
252  {
253  return $this->mdb2Query;
254  }
255 
256  }
257 
263  public function getMDB2DataValue()
264  {
265  if($this->mdb2DataValue != '')
266  {
267  return $this->mdb2DataValue;
268  }
269  }
270 
276  public function getMDB2DataType()
277  {
278  if($this->mdb2DataType != '')
279  {
280  return $this->mdb2DataType;
281  }
282  }
283 
290  public function setPageHits($pageHits)
291  {
292  if ($pageHits < 1)
293  {
294  die($this->className . "::setPageHits(): No int pageHits given.");
295  }
296  else
297  {
298  $this->pageHits = $pageHits;
299  return true;
300  }
301  }
302 
309  public function getPageHits()
310  {
311  return $this->pageHits;
312  }
313 
314  // *******************************************************************************
315 
321  public function getOneTopic()
322  {
323  global $ilDB;
324 
325  $data_type = array();
326  $data_value = array();
327 
328  $query = 'SELECT * FROM frm_data WHERE ';
329 
330  if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
331  {
332  $query .= ''.$this->getMDB2Query().'';
333  $data_type = $data_type + $this->getMDB2DataType();
334  $data_value = $data_value + $this->getMDB2DataValue();
335 
336  $res = $ilDB->queryf($query, $data_type, $data_value);
337  $row = $ilDB->fetchAssoc($res);
338 
339  if(is_null($row)) return NULL;
340 
341  $row["top_name"] = trim($row["top_name"]);
342  $row["top_description"] = nl2br($row["top_description"]);
343 
344  return $row;
345 
346  }
347  else
348  {
349  $query .= '1 = 1';
350 
351  $res = $ilDB->query($query);
352  $row = $ilDB->fetchAssoc($res);
353 
354  if(!is_array($row) || !count($row)) return null;
355 
356  $row['top_name'] = trim($row['top_name']);
357  $row['top_description'] = nl2br($row['top_description']);
358 
359  return $row;
360  }
361  }
362 
368  public function getOneThread()
369  {
370  global $ilDB;
371 
372  $data_type = array();
373  $data_value = array();
374 
375  $query = 'SELECT * FROM frm_threads WHERE ';
376 
377  if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
378  {
379  $query .= $this->getMDB2Query();
380  $data_type = $data_type + $this->getMDB2DataType();
381  $data_value = $data_value + $this->getMDB2DataValue();
382 
383  $sql_res = $ilDB->queryf($query, $data_type, $data_value);
384  $result = $ilDB->fetchAssoc($sql_res);
385  $result["thr_subject"] = trim($result["thr_subject"]);
386  }
387 
388  return $result;
389  }
390 
397  public function getOnePost($post)
398  {
399  global $ilDB;
400 
401  $res = $ilDB->queryf('
402  SELECT frm_posts.*, usr_data.lastname FROM frm_posts, usr_data
403  WHERE pos_pk = %s
404  AND pos_display_user_id = usr_id',
405  array('integer'), array($post));
406 
407  $row = $ilDB->fetchAssoc($res);
408 
409 
410  $row["pos_date"] = $this->convertDate($row["pos_date"]);
411  $row["pos_message"] = nl2br($row["pos_message"]);
412 
413  return $row;
414  }
415 
416  public function _lookupPostMessage($a_id)
417  {
418  global $ilDB;
419 
420  $res = $ilDB->queryf('
421  SELECT * FROM frm_posts WHERE pos_pk = %s',
422  array('integer'), array($a_id));
423 
424  while($row = $ilDB->fetchObject($res))
425  {
426  return $row->pos_message;
427  }
428  return '';
429  }
430 
446  public function generatePost($forum_id, $thread_id, $author_id, $display_user_id, $message, $parent_pos, $notify, $subject = '', $alias = '', $date = '', $status = 1, $send_activation_mail = 0)
447  {
448  global $ilDB;
449 
450  $objNewPost = new ilForumPost();
451  $objNewPost->setForumId($forum_id);
452  $objNewPost->setThreadId($thread_id);
453  $objNewPost->setSubject($subject);
454  $objNewPost->setMessage($message);
455  $objNewPost->setDisplayUserId($display_user_id);
456  $objNewPost->setUserAlias($alias);
457  $objNewPost->setPosAuthorId($author_id);
458 
459  $this->_isModerator($this->getForumRefId(), $author_id) ? $is_moderator = true : $is_moderator = false;
460  $objNewPost->setIsAuthorModerator($is_moderator);
461 
462  if ($date == "")
463  {
464  $objNewPost->setCreateDate(date("Y-m-d H:i:s"));
465  }
466  else
467  {
468  if (strpos($date, "-") > 0) // in mysql format
469  {
470  $objNewPost->setCreateDate($date);
471  }
472  else // a timestamp
473  {
474  $objNewPost->setCreateDate(date("Y-m-d H:i:s", $date));
475  }
476  }
477 
478  $objNewPost->setImportName($this->getImportName());
479  $objNewPost->setNotification($notify);
480  $objNewPost->setStatus($status);
481  $objNewPost->insert();
482 
483  // entry in tree-table
484  if ($parent_pos == 0)
485  {
486  $this->addPostTree($objNewPost->getThreadId(), $objNewPost->getId(), $objNewPost->getCreateDate());
487  }
488  else
489  {
490  $this->insertPostNode($objNewPost->getId(), $parent_pos, $objNewPost->getThreadId(), $objNewPost->getCreateDate());
491  }
492 //echo "<br>->".$objNewPost->getId()."-".$parent_pos."-".$objNewPost->getThreadId()."-".
493 // $objNewPost->getCreateDate()."-".$forum_id."-".$message."-".$user."-";
494  // string last post
495  $lastPost = $objNewPost->getForumId()."#".$objNewPost->getThreadId()."#".$objNewPost->getId();
496 
497  // update thread
498  $result = $ilDB->manipulateF('
499  UPDATE frm_threads
500  SET thr_num_posts = thr_num_posts + 1,
501  thr_last_post = %s
502  WHERE thr_pk = %s',
503  array('text', 'integer'),
504  array($lastPost, $objNewPost->getThreadId()));
505 
506  // update forum
507  $result = $ilDB->manipulateF('
508  UPDATE frm_data
509  SET top_num_posts = top_num_posts + 1,
510  top_last_post = %s
511  WHERE top_pk = %s',
512  array('text', 'integer'),
513  array($lastPost, $objNewPost->getForumId()));
514 
515  // MARK READ
516  $forum_obj = ilObjectFactory::getInstanceByRefId($this->getForumRefId());
517  $forum_obj->markPostRead($objNewPost->getPosAuthorId(), $objNewPost->getThreadId(), $objNewPost->getId());
518 
519  $pos_data = $objNewPost->getDataAsArray();
520  $pos_data["ref_id"] = $this->getForumRefId();
521 
522  // Send notification to moderators if they have to enable a post
523 
524  if (!$status && $send_activation_mail)
525  {
526  $pos_data["top_name"] = $forum_obj->getTitle();
527  $this->sendPostActivationNotification($pos_data);
528  }
529 
530  // Add Notification to news
531  if ($status)
532  {
533  require_once 'Services/RTE/classes/class.ilRTE.php';
534  include_once("./Services/News/classes/class.ilNewsItem.php");
535  $news_item = new ilNewsItem();
536  $news_item->setContext($forum_obj->getId(), 'frm', $objNewPost->getId(), 'pos');
537  $news_item->setPriority(NEWS_NOTICE);
538  $news_item->setTitle($objNewPost->getSubject());
539  $news_item->setContent(ilRTE::_replaceMediaObjectImageSrc($this->prepareText($objNewPost->getMessage(), 0), 1));
540  $news_item->setUserId($display_user_id);
541  $news_item->setVisibility(NEWS_USERS);
542  $news_item->create();
543  }
544 
545  return $objNewPost->getId();
546  }
547 
562  public function generateThread($forum_id, $author_id, $display_user_id, $subject, $message, $notify, $notify_posts, $alias = '', $date = '', $status = 1)
563  {
564  global $ilDB;
565 
566  $objNewThread = new ilForumTopic();
567  $objNewThread->setForumId($forum_id);
568  $objNewThread->setDisplayUserId($display_user_id);
569  $objNewThread->setSubject($subject);
570  $objNewThread->setThrAuthorId($author_id);
571 
572  if ($date == "")
573  {
574  $objNewThread->setCreateDate(date("Y-m-d H:i:s"));
575  }
576  else
577  {
578  if (strpos($date, "-") > 0) // in mysql format
579  {
580  $objNewThread->setCreateDate($date);
581  }
582  else // a timestamp
583  {
584  $objNewThread->setCreateDate(date("Y-m-d H:i:s", $date));
585  }
586  }
587  $objNewThread->setImportName($this->getImportName());
588  $objNewThread->setUserAlias($alias);
589  $objNewThread->insert();
590 
591  if ($notify_posts == 1)
592  {
593  $objNewThread->enableNotification($author_id);
594  }
595 
596  // update forum
597  $statement = $ilDB->manipulateF('
598  UPDATE frm_data
599  SET top_num_threads = top_num_threads + 1
600  WHERE top_pk = %s',
601  array('integer'), array($forum_id));
602 
603  return $this->generatePost($forum_id, $objNewThread->getId(), $author_id, $display_user_id, $message, 0, $notify, $subject, $alias, $objNewThread->getCreateDate(), $status, 0);
604  }
605 
614  public function moveThreads($thread_ids = array(), $src_ref_id = 0, $dest_top_frm_fk = 0)
615  {
616  global $ilDB;
617 
618  $src_top_frm_fk = ilObject::_lookupObjectId($src_ref_id);
619 
620  if (is_numeric($src_top_frm_fk) && $src_top_frm_fk > 0 && is_numeric($dest_top_frm_fk) && $dest_top_frm_fk > 0)
621  {
622 
623  $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($src_top_frm_fk));
624 
625  $oldFrmData = $this->getOneTopic();
626 
627  $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($dest_top_frm_fk));
628 
629  $newFrmData = $this->getOneTopic();
630 
631  if ($oldFrmData['top_pk'] && $newFrmData['top_pk'])
632  {
633  $moved_posts = 0;
634  $moved_threads = 0;
635  $visits = 0;
636  foreach ($thread_ids as $id)
637  {
638  $objTmpThread = new ilForumTopic($id);
639 
640  $numPosts = $objTmpThread->movePosts($src_top_frm_fk, $oldFrmData['top_pk'], $dest_top_frm_fk, $newFrmData['top_pk']);
641  if (($last_post_string = $objTmpThread->getLastPostString()) != '')
642  {
643  $last_post_string = explode('#', $last_post_string);
644  $last_post_string[0] = $newFrmData['top_pk'];
645  $last_post_string = implode('#', $last_post_string);
646  $objTmpThread->setLastPostString($last_post_string);
647  }
648 
649  $visits += $objTmpThread->getVisits();
650 
651  $moved_posts += $numPosts;
652  ++$moved_threads;
653 
654  $objTmpThread->setForumId($newFrmData['top_pk']);
655  $objTmpThread->update();
656 
657  unset($objTmpThread);
658  }
659 
660  // update frm_data source forum
661  $ilDB->setLimit(1);
662  $res = $ilDB->queryf('
663  SELECT pos_thr_fk, pos_pk
664  FROM frm_posts
665  WHERE pos_top_fk = %s
666  ORDER BY pos_date DESC',
667  array('integer'), array($oldFrmData['top_pk']));
668 
669  $row = $ilDB->fetchObject($res);
670  $last_post_src = $oldFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
671 
672  $statement = $ilDB->manipulateF('
673  UPDATE frm_data
674  SET top_num_posts = top_num_posts - %s,
675  top_num_threads = top_num_threads - %s,
676  visits = visits - %s,
677  top_last_post = %s
678  WHERE top_pk = %s',
679  array('integer', 'integer', 'integer', 'text', 'integer'),
680  array( $moved_posts,
681  $moved_threads,
682  $visits,
683  $last_post_src,
684  $oldFrmData['top_pk']));
685 
686  // update frm_data destination forum
687 
688  $ilDB->setLimit(1);
689  $res = $ilDB->queryf('
690  SELECT pos_thr_fk, pos_pk
691  FROM frm_posts
692  WHERE pos_top_fk = %s
693  ORDER BY pos_date DESC',
694  array('integer'), array($newFrmData['top_kp']));
695 
696  $row = $ilDB->fetchObject($res);
697  $last_post_dest = $newFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
698 
699  $statement = $ilDB->manipulateF('
700  UPDATE frm_data
701  SET top_num_posts = top_num_posts + %s,
702  top_num_threads = top_num_threads + %s,
703  visits = visits + %s,
704  top_last_post = %s
705  WHERE top_pk = %s',
706  array('integer', 'integer', 'integer', 'text', 'integer'),
707  array($moved_posts, $moved_threads, $visits, $last_post_dest, $newFrmData['top_pk']));
708 
709  /*
710  // update news items
711  include_once("./Services/News/classes/class.ilNewsItem.php");
712  $objNewsItem = new ilNewsItem();
713  $news_items = $objNewsItem->getNewsForRefId($src_ref_id);
714  foreach ($news_items as $news_item)
715  {
716  $tmpObjNewsItem = new ilNewsItem($news_item['id']);
717  if ($tmpObjNewsItem->getContextObjId() == $src_top_frm_fk)
718  {
719  $tmpObjNewsItem->setContextObjId($dest_top_frm_fk);
720  $tmpObjNewsItem->update();
721  }
722  unset($tmpObjNewsItem);
723  }
724  */
725  }
726  }
727 }
728 
729 
737  public function postCensorship($message, $pos_pk, $cens = 0)
738  {
739  global $ilDB;
740 
741  $statement = $ilDB->manipulateF('
742  UPDATE frm_posts
743  SET pos_cens_com = %s,
744  pos_update = %s,
745  pos_cens = %s,
746  update_user = %s
747  WHERE pos_pk = %s',
748  array('text', 'timestamp', 'integer', 'integer', 'integer'),
749  array($message, date("Y-m-d H:i:s"), $cens, $_SESSION['AccountId'], $pos_pk));
750 
751  // Change news item accordingly
752  include_once("./Services/News/classes/class.ilNewsItem.php");
753  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
754  "frm", $pos_pk, "pos");
755  if ($news_id > 0)
756  {
757  if ($cens > 0) // censor
758  {
759  $news_item = new ilNewsItem($news_id);
760  //$news_item->setTitle($subject);
761  $news_item->setContent(nl2br($this->prepareText($message, 0)));
762  $news_item->update();
763  }
764  else // revoke censorship
765  {
766  // get original message
767  $res = $ilDB->queryf('
768  SELECT * FROM frm_posts
769  WHERE pos_pk = %s',
770  array('integer'), array($pos_pk));
771 
772  $rec = $ilDB->fetchAssoc($res);
773 
774  $news_item = new ilNewsItem($news_id);
775  //$news_item->setTitle($subject);
776  $news_item->setContent(nl2br($this->prepareText($rec["pos_message"], 0)));
777  $news_item->update();
778  }
779  }
780 
781  return true;
782  }
783 
790  public function deletePost($post)
791  {
792  global $ilDB;
793 
794  include_once "./Modules/Forum/classes/class.ilObjForum.php";
795 
796  // delete tree and get id's of all posts to delete
797  $p_node = $this->getPostNode($post);
798  $del_id = $this->deletePostTree($p_node);
799 
800 
801  // Delete User read entries
802  foreach($del_id as $post_id)
803  {
804  ilObjForum::_deleteReadEntries($post_id);
805  }
806 
807  // DELETE ATTACHMENTS ASSIGNED TO POST
808  $this->__deletePostFiles($del_id);
809 
810  $dead_pos = count($del_id);
811  $dead_thr = 0;
812 
813  // if deletePost is thread opener ...
814  if ($p_node["parent"] == 0)
815  {
816  // delete thread access data
817  include_once './Modules/Forum/classes/class.ilObjForum.php';
818 
819  ilObjForum::_deleteAccessEntries($p_node['tree']);
820 
821  // delete thread
822  $dead_thr = $p_node["tree"];
823 
824  $statement = $ilDB->manipulateF('
825  DELETE FROM frm_threads
826  WHERE thr_pk = %s',
827  array('integer'), array($p_node['tree']));
828 
829  // update num_threads
830  $statement = $ilDB->manipulateF('
831  UPDATE frm_data
832  SET top_num_threads = top_num_threads - 1
833  WHERE top_frm_fk = %s',
834  array('integer'), array($this->id));
835 
836  // delete all related news
837  $posset = $ilDB->queryf('
838  SELECT * FROM frm_posts
839  WHERE pos_thr_fk = %s',
840  array('integer'), array($p_node['tree']));
841 
842  while ($posrec = $ilDB->fetchAssoc($posset))
843  {
844  include_once("./Services/News/classes/class.ilNewsItem.php");
845  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
846  "frm", $posrec["pos_pk"], "pos");
847  if ($news_id > 0)
848  {
849  $news_item = new ilNewsItem($news_id);
850  $news_item->delete();
851  }
852 
853  try
854  {
855  include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
856  $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $posrec['pos_pk']);
857  foreach($mobs as $mob)
858  {
859  if(ilObjMediaObject::_exists($mob))
860  {
861  ilObjMediaObject::_removeUsage($mob, 'frm:html', $posrec['pos_pk']);
862  $mob_obj = new ilObjMediaObject($mob);
863  $mob_obj->delete();
864  }
865  }
866  }
867  catch(Exception $e)
868  {
869  }
870  }
871 
872 
873  // delete all posts of this thread
874  $statement = $ilDB->manipulateF('
875  DELETE FROM frm_posts
876  WHERE pos_thr_fk = %s',
877  array('integer'), array($p_node['tree']));
878 
879  }
880  else
881  {
882 
883  // delete this post and its sub-posts
884  for ($i = 0; $i < $dead_pos; $i++)
885  {
886  $statement = $ilDB->manipulateF('
887  DELETE FROM frm_posts
888  WHERE pos_pk = %s',
889  array('integer'), array($del_id[$i]));
890 
891  // delete related news item
892  include_once("./Services/News/classes/class.ilNewsItem.php");
893  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
894  "frm", $del_id[$i], "pos");
895  if ($news_id > 0)
896  {
897  $news_item = new ilNewsItem($news_id);
898  $news_item->delete();
899  }
900 
901  try
902  {
903  include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
904  $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $del_id[$i]);
905  foreach($mobs as $mob)
906  {
907  if(ilObjMediaObject::_exists($mob))
908  {
909  ilObjMediaObject::_removeUsage($mob, 'frm:html', $del_id[$i]);
910  $mob_obj = new ilObjMediaObject($mob);
911  $mob_obj->delete();
912  }
913  }
914  }
915  catch(Exception $e)
916  {
917  }
918  }
919 
920  // update num_posts in frm_threads
921  $statement = $ilDB->manipulateF('
922  UPDATE frm_threads
923  SET thr_num_posts = thr_num_posts - %s
924  WHERE thr_pk = %s',
925  array('integer', 'integer'),
926  array($dead_pos, $p_node['tree']));
927 
928 
929  // get latest post of thread and update last_post
930  $res1 = $ilDB->queryf('
931  SELECT * FROM frm_posts
932  WHERE pos_thr_fk = %s
933  ORDER BY pos_date DESC',
934  array('integer'), array($p_node['tree']));
935 
936  if ($res1->numRows() == 0)
937  {
938  $lastPost_thr = "";
939  }
940  else
941  {
942  $z = 0;
943 
944  while ($selData = $ilDB->fetchAssoc($res1))
945  {
946  if ($z > 0)
947  {
948  break;
949  }
950 
951  $lastPost_thr = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
952  $z ++;
953  }
954  }
955 
956  $statement = $ilDB->manipulateF('
957  UPDATE frm_threads
958  SET thr_last_post = %s
959  WHERE thr_pk = %s',
960  array('text', 'integer'), array($lastPost_thr, $p_node['tree']));
961  }
962 
963  // update num_posts in frm_data
964  $statement = $ilDB->manipulateF('
965  UPDATE frm_data
966  SET top_num_posts = top_num_posts - %s
967  WHERE top_frm_fk = %s',
968  array('integer', 'integer'), array($dead_pos, $this->id));
969 
970 
971  // get latest post of forum and update last_post
972  $res2 = $ilDB->queryf('
973  SELECT * FROM frm_posts, frm_data
974  WHERE pos_top_fk = top_pk
975  AND top_frm_fk = %s
976  ORDER BY pos_date DESC',
977  array('integer'), array($this->id));
978 
979  if ($res2->numRows() == 0)
980  {
981  $lastPost_top = "";
982  }
983  else
984  {
985  $z = 0;
986 
987  while ($selData = $ilDB->fetchAssoc($res2))
988  {
989  if ($z > 0)
990  {
991  break;
992  }
993 
994  $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
995  $z ++;
996  }
997  }
998 
999  $statement = $ilDB->manipulateF('
1000  UPDATE frm_data
1001  SET top_last_post = %s
1002  WHERE top_frm_fk = %s',
1003  array('text', 'integer'), array($lastPost_top, $this->id));
1004 
1005  return $dead_thr;
1006  }
1007 
1015  public function getAllThreads($a_topic_id, array $params = array(), $limit = 0, $offset = 0)
1016  {
1022  global $ilDB, $ilUser, $ilSetting;
1023 
1024  $frm_overview_setting = (int)$ilSetting::_lookupValue('frma','forum_overview');
1025  $frm_props = ilForumProperties::getInstance($this->getForumId());
1026 
1027  $excluded_ids_condition = '';
1028  if(isset($params['excluded_ids']) && is_array($params['excluded_ids']) && $params['excluded_ids'])
1029  {
1030  $excluded_ids_condition = ' AND ' . $ilDB->in('thr_pk', $params['excluded_ids'], true, 'integer'). ' ';
1031  }
1032 
1033  if(!in_array(strtolower($params['order_column']), array('lp_date', 'rating')))
1034  {
1035  $params['order_column'] = 'post_date';
1036  }
1037  if(!in_array(strtolower($params['order_direction']), array('asc', 'desc')))
1038  {
1039  $params['order_direction'] = 'desc';
1040  }
1041 
1042  // Count all threads for the passed forum
1043  $query = "SELECT COUNT(thr_pk) cnt
1044  FROM frm_threads
1045  WHERE thr_top_fk = %s {$excluded_ids_condition}";
1046  $res = $ilDB->queryF($query, array('integer'), array($a_topic_id));
1047  $data = $ilDB->fetchAssoc($res);
1048  $cnt = (int) $data['cnt'];
1049 
1050  $threads = array();
1051 
1052  $data = array();
1053  $data_types = array();
1054 
1055  $active_query = '';
1056  $active_inner_query = '';
1057  $is_post_activation_enabled = $frm_props->isPostActivationEnabled();
1058  if($is_post_activation_enabled && !$params['is_moderator'])
1059  {
1060  $active_query = ' AND (pos_status = %s OR pos_author_id = %s) ';
1061  $active_inner_query = ' AND (ipos.pos_status = %s OR ipos.pos_author_id = %s) ';
1062  }
1063 
1064  $optional_fields = '';
1065  if($frm_props->isIsThreadRatingEnabled())
1066  {
1067  $optional_fields = ',avg_rating';
1068  }
1069  if($frm_props->getThreadSorting() == 1)
1070  {
1071  $optional_fields = ',thread_sorting';
1072  }
1073 
1074  $additional_sort = '';
1075  if($frm_props->getThreadSorting())
1076  {
1077  $additional_sort .= ' ,thread_sorting ASC ';
1078  }
1079 
1080  $dynamic_columns = array(
1081  ' ,post_date ' . $params['order_direction']
1082  );
1083  if($frm_props->isIsThreadRatingEnabled())
1084  {
1085  $dynamic_columns[] = ' ,avg_rating ' . $params['order_direction'];
1086  }
1087  if('rating' == strtolower($params['order_column']))
1088  {
1089  $dynamic_columns = array_reverse($dynamic_columns);
1090  }
1091  $additional_sort .= implode(' ', $dynamic_columns);
1092 
1093  if(!$ilUser->isAnonymous())
1094  {
1095  $query = "SELECT
1096  (CASE WHEN COUNT(DISTINCT(notification_id)) > 0 THEN 1 ELSE 0 END) usr_notification_is_enabled,
1097  MAX(pos_date) post_date,
1098  COUNT(DISTINCT(pos_pk)) num_posts,
1099  COUNT(DISTINCT(pos_pk)) - COUNT(DISTINCT(postread.post_id)) num_unread_posts, ";
1100 
1101  // new posts query
1102  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1103  {
1104  $query .= "
1105  (SELECT COUNT(DISTINCT(ipos.pos_pk))
1106  FROM frm_posts ipos
1107  LEFT JOIN frm_user_read iread ON iread.post_id = ipos.pos_pk AND iread.usr_id = %s
1108  LEFT JOIN frm_thread_access iacc ON (iacc.thread_id = ipos.pos_thr_fk AND iacc.usr_id = %s)
1109  WHERE ipos.pos_thr_fk = thr_pk
1110 
1111  AND (ipos.pos_update > iacc.access_old_ts
1112  OR
1113  (iacc.access_old IS NULL AND (ipos.pos_update > " . $ilDB->quote(date('Y-m-d H:i:s', NEW_DEADLINE), 'timestamp') . "))
1114  )
1115 
1116  AND ipos.pos_author_id != %s
1117  AND iread.usr_id IS NULL $active_inner_query
1118  ) num_new_posts, ";
1119  }
1120 
1121  $query .= " thr_pk, thr_top_fk, thr_subject, thr_author_id, thr_display_user_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1122  {$optional_fields}
1123  FROM frm_threads
1124 
1125  LEFT JOIN frm_notification
1126  ON frm_notification.thread_id = thr_pk
1127  AND frm_notification.user_id = %s
1128 
1129  LEFT JOIN frm_posts
1130  ON pos_thr_fk = thr_pk $active_query
1131 
1132  LEFT JOIN frm_user_read postread
1133  ON postread.post_id = pos_pk
1134  AND postread.usr_id = %s";
1135 
1136  $query .= " WHERE thr_top_fk = %s
1137  {$excluded_ids_condition}
1138  GROUP BY thr_pk, thr_top_fk, thr_subject, thr_author_id, thr_display_user_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1139  {$optional_fields}
1140  ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1141 
1142 
1143  // data_types for new posts query and $active_inner_query
1144  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1145  {
1146  $data_types[] = 'integer';
1147  $data_types[] = 'integer';
1148  $data_types[] = 'integer';
1149  if($is_post_activation_enabled && !$params['is_moderator'])
1150  {
1151  array_push($data_types, 'integer', 'integer');
1152  }
1153  }
1154  $data_types[] = 'integer';
1155  if($is_post_activation_enabled && !$params['is_moderator'])
1156  {
1157  array_push($data_types, 'integer', 'integer');
1158  }
1159  $data_types[] = 'integer';
1160  $data_types[] = 'integer';
1161 
1162  // data_values for new posts query and $active_inner_query
1163  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1164  {
1165  $data[] = $ilUser->getId();
1166  $data[] = $ilUser->getId();
1167  $data[] = $ilUser->getId();
1168  if($is_post_activation_enabled && !$params['is_moderator'])
1169  {
1170  array_push($data, '1', $ilUser->getId());
1171  }
1172  }
1173  $data[] = $ilUser->getId();
1174  if($is_post_activation_enabled && !$params['is_moderator'])
1175  {
1176  array_push($data, '1', $ilUser->getId());
1177  }
1178  $data[] = $ilUser->getId();
1179  $data[] = $a_topic_id;
1180  }
1181  else
1182  {
1183  $query = "SELECT
1184  0 usr_notification_is_enabled,
1185  MAX(pos_date) post_date,
1186  COUNT(DISTINCT(pos_pk)) num_posts,
1187  COUNT(DISTINCT(pos_pk)) num_unread_posts,
1188  COUNT(DISTINCT(pos_pk)) num_new_posts,
1189  thr_pk, thr_top_fk, thr_subject, thr_author_id, thr_display_user_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1190  {$optional_fields}
1191  FROM frm_threads
1192 
1193  LEFT JOIN frm_posts
1194  ON pos_thr_fk = thr_pk $active_query";
1195 
1196  $query .= " WHERE thr_top_fk = %s
1197  {$excluded_ids_condition}
1198  GROUP BY thr_pk, thr_top_fk, thr_subject, thr_author_id, thr_display_user_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1199  {$optional_fields}
1200  ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1201 
1202  if($is_post_activation_enabled && !$params['is_moderator'])
1203  {
1204  array_push($data_types, 'integer', 'integer');
1205  }
1206  $data_types[] = 'integer';
1207  if($is_post_activation_enabled && !$params['is_moderator'])
1208  {
1209  array_push($data, '1', $ilUser->getId());
1210  }
1211  $data[] = $a_topic_id;
1212  }
1213 
1214  if($limit || $offset)
1215  {
1216  $ilDB->setLimit($limit, $offset);
1217  }
1218  $res = $ilDB->queryF($query, $data_types, $data);
1219  while($row = $ilDB->fetchAssoc($res))
1220  {
1221  $thread = new ilForumTopic($row['thr_pk'], $params['is_moderator'], true);
1222  $thread->assignData($row);
1223  $threads[] = $thread;
1224  }
1225 
1226  return array(
1227  'items' => $threads,
1228  'cnt' => $cnt
1229  );
1230  }
1231 
1232  public function getUserStatistic($is_moderator = false)
1233  {
1234  global $ilDB, $ilUser;
1235 
1236  $statistic = array();
1237 
1238  $data_types = array();
1239  $data = array();
1240 
1241  $query = "SELECT COUNT(f.pos_display_user_id) ranking, u.login, p.value, u.lastname, u.firstname
1242  FROM frm_posts f
1243  INNER JOIN frm_posts_tree t
1244  ON f.pos_pk = t.pos_fk
1245  INNER JOIN frm_threads th
1246  ON t.thr_fk = th.thr_pk
1247  INNER JOIN usr_data u
1248  ON u.usr_id = f.pos_display_user_id
1249  INNER JOIN frm_data d
1250  ON d.top_pk = f.pos_top_fk
1251  LEFT JOIN usr_pref p
1252  ON p.usr_id = u.usr_id AND p.keyword = %s
1253  WHERE 1 = 1";
1254 
1255  array_push($data_types, 'text');
1256  array_push($data, 'public_profile');
1257 
1258  if (!$is_moderator)
1259  {
1260  $query .= ' AND (pos_status = %s
1261  OR (pos_status = %s
1262  AND pos_author_id = %s ))';
1263 
1264  array_push($data_types,'integer', 'integer', 'integer');
1265  array_push($data, '1', '0', $ilUser->getId());
1266  }
1267 
1268  $query .= ' AND d.top_frm_fk = %s
1269  GROUP BY pos_display_user_id, u.login, p.value,u.lastname, u.firstname';
1270 
1271  array_push($data_types,'integer');
1272  array_push($data, $this->getForumId());
1273 
1274 
1275 
1276  $res = $ilDB->queryf($query, $data_types, $data);
1277 
1278  $counter = 0;
1279  while ($row = $ilDB->fetchAssoc($res))
1280  {
1281  $statistic[$counter][] = $row['ranking'];
1282  $statistic[$counter][] = $row['login'];
1283 
1284  $lastname ='';
1285  $firstname = '';
1286  if(!$ilUser->isAnonymous() && in_array($row['value'], array('y', 'g')) ||
1287  $ilUser->isAnonymous() && 'g' == $row['value'])
1288  {
1289  $lastname = $row['lastname'];
1290  $firstname = $row['firstname'];
1291  }
1292 
1293  $statistic[$counter][] = $lastname;
1294  $statistic[$counter][] = $firstname;
1295 
1296  ++$counter;
1297  }
1298 
1299  return is_array($statistic) ? $statistic : array();
1300  }
1301 
1302 
1310  public function getFirstPostByThread($a_thread_id)
1311  {
1312  global $ilDB;
1313 
1314  $res = $ilDB->queryf('
1315  SELECT * FROM frm_posts_tree
1316  WHERE thr_fk = %s
1317  AND parent_pos = %s',
1318  array('integer', 'integer'), array($a_thread_id, '0'));
1319 
1320  $row = $ilDB->fetchObject($res);
1321 
1322  return $row->pos_fk ? $row->pos_fk : 0;
1323  }
1324 
1331  public function getModerators()
1332  {
1333  global $rbacreview;
1334 
1335  return $this->_getModerators($this->getForumRefId());
1336  }
1337 
1345  function _getModerators($a_ref_id)
1346  {
1347  global $rbacreview;
1348 
1349  $role_arr = $rbacreview->getRolesOfRoleFolder($a_ref_id);
1350 
1351  foreach ($role_arr as $role_id)
1352  {
1353  //$roleObj = $this->ilias->obj_factory->getInstanceByObjId($role_id);
1354  $title = ilObject::_lookupTitle($role_id);
1355  if ($title == "il_frm_moderator_".$a_ref_id)
1356  {
1357  #return $rbacreview->assignedUsers($roleObj->getId());
1358  return $title = $rbacreview->assignedUsers($role_id);
1359  }
1360  }
1361 
1362  return array();
1363  }
1364 
1373  public static function _isModerator($a_ref_id, $a_usr_id)
1374  {
1375  return in_array($a_usr_id, ilForum::_getModerators($a_ref_id));
1376  }
1377 
1385  public function countUserArticles($a_user_id)
1386  {
1387  global $ilDB;
1388 
1389  $res = $ilDB->queryf('
1390  SELECT * FROM frm_data
1391  INNER JOIN frm_posts ON pos_top_fk = top_pk
1392  WHERE top_frm_fk = %s
1393  AND pos_author_id = %s',
1394  array('integer', 'integer'),
1395  array($this->getForumId(), $a_user_id));
1396 
1397  return $res->numRows();
1398  }
1399 
1400  public function countActiveUserArticles($a_user_id)
1401  {
1402  global $ilDB, $ilUser;
1403 
1404  $res = $ilDB->queryf('
1405  SELECT * FROM frm_data
1406  INNER JOIN frm_posts ON pos_top_fk = top_pk
1407  WHERE top_frm_fk = %s
1408  AND (pos_status = %s
1409  OR (pos_status = %s
1410  AND pos_author_id = %s
1411  )
1412  )
1413  AND pos_author_id = %s',
1414  array('integer', 'integer', 'integer', 'integer', 'integer'),
1415  array($this->getForumId(),'1', '0', $ilUser->getId(), $a_user_id));
1416 
1417  return $res->numRows();
1418  }
1419 
1426  public function convertDate($date)
1427  {
1429  }
1430 
1438  public function addPostTree($a_tree_id, $a_node_id = -1, $a_date = '')
1439  {
1440  global $ilDB;
1441 
1442  $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1443 
1444  if ($a_node_id <= 0)
1445  {
1446  $a_node_id = $a_tree_id;
1447  }
1448 
1449  $nextId = $ilDB->nextId('frm_posts_tree');
1450 
1451  $statement = $ilDB->manipulateF('
1452  INSERT INTO frm_posts_tree
1453  ( fpt_pk,
1454  thr_fk,
1455  pos_fk,
1456  parent_pos,
1457  lft,
1458  rgt,
1459  depth,
1460  fpt_date
1461  )
1462  VALUES(%s, %s, %s, %s, %s, %s, %s, %s )',
1463  array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1464  array($nextId, $a_tree_id, $a_node_id, '0', '1', '2', '1', $a_date));
1465 
1466  return true;
1467  }
1468 
1476  public function insertPostNode($a_node_id, $a_parent_id, $tree_id, $a_date = '')
1477  {
1478  global $ilDB;
1479 
1480  $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1481 
1482  // get left value
1483  $sql_res = $ilDB->queryf('
1484  SELECT * FROM frm_posts_tree
1485  WHERE pos_fk = %s
1486  AND thr_fk = %s',
1487  array('integer', 'integer'),
1488  array($a_parent_id, $tree_id));
1489 
1490  $res = $ilDB->fetchObject($sql_res);
1491 
1492  $left = $res->lft;
1493 
1494  $lft = $left + 1;
1495  $rgt = $left + 2;
1496 
1497  // spread tree
1498  $statement = $ilDB->manipulateF('
1499  UPDATE frm_posts_tree
1500  SET lft = CASE
1501  WHEN lft > %s
1502  THEN lft + 2
1503  ELSE lft
1504  END,
1505  rgt = CASE
1506  WHEN rgt > %s
1507  THEN rgt + 2
1508  ELSE rgt
1509  END
1510  WHERE thr_fk = %s',
1511  array('integer', 'integer', 'integer'),
1512  array($left, $left, $tree_id));
1513 
1514  $depth = $this->getPostDepth($a_parent_id, $tree_id) + 1;
1515 
1516  // insert node
1517  $nextId = $ilDB->nextId('frm_posts_tree');
1518  $statement = $ilDB->manipulateF('
1519  INSERT INTO frm_posts_tree
1520  ( fpt_pk,
1521  thr_fk,
1522  pos_fk,
1523  parent_pos,
1524  lft,
1525  rgt,
1526  depth,
1527  fpt_date
1528  )
1529  VALUES(%s,%s,%s, %s, %s, %s,%s, %s)',
1530  array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1531  array( $nextId,
1532  $tree_id,
1533  $a_node_id,
1534  $a_parent_id,
1535  $lft,
1536  $rgt,
1537  $depth,
1538  $a_date)
1539  );
1540 
1541  }
1542 
1550  public function getPostDepth($a_node_id, $tree_id)
1551  {
1552  global $ilDB;
1553 
1554  if ($tree_id)
1555  {
1556  $sql_res = $ilDB->queryf('
1557  SELECT depth FROM frm_posts_tree
1558  WHERE pos_fk = %s
1559  AND thr_fk = %s',
1560  array('integer', 'integer'),
1561  array($a_node_id, $tree_id));
1562 
1563  $res = $ilDB->fetchObject($sql_res);
1564 
1565  return $res->depth;
1566  }
1567  else
1568  {
1569  return 0;
1570  }
1571  }
1572 
1579  public function getFirstPostNode($tree_id)
1580  {
1581  global $ilDB;
1582 
1583  $res = $ilDB->queryf('
1584  SELECT * FROM frm_posts, frm_posts_tree
1585  WHERE pos_pk = pos_fk
1586  AND parent_pos = %s
1587  AND thr_fk = %s',
1588  array('integer', 'integer'),
1589  array('0', $tree_id));
1590 
1591  $row = $ilDB->fetchObject($res);
1592 
1593  return $this->fetchPostNodeData($row);
1594  }
1595 
1602  public function getPostNode($post_id)
1603  {
1604  global $ilDB;
1605 
1606  $res = $ilDB->queryf('
1607  SELECT * FROM frm_posts, frm_posts_tree
1608  WHERE pos_pk = pos_fk
1609  AND pos_pk = %s',
1610  array('integer'),
1611  array($post_id));
1612 
1613  $row = $ilDB->fetchObject($res);
1614 
1615  return $this->fetchPostNodeData($row);
1616  }
1617 
1624  public function fetchPostNodeData($a_row)
1625  {
1626  global $lng;
1627 
1628  require_once('./Services/User/classes/class.ilObjUser.php');
1629 
1630  if (ilObject::_exists($a_row->pos_display_user_id))
1631  {
1632  $tmp_user = new ilObjUser($a_row->pos_display_user_id);
1633  $fullname = $tmp_user->getFullname();
1634  $loginname = $tmp_user->getLogin();
1635  }
1636 
1637  $fullname = $fullname ? $fullname : ($a_row->import_name ? $a_row->import_name : $lng->txt("unknown"));
1638 
1639  $data = array(
1640  "pos_pk" => $a_row->pos_pk,
1641  "child" => $a_row->pos_pk,
1642  "author" => $a_row->pos_display_user_id,
1643  "alias" => $a_row->pos_usr_alias,
1644  "title" => $fullname,
1645  "loginname" => $loginname,
1646  "type" => "post",
1647  "message" => $a_row->pos_message,
1648  "subject" => $a_row->pos_subject,
1649  "pos_cens_com" => $a_row->pos_cens_com,
1650  "pos_cens" => $a_row->pos_cens,
1651  // "date" => $a_row->date,
1652  "date" => $a_row->fpt_date,
1653  "create_date" => $a_row->pos_date,
1654  "update" => $a_row->pos_update,
1655  "update_user" => $a_row->update_user,
1656  "tree" => $a_row->thr_fk,
1657  "parent" => $a_row->parent_pos,
1658  "lft" => $a_row->lft,
1659  "rgt" => $a_row->rgt,
1660  "depth" => $a_row->depth,
1661  "id" => $a_row->fpt_pk,
1662  "notify" => $a_row->notify,
1663  "import_name" => $a_row->import_name,
1664  "pos_status" => $a_row->pos_status
1665  );
1666 
1667  // why this line? data should be stored without slashes in db
1668  //$data["message"] = stripslashes($data["message"]);
1669 
1670  return $data ? $data : array();
1671  }
1672 
1679  public function deletePostTree($a_node)
1680  {
1681  global $ilDB;
1682 
1683  // GET LEFT AND RIGHT VALUES
1684  $res = $ilDB->queryf('
1685  SELECT * FROM frm_posts_tree
1686  WHERE thr_fk = %s
1687  AND pos_fk = %s
1688  AND parent_pos = %s',
1689  array('integer', 'integer', 'integer'),
1690  array($a_node['tree'], $a_node['pos_pk'], $a_node['parent']));
1691 
1692  while($row = $ilDB->fetchObject($res))
1693  {
1694  $a_node["lft"] = $row->lft;
1695  $a_node["rgt"] = $row->rgt;
1696  }
1697 
1698  $diff = $a_node["rgt"] - $a_node["lft"] + 1;
1699 
1700  // get data of posts
1701  $result = $ilDB->queryf('
1702  SELECT * FROM frm_posts_tree
1703  WHERE lft BETWEEN %s AND %s
1704  AND thr_fk = %s',
1705  array('integer', 'integer', 'integer'),
1706  array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1707 
1708  $del_id = array();
1709 
1710  while ($treeData = $ilDB->fetchAssoc($result))
1711  {
1712  $del_id[] = $treeData["pos_fk"];
1713  }
1714 
1715  // delete subtree
1716  $statement = $ilDB->manipulateF('
1717  DELETE FROM frm_posts_tree
1718  WHERE lft BETWEEN %s AND %s
1719  AND thr_fk = %s',
1720  array('integer', 'integer', 'integer'),
1721  array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1722 
1723 
1724  // close gaps
1725  $statement = $ilDB->manipulateF('
1726  UPDATE frm_posts_tree
1727  SET lft = CASE
1728  WHEN lft > %s
1729  THEN lft - %s
1730  ELSE lft
1731  END,
1732  rgt = CASE
1733  WHEN rgt > %s
1734  THEN rgt - %s
1735  ELSE rgt
1736  END
1737  WHERE thr_fk = %s',
1738  array('integer', 'integer', 'integer', 'integer', 'integer'),
1739  array($a_node['lft'], $diff, $a_node['lft'], $diff, $a_node['tree']));
1740 
1741  return $del_id;
1742 
1743  }
1744 
1750  public function updateVisits($ID)
1751  {
1752 
1753  global $ilDB;
1754 
1755  $checkTime = time() - (60*60);
1756 
1757  if ($_SESSION["frm_visit_".$this->dbTable."_".$ID] < $checkTime)
1758  {
1759 
1760  $_SESSION["frm_visit_".$this->dbTable."_".$ID] = time();
1761  $query = 'UPDATE '.$this->dbTable.' SET visits = visits + 1 WHERE ';
1762 
1763  $data_type = array();
1764  $data_value = array();
1765 
1766  if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
1767  {
1768  $query .= $this->getMDB2Query();
1769  $data_type = $data_type + $this->getMDB2DataType();
1770  $data_value = $data_value + $this->getMDB2DataValue();
1771 
1772  $res = $ilDB->queryf($query, $data_type, $data_value);
1773  }
1774  }
1775  }
1776 
1784  public function prepareText($text, $edit=0, $quote_user = '', $type = '')
1785  {
1786  global $lng;
1787 
1788  if($type == 'export')
1789  {
1790  $this->replQuote1 = "<blockquote class=\"quote\"><hr size=\"1\" color=\"#000000\">";
1791  $this->replQuote2 = "<hr size=\"1\" color=\"#000000\"/></blockquote>";
1792  }
1793 
1794  if($edit == 1)
1795  {
1796  // add login name of quoted users
1797  $lname = ($quote_user != "")
1798  ? '="'.$quote_user.'"'
1799  : "";
1800 
1801  $text = "[quote$lname]".$text."[/quote]";
1802  }
1803  else
1804  {
1805  // check for quotation
1806  $startZ = substr_count ($text, "[quote"); // also count [quote="..."]
1807  $endZ = substr_count ($text, "[/quote]");
1808 
1809  if ($startZ > 0 || $endZ > 0)
1810  {
1811  // add missing opening and closing tags
1812  if ($startZ > $endZ)
1813  {
1814  $diff = $startZ - $endZ;
1815 
1816  for ($i = 0; $i < $diff; $i++)
1817  {
1818  if ($type == 'export') $text .= $this->txtQuote2;
1819  else $text .= "[/quote]";
1820  }
1821  }
1822  elseif ($startZ < $endZ)
1823  {
1824  $diff = $endZ - $startZ;
1825 
1826  for ($i = 0; $i < $diff; $i++)
1827  {
1828  if ($type == 'export') $text = $this->txtQuote1.$text;
1829  else $text = "[quote]".$text;
1830  }
1831  }
1832 
1833  if($edit == 0)
1834  {
1835  $ws= "[ \t\r\f\v\n]*";
1836 
1837  $text = eregi_replace("\[(quote$ws=$ws\"([^\"]*)\"$ws)\]",
1838  $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote")." (\\2)".'</div>', $text);
1839 
1840  $text = str_replace("[quote]",
1841  $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote").'</div>', $text);
1842 
1843  $text = str_replace("[/quote]", $this->replQuote2, $text);
1844  }
1845  }
1846  }
1847 
1848  if($type != 'export')
1849  {
1850  if($edit == 0)
1851  {
1852  $text = ilUtil::insertLatexImages($text, "<span class\=\"latex\">", "<\/span>");
1853  $text = ilUtil::insertLatexImages($text, "\[tex\]", "\[\/tex\]");
1854  }
1855 
1856  // workaround for preventing template engine
1857  // from hiding text that is enclosed
1858  // in curly brackets (e.g. "{a}")
1859  $text = str_replace("{", "&#123;", $text);
1860  $text = str_replace("}", "&#125;", $text);
1861  }
1862 
1863  return $text;
1864  }
1865 
1866 
1873  public function getModeratorFromPost($pos_pk)
1874  {
1875  global $ilDB;
1876 
1877  $res = $ilDB->queryf('
1878  SELECT frm_data.* FROM frm_data, frm_posts
1879  WHERE pos_pk = %s
1880  AND pos_top_fk = top_pk',
1881  array('integer'), array($pos_pk));
1882 
1883  $row = $ilDB->fetchAssoc($res);
1884 
1885  return $row;
1886 
1887  }
1888 
1889  function __deletePostFiles($a_ids)
1890  {
1891  if(!is_array($a_ids))
1892  {
1893  return false;
1894  }
1895  include_once "./Modules/Forum/classes/class.ilFileDataForum.php";
1896 
1897  $tmp_file_obj =& new ilFileDataForum($this->getForumId());
1898  foreach($a_ids as $pos_id)
1899  {
1900  $tmp_file_obj->setPosId($pos_id);
1901  $files = $tmp_file_obj->getFilesOfPost();
1902  foreach($files as $file)
1903  {
1904  $tmp_file_obj->unlinkFile($file["name"]);
1905  }
1906  }
1907  unset($tmp_file_obj);
1908  return true;
1909  }
1910 
1911 
1912  function __sendMessage($a_parent_pos, $post_data = array())
1913  {
1914  global $ilUser, $ilDB;
1915 
1916  $parent_data = $this->getOnePost($a_parent_pos);
1917 
1918  // only if the current user is not the owner of the parent post and the parent's notification flag is set...
1919  if($parent_data["notify"] && $parent_data["pos_author_id"] != $ilUser->getId())
1920  {
1921  // SEND MESSAGE
1922  include_once "Services/Mail/classes/class.ilMail.php";
1923  include_once './Services/User/classes/class.ilObjUser.php';
1924 
1925  $tmp_user =& new ilObjUser($parent_data["pos_author_id"]);
1926 
1927  // NONSENSE
1928  $this->setMDB2WhereCondition('thr_pk = %s ', array('integer'), array($parent_data["pos_thr_fk"]));
1929 
1930  $thread_data = $this->getOneThread();
1931 
1932  $tmp_mail_obj = new ilMail(ANONYMOUS_USER_ID);
1933  $message = $tmp_mail_obj->sendMail($tmp_user->getLogin(),"","",
1934  $this->formatNotificationSubject($post_data),
1935  $this->__formatMessage($thread_data, $post_data, $tmp_user),
1936  array(),array("system"));
1937 
1938  unset($tmp_user);
1939  unset($tmp_mail_obj);
1940  }
1941  }
1942 
1951  private function __formatMessage($thread_data, $post_data = array(), $user_obj)
1952  {
1953  include_once "./Services/Object/classes/class.ilObjectFactory.php";
1954  $user_lang = self::_getLanguageInstanceByUsrId($user_obj->getId());
1955 
1956  $frm_obj =& ilObjectFactory::getInstanceByRefId($this->getForumRefId());
1957  $title = $frm_obj->getTitle();
1958  unset($frm_obj);
1959 
1960  $message = '';
1961  $message .= ilMail::getSalutation($user_obj->getId(), $user_lang);
1962 
1963  $message .= "\n\n";
1964  $message .= $this->lng->txt("forum_post_replied");
1965  $message .= $this->lng->txt("forum").": ".$title." -> ".$thread_data["thr_subject"]."\n\n";
1966 
1967  $message .= "\n------------------------------------------------------------\n";
1968  $message .= $post_data["pos_message"];
1969  $message .= "\n------------------------------------------------------------\n";
1970  $message .= sprintf($this->lng->txt("forums_notification_show_post"), "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"])."/goto.php?target=frm_".$post_data["ref_id"]."_".$post_data["pos_thr_fk"].'&client_id='.CLIENT_ID)."\n\n";
1971 
1972  $message .= ilMail::_getInstallationSignature();
1973  return $message;
1974  }
1975 
1976  function getImportName()
1977  {
1978  return $this->import_name;
1979  }
1980  function setImportName($a_import_name)
1981  {
1982  $this->import_name = $a_import_name;
1983  }
1984 
1991  function enableForumNotification($user_id)
1992  {
1993  global $ilDB;
1994 
1995  if (!$this->isForumNotificationEnabled($user_id))
1996  {
1997  /* Remove all notifications of threads that belong to the forum */
1998 
1999  $res = $ilDB->queryf('
2000  SELECT frm_notification.thread_id FROM frm_data, frm_notification, frm_threads
2001  WHERE frm_notification.user_id = %s
2002  AND frm_notification.thread_id = frm_threads.thr_pk
2003  AND frm_threads.thr_top_fk = frm_data.top_pk
2004  AND frm_data.top_frm_fk = %s
2005  GROUP BY frm_notification.thread_id',
2006  array('integer', 'integer'),
2007  array($user_id, $this->id));
2008 
2009  if (is_object($res) && $res->numRows() > 0)
2010  {
2011  $thread_data = array();
2012  $thread_data_types = array();
2013 
2014  $query = ' DELETE FROM frm_notification
2015  WHERE user_id = %s
2016  AND thread_id IN (';
2017 
2018  array_push($thread_data, $user_id);
2019  array_push($thread_data_types, 'integer');
2020 
2021  $counter = 1;
2022 
2023  while($row = $ilDB->fetchAssoc($res))
2024  {
2025  if($counter < $res->numRows())
2026  {
2027  $query .= '%s, ';
2028  array_push($thread_data, $row['thread_id']);
2029  array_push($thread_data_types, 'integer');
2030  }
2031 
2032  if($counter == $res->numRows())
2033  {
2034  $query .= '%s)';
2035  array_push($thread_data, $row['thread_id']);
2036  array_push($thread_data_types, 'integer');
2037 
2038  }
2039  $counter++;
2040  }
2041 
2042  $statement = $ilDB->manipulateF($query, $thread_data_types, $thread_data);
2043  }
2044 
2045  /* Insert forum notification */
2046 
2047  $nextId = $ilDB->nextId('frm_notification');
2048 
2049  $statement = $ilDB->manipulateF('
2050  INSERT INTO frm_notification
2051  ( notification_id,
2052  user_id,
2053  frm_id
2054  )
2055  VALUES(%s, %s, %s)',
2056  array('integer','integer', 'integer'),
2057  array($nextId, $user_id, $this->id));
2058 
2059  }
2060 
2061  return true;
2062  }
2063 
2070  function disableForumNotification($user_id)
2071  {
2072  global $ilDB;
2073 
2074  $statement = $ilDB->manipulateF('
2075  DELETE FROM frm_notification
2076  WHERE user_id = %s
2077  AND frm_id = %s',
2078  array('integer', 'integer'),
2079  array($user_id, $this->id));
2080 
2081  return true;
2082  }
2083 
2090  function isForumNotificationEnabled($user_id)
2091  {
2092  global $ilDB;
2093 
2094  $result = $ilDB->queryf('SELECT COUNT(*) cnt FROM frm_notification WHERE user_id = %s AND frm_id = %s',
2095  array('integer', 'integer'), array($user_id, $this->id));
2096 
2097  while($record = $ilDB->fetchAssoc($result))
2098  {
2099  return (bool)$record['cnt'];
2100  }
2101 
2102  return false;
2103  }
2104 
2112  function enableThreadNotification($user_id, $thread_id)
2113  {
2114  global $ilDB;
2115 
2116  if (!$this->isThreadNotificationEnabled($user_id, $thread_id))
2117  {
2118  $nextId = $ilDB->nextId('frm_notification');
2119  $statement = $ilDB->manipulateF('
2120  INSERT INTO frm_notification
2121  ( notification_id,
2122  user_id,
2123  thread_id
2124  )
2125  VALUES (%s, %s, %s)',
2126  array('integer', 'integer', 'integer'), array($nextId, $user_id, $thread_id));
2127 
2128  }
2129 
2130  return true;
2131  }
2132 
2140  function isThreadNotificationEnabled($user_id, $thread_id)
2141  {
2142  global $ilDB;
2143 
2144  $result = $ilDB->queryf('
2145  SELECT COUNT(*) cnt FROM frm_notification
2146  WHERE user_id = %s
2147  AND thread_id = %s',
2148  array('integer', 'integer'),
2149  array($user_id, $thread_id));
2150 
2151 
2152  while($record = $ilDB->fetchAssoc($result))
2153  {
2154  return (bool)$record['cnt'];
2155  }
2156 
2157  return false;
2158  }
2159 
2160  function sendThreadNotifications($post_data)
2161  {
2162  global $ilDB, $ilAccess, $lng;
2163 
2164  include_once "Services/Mail/classes/class.ilMail.php";
2165  include_once './Services/User/classes/class.ilObjUser.php';
2166 
2167  // GET THREAD DATA
2168  $result = $ilDB->queryf('
2169  SELECT thr_subject FROM frm_threads
2170  WHERE thr_pk = %s',
2171  array('integer'), array($post_data['pos_thr_fk']));
2172 
2173  while($record = $ilDB->fetchAssoc($result))
2174  {
2175  $post_data['thr_subject'] = $record['thr_subject'];
2176  break;
2177  }
2178 
2179  // determine obj_id of the forum
2180  $obj_id = self::_lookupObjIdForForumId($post_data['pos_top_fk']);
2181 
2182  // GET AUTHOR OF NEW POST
2183  if($post_data['pos_display_user_id'])
2184  {
2185  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_display_user_id']);
2186  }
2187  else if(strlen($post_data['pos_usr_alias']))
2188  {
2189  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2190  }
2191 
2192  if($post_data['pos_usr_name'] == '')
2193  {
2194  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2195  }
2196 
2197  // GET USERS WHO WANT TO BE INFORMED ABOUT NEW POSTS
2198  $res = $ilDB->queryf('
2199  SELECT user_id FROM frm_notification
2200  WHERE thread_id = %s
2201  AND user_id <> %s',
2202  array('integer', 'integer'),
2203  array($post_data['pos_thr_fk'], $_SESSION['AccountId']));
2204 
2205  // get all references of obj_id
2206  $frm_references = ilObject::_getAllReferences($obj_id);
2207 
2208  // save language of the current user
2209  global $lng;
2210  $userLanguage = $lng;
2211 
2212  // get attachments data
2213  $fileDataForum = new ilFileDataForum($obj_id, $post_data['pos_pk']);
2214  $filesOfPost = $fileDataForum->getFilesOfPost();
2215 
2216  $attachments = array();
2217  foreach($filesOfPost as $attachment)
2218  {
2219  $attachments[] = $attachment['name'];
2220  }
2221 
2222  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2223  while($row = $ilDB->fetchAssoc($res))
2224  {
2225  // do rbac check before sending notification
2226  $send_mail = false;
2227  foreach((array)$frm_references as $ref_id)
2228  {
2229  if($ilAccess->checkAccessOfUser($row['user_id'], 'read', '', $ref_id))
2230  {
2231  $send_mail = true;
2232  break;
2233  }
2234  }
2235 
2236  if($send_mail)
2237  {
2238  $this->setLanguage(self::_getLanguageInstanceByUsrId($row['user_id']));
2239  $mail_obj->sendMail(
2240  ilObjUser::_lookupLogin($row["user_id"]), "", "",
2241  $this->formatNotificationSubject($post_data),
2242  $this->formatNotification($post_data, 0, $attachments, $row['user_id']),
2243  array(), array("system")
2244  );
2245  }
2246  }
2247 
2248  // reset language
2249  $this->setLanguage($userLanguage);
2250  }
2251 
2252  function sendForumNotifications($post_data)
2253  {
2254  global $ilDB, $ilAccess, $lng, $ilUser;
2255 
2256  include_once "Services/Mail/classes/class.ilMail.php";
2257  include_once './Services/User/classes/class.ilObjUser.php';
2258 
2259  // GET THREAD DATA
2260  $result = $ilDB->queryf('
2261  SELECT thr_subject FROM frm_threads
2262  WHERE thr_pk = %s',
2263  array('integer'),
2264  array($post_data['pos_thr_fk']));
2265 
2266  while($record = $ilDB->fetchAssoc($result))
2267  {
2268  $post_data['thr_subject'] = $record['thr_subject'];
2269  break;
2270  }
2271 
2272  // determine obj_id of the forum
2273  $obj_id = self::_lookupObjIdForForumId($post_data['pos_top_fk']);
2274 
2275  // GET AUTHOR OF NEW POST
2276  if($post_data['pos_display_user_id'])
2277  {
2278  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_display_user_id']);
2279  }
2280  else if(strlen($post_data['pos_usr_alias']))
2281  {
2282  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2283  }
2284 
2285  if($post_data['pos_usr_name'] == '')
2286  {
2287  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2288  }
2289 
2290  // GET USERS WHO WANT TO BE INFORMED ABOUT NEW POSTS
2291  $res = $ilDB->queryf('
2292  SELECT frm_notification.user_id FROM frm_notification, frm_data
2293  WHERE frm_data.top_pk = %s
2294  AND frm_notification.frm_id = frm_data.top_frm_fk
2295  AND frm_notification.user_id <> %s
2296  GROUP BY frm_notification.user_id',
2297  array('integer', 'integer'),
2298  array($post_data['pos_top_fk'], $ilUser->getId()));
2299 
2300  // get all references of obj_id
2301  $frm_references = ilObject::_getAllReferences($obj_id);
2302 
2303  // save language of the current user
2304  global $lng;
2305  $userLanguage = $lng;
2306 
2307  // get attachments data
2308  $fileDataForum = new ilFileDataForum($obj_id, $post_data['pos_pk']);
2309  $filesOfPost = $fileDataForum->getFilesOfPost();
2310  $attachments = array();
2311  foreach($filesOfPost as $attachment)
2312  {
2313  $attachments[] = $attachment['name'];
2314  }
2315 
2316  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2317  while($row = $ilDB->fetchAssoc($res))
2318  {
2319  // do rbac check before sending notification
2320  $send_mail = false;
2321  foreach((array)$frm_references as $ref_id)
2322  {
2323  if($ilAccess->checkAccessOfUser($row['user_id'], 'read', '', $ref_id))
2324  {
2325  $send_mail = true;
2326  break;
2327  }
2328  }
2329 
2330  if($send_mail)
2331  {
2332  $this->setLanguage(self::_getLanguageInstanceByUsrId($row['user_id']));
2333  $mail_obj->sendMail(
2334  ilObjUser::_lookupLogin($row["user_id"]), "", "",
2335  $this->formatNotificationSubject($post_data),
2336  $this->formatNotification($post_data, 0, $attachments, $row['user_id']),
2337  array(), array("system")
2338  );
2339  }
2340  }
2341 
2342  // reset language
2343  $this->setLanguage($userLanguage);
2344  }
2345 
2351  private function formatPostActivationNotification($post_data, $user_id)
2352  {
2353  $user_lang = self::_getLanguageInstanceByUsrId($user_id);
2354 
2355  $message = "";
2356  $message .= ilMail::getSalutation($user_id, $user_lang);
2357  $message .= "\n\n";
2358  $message .= $this->lng->txt('forums_post_activation_mail')."\n\n";
2359 
2360  $message .= $this->lng->txt("forum").": ".$post_data["top_name"]."\n\n";
2361  $message .= $this->lng->txt("thread").": ".$post_data["thr_subject"]."\n\n";
2362  $message .= $this->lng->txt("new_post").":\n------------------------------------------------------------\n";
2363  $message .= $this->lng->txt("author").": ".$post_data["pos_usr_name"]."\n";
2364  $message .= $this->lng->txt("date").": ".$post_data["pos_date"]."\n";
2365  $message .= $this->lng->txt("subject").": ".$post_data["pos_subject"]."\n\n";
2366  if ($post_data["pos_cens"] == 1)
2367  {
2368  $message .= $post_data["pos_cens_com"]."\n";
2369  }
2370  else
2371  {
2372  $pos_message = $post_data['pos_message'];
2373  if(strip_tags($pos_message) != $pos_message)
2374  {
2375  $pos_message = preg_replace("/\n/i", "", $pos_message);
2376  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
2377  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
2378  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
2379  }
2380  $message .= strip_tags($pos_message)."\n";
2381  }
2382  $message .= "------------------------------------------------------------\n";
2383 
2384  $message .= sprintf($this->lng->txt('forums_notification_show_post'), ILIAS_HTTP_PATH."/goto.php?target=frm_".$post_data["ref_id"]."_".$post_data["pos_thr_fk"]."_".$post_data["pos_pk"].'&client_id='.CLIENT_ID)."\n\n";
2385  $message .= sprintf($this->lng->txt('forums_notification_intro'),
2386  $this->ilias->ini->readVariable('client', 'name'),
2387  ILIAS_HTTP_PATH.'/?client_id='.CLIENT_ID)."\n\n";
2388 
2389  return $message;
2390  }
2391 
2392  function sendPostActivationNotification($post_data)
2393  {
2394  global $ilDB, $ilUser, $lng;
2395 
2396  if (is_array($moderators = $this->getModerators()))
2397  {
2398  // GET THREAD DATA
2399  $result = $ilDB->queryf('
2400  SELECT thr_subject FROM frm_threads
2401  WHERE thr_pk = %s',
2402  array('integer'),
2403  array($post_data['pos_thr_fk']));
2404 
2405  while($record = $ilDB->fetchAssoc($result))
2406  {
2407  $post_data['thr_subject'] = $record['thr_subject'];
2408  break;
2409  }
2410 
2411  // GET AUTHOR OF NEW POST
2412  if($post_data['pos_display_user_id'])
2413  {
2414  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_display_user_id']);
2415  }
2416  else if(strlen($post_data['pos_usr_alias']))
2417  {
2418  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2419  }
2420 
2421  if($post_data['pos_usr_name'] == '')
2422  {
2423  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2424  }
2425 
2426  // save language of the current user
2427  global $lng;
2428  $userLanguage = $lng;
2429 
2430  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2431  foreach($moderators as $moderator)
2432  {
2433  // set forum language instance for earch user
2434  $this->setLanguage(self::_getLanguageInstanceByUsrId($moderator));
2435  $subject = $this->formatNotificationSubject($post_data);
2436  $message = $this->formatPostActivationNotification($post_data, $moderator);
2437  $mail_obj->sendMail(
2438  ilObjUser::_lookupLogin($moderator), '', '',
2439  $subject,
2440  $message,
2441  array(), array("system")
2442  );
2443  }
2444 
2445  // reset language
2446  $this->setLanguage($userLanguage);
2447  }
2448  }
2449 
2454  public function formatNotificationSubject($post_data)
2455  {
2456  return $this->lng->txt("forums_notification_subject").' '.$post_data['top_name'];
2457  }
2458 
2466  public function formatNotification($post_data, $cron = 0, $attachments = array(), $user_id)
2467  {
2468  global $ilIliasIniFile;
2469 
2470  $user_lang = self::_getLanguageInstanceByUsrId($user_id);
2471 
2472  $message = "";
2473 
2474  $message .= ilMail::getSalutation($user_id, $user_lang);
2475  $message .= "\n\n";
2476  $message .= $this->lng->txt("forums_notification_subject")." ".$post_data['top_name']."\n\n";
2477 
2478  $message .= $this->lng->txt("forum").": ".$post_data["top_name"]."\n\n";
2479  $message .= $this->lng->txt("thread").": ".$post_data["thr_subject"]."\n\n";
2480  $message .= $this->lng->txt("new_post").":\n------------------------------------------------------------\n";
2481  $message .= $this->lng->txt("author").": ".$post_data["pos_usr_name"]."\n";
2482  $message .= $this->lng->txt("date").": ".$post_data["pos_date"]."\n";
2483  $message .= $this->lng->txt("subject").": ".$post_data["pos_subject"]."\n\n";
2484 
2485  if ($post_data["pos_cens"] == 1)
2486  {
2487  $message .= $post_data["pos_cens_com"]."\n";
2488  }
2489  else
2490  {
2491  $pos_message = $post_data['pos_message'];
2492  if(strip_tags($pos_message) != $pos_message)
2493  {
2494  $pos_message = preg_replace("/\n/i", "", $pos_message);
2495  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
2496  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
2497  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
2498  }
2499  $message .= strip_tags($pos_message)."\n";
2500  }
2501  $message .= "------------------------------------------------------------\n";
2502 
2503  if(count($attachments) > 0)
2504  {
2505  foreach($attachments as $attachment)
2506  {
2507  $message .= $this->lng->txt('attachment').": ".$attachment."\n";
2508  }
2509  $message .= "\n------------------------------------------------------------\n";
2510  }
2511 
2512  if ($cron == 1)
2513  {
2514  $message .= sprintf($this->lng->txt("forums_notification_show_post"), $ilIliasIniFile->readVariable("server","http_path")."/goto.php?target=frm_".$post_data["ref_id"]."_".$post_data["pos_thr_fk"]."_".$post_data["pos_pk"].'&client_id='.CLIENT_ID)."\n\n";
2515  }
2516  else
2517  {
2518  $message .= sprintf($this->lng->txt("forums_notification_show_post"), ILIAS_HTTP_PATH."/goto.php?target=frm_".$post_data["ref_id"]."_".$post_data["pos_thr_fk"]."_".$post_data["pos_pk"].'&client_id='.CLIENT_ID)."\n\n";
2519  }
2520 
2521  if ($cron == 1)
2522  {
2523  $message .= sprintf($this->lng->txt("forums_notification_intro"),
2524  $this->ilias->ini->readVariable("client","name"),
2525  $ilIliasIniFile->readVariable("server","http_path").'/?client_id='.CLIENT_ID)."\n\n";
2526  }
2527  else
2528  {
2529  $message .= sprintf($this->lng->txt("forums_notification_intro"),
2530  $this->ilias->ini->readVariable("client","name"),
2531  ILIAS_HTTP_PATH.'/?client_id='.CLIENT_ID)."\n\n";
2532  }
2533 
2534  return $message;
2535  }
2536 
2546  public static function _getThreads($a_obj_id,$a_sort_mode = self::SORT_DATE)
2547  {
2548  global $ilDB;
2549 
2550  switch($a_sort_mode)
2551  {
2552  case self::SORT_DATE:
2553  $sort = 'thr_date';
2554  break;
2555 
2556  case self::SORT_TITLE:
2557  default:
2558  $sort = 'thr_subject';
2559  break;
2560  }
2561 
2562  $res = $ilDB->queryf('
2563  SELECT * FROM frm_threads
2564  JOIN frm_data ON top_pk = thr_top_fk
2565  WHERE top_frm_fk = %s
2566  ORDER BY %s',
2567  array('integer', 'text'), array($a_obj_id, $sort));
2568 
2569  while($row = $ilDB->fetchObject($res))
2570  {
2571  $threads[$row->thr_pk] = $row->thr_subject;
2572  }
2573  return $threads ? $threads : array();
2574  }
2575 
2576  public static function _lookupObjIdForForumId($a_for_id)
2577  {
2578  global $ilDB;
2579 
2580  $res = $ilDB->queryf('
2581  SELECT top_frm_fk FROM frm_data
2582  WHERE top_pk = %s',
2583  array('integer'), array($a_for_id));
2584 
2585  if ($fdata = $ilDB->fetchAssoc($res))
2586  {
2587  return $fdata["top_frm_fk"];
2588  }
2589  return false;
2590  }
2591 
2592  public static function updateLastPostByObjId($a_obj_id)
2593  {
2594  global $ilDB;
2595  // get latest post of forum and update last_post
2596  $ilDB->setLimit(1);
2597  $res2 = $ilDB->queryf('
2598  SELECT pos_top_fk, pos_thr_fk, pos_pk FROM frm_posts, frm_data
2599  WHERE pos_top_fk = top_pk
2600  AND top_frm_fk = %s
2601  ORDER BY pos_date DESC',
2602  array('integer'), array($a_obj_id));
2603 
2604  if ($res2->numRows() == 0)
2605  {
2606  $lastPost_top = "";
2607  }
2608  else
2609  {
2610  $z = 0;
2611 
2612  while ($selData = $ilDB->fetchAssoc($res2))
2613  {
2614  if ($z > 0)
2615  {
2616  break;
2617  }
2618 
2619  $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
2620  $z ++;
2621  }
2622  }
2623 
2624  $ilDB->update('frm_data',
2625  array('top_last_post' => array('text', $lastPost_top)),
2626  array('top_frm_fk' => array('integer', $a_obj_id))
2627  );
2628 
2629  }
2630 
2637  public static function mergeThreads($obj_id, $source_id, $target_id)
2638  {
2639  // selected source & target objects
2640  $source_thread_obj = new ilForumTopic((int)$source_id);
2641  $target_thread_obj = new ilForumTopic((int)$target_id);
2642 
2643  if($source_thread_obj->getForumId() != $target_thread_obj->getForumId())
2644  {
2645  throw new ilException('not_allowed_to_merge_into_another_forum');
2646  }
2647  // use the "older" thread as target
2648  if($source_thread_obj->getCreateDate() > $target_thread_obj->getCreateDate())
2649  {
2650  $merge_thread_source = $source_thread_obj;
2651  $merge_thread_target = $target_thread_obj;
2652  }
2653  else
2654  {
2655  $merge_thread_source = $target_thread_obj;
2656  $merge_thread_target = $source_thread_obj;
2657  }
2658 
2659  $thread_subject = $target_thread_obj->getSubject();
2660 
2661  // remember if the threads are open or closed and then close both threads !
2662  $targed_was_closed = $merge_thread_target->isClosed();
2663 
2664  $merge_thread_source->close();
2665 
2666  if($targed_was_closed == false)
2667  {
2668  $merge_thread_target->close();
2669  }
2670 
2671  $source_all_posts = $merge_thread_source->getAllPosts();
2672  $source_root_node = $merge_thread_source->getFirstPostNode();
2673  $target_root_node = $merge_thread_target->getFirstPostNode();
2674 
2675  $add_difference = $target_root_node->getRgt();
2676 
2677 // update target root node rgt
2678  include_once 'Modules/Forum/classes/class.ilForumPostsTree.php';
2679 // $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt() + 1);
2680  $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt());
2681  ilForumPostsTree::updateTargetRootRgt($target_root_node->getId(), $new_target_rgt);
2682 
2683  $new_target_root = $target_root_node->getId();
2684 
2685  // get source post tree and update posts tree
2686  foreach($source_all_posts as $post)
2687  {
2688  $post_obj = new ilForumPost($post->pos_pk);
2689 
2690  $posts_tree_obj = new ilForumPostsTree();
2691  $posts_tree_obj->setPosFk($post->pos_pk);
2692 
2693  if($post_obj->getParentId() == 0)
2694  {
2695  $posts_tree_obj->setParentPos($new_target_root);
2696 
2697  //$posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference));
2698  $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2699  $posts_tree_obj->setLft($target_root_node->getRgt());
2700 
2701  $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2702  $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2703 
2704  $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2705 
2706  $posts_tree_obj->mergeParentPos();
2707  }
2708  else
2709  {
2710  $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2711  $posts_tree_obj->setLft(($post_obj->getLft() + $add_difference) - 1);
2712 
2713  $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2714  $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2715 
2716  $posts_tree_obj->setParentPos($post_obj->getParentId());
2717  $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2718 
2719  $posts_tree_obj->merge();
2720  }
2721  }
2722 
2723 // update frm_posts pos_thr_fk = target_thr_id
2724  include_once 'Modules/Forum/classes/class.ilForumPost.php';
2725  ilForumPost::mergePosts($merge_thread_source->getId(), $merge_thread_target->getId());
2726 
2727 // check notifications
2728  include_once 'Modules/Forum/classes/class.ilForumNotification.php';
2729  ilForumNotification::mergeThreadNotificiations($merge_thread_source->getId(), $merge_thread_target->getId());
2730 
2731 // delete frm_thread_access entries
2732  include_once './Modules/Forum/classes/class.ilObjForum.php';
2733  ilObjForum::_deleteAccessEntries($merge_thread_source->getId());
2734 
2735 // update frm_user_read
2736  ilObjForum::mergeForumUserRead($merge_thread_source->getId(), $merge_thread_target->getId());
2737 
2738 // update visits, thr_num_posts, last_post, subject
2739  $post_date_source = $merge_thread_source->getLastPost()->getCreateDate();
2740  $post_date_target = $merge_thread_target->getLastPost()->getCreateDate();
2741 
2742  $target_last_post = $merge_thread_target->getLastPostString();
2743  $exp = explode('#', $target_last_post);
2744 
2745  if($post_date_source > $post_date_target)
2746  {
2747  $exp[2] = $merge_thread_source->getLastPost()->getId();
2748  }
2749  else
2750  {
2751  $exp[2] = $merge_thread_target->getLastPost()->getId();
2752  }
2753  $new_thr_last_post = implode('#', $exp);
2754 
2755  $num_posts_source = (int)$merge_thread_source->getNumPosts();
2756  $num_visits_source = (int)$merge_thread_source->getVisits();
2757  $num_posts_target = (int)$merge_thread_target->getNumPosts();
2758  $num_visits_target = (int)$merge_thread_source->getVisits();
2759 
2760  $frm_topic_obj = new ilForumTopic(0, false, true);
2761  $frm_topic_obj->setNumPosts(($num_posts_source + $num_posts_target));
2762  $frm_topic_obj->setVisits(($num_visits_source + $num_visits_target));
2763  $frm_topic_obj->setLastPostString($new_thr_last_post);
2764  $frm_topic_obj->setSubject($thread_subject);
2765  $frm_topic_obj->setId($merge_thread_target->getId());
2766 
2767  $frm_topic_obj->updateMergedThread();
2768 
2769 // update frm_data: top_last_post , top_num_threads
2771 
2772 // reopen target if was not "closed" before merging
2773  if(!$targed_was_closed)
2774  {
2775  $merge_thread_target->reopen();
2776  }
2777 
2778 // delete source thread
2779  ilForumTopic::deleteByThreadId($merge_thread_source->getId());
2780  }
2781 } // END class.Forum
< a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">< img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/></a >< br/>< strong > Enter Code *if($_SERVER['REQUEST_METHOD']=='POST' &&@ $_POST['do']=='contact') $_SESSION['ctform']['success']
static _isModerator($a_ref_id, $a_usr_id)
checks whether a user is moderator of a given forum object
static mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
setForumId($a_obj_id)
set object id which refers to ILIAS obj_id
print $file
const SORT_TITLE
generateThread($forum_id, $author_id, $display_user_id, $subject, $message, $notify, $notify_posts, $alias='', $date='', $status=1)
generate new dataset in frm_threads
static _lookupObjIdForForumId($a_for_id)
Class Forum core functions for forum.
Base class for ILIAS Exception handling.
getOnePost($post)
get one post-dataset
const NEWS_USERS
const IL_CAL_DATETIME
getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
$result
static mergeThreads($obj_id, $source_id, $target_id)
static _exists($a_id, $a_reference=false, $a_type=null)
checks if an object exists in object_data
static getFirstNewsIdForContext($a_context_obj_id, $a_context_obj_type, $a_context_sub_obj_id="", $a_context_sub_obj_type="")
Get first new id of news set related to a certain context.
sendForumNotifications($post_data)
static insertLatexImages($a_text, $a_start="\ex\, $a_end="\\tex\")
replace [text]...[/tex] tags with formula image code
countUserArticles($a_user_id)
get number of articles from given user-ID
getForumRefId()
get forum ref_id public
disableForumNotification($user_id)
Disable a user&#39;s notification about new posts in this forum.
postCensorship($message, $pos_pk, $cens=0)
update dataset in frm_posts with censorship info
getMDB2DataType()
get content of additional condition
getPostDepth($a_node_id, $tree_id)
Return depth of an object private.
$target_id
Definition: goto.php:88
insertPostNode($a_node_id, $a_parent_id, $tree_id, $a_date='')
insert node under parent node public
static _lookupTitle($a_id)
lookup object title
static updateTargetRootRgt($root_node_id, $rgt)
getFirstPostByThread($a_thread_id)
Get first post of thread.
updateVisits($ID)
update page hits of given forum- or thread-ID public
getUserStatistic($is_moderator=false)
deletePost($post)
delete post and sub-posts
getFirstPostNode($tree_id)
get data of the first node from frm_posts_tree and frm_posts public
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
fetchPostNodeData($a_row)
get data of parent node from frm_posts_tree and frm_posts private
static _getAllReferences($a_id)
get all reference ids of object
_removeUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Remove usage of mob in another container.
enableForumNotification($user_id)
Enable a user&#39;s notification about new posts in this forum.
static mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
static _lookupObjectId($a_ref_id)
lookup object id
setImportName($a_import_name)
setDbTable($dbTable)
set database table
sendPostActivationNotification($post_data)
__deletePostFiles($a_ids)
static getInstance($a_obj_id=0)
generatePost($forum_id, $thread_id, $author_id, $display_user_id, $message, $parent_pos, $notify, $subject='', $alias='', $date='', $status=1, $send_activation_mail=0)
generate new dataset in frm_posts
setMDB2WhereCondition($query_string, $data_type, $data_value)
set content for additional condition
getModeratorFromPost($pos_pk)
get one post-dataset
$mobs
getOneThread()
get one thread-dataset by WhereCondition
prepareText($text, $edit=0, $quote_user='', $type='')
prepares given string public
static _exists($a_id)
checks wether a lm content object with specified id exists or not
enableThreadNotification($user_id, $thread_id)
Enable a user&#39;s notification about new posts in a thread.
Class Mail this class handles base functions for mail handling.
static updateLastPostByObjId($a_obj_id)
const NEWS_NOTICE
getOrderField()
get name of orderField
getModerators()
get all users assigned to local role il_frm_moderator_<frm_ref_id>
__formatMessage($thread_data, $post_data=array(), $user_obj)
generates the notificiation message, if a post has been answered
static formatDate(ilDateTime $date)
Format a date public.
formatNotification($post_data, $cron=0, $attachments=array(), $user_id)
global $ilIliasIniFile
Date and time handling
static getSalutation($a_usr_id, $a_language=null)
Get salutation.
redirection script todo: (a better solution should control the processing via a xml file) ...
_lookupLogin($a_user_id)
lookup login
deletePostTree($a_node)
delete node and the whole subtree under this node public
Class ilObjMediaObject.
const SORT_DATE
getForumId()
//
isForumNotificationEnabled($user_id)
Check whether a user&#39;s notification about new posts in this forum is enabled (result > 0) or not (res...
convertDate($date)
converts the date format
static _getThreads($a_obj_id, $a_sort_mode=self::SORT_DATE)
Get thread infos of object.
getOneTopic()
get one topic-dataset by WhereCondition
countActiveUserArticles($a_user_id)
getMDB2Query()
get content of additional condition
formatNotificationSubject($post_data)
addPostTree($a_tree_id, $a_node_id=-1, $a_date='')
create a new post-tree
_getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
formatPostActivationNotification($post_data, $user_id)
isThreadNotificationEnabled($user_id, $thread_id)
Check whether a user&#39;s notification about new posts in a thread is enabled (result > 0) or not (resul...
static mergePosts($source_thread_id, $target_thread_id)
moveThreads($thread_ids=array(), $src_ref_id=0, $dest_top_frm_fk=0)
Moves all chosen threads and their posts to a new forum.
static _lookupLanguage($a_usr_id)
global $ilUser
Definition: imgupload.php:15
$ref_id
Definition: sahs_server.php:39
static deleteByThreadId($thr_id)
global $ilSetting
Definition: privfeed.php:40
setPageHits($pageHits)
set number of max.
This class handles all operations on files for the forum object.
global $ilDB
sendThreadNotifications($post_data)
_lookupPostMessage($a_id)
getDbTable()
get name of database table
language handling
static _getLanguageInstanceByUsrId($usr_id)
Get the ilLanguage instance for the passed user id.
getMDB2DataValue()
/* get content of additional condition
getPageHits()
get number of max.
setForumRefId($a_ref_id)
set reference id which refers to ILIAS obj_id
setLanguage($lng)
__sendMessage($a_parent_pos, $post_data=array())
_getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)
static _getInstallationSignature()
Static getter for the installation signature.
setOrderField($orderField)
set database field for sorting results
__construct()
Constructor public.
getPostNode($post_id)
get data of given node from frm_posts_tree and frm_posts public