ILIAS  release_4-4 Revision
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_usr_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 
445  public function generatePost($forum_id, $thread_id, $user, $message, $parent_pos, $notify, $subject = '', $alias = '', $date = '', $status = 1, $send_activation_mail = 0)
446  {
447  global $ilUser, $ilDB;
448 
449  $objNewPost = new ilForumPost();
450  $objNewPost->setForumId($forum_id);
451  $objNewPost->setThreadId($thread_id);
452  $objNewPost->setSubject($subject);
453  $objNewPost->setMessage($message);
454  $objNewPost->setUserId($user);
455  $objNewPost->setUserAlias($alias);
456  if ($date == "")
457  {
458  $objNewPost->setCreateDate(date("Y-m-d H:i:s"));
459  }
460  else
461  {
462  if (strpos($date, "-") > 0) // in mysql format
463  {
464  $objNewPost->setCreateDate($date);
465  }
466  else // a timestamp
467  {
468  $objNewPost->setCreateDate(date("Y-m-d H:i:s", $date));
469  }
470  }
471 
472  $objNewPost->setImportName($this->getImportName());
473  $objNewPost->setNotification($notify);
474  $objNewPost->setStatus($status);
475  $objNewPost->insert();
476 
477  // entry in tree-table
478  if ($parent_pos == 0)
479  {
480  $this->addPostTree($objNewPost->getThreadId(), $objNewPost->getId(), $objNewPost->getCreateDate());
481  }
482  else
483  {
484  $this->insertPostNode($objNewPost->getId(), $parent_pos, $objNewPost->getThreadId(), $objNewPost->getCreateDate());
485  }
486 //echo "<br>->".$objNewPost->getId()."-".$parent_pos."-".$objNewPost->getThreadId()."-".
487 // $objNewPost->getCreateDate()."-".$forum_id."-".$message."-".$user."-";
488  // string last post
489  $lastPost = $objNewPost->getForumId()."#".$objNewPost->getThreadId()."#".$objNewPost->getId();
490 
491  // update thread
492  $result = $ilDB->manipulateF('
493  UPDATE frm_threads
494  SET thr_num_posts = thr_num_posts + 1,
495  thr_last_post = %s
496  WHERE thr_pk = %s',
497  array('text', 'integer'),
498  array($lastPost, $objNewPost->getThreadId()));
499 
500  // update forum
501  $result = $ilDB->manipulateF('
502  UPDATE frm_data
503  SET top_num_posts = top_num_posts + 1,
504  top_last_post = %s
505  WHERE top_pk = %s',
506  array('text', 'integer'),
507  array($lastPost, $objNewPost->getForumId()));
508 
509  // MARK READ
510  $forum_obj = ilObjectFactory::getInstanceByRefId($this->getForumRefId());
511  $forum_obj->markPostRead($objNewPost->getUserId(), $objNewPost->getThreadId(), $objNewPost->getId());
512 
513  $pos_data = $objNewPost->getDataAsArray();
514  $pos_data["ref_id"] = $this->getForumRefId();
515 
516  // Send notification to moderators if they have to enable a post
517 
518  if (!$status && $send_activation_mail)
519  {
520  $pos_data["top_name"] = $forum_obj->getTitle();
521  $this->sendPostActivationNotification($pos_data);
522  }
523 
524  // Add Notification to news
525  if ($status)
526  {
527  require_once 'Services/RTE/classes/class.ilRTE.php';
528  include_once("./Services/News/classes/class.ilNewsItem.php");
529  $news_item = new ilNewsItem();
530  $news_item->setContext($forum_obj->getId(), 'frm', $objNewPost->getId(), 'pos');
531  $news_item->setPriority(NEWS_NOTICE);
532  $news_item->setTitle($objNewPost->getSubject());
533  $news_item->setContent(ilRTE::_replaceMediaObjectImageSrc($this->prepareText($objNewPost->getMessage(), 0), 1));
534  $news_item->setUserId($user);
535  $news_item->setVisibility(NEWS_USERS);
536  $news_item->create();
537  }
538 
539  return $objNewPost->getId();
540  }
541 
555  public function generateThread($forum_id, $user, $subject, $message, $notify, $notify_posts, $alias = '', $date = '', $status = 1)
556  {
557  global $ilDB;
558 
559  $objNewThread = new ilForumTopic();
560  $objNewThread->setForumId($forum_id);
561  $objNewThread->setUserId($user);
562  $objNewThread->setSubject($subject);
563  if ($date == "")
564  {
565  $objNewThread->setCreateDate(date("Y-m-d H:i:s"));
566  }
567  else
568  {
569  if (strpos($date, "-") > 0) // in mysql format
570  {
571  $objNewThread->setCreateDate($date);
572  }
573  else // a timestamp
574  {
575  $objNewThread->setCreateDate(date("Y-m-d H:i:s", $date));
576  }
577  }
578  $objNewThread->setImportName($this->getImportName());
579  $objNewThread->setUserAlias($alias);
580  $objNewThread->insert();
581 
582  if ($notify_posts == 1)
583  {
584  $objNewThread->enableNotification($user);
585  }
586 
587  // update forum
588  $statement = $ilDB->manipulateF('
589  UPDATE frm_data
590  SET top_num_threads = top_num_threads + 1
591  WHERE top_pk = %s',
592  array('integer'), array($forum_id));
593 
594  return $this->generatePost($forum_id, $objNewThread->getId(), $user, $message, 0, $notify, $subject, $alias, $objNewThread->getCreateDate(), $status);
595  }
596 
605  public function moveThreads($thread_ids = array(), $src_ref_id = 0, $dest_top_frm_fk = 0)
606  {
607  global $ilDB;
608 
609  $src_top_frm_fk = ilObject::_lookupObjectId($src_ref_id);
610 
611  if (is_numeric($src_top_frm_fk) && $src_top_frm_fk > 0 && is_numeric($dest_top_frm_fk) && $dest_top_frm_fk > 0)
612  {
613 
614  $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($src_top_frm_fk));
615 
616  $oldFrmData = $this->getOneTopic();
617 
618  $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($dest_top_frm_fk));
619 
620  $newFrmData = $this->getOneTopic();
621 
622  if ($oldFrmData['top_pk'] && $newFrmData['top_pk'])
623  {
624  $moved_posts = 0;
625  $moved_threads = 0;
626  $visits = 0;
627  foreach ($thread_ids as $id)
628  {
629  $objTmpThread = new ilForumTopic($id);
630 
631  $numPosts = $objTmpThread->movePosts($src_top_frm_fk, $oldFrmData['top_pk'], $dest_top_frm_fk, $newFrmData['top_pk']);
632  if (($last_post_string = $objTmpThread->getLastPostString()) != '')
633  {
634  $last_post_string = explode('#', $last_post_string);
635  $last_post_string[0] = $newFrmData['top_pk'];
636  $last_post_string = implode('#', $last_post_string);
637  $objTmpThread->setLastPostString($last_post_string);
638  }
639 
640  $visits += $objTmpThread->getVisits();
641 
642  $moved_posts += $numPosts;
643  ++$moved_threads;
644 
645  $objTmpThread->setForumId($newFrmData['top_pk']);
646  $objTmpThread->update();
647 
648  unset($objTmpThread);
649  }
650 
651  // update frm_data source forum
652  $ilDB->setLimit(1);
653  $res = $ilDB->queryf('
654  SELECT pos_thr_fk, pos_pk
655  FROM frm_posts
656  WHERE pos_top_fk = %s
657  ORDER BY pos_date DESC',
658  array('integer'), array($oldFrmData['top_pk']));
659 
660  $row = $ilDB->fetchObject($res);
661  $last_post_src = $oldFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
662 
663  $statement = $ilDB->manipulateF('
664  UPDATE frm_data
665  SET top_num_posts = top_num_posts - %s,
666  top_num_threads = top_num_threads - %s,
667  visits = visits - %s,
668  top_last_post = %s
669  WHERE top_pk = %s',
670  array('integer', 'integer', 'integer', 'text', 'integer'),
671  array( $moved_posts,
672  $moved_threads,
673  $visits,
674  $last_post_src,
675  $oldFrmData['top_pk']));
676 
677  // update frm_data destination forum
678 
679  $ilDB->setLimit(1);
680  $res = $ilDB->queryf('
681  SELECT pos_thr_fk, pos_pk
682  FROM frm_posts
683  WHERE pos_top_fk = %s
684  ORDER BY pos_date DESC',
685  array('integer'), array($newFrmData['top_kp']));
686 
687  $row = $ilDB->fetchObject($res);
688  $last_post_dest = $newFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
689 
690  $statement = $ilDB->manipulateF('
691  UPDATE frm_data
692  SET top_num_posts = top_num_posts + %s,
693  top_num_threads = top_num_threads + %s,
694  visits = visits + %s,
695  top_last_post = %s
696  WHERE top_pk = %s',
697  array('integer', 'integer', 'integer', 'text', 'integer'),
698  array($moved_posts, $moved_threads, $visits, $last_post_dest, $newFrmData['top_pk']));
699 
700  /*
701  // update news items
702  include_once("./Services/News/classes/class.ilNewsItem.php");
703  $objNewsItem = new ilNewsItem();
704  $news_items = $objNewsItem->getNewsForRefId($src_ref_id);
705  foreach ($news_items as $news_item)
706  {
707  $tmpObjNewsItem = new ilNewsItem($news_item['id']);
708  if ($tmpObjNewsItem->getContextObjId() == $src_top_frm_fk)
709  {
710  $tmpObjNewsItem->setContextObjId($dest_top_frm_fk);
711  $tmpObjNewsItem->update();
712  }
713  unset($tmpObjNewsItem);
714  }
715  */
716  }
717  }
718 }
719 
720 
728  public function postCensorship($message, $pos_pk, $cens = 0)
729  {
730  global $ilDB;
731 
732  $statement = $ilDB->manipulateF('
733  UPDATE frm_posts
734  SET pos_cens_com = %s,
735  pos_update = %s,
736  pos_cens = %s,
737  update_user = %s
738  WHERE pos_pk = %s',
739  array('text', 'timestamp', 'integer', 'integer', 'integer'),
740  array($message, date("Y-m-d H:i:s"), $cens, $_SESSION['AccountId'], $pos_pk));
741 
742  // Change news item accordingly
743  include_once("./Services/News/classes/class.ilNewsItem.php");
744  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
745  "frm", $pos_pk, "pos");
746  if ($news_id > 0)
747  {
748  if ($cens > 0) // censor
749  {
750  $news_item = new ilNewsItem($news_id);
751  //$news_item->setTitle($subject);
752  $news_item->setContent(nl2br($this->prepareText($message, 0)));
753  $news_item->update();
754  }
755  else // revoke censorship
756  {
757  // get original message
758  $res = $ilDB->queryf('
759  SELECT * FROM frm_posts
760  WHERE pos_pk = %s',
761  array('integer'), array($pos_pk));
762 
763  $rec = $ilDB->fetchAssoc($res);
764 
765  $news_item = new ilNewsItem($news_id);
766  //$news_item->setTitle($subject);
767  $news_item->setContent(nl2br($this->prepareText($rec["pos_message"], 0)));
768  $news_item->update();
769  }
770  }
771 
772  return true;
773  }
774 
781  public function deletePost($post)
782  {
783  global $ilDB;
784 
785  include_once "./Modules/Forum/classes/class.ilObjForum.php";
786 
787  // delete tree and get id's of all posts to delete
788  $p_node = $this->getPostNode($post);
789  $del_id = $this->deletePostTree($p_node);
790 
791 
792  // Delete User read entries
793  foreach($del_id as $post_id)
794  {
795  ilObjForum::_deleteReadEntries($post_id);
796  }
797 
798  // DELETE ATTACHMENTS ASSIGNED TO POST
799  $this->__deletePostFiles($del_id);
800 
801  $dead_pos = count($del_id);
802  $dead_thr = 0;
803 
804  // if deletePost is thread opener ...
805  if ($p_node["parent"] == 0)
806  {
807  // delete thread access data
808  include_once './Modules/Forum/classes/class.ilObjForum.php';
809 
810  ilObjForum::_deleteAccessEntries($p_node['tree']);
811 
812  // delete thread
813  $dead_thr = $p_node["tree"];
814 
815  $statement = $ilDB->manipulateF('
816  DELETE FROM frm_threads
817  WHERE thr_pk = %s',
818  array('integer'), array($p_node['tree']));
819 
820  // update num_threads
821  $statement = $ilDB->manipulateF('
822  UPDATE frm_data
823  SET top_num_threads = top_num_threads - 1
824  WHERE top_frm_fk = %s',
825  array('integer'), array($this->id));
826 
827  // delete all related news
828  $posset = $ilDB->queryf('
829  SELECT * FROM frm_posts
830  WHERE pos_thr_fk = %s',
831  array('integer'), array($p_node['tree']));
832 
833  while ($posrec = $ilDB->fetchAssoc($posset))
834  {
835  include_once("./Services/News/classes/class.ilNewsItem.php");
836  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
837  "frm", $posrec["pos_pk"], "pos");
838  if ($news_id > 0)
839  {
840  $news_item = new ilNewsItem($news_id);
841  $news_item->delete();
842  }
843 
844  try
845  {
846  include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
847  $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $posrec['pos_pk']);
848  foreach($mobs as $mob)
849  {
850  if(ilObjMediaObject::_exists($mob))
851  {
852  ilObjMediaObject::_removeUsage($mob, 'frm:html', $posrec['pos_pk']);
853  $mob_obj = new ilObjMediaObject($mob);
854  $mob_obj->delete();
855  }
856  }
857  }
858  catch(Exception $e)
859  {
860  }
861  }
862 
863 
864  // delete all posts of this thread
865  $statement = $ilDB->manipulateF('
866  DELETE FROM frm_posts
867  WHERE pos_thr_fk = %s',
868  array('integer'), array($p_node['tree']));
869 
870  }
871  else
872  {
873 
874  // delete this post and its sub-posts
875  for ($i = 0; $i < $dead_pos; $i++)
876  {
877  $statement = $ilDB->manipulateF('
878  DELETE FROM frm_posts
879  WHERE pos_pk = %s',
880  array('integer'), array($del_id[$i]));
881 
882  // delete related news item
883  include_once("./Services/News/classes/class.ilNewsItem.php");
884  $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
885  "frm", $del_id[$i], "pos");
886  if ($news_id > 0)
887  {
888  $news_item = new ilNewsItem($news_id);
889  $news_item->delete();
890  }
891 
892  try
893  {
894  include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
895  $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $del_id[$i]);
896  foreach($mobs as $mob)
897  {
898  if(ilObjMediaObject::_exists($mob))
899  {
900  ilObjMediaObject::_removeUsage($mob, 'frm:html', $del_id[$i]);
901  $mob_obj = new ilObjMediaObject($mob);
902  $mob_obj->delete();
903  }
904  }
905  }
906  catch(Exception $e)
907  {
908  }
909  }
910 
911  // update num_posts in frm_threads
912  $statement = $ilDB->manipulateF('
913  UPDATE frm_threads
914  SET thr_num_posts = thr_num_posts - %s
915  WHERE thr_pk = %s',
916  array('integer', 'integer'),
917  array($dead_pos, $p_node['tree']));
918 
919 
920  // get latest post of thread and update last_post
921  $res1 = $ilDB->queryf('
922  SELECT * FROM frm_posts
923  WHERE pos_thr_fk = %s
924  ORDER BY pos_date DESC',
925  array('integer'), array($p_node['tree']));
926 
927  if ($res1->numRows() == 0)
928  {
929  $lastPost_thr = "";
930  }
931  else
932  {
933  $z = 0;
934 
935  while ($selData = $ilDB->fetchAssoc($res1))
936  {
937  if ($z > 0)
938  {
939  break;
940  }
941 
942  $lastPost_thr = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
943  $z ++;
944  }
945  }
946 
947  $statement = $ilDB->manipulateF('
948  UPDATE frm_threads
949  SET thr_last_post = %s
950  WHERE thr_pk = %s',
951  array('text', 'integer'), array($lastPost_thr, $p_node['tree']));
952  }
953 
954  // update num_posts in frm_data
955  $statement = $ilDB->manipulateF('
956  UPDATE frm_data
957  SET top_num_posts = top_num_posts - %s
958  WHERE top_frm_fk = %s',
959  array('integer', 'integer'), array($dead_pos, $this->id));
960 
961 
962  // get latest post of forum and update last_post
963  $res2 = $ilDB->queryf('
964  SELECT * FROM frm_posts, frm_data
965  WHERE pos_top_fk = top_pk
966  AND top_frm_fk = %s
967  ORDER BY pos_date DESC',
968  array('integer'), array($this->id));
969 
970  if ($res2->numRows() == 0)
971  {
972  $lastPost_top = "";
973  }
974  else
975  {
976  $z = 0;
977 
978  while ($selData = $ilDB->fetchAssoc($res2))
979  {
980  if ($z > 0)
981  {
982  break;
983  }
984 
985  $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
986  $z ++;
987  }
988  }
989 
990  $statement = $ilDB->manipulateF('
991  UPDATE frm_data
992  SET top_last_post = %s
993  WHERE top_frm_fk = %s',
994  array('text', 'integer'), array($lastPost_top, $this->id));
995 
996  return $dead_thr;
997  }
998 
1006  public function getAllThreads($a_topic_id, array $params = array(), $limit = 0, $offset = 0)
1007  {
1013  global $ilDB, $ilUser, $ilSetting;
1014 
1015  $frm_overview_setting = (int)$ilSetting::_lookupValue('frma','forum_overview');
1016 
1017  $excluded_ids_condition = '';
1018  if(isset($params['excluded_ids']) && is_array($params['excluded_ids']) && $params['excluded_ids'])
1019  {
1020  $excluded_ids_condition = ' AND ' . $ilDB->in('thr_pk', $params['excluded_ids'], true, 'integer'). ' ';
1021  }
1022 
1023  // Count all threads for the passed forum
1024  $query = "SELECT COUNT(thr_pk) cnt
1025  FROM frm_threads
1026  WHERE thr_top_fk = %s {$excluded_ids_condition}";
1027  $res = $ilDB->queryF($query, array('integer'), array($a_topic_id));
1028  $data = $ilDB->fetchAssoc($res);
1029  $cnt = (int) $data['cnt'];
1030 
1031  $threads = array();
1032 
1033  $data = array();
1034  $data_types = array();
1035 
1036  $active_query = '';
1037  $active_inner_query = '';
1038  if(!$params['is_moderator'])
1039  {
1040  $active_query = ' AND (pos_status = %s OR pos_usr_id = %s) ';
1041  $active_inner_query = ' AND (ipos.pos_status = %s OR ipos.pos_usr_id = %s) ';
1042  }
1043 
1044  $frm_props = ilForumProperties::getInstance($this->getForumId());
1045 
1046  $optional_fields = '';
1047  if($frm_props->getThreadSorting() == 1)
1048  {
1049  $optional_fields = ',thread_sorting';
1050  }
1051 
1052  if(!$ilUser->isAnonymous())
1053  {
1054  $query = "SELECT
1055  (CASE WHEN COUNT(DISTINCT(notification_id)) > 0 THEN 1 ELSE 0 END) usr_notification_is_enabled,
1056  MAX(pos_date) post_date,
1057  COUNT(DISTINCT(pos_pk)) num_posts,
1058  COUNT(DISTINCT(pos_pk)) - COUNT(DISTINCT(postread.post_id)) num_unread_posts, ";
1059 
1060  // new posts query
1061  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1062  {
1063  $query .= "
1064  (SELECT COUNT(DISTINCT(ipos.pos_pk))
1065  FROM frm_posts ipos
1066  LEFT JOIN frm_user_read iread ON iread.post_id = ipos.pos_pk AND iread.usr_id = %s
1067  LEFT JOIN frm_thread_access iacc ON (iacc.thread_id = ipos.pos_thr_fk AND iacc.usr_id = %s)
1068  WHERE ipos.pos_thr_fk = thr_pk
1069 
1070  AND (ipos.pos_update > iacc.access_old_ts
1071  OR
1072  (iacc.access_old IS NULL AND (ipos.pos_update > " . $ilDB->quote(date('Y-m-d H:i:s', NEW_DEADLINE), 'timestamp') . "))
1073  )
1074 
1075  AND ipos.pos_usr_id != %s
1076  AND iread.usr_id IS NULL $active_inner_query
1077  ) num_new_posts, ";
1078  }
1079 
1080  $query .= " thr_pk, thr_top_fk, thr_subject, thr_usr_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1081  {$optional_fields}
1082  FROM frm_threads
1083 
1084  LEFT JOIN frm_notification
1085  ON frm_notification.thread_id = thr_pk
1086  AND frm_notification.user_id = %s
1087 
1088  LEFT JOIN frm_posts
1089  ON pos_thr_fk = thr_pk $active_query
1090 
1091  LEFT JOIN frm_user_read postread
1092  ON postread.post_id = pos_pk
1093  AND postread.usr_id = %s";
1094 
1095  if($frm_props->getThreadSorting() == 1)
1096  {
1097  $additional_sort = ', thread_sorting ASC, post_date DESC ';
1098  }
1099  else
1100  {
1101  $additional_sort = ', post_date DESC';
1102  }
1103 
1104  $query .= " WHERE thr_top_fk = %s
1105  {$excluded_ids_condition}
1106  GROUP BY thr_pk, thr_top_fk, thr_subject, thr_usr_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1107  {$optional_fields}
1108  ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1109 
1110 
1111  // data_types for new posts query and $active_inner_query
1112  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1113  {
1114  $data_types[] = 'integer';
1115  $data_types[] = 'integer';
1116  $data_types[] = 'integer';
1117 
1118  if(!$params['is_moderator'])
1119  {
1120  array_push($data_types, 'integer', 'integer');
1121  }
1122  }
1123  $data_types[] = 'integer';
1124  if(!$params['is_moderator'])
1125  {
1126  array_push($data_types, 'integer', 'integer');
1127  }
1128  $data_types[] = 'integer';
1129  $data_types[] = 'integer';
1130 
1131  // data_values for new posts query and $active_inner_query
1132  if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1133  {
1134  $data[] = $ilUser->getId();
1135  $data[] = $ilUser->getId();
1136  $data[] = $ilUser->getId();
1137 
1138  if(!$params['is_moderator'])
1139  {
1140  array_push($data, '1', $ilUser->getId());
1141  }
1142  }
1143  $data[] = $ilUser->getId();
1144  if(!$params['is_moderator'])
1145  {
1146  array_push($data, '1', $ilUser->getId());
1147  }
1148  $data[] = $ilUser->getId();
1149  $data[] = $a_topic_id;
1150  }
1151  else
1152  {
1153  $query = "SELECT
1154  0 usr_notification_is_enabled,
1155  MAX(pos_date) post_date,
1156  COUNT(DISTINCT(pos_pk)) num_posts,
1157  COUNT(DISTINCT(pos_pk)) num_unread_posts,
1158  COUNT(DISTINCT(pos_pk)) num_new_posts,
1159  thr_pk, thr_top_fk, thr_subject, thr_usr_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1160  {$optional_fields}
1161  FROM frm_threads
1162 
1163  LEFT JOIN frm_posts
1164  ON pos_thr_fk = thr_pk $active_query";
1165 
1166  if($frm_props->getThreadSorting() == 1)
1167  {
1168  $additional_sort = ', thread_sorting ASC, post_date DESC ';
1169  }
1170  else
1171  {
1172  $additional_sort = ', post_date DESC';
1173  }
1174 
1175  $query .= " WHERE thr_top_fk = %s
1176  {$excluded_ids_condition}
1177  GROUP BY thr_pk, thr_top_fk, thr_subject, thr_usr_id, thr_usr_alias, thr_num_posts, thr_last_post, thr_date, thr_update, visits, frm_threads.import_name, is_sticky, is_closed
1178  {$optional_fields}
1179  ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1180 
1181  if(!$params['is_moderator'])
1182  {
1183  array_push($data_types, 'integer', 'integer');
1184  }
1185  $data_types[] = 'integer';
1186 
1187  if(!$params['is_moderator'])
1188  {
1189  array_push($data, '1', $ilUser->getId());
1190  }
1191  $data[] = $a_topic_id;
1192  }
1193 
1194  if($limit || $offset)
1195  {
1196  $ilDB->setLimit($limit, $offset);
1197  }
1198  $res = $ilDB->queryF($query, $data_types, $data);
1199  while($row = $ilDB->fetchAssoc($res))
1200  {
1201  $thread = new ilForumTopic($row['thr_pk'], $params['is_moderator'], true);
1202  $thread->assignData($row);
1203  $threads[] = $thread;
1204  }
1205 
1206  return array(
1207  'items' => $threads,
1208  'cnt' => $cnt
1209  );
1210  }
1211 
1212  public function getUserStatistic($is_moderator = false)
1213  {
1214  global $ilDB, $ilUser;
1215 
1216  $statistic = array();
1217 
1218  $data_types = array();
1219  $data = array();
1220 
1221  $query = "SELECT COUNT(f.pos_usr_id) ranking, u.login, p.value, u.lastname, u.firstname
1222  FROM frm_posts f
1223  INNER JOIN frm_posts_tree t
1224  ON f.pos_pk = t.pos_fk
1225  INNER JOIN frm_threads th
1226  ON t.thr_fk = th.thr_pk
1227  INNER JOIN usr_data u
1228  ON u.usr_id = f.pos_usr_id
1229  INNER JOIN frm_data d
1230  ON d.top_pk = f.pos_top_fk
1231  LEFT JOIN usr_pref p
1232  ON p.usr_id = u.usr_id AND p.keyword = %s
1233  WHERE 1 = 1";
1234 
1235  array_push($data_types, 'text');
1236  array_push($data, 'public_profile');
1237 
1238  if (!$is_moderator)
1239  {
1240  $query .= ' AND (pos_status = %s
1241  OR (pos_status = %s
1242  AND pos_usr_id = %s ))';
1243 
1244  array_push($data_types,'integer', 'integer', 'integer');
1245  array_push($data, '1', '0', $ilUser->getId());
1246  }
1247 
1248  $query .= ' AND d.top_frm_fk = %s
1249  GROUP BY pos_usr_id, u.login, p.value,u.lastname, u.firstname';
1250 
1251  array_push($data_types,'integer');
1252  array_push($data, $this->getForumId());
1253 
1254  $res = $ilDB->queryf($query, $data_types, $data);
1255 
1256  $counter = 0;
1257  while ($row = $ilDB->fetchAssoc($res))
1258  {
1259  $statistic[$counter][] = $row['ranking'];
1260  $statistic[$counter][] = $row['login'];
1261 
1262  $lastname ='';
1263  $firstname = '';
1264  if(!$ilUser->isAnonymous() && in_array($row['value'], array('y', 'g')) ||
1265  $ilUser->isAnonymous() && 'g' == $row['value'])
1266  {
1267  $lastname = $row['lastname'];
1268  $firstname = $row['firstname'];
1269  }
1270 
1271  $statistic[$counter][] = $lastname;
1272  $statistic[$counter][] = $firstname;
1273 
1274  ++$counter;
1275  }
1276 
1277  return is_array($statistic) ? $statistic : array();
1278  }
1279 
1280 
1288  public function getFirstPostByThread($a_thread_id)
1289  {
1290  global $ilDB;
1291 
1292  $res = $ilDB->queryf('
1293  SELECT * FROM frm_posts_tree
1294  WHERE thr_fk = %s
1295  AND parent_pos = %s',
1296  array('integer', 'integer'), array($a_thread_id, '0'));
1297 
1298  $row = $ilDB->fetchObject($res);
1299 
1300  return $row->pos_fk ? $row->pos_fk : 0;
1301  }
1302 
1309  public function getModerators()
1310  {
1311  global $rbacreview;
1312 
1313  return $this->_getModerators($this->getForumRefId());
1314  }
1315 
1323  function _getModerators($a_ref_id)
1324  {
1325  global $rbacreview;
1326 
1327  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
1328  $role_arr = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"]);
1329 
1330  foreach ($role_arr as $role_id)
1331  {
1332  //$roleObj = $this->ilias->obj_factory->getInstanceByObjId($role_id);
1333  $title = ilObject::_lookupTitle($role_id);
1334  if ($title == "il_frm_moderator_".$a_ref_id)
1335  {
1336  #return $rbacreview->assignedUsers($roleObj->getId());
1337  return $title = $rbacreview->assignedUsers($role_id);
1338  }
1339  }
1340 
1341  return array();
1342  }
1343 
1352  public static function _isModerator($a_ref_id, $a_usr_id)
1353  {
1354  return in_array($a_usr_id, ilForum::_getModerators($a_ref_id));
1355  }
1356 
1364  public function countUserArticles($a_user_id)
1365  {
1366  global $ilDB;
1367 
1368  $res = $ilDB->queryf('
1369  SELECT * FROM frm_data
1370  INNER JOIN frm_posts ON pos_top_fk = top_pk
1371  WHERE top_frm_fk = %s
1372  AND pos_usr_id = %s',
1373  array('integer', 'integer'),
1374  array($this->getForumId(), $a_user_id));
1375 
1376  return $res->numRows();
1377  }
1378 
1379  public function countActiveUserArticles($a_user_id)
1380  {
1381  global $ilDB, $ilUser;
1382 
1383  $res = $ilDB->queryf('
1384  SELECT * FROM frm_data
1385  INNER JOIN frm_posts ON pos_top_fk = top_pk
1386  WHERE top_frm_fk = %s
1387  AND (pos_status = %s
1388  OR (pos_status = %s
1389  AND pos_usr_id = %s
1390  )
1391  )
1392  AND pos_usr_id = %s',
1393  array('integer', 'integer', 'integer', 'integer', 'integer'),
1394  array($this->getForumId(),'1', '0', $ilUser->getId(), $a_user_id));
1395 
1396  return $res->numRows();
1397  }
1398 
1405  public function convertDate($date)
1406  {
1408  }
1409 
1417  public function addPostTree($a_tree_id, $a_node_id = -1, $a_date = '')
1418  {
1419  global $ilDB;
1420 
1421  $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1422 
1423  if ($a_node_id <= 0)
1424  {
1425  $a_node_id = $a_tree_id;
1426  }
1427 
1428  $nextId = $ilDB->nextId('frm_posts_tree');
1429 
1430  $statement = $ilDB->manipulateF('
1431  INSERT INTO frm_posts_tree
1432  ( fpt_pk,
1433  thr_fk,
1434  pos_fk,
1435  parent_pos,
1436  lft,
1437  rgt,
1438  depth,
1439  fpt_date
1440  )
1441  VALUES(%s, %s, %s, %s, %s, %s, %s, %s )',
1442  array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1443  array($nextId, $a_tree_id, $a_node_id, '0', '1', '2', '1', $a_date));
1444 
1445  return true;
1446  }
1447 
1455  public function insertPostNode($a_node_id, $a_parent_id, $tree_id, $a_date = '')
1456  {
1457  global $ilDB;
1458 
1459  $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1460 
1461  // get left value
1462  $sql_res = $ilDB->queryf('
1463  SELECT * FROM frm_posts_tree
1464  WHERE pos_fk = %s
1465  AND thr_fk = %s',
1466  array('integer', 'integer'),
1467  array($a_parent_id, $tree_id));
1468 
1469  $res = $ilDB->fetchObject($sql_res);
1470 
1471  $left = $res->lft;
1472 
1473  $lft = $left + 1;
1474  $rgt = $left + 2;
1475 
1476  // spread tree
1477  $statement = $ilDB->manipulateF('
1478  UPDATE frm_posts_tree
1479  SET lft = CASE
1480  WHEN lft > %s
1481  THEN lft + 2
1482  ELSE lft
1483  END,
1484  rgt = CASE
1485  WHEN rgt > %s
1486  THEN rgt + 2
1487  ELSE rgt
1488  END
1489  WHERE thr_fk = %s',
1490  array('integer', 'integer', 'integer'),
1491  array($left, $left, $tree_id));
1492 
1493  $depth = $this->getPostDepth($a_parent_id, $tree_id) + 1;
1494 
1495  // insert node
1496  $nextId = $ilDB->nextId('frm_posts_tree');
1497  $statement = $ilDB->manipulateF('
1498  INSERT INTO frm_posts_tree
1499  ( fpt_pk,
1500  thr_fk,
1501  pos_fk,
1502  parent_pos,
1503  lft,
1504  rgt,
1505  depth,
1506  fpt_date
1507  )
1508  VALUES(%s,%s,%s, %s, %s, %s,%s, %s)',
1509  array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1510  array( $nextId,
1511  $tree_id,
1512  $a_node_id,
1513  $a_parent_id,
1514  $lft,
1515  $rgt,
1516  $depth,
1517  $a_date)
1518  );
1519 
1520  }
1521 
1529  public function getPostDepth($a_node_id, $tree_id)
1530  {
1531  global $ilDB;
1532 
1533  if ($tree_id)
1534  {
1535  $sql_res = $ilDB->queryf('
1536  SELECT depth FROM frm_posts_tree
1537  WHERE pos_fk = %s
1538  AND thr_fk = %s',
1539  array('integer', 'integer'),
1540  array($a_node_id, $tree_id));
1541 
1542  $res = $ilDB->fetchObject($sql_res);
1543 
1544  return $res->depth;
1545  }
1546  else
1547  {
1548  return 0;
1549  }
1550  }
1551 
1558  public function getFirstPostNode($tree_id)
1559  {
1560  global $ilDB;
1561 
1562  $res = $ilDB->queryf('
1563  SELECT * FROM frm_posts, frm_posts_tree
1564  WHERE pos_pk = pos_fk
1565  AND parent_pos = %s
1566  AND thr_fk = %s',
1567  array('integer', 'integer'),
1568  array('0', $tree_id));
1569 
1570  $row = $ilDB->fetchObject($res);
1571 
1572  return $this->fetchPostNodeData($row);
1573  }
1574 
1581  public function getPostNode($post_id)
1582  {
1583  global $ilDB;
1584 
1585  $res = $ilDB->queryf('
1586  SELECT * FROM frm_posts, frm_posts_tree
1587  WHERE pos_pk = pos_fk
1588  AND pos_pk = %s',
1589  array('integer'),
1590  array($post_id));
1591 
1592  $row = $ilDB->fetchObject($res);
1593 
1594  return $this->fetchPostNodeData($row);
1595  }
1596 
1603  public function fetchPostNodeData($a_row)
1604  {
1605  global $lng;
1606 
1607  require_once('./Services/User/classes/class.ilObjUser.php');
1608 
1609  if (ilObject::_exists($a_row->pos_usr_id))
1610  {
1611  $tmp_user = new ilObjUser($a_row->pos_usr_id);
1612  $fullname = $tmp_user->getFullname();
1613  $loginname = $tmp_user->getLogin();
1614  }
1615 
1616  $fullname = $fullname ? $fullname : ($a_row->import_name ? $a_row->import_name : $lng->txt("unknown"));
1617 
1618  $data = array(
1619  "pos_pk" => $a_row->pos_pk,
1620  "child" => $a_row->pos_pk,
1621  "author" => $a_row->pos_usr_id,
1622  "alias" => $a_row->pos_usr_alias,
1623  "title" => $fullname,
1624  "loginname" => $loginname,
1625  "type" => "post",
1626  "message" => $a_row->pos_message,
1627  "subject" => $a_row->pos_subject,
1628  "pos_cens_com" => $a_row->pos_cens_com,
1629  "pos_cens" => $a_row->pos_cens,
1630  // "date" => $a_row->date,
1631  "date" => $a_row->fpt_date,
1632  "create_date" => $a_row->pos_date,
1633  "update" => $a_row->pos_update,
1634  "update_user" => $a_row->update_user,
1635  "tree" => $a_row->thr_fk,
1636  "parent" => $a_row->parent_pos,
1637  "lft" => $a_row->lft,
1638  "rgt" => $a_row->rgt,
1639  "depth" => $a_row->depth,
1640  "id" => $a_row->fpt_pk,
1641  "notify" => $a_row->notify,
1642  "import_name" => $a_row->import_name,
1643  "pos_status" => $a_row->pos_status
1644  );
1645 
1646  // why this line? data should be stored without slashes in db
1647  //$data["message"] = stripslashes($data["message"]);
1648 
1649  return $data ? $data : array();
1650  }
1651 
1658  public function deletePostTree($a_node)
1659  {
1660  global $ilDB;
1661 
1662  // GET LEFT AND RIGHT VALUES
1663  $res = $ilDB->queryf('
1664  SELECT * FROM frm_posts_tree
1665  WHERE thr_fk = %s
1666  AND pos_fk = %s
1667  AND parent_pos = %s',
1668  array('integer', 'integer', 'integer'),
1669  array($a_node['tree'], $a_node['pos_pk'], $a_node['parent']));
1670 
1671  while($row = $ilDB->fetchObject($res))
1672  {
1673  $a_node["lft"] = $row->lft;
1674  $a_node["rgt"] = $row->rgt;
1675  }
1676 
1677  $diff = $a_node["rgt"] - $a_node["lft"] + 1;
1678 
1679  // get data of posts
1680  $result = $ilDB->queryf('
1681  SELECT * FROM frm_posts_tree
1682  WHERE lft BETWEEN %s AND %s
1683  AND thr_fk = %s',
1684  array('integer', 'integer', 'integer'),
1685  array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1686 
1687  $del_id = array();
1688 
1689  while ($treeData = $ilDB->fetchAssoc($result))
1690  {
1691  $del_id[] = $treeData["pos_fk"];
1692  }
1693 
1694  // delete subtree
1695  $statement = $ilDB->manipulateF('
1696  DELETE FROM frm_posts_tree
1697  WHERE lft BETWEEN %s AND %s
1698  AND thr_fk = %s',
1699  array('integer', 'integer', 'integer'),
1700  array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1701 
1702 
1703  // close gaps
1704  $statement = $ilDB->manipulateF('
1705  UPDATE frm_posts_tree
1706  SET lft = CASE
1707  WHEN lft > %s
1708  THEN lft - %s
1709  ELSE lft
1710  END,
1711  rgt = CASE
1712  WHEN rgt > %s
1713  THEN rgt - %s
1714  ELSE rgt
1715  END
1716  WHERE thr_fk = %s',
1717  array('integer', 'integer', 'integer', 'integer', 'integer'),
1718  array($a_node['lft'], $diff, $a_node['lft'], $diff, $a_node['tree']));
1719 
1720  return $del_id;
1721 
1722  }
1723 
1729  public function updateVisits($ID)
1730  {
1731 
1732  global $ilDB;
1733 
1734  $checkTime = time() - (60*60);
1735 
1736  if ($_SESSION["frm_visit_".$this->dbTable."_".$ID] < $checkTime)
1737  {
1738 
1739  $_SESSION["frm_visit_".$this->dbTable."_".$ID] = time();
1740  $query = 'UPDATE '.$this->dbTable.' SET visits = visits + 1 WHERE ';
1741 
1742  $data_type = array();
1743  $data_value = array();
1744 
1745  if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
1746  {
1747  $query .= $this->getMDB2Query();
1748  $data_type = $data_type + $this->getMDB2DataType();
1749  $data_value = $data_value + $this->getMDB2DataValue();
1750 
1751  $res = $ilDB->queryf($query, $data_type, $data_value);
1752  }
1753  }
1754  }
1755 
1763  public function prepareText($text, $edit=0, $quote_user = '', $type = '')
1764  {
1765  global $lng;
1766 
1767  if($type == 'export')
1768  {
1769  $this->replQuote1 = "<blockquote class=\"quote\"><hr size=\"1\" color=\"#000000\">";
1770  $this->replQuote2 = "<hr size=\"1\" color=\"#000000\"/></blockquote>";
1771  }
1772 
1773  if($edit == 1)
1774  {
1775  // add login name of quoted users
1776  $lname = ($quote_user != "")
1777  ? '="'.$quote_user.'"'
1778  : "";
1779 
1780  $text = "[quote$lname]".$text."[/quote]";
1781  }
1782  else
1783  {
1784  // check for quotation
1785  $startZ = substr_count ($text, "[quote"); // also count [quote="..."]
1786  $endZ = substr_count ($text, "[/quote]");
1787 
1788  if ($startZ > 0 || $endZ > 0)
1789  {
1790  // add missing opening and closing tags
1791  if ($startZ > $endZ)
1792  {
1793  $diff = $startZ - $endZ;
1794 
1795  for ($i = 0; $i < $diff; $i++)
1796  {
1797  if ($type == 'export') $text .= $this->txtQuote2;
1798  else $text .= "[/quote]";
1799  }
1800  }
1801  elseif ($startZ < $endZ)
1802  {
1803  $diff = $endZ - $startZ;
1804 
1805  for ($i = 0; $i < $diff; $i++)
1806  {
1807  if ($type == 'export') $text = $this->txtQuote1.$text;
1808  else $text = "[quote]".$text;
1809  }
1810  }
1811 
1812  if($edit == 0)
1813  {
1814  $ws= "[ \t\r\f\v\n]*";
1815 
1816  $text = eregi_replace("\[(quote$ws=$ws\"([^\"]*)\"$ws)\]",
1817  $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote")." (\\2)".'</div>', $text);
1818 
1819  $text = str_replace("[quote]",
1820  $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote").'</div>', $text);
1821 
1822  $text = str_replace("[/quote]", $this->replQuote2, $text);
1823  }
1824  }
1825  }
1826 
1827  if($type != 'export')
1828  {
1829  if($edit == 0)
1830  {
1831  $text = ilUtil::insertLatexImages($text, "<span class\=\"latex\">", "<\/span>");
1832  $text = ilUtil::insertLatexImages($text, "\[tex\]", "\[\/tex\]");
1833  }
1834 
1835  // workaround for preventing template engine
1836  // from hiding text that is enclosed
1837  // in curly brackets (e.g. "{a}")
1838  $text = str_replace("{", "&#123;", $text);
1839  $text = str_replace("}", "&#125;", $text);
1840  }
1841 
1842  return $text;
1843  }
1844 
1845 
1852  public function getModeratorFromPost($pos_pk)
1853  {
1854  global $ilDB;
1855 
1856  $res = $ilDB->queryf('
1857  SELECT frm_data.* FROM frm_data, frm_posts
1858  WHERE pos_pk = %s
1859  AND pos_top_fk = top_pk',
1860  array('integer'), array($pos_pk));
1861 
1862  $row = $ilDB->fetchAssoc($res);
1863 
1864  return $row;
1865 
1866  }
1867 
1868  function __deletePostFiles($a_ids)
1869  {
1870  if(!is_array($a_ids))
1871  {
1872  return false;
1873  }
1874  include_once "./Modules/Forum/classes/class.ilFileDataForum.php";
1875 
1876  $tmp_file_obj =& new ilFileDataForum($this->getForumId());
1877  foreach($a_ids as $pos_id)
1878  {
1879  $tmp_file_obj->setPosId($pos_id);
1880  $files = $tmp_file_obj->getFilesOfPost();
1881  foreach($files as $file)
1882  {
1883  $tmp_file_obj->unlinkFile($file["name"]);
1884  }
1885  }
1886  unset($tmp_file_obj);
1887  return true;
1888  }
1889 
1890 
1891  function __sendMessage($a_parent_pos, $post_data = array())
1892  {
1893  global $ilUser, $ilDB;
1894 
1895  $parent_data = $this->getOnePost($a_parent_pos);
1896 
1897  // only if the current user is not the owner of the parent post and the parent's notification flag is set...
1898  if($parent_data["notify"] && $parent_data["pos_usr_id"] != $ilUser->getId())
1899  {
1900  // SEND MESSAGE
1901  include_once "Services/Mail/classes/class.ilMail.php";
1902  include_once './Services/User/classes/class.ilObjUser.php';
1903 
1904  $tmp_user =& new ilObjUser($parent_data["pos_usr_id"]);
1905 
1906  // NONSENSE
1907  $this->setMDB2WhereCondition('thr_pk = %s ', array('integer'), array($parent_data["pos_thr_fk"]));
1908 
1909  $thread_data = $this->getOneThread();
1910 
1911  $tmp_mail_obj = new ilMail(ANONYMOUS_USER_ID);
1912  $message = $tmp_mail_obj->sendMail($tmp_user->getLogin(),"","",
1913  $this->formatNotificationSubject($post_data),
1914  $this->__formatMessage($thread_data, $post_data, $tmp_user),
1915  array(),array("system"));
1916 
1917  unset($tmp_user);
1918  unset($tmp_mail_obj);
1919  }
1920  }
1921 
1930  private function __formatMessage($thread_data, $post_data = array(), $user_obj)
1931  {
1932  include_once "./Services/Object/classes/class.ilObjectFactory.php";
1933  $user_lang = self::_getLanguageInstanceByUsrId($user_obj->getId());
1934 
1935  $frm_obj =& ilObjectFactory::getInstanceByRefId($this->getForumRefId());
1936  $title = $frm_obj->getTitle();
1937  unset($frm_obj);
1938 
1939  $message = '';
1940  $message .= ilMail::getSalutation($user_obj->getId(), $user_lang);
1941 
1942  $message .= "\n\n";
1943  $message .= $this->lng->txt("forum_post_replied");
1944  $message .= $this->lng->txt("forum").": ".$title." -> ".$thread_data["thr_subject"]."\n\n";
1945 
1946  $message .= "\n------------------------------------------------------------\n";
1947  $message .= $post_data["pos_message"];
1948  $message .= "\n------------------------------------------------------------\n";
1949  $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";
1950 
1951  $message .= ilMail::_getInstallationSignature();
1952  return $message;
1953  }
1954 
1955  function getImportName()
1956  {
1957  return $this->import_name;
1958  }
1959  function setImportName($a_import_name)
1960  {
1961  $this->import_name = $a_import_name;
1962  }
1963 
1970  function enableForumNotification($user_id)
1971  {
1972  global $ilDB;
1973 
1974  if (!$this->isForumNotificationEnabled($user_id))
1975  {
1976  /* Remove all notifications of threads that belong to the forum */
1977 
1978  $res = $ilDB->queryf('
1979  SELECT frm_notification.thread_id FROM frm_data, frm_notification, frm_threads
1980  WHERE frm_notification.user_id = %s
1981  AND frm_notification.thread_id = frm_threads.thr_pk
1982  AND frm_threads.thr_top_fk = frm_data.top_pk
1983  AND frm_data.top_frm_fk = %s
1984  GROUP BY frm_notification.thread_id',
1985  array('integer', 'integer'),
1986  array($user_id, $this->id));
1987 
1988  if (is_object($res) && $res->numRows() > 0)
1989  {
1990  $thread_data = array();
1991  $thread_data_types = array();
1992 
1993  $query = ' DELETE FROM frm_notification
1994  WHERE user_id = %s
1995  AND thread_id IN (';
1996 
1997  array_push($thread_data, $user_id);
1998  array_push($thread_data_types, 'integer');
1999 
2000  $counter = 1;
2001 
2002  while($row = $ilDB->fetchAssoc($res))
2003  {
2004  if($counter < $res->numRows())
2005  {
2006  $query .= '%s, ';
2007  array_push($thread_data, $row['thread_id']);
2008  array_push($thread_data_types, 'integer');
2009  }
2010 
2011  if($counter == $res->numRows())
2012  {
2013  $query .= '%s)';
2014  array_push($thread_data, $row['thread_id']);
2015  array_push($thread_data_types, 'integer');
2016 
2017  }
2018  $counter++;
2019  }
2020 
2021  $statement = $ilDB->manipulateF($query, $thread_data_types, $thread_data);
2022  }
2023 
2024  /* Insert forum notification */
2025 
2026  $nextId = $ilDB->nextId('frm_notification');
2027 
2028  $statement = $ilDB->manipulateF('
2029  INSERT INTO frm_notification
2030  ( notification_id,
2031  user_id,
2032  frm_id
2033  )
2034  VALUES(%s, %s, %s)',
2035  array('integer','integer', 'integer'),
2036  array($nextId, $user_id, $this->id));
2037 
2038  }
2039 
2040  return true;
2041  }
2042 
2049  function disableForumNotification($user_id)
2050  {
2051  global $ilDB;
2052 
2053  $statement = $ilDB->manipulateF('
2054  DELETE FROM frm_notification
2055  WHERE user_id = %s
2056  AND frm_id = %s',
2057  array('integer', 'integer'),
2058  array($user_id, $this->id));
2059 
2060  return true;
2061  }
2062 
2069  function isForumNotificationEnabled($user_id)
2070  {
2071  global $ilDB;
2072 
2073  $result = $ilDB->queryf('SELECT COUNT(*) cnt FROM frm_notification WHERE user_id = %s AND frm_id = %s',
2074  array('integer', 'integer'), array($user_id, $this->id));
2075 
2076  while($record = $ilDB->fetchAssoc($result))
2077  {
2078  return (bool)$record['cnt'];
2079  }
2080 
2081  return false;
2082  }
2083 
2091  function enableThreadNotification($user_id, $thread_id)
2092  {
2093  global $ilDB;
2094 
2095  if (!$this->isThreadNotificationEnabled($user_id, $thread_id))
2096  {
2097  $nextId = $ilDB->nextId('frm_notification');
2098  $statement = $ilDB->manipulateF('
2099  INSERT INTO frm_notification
2100  ( notification_id,
2101  user_id,
2102  thread_id
2103  )
2104  VALUES (%s, %s, %s)',
2105  array('integer', 'integer', 'integer'), array($nextId, $user_id, $thread_id));
2106 
2107  }
2108 
2109  return true;
2110  }
2111 
2119  function isThreadNotificationEnabled($user_id, $thread_id)
2120  {
2121  global $ilDB;
2122 
2123  $result = $ilDB->queryf('
2124  SELECT COUNT(*) cnt FROM frm_notification
2125  WHERE user_id = %s
2126  AND thread_id = %s',
2127  array('integer', 'integer'),
2128  array($user_id, $thread_id));
2129 
2130 
2131  while($record = $ilDB->fetchAssoc($result))
2132  {
2133  return (bool)$record['cnt'];
2134  }
2135 
2136  return false;
2137  }
2138 
2139  function sendThreadNotifications($post_data)
2140  {
2141  global $ilDB, $ilAccess, $lng;
2142 
2143  include_once "Services/Mail/classes/class.ilMail.php";
2144  include_once './Services/User/classes/class.ilObjUser.php';
2145 
2146  // GET THREAD DATA
2147  $result = $ilDB->queryf('
2148  SELECT thr_subject FROM frm_threads
2149  WHERE thr_pk = %s',
2150  array('integer'), array($post_data['pos_thr_fk']));
2151 
2152  while($record = $ilDB->fetchAssoc($result))
2153  {
2154  $post_data['thr_subject'] = $record['thr_subject'];
2155  break;
2156  }
2157 
2158  // determine obj_id of the forum
2159  $obj_id = self::_lookupObjIdForForumId($post_data['pos_top_fk']);
2160 
2161  // GET AUTHOR OF NEW POST
2162  if($post_data['pos_usr_id'])
2163  {
2164  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_usr_id']);
2165  }
2166  else if(strlen($post_data['pos_usr_alias']))
2167  {
2168  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2169  }
2170 
2171  if($post_data['pos_usr_name'] == '')
2172  {
2173  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2174  }
2175 
2176  // GET USERS WHO WANT TO BE INFORMED ABOUT NEW POSTS
2177  $res = $ilDB->queryf('
2178  SELECT user_id FROM frm_notification
2179  WHERE thread_id = %s
2180  AND user_id <> %s',
2181  array('integer', 'integer'),
2182  array($post_data['pos_thr_fk'], $_SESSION['AccountId']));
2183 
2184  // get all references of obj_id
2185  $frm_references = ilObject::_getAllReferences($obj_id);
2186 
2187  // save language of the current user
2188  global $lng;
2189  $userLanguage = $lng;
2190 
2191  // get attachments data
2192  $fileDataForum = new ilFileDataForum($obj_id, $post_data['pos_pk']);
2193  $filesOfPost = $fileDataForum->getFilesOfPost();
2194 
2195  $attachments = array();
2196  foreach($filesOfPost as $attachment)
2197  {
2198  $attachments[] = $attachment['name'];
2199  }
2200 
2201  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2202  while($row = $ilDB->fetchAssoc($res))
2203  {
2204  // do rbac check before sending notification
2205  $send_mail = false;
2206  foreach((array)$frm_references as $ref_id)
2207  {
2208  if($ilAccess->checkAccessOfUser($row['user_id'], 'read', '', $ref_id))
2209  {
2210  $send_mail = true;
2211  break;
2212  }
2213  }
2214 
2215  if($send_mail)
2216  {
2217  $this->setLanguage(self::_getLanguageInstanceByUsrId($row['user_id']));
2218  $mail_obj->sendMail(
2219  ilObjUser::_lookupLogin($row["user_id"]), "", "",
2220  $this->formatNotificationSubject($post_data),
2221  $this->formatNotification($post_data, 0, $attachments, $row['user_id']),
2222  array(), array("system")
2223  );
2224  }
2225  }
2226 
2227  // reset language
2228  $this->setLanguage($userLanguage);
2229  }
2230 
2231  function sendForumNotifications($post_data)
2232  {
2233  global $ilDB, $ilAccess, $lng, $ilUser;
2234 
2235  include_once "Services/Mail/classes/class.ilMail.php";
2236  include_once './Services/User/classes/class.ilObjUser.php';
2237 
2238  // GET THREAD DATA
2239  $result = $ilDB->queryf('
2240  SELECT thr_subject FROM frm_threads
2241  WHERE thr_pk = %s',
2242  array('integer'),
2243  array($post_data['pos_thr_fk']));
2244 
2245  while($record = $ilDB->fetchAssoc($result))
2246  {
2247  $post_data['thr_subject'] = $record['thr_subject'];
2248  break;
2249  }
2250 
2251  // determine obj_id of the forum
2252  $obj_id = self::_lookupObjIdForForumId($post_data['pos_top_fk']);
2253 
2254  // GET AUTHOR OF NEW POST
2255  if($post_data['pos_usr_id'])
2256  {
2257  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_usr_id']);
2258  }
2259  else if(strlen($post_data['pos_usr_alias']))
2260  {
2261  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2262  }
2263 
2264  if($post_data['pos_usr_name'] == '')
2265  {
2266  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2267  }
2268 
2269  // GET USERS WHO WANT TO BE INFORMED ABOUT NEW POSTS
2270  $res = $ilDB->queryf('
2271  SELECT frm_notification.user_id FROM frm_notification, frm_data
2272  WHERE frm_data.top_pk = %s
2273  AND frm_notification.frm_id = frm_data.top_frm_fk
2274  AND frm_notification.user_id <> %s
2275  GROUP BY frm_notification.user_id',
2276  array('integer', 'integer'),
2277  array($post_data['pos_top_fk'], $ilUser->getId()));
2278 
2279  // get all references of obj_id
2280  $frm_references = ilObject::_getAllReferences($obj_id);
2281 
2282  // save language of the current user
2283  global $lng;
2284  $userLanguage = $lng;
2285 
2286  // get attachments data
2287  $fileDataForum = new ilFileDataForum($obj_id, $post_data['pos_pk']);
2288  $filesOfPost = $fileDataForum->getFilesOfPost();
2289  $attachments = array();
2290  foreach($filesOfPost as $attachment)
2291  {
2292  $attachments[] = $attachment['name'];
2293  }
2294 
2295  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2296  while($row = $ilDB->fetchAssoc($res))
2297  {
2298  // do rbac check before sending notification
2299  $send_mail = false;
2300  foreach((array)$frm_references as $ref_id)
2301  {
2302  if($ilAccess->checkAccessOfUser($row['user_id'], 'read', '', $ref_id))
2303  {
2304  $send_mail = true;
2305  break;
2306  }
2307  }
2308 
2309  if($send_mail)
2310  {
2311  $this->setLanguage(self::_getLanguageInstanceByUsrId($row['user_id']));
2312  $mail_obj->sendMail(
2313  ilObjUser::_lookupLogin($row["user_id"]), "", "",
2314  $this->formatNotificationSubject($post_data),
2315  $this->formatNotification($post_data, 0, $attachments, $row['user_id']),
2316  array(), array("system")
2317  );
2318  }
2319  }
2320 
2321  // reset language
2322  $this->setLanguage($userLanguage);
2323  }
2324 
2330  private function formatPostActivationNotification($post_data, $user_id)
2331  {
2332  $user_lang = self::_getLanguageInstanceByUsrId($user_id);
2333 
2334  $message = "";
2335  $message .= ilMail::getSalutation($user_id, $user_lang);
2336  $message .= "\n\n";
2337  $message .= $this->lng->txt('forums_post_activation_mail')."\n\n";
2338 
2339  $message .= $this->lng->txt("forum").": ".$post_data["top_name"]."\n\n";
2340  $message .= $this->lng->txt("thread").": ".$post_data["thr_subject"]."\n\n";
2341  $message .= $this->lng->txt("new_post").":\n------------------------------------------------------------\n";
2342  $message .= $this->lng->txt("author").": ".$post_data["pos_usr_name"]."\n";
2343  $message .= $this->lng->txt("date").": ".$post_data["pos_date"]."\n";
2344  $message .= $this->lng->txt("subject").": ".$post_data["pos_subject"]."\n\n";
2345  if ($post_data["pos_cens"] == 1)
2346  {
2347  $message .= $post_data["pos_cens_com"]."\n";
2348  }
2349  else
2350  {
2351  $pos_message = $post_data['pos_message'];
2352  if(strip_tags($pos_message) != $pos_message)
2353  {
2354  $pos_message = preg_replace("/\n/i", "", $pos_message);
2355  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
2356  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
2357  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
2358  }
2359  $message .= strip_tags($pos_message)."\n";
2360  }
2361  $message .= "------------------------------------------------------------\n";
2362 
2363  $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";
2364  $message .= sprintf($this->lng->txt('forums_notification_intro'),
2365  $this->ilias->ini->readVariable('client', 'name'),
2366  ILIAS_HTTP_PATH.'/?client_id='.CLIENT_ID)."\n\n";
2367 
2368  return $message;
2369  }
2370 
2371  function sendPostActivationNotification($post_data)
2372  {
2373  global $ilDB, $ilUser, $lng;
2374 
2375  if (is_array($moderators = $this->getModerators()))
2376  {
2377  // GET THREAD DATA
2378  $result = $ilDB->queryf('
2379  SELECT thr_subject FROM frm_threads
2380  WHERE thr_pk = %s',
2381  array('integer'),
2382  array($post_data['pos_thr_fk']));
2383 
2384  while($record = $ilDB->fetchAssoc($result))
2385  {
2386  $post_data['thr_subject'] = $record['thr_subject'];
2387  break;
2388  }
2389 
2390  // GET AUTHOR OF NEW POST
2391  if($post_data['pos_usr_id'])
2392  {
2393  $post_data['pos_usr_name'] = ilObjUser::_lookupLogin($post_data['pos_usr_id']);
2394  }
2395  else if(strlen($post_data['pos_usr_alias']))
2396  {
2397  $post_data['pos_usr_name'] = $post_data['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
2398  }
2399 
2400  if($post_data['pos_usr_name'] == '')
2401  {
2402  $post_data['pos_usr_name'] = $this->lng->txt('forums_anonymous');
2403  }
2404 
2405  // save language of the current user
2406  global $lng;
2407  $userLanguage = $lng;
2408 
2409  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2410  foreach($moderators as $moderator)
2411  {
2412  // set forum language instance for earch user
2413  $this->setLanguage(self::_getLanguageInstanceByUsrId($moderator));
2414  $subject = $this->formatNotificationSubject($post_data);
2415  $message = $this->formatPostActivationNotification($post_data, $moderator);
2416  $mail_obj->sendMail(
2417  ilObjUser::_lookupLogin($moderator), '', '',
2418  $subject,
2419  $message,
2420  array(), array("system")
2421  );
2422  }
2423 
2424  // reset language
2425  $this->setLanguage($userLanguage);
2426  }
2427  }
2428 
2433  public function formatNotificationSubject($post_data)
2434  {
2435  return $this->lng->txt("forums_notification_subject").' '.$post_data['top_name'];
2436  }
2437 
2445  public function formatNotification($post_data, $cron = 0, $attachments = array(), $user_id)
2446  {
2447  global $ilIliasIniFile;
2448 
2449  $user_lang = self::_getLanguageInstanceByUsrId($user_id);
2450 
2451  $message = "";
2452 
2453  $message .= ilMail::getSalutation($user_id, $user_lang);
2454  $message .= "\n\n";
2455  $message .= $this->lng->txt("forums_notification_subject")." ".$post_data['top_name']."\n\n";
2456 
2457  $message .= $this->lng->txt("forum").": ".$post_data["top_name"]."\n\n";
2458  $message .= $this->lng->txt("thread").": ".$post_data["thr_subject"]."\n\n";
2459  $message .= $this->lng->txt("new_post").":\n------------------------------------------------------------\n";
2460  $message .= $this->lng->txt("author").": ".$post_data["pos_usr_name"]."\n";
2461  $message .= $this->lng->txt("date").": ".$post_data["pos_date"]."\n";
2462  $message .= $this->lng->txt("subject").": ".$post_data["pos_subject"]."\n\n";
2463 
2464  if ($post_data["pos_cens"] == 1)
2465  {
2466  $message .= $post_data["pos_cens_com"]."\n";
2467  }
2468  else
2469  {
2470  $pos_message = $post_data['pos_message'];
2471  if(strip_tags($pos_message) != $pos_message)
2472  {
2473  $pos_message = preg_replace("/\n/i", "", $pos_message);
2474  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
2475  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
2476  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
2477  }
2478  $message .= strip_tags($pos_message)."\n";
2479  }
2480  $message .= "------------------------------------------------------------\n";
2481 
2482  if(count($attachments) > 0)
2483  {
2484  foreach($attachments as $attachment)
2485  {
2486  $message .= $this->lng->txt('attachment').": ".$attachment."\n";
2487  }
2488  $message .= "\n------------------------------------------------------------\n";
2489  }
2490 
2491  if ($cron == 1)
2492  {
2493  $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";
2494  }
2495  else
2496  {
2497  $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";
2498  }
2499 
2500  if ($cron == 1)
2501  {
2502  $message .= sprintf($this->lng->txt("forums_notification_intro"),
2503  $this->ilias->ini->readVariable("client","name"),
2504  $ilIliasIniFile->readVariable("server","http_path").'/?client_id='.CLIENT_ID)."\n\n";
2505  }
2506  else
2507  {
2508  $message .= sprintf($this->lng->txt("forums_notification_intro"),
2509  $this->ilias->ini->readVariable("client","name"),
2510  ILIAS_HTTP_PATH.'/?client_id='.CLIENT_ID)."\n\n";
2511  }
2512 
2513  return $message;
2514  }
2515 
2525  public static function _getThreads($a_obj_id,$a_sort_mode = self::SORT_DATE)
2526  {
2527  global $ilDB;
2528 
2529  switch($a_sort_mode)
2530  {
2531  case self::SORT_DATE:
2532  $sort = 'thr_date';
2533  break;
2534 
2535  case self::SORT_TITLE:
2536  default:
2537  $sort = 'thr_subject';
2538  break;
2539  }
2540 
2541  $res = $ilDB->queryf('
2542  SELECT * FROM frm_threads
2543  JOIN frm_data ON top_pk = thr_top_fk
2544  WHERE top_frm_fk = %s
2545  ORDER BY %s',
2546  array('integer', 'text'), array($a_obj_id, $sort));
2547 
2548  while($row = $ilDB->fetchObject($res))
2549  {
2550  $threads[$row->thr_pk] = $row->thr_subject;
2551  }
2552  return $threads ? $threads : array();
2553  }
2554 
2555  public static function _lookupObjIdForForumId($a_for_id)
2556  {
2557  global $ilDB;
2558 
2559  $res = $ilDB->queryf('
2560  SELECT top_frm_fk FROM frm_data
2561  WHERE top_pk = %s',
2562  array('integer'), array($a_for_id));
2563 
2564  if ($fdata = $ilDB->fetchAssoc($res))
2565  {
2566  return $fdata["top_frm_fk"];
2567  }
2568  return false;
2569  }
2570 
2571  public static function updateLastPostByObjId($a_obj_id)
2572  {
2573  global $ilDB;
2574  // get latest post of forum and update last_post
2575  $ilDB->setLimit(1);
2576  $res2 = $ilDB->queryf('
2577  SELECT pos_top_fk, pos_thr_fk, pos_pk FROM frm_posts, frm_data
2578  WHERE pos_top_fk = top_pk
2579  AND top_frm_fk = %s
2580  ORDER BY pos_date DESC',
2581  array('integer'), array($a_obj_id));
2582 
2583  if ($res2->numRows() == 0)
2584  {
2585  $lastPost_top = "";
2586  }
2587  else
2588  {
2589  $z = 0;
2590 
2591  while ($selData = $ilDB->fetchAssoc($res2))
2592  {
2593  if ($z > 0)
2594  {
2595  break;
2596  }
2597 
2598  $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
2599  $z ++;
2600  }
2601  }
2602 
2603  $ilDB->update('frm_data',
2604  array('top_last_post' => array('text', $lastPost_top)),
2605  array('top_frm_fk' => array('integer', $a_obj_id))
2606  );
2607 
2608  }
2609 
2616  public static function mergeThreads($obj_id, $source_id, $target_id)
2617  {
2618  // selected source & target objects
2619  $source_thread_obj = new ilForumTopic((int)$source_id);
2620  $target_thread_obj = new ilForumTopic((int)$target_id);
2621 
2622  if($source_thread_obj->getForumId() != $target_thread_obj->getForumId())
2623  {
2624  throw new ilException('not_allowed_to_merge_into_another_forum');
2625  }
2626  // use the "older" thread as target
2627  if($source_thread_obj->getCreateDate() > $target_thread_obj->getCreateDate())
2628  {
2629  $merge_thread_source = $source_thread_obj;
2630  $merge_thread_target = $target_thread_obj;
2631  }
2632  else
2633  {
2634  $merge_thread_source = $target_thread_obj;
2635  $merge_thread_target = $source_thread_obj;
2636  }
2637 
2638  $thread_subject = $target_thread_obj->getSubject();
2639 
2640  // remember if the threads are open or closed and then close both threads !
2641  $targed_was_closed = $merge_thread_target->isClosed();
2642 
2643  $merge_thread_source->close();
2644 
2645  if($targed_was_closed == false)
2646  {
2647  $merge_thread_target->close();
2648  }
2649 
2650  $source_all_posts = $merge_thread_source->getAllPosts();
2651  $source_root_node = $merge_thread_source->getFirstPostNode();
2652  $target_root_node = $merge_thread_target->getFirstPostNode();
2653 
2654  $add_difference = $target_root_node->getRgt();
2655 
2656 // update target root node rgt
2657  include_once 'Modules/Forum/classes/class.ilForumPostsTree.php';
2658 // $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt() + 1);
2659  $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt());
2660  ilForumPostsTree::updateTargetRootRgt($target_root_node->getId(), $new_target_rgt);
2661 
2662  $new_target_root = $target_root_node->getId();
2663 
2664  // get source post tree and update posts tree
2665  foreach($source_all_posts as $post)
2666  {
2667  $post_obj = new ilForumPost($post->pos_pk);
2668 
2669  $posts_tree_obj = new ilForumPostsTree();
2670  $posts_tree_obj->setPosFk($post->pos_pk);
2671 
2672  if($post_obj->getParentId() == 0)
2673  {
2674  $posts_tree_obj->setParentPos($new_target_root);
2675 
2676  //$posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference));
2677  $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2678  $posts_tree_obj->setLft($target_root_node->getRgt());
2679 
2680  $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2681  $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2682 
2683  $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2684 
2685  $posts_tree_obj->mergeParentPos();
2686  }
2687  else
2688  {
2689  $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2690  $posts_tree_obj->setLft(($post_obj->getLft() + $add_difference) - 1);
2691 
2692  $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2693  $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2694 
2695  $posts_tree_obj->setParentPos($post_obj->getParentId());
2696  $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2697 
2698  $posts_tree_obj->merge();
2699  }
2700  }
2701 
2702 // update frm_posts pos_thr_fk = target_thr_id
2703  include_once 'Modules/Forum/classes/class.ilForumPost.php';
2704  ilForumPost::mergePosts($merge_thread_source->getId(), $merge_thread_target->getId());
2705 
2706 // check notifications
2707  include_once 'Modules/Forum/classes/class.ilForumNotification.php';
2708  ilForumNotification::mergeThreadNotificiations($merge_thread_source->getId(), $merge_thread_target->getId());
2709 
2710 // delete frm_thread_access entries
2711  include_once './Modules/Forum/classes/class.ilObjForum.php';
2712  ilObjForum::_deleteAccessEntries($merge_thread_source->getId());
2713 
2714 // update frm_user_read
2715  ilObjForum::mergeForumUserRead($merge_thread_source->getId(), $merge_thread_target->getId());
2716 
2717 // update visits, thr_num_posts, last_post, subject
2718  $post_date_source = $merge_thread_source->getLastPost()->getCreateDate();
2719  $post_date_target = $merge_thread_target->getLastPost()->getCreateDate();
2720 
2721  $target_last_post = $merge_thread_target->getLastPostString();
2722  $exp = explode('#', $target_last_post);
2723 
2724  if($post_date_source > $post_date_target)
2725  {
2726  $exp[2] = $merge_thread_source->getLastPost()->getId();
2727  }
2728  else
2729  {
2730  $exp[2] = $merge_thread_target->getLastPost()->getId();
2731  }
2732  $new_thr_last_post = implode('#', $exp);
2733 
2734  $num_posts_source = (int)$merge_thread_source->getNumPosts();
2735  $num_visits_source = (int)$merge_thread_source->getVisits();
2736  $num_posts_target = (int)$merge_thread_target->getNumPosts();
2737  $num_visits_target = (int)$merge_thread_source->getVisits();
2738 
2739  $frm_topic_obj = new ilForumTopic(0, false, true);
2740  $frm_topic_obj->setNumPosts(($num_posts_source + $num_posts_target));
2741  $frm_topic_obj->setVisits(($num_visits_source + $num_visits_target));
2742  $frm_topic_obj->setLastPostString($new_thr_last_post);
2743  $frm_topic_obj->setSubject($thread_subject);
2744  $frm_topic_obj->setId($merge_thread_target->getId());
2745 
2746  $frm_topic_obj->updateMergedThread();
2747 
2748 // update frm_data: top_last_post , top_num_threads
2750 
2751 // reopen target if was not "closed" before merging
2752  if(!$targed_was_closed)
2753  {
2754  $merge_thread_target->reopen();
2755  }
2756 
2757 // delete source thread
2758  ilForumTopic::deleteByThreadId($merge_thread_source->getId());
2759  }
2760 } // 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
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
generatePost($forum_id, $thread_id, $user, $message, $parent_pos, $notify, $subject='', $alias='', $date='', $status=1, $send_activation_mail=0)
generate new dataset in frm_posts
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)
$mobs
setDbTable($dbTable)
set database table
fetchAssoc($a_set)
Fetch row as associative array from result set.
sendPostActivationNotification($post_data)
__deletePostFiles($a_ids)
static getInstance($a_obj_id=0)
setMDB2WhereCondition($query_string, $data_type, $data_value)
set content for additional condition
getModeratorFromPost($pos_pk)
get one post-dataset
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)
setLimit($a_limit, $a_offset=0)
Set limit and offset for a query.
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)
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.
global $ilIliasIniFile
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 _replaceMediaObjectImageSrc($a_text, $a_direction=0)
replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
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.
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
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.
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.
generateThread($forum_id, $user, $subject, $message, $notify, $notify_posts, $alias='', $date='', $status=1)
generate new dataset in frm_threads
getPostNode($post_id)
get data of given node from frm_posts_tree and frm_posts public