ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4require_once './Modules/Forum/classes/class.ilForumProperties.php';
5require_once './Modules/Forum/classes/class.ilObjForum.php';
6require_once './Modules/Forum/classes/class.ilForumTopic.php';
7require_once './Modules/Forum/classes/class.ilForumPost.php';
8
19{
20 const SORT_TITLE = 1;
21 const SORT_DATE = 2;
22
24
28 protected static $moderators_by_ref_id_map = array();
29
35 public $ilias;
36 public $lng;
37
44 private $dbTable;
45
51 private $className="ilForum";
52
59 private $orderField;
60
61 private $mdb2Query;
64
65 private $txtQuote1 = "[quote]";
66 private $txtQuote2 = "[/quote]";
67 private $replQuote1 = '<blockquote class="ilForumQuote">';
68 private $replQuote2 = '</blockquote>';
69
70 // max. datasets per page
72
73 // object id
74 private $id;
75
80 public function __construct()
81 {
82 global $ilias,$lng;
83
84 $this->ilias = $ilias;
85 $this->lng = $lng;
86 }
87
88 public function setLanguage($lng)
89 {
90 $this->lng = $lng;
91 }
92
103 public static function _getLanguageInstanceByUsrId($usr_id)
104 {
105 static $lngCache = array();
106
107 $languageShorthandle = ilObjUser::_lookupLanguage($usr_id);
108
109 // lookup in cache array
110 if(!isset($lngCache[$languageShorthandle]))
111 {
112 $lngCache[$languageShorthandle] = new ilLanguage($languageShorthandle);
113 $lngCache[$languageShorthandle]->loadLanguageModule('forum');
114 }
115
116 return $lngCache[$languageShorthandle];
117 }
118
124 public function setForumId($a_obj_id)
125 {
126
127 if (!isset($a_obj_id))
128 {
129 $message = get_class($this)."::setForumId(): No obj_id given!";
130 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
131 }
132
133 $this->id = $a_obj_id;
134 }
135
141 public function setForumRefId($a_ref_id)
142 {
143 if (!isset($a_ref_id))
144 {
145 $message = get_class($this)."::setForumRefId(): No ref_id given!";
146 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
147 }
148
149 $this->ref_id = $a_ref_id;
150 }
151
157 public function getForumId()
158 {
159 return $this->id;
160 }
161
167 public function getForumRefId()
168 {
169 return $this->ref_id;
170 }
171
178 private function setOrderField($orderField)
179 {
180 if ($orderField == "")
181 {
182 die($this->className . "::setOrderField(): No orderField given.");
183 }
184 else
185 {
186 $this->orderField = $orderField;
187 }
188 }
189
196 public function getOrderField()
197 {
198 return $this->orderField;
199 }
200
207 public function setDbTable($dbTable)
208 {
209 if ($dbTable == "")
210 {
211 die($this->className . "::setDbTable(): No database table given.");
212 }
213 else
214 {
215 $this->dbTable = $dbTable;
216 }
217 }
218
225 public function getDbTable()
226 {
227 return $this->dbTable;
228 }
229
230
240 public function setMDB2WhereCondition($query_string, $data_type, $data_value)
241 {
242 $this->mdb2Query = $query_string;
243 $this->mdb2DataValue = $data_value;
244 $this->mdb2DataType = $data_type;
245
246 return true;
247 }
248
249
255 public function getMDB2Query()
256 {
257 if($this->mdb2Query != '')
258 {
259 return $this->mdb2Query;
260 }
261
262 }
263
269 public function getMDB2DataValue()
270 {
271 if($this->mdb2DataValue != '')
272 {
274 }
275 }
276
282 public function getMDB2DataType()
283 {
284 if($this->mdb2DataType != '')
285 {
286 return $this->mdb2DataType;
287 }
288 }
289
294 public function setPageHits($pageHits)
295 {
296 if($pageHits < 1 || !is_numeric($pageHits))
297 {
298 $pageHits = 1;
299 }
300
301 $this->pageHits = (int)$pageHits;
302 return true;
303 }
304
311 public function getPageHits()
312 {
313 return $this->pageHits;
314 }
315
316 // *******************************************************************************
317
323 public function getOneTopic()
324 {
325 global $ilDB;
326
327 $data_type = array();
328 $data_value = array();
329
330 $query = 'SELECT * FROM frm_data WHERE ';
331
332 if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
333 {
334 $query .= ''.$this->getMDB2Query().'';
335 $data_type = $data_type + $this->getMDB2DataType();
336 $data_value = $data_value + $this->getMDB2DataValue();
337
338 $res = $ilDB->queryf($query, $data_type, $data_value);
339 $row = $ilDB->fetchAssoc($res);
340
341 if(is_null($row)) return NULL;
342
343 $row["top_name"] = trim($row["top_name"]);
344 $row["top_description"] = nl2br($row["top_description"]);
345
346 return $row;
347
348 }
349 else
350 {
351 $query .= '1 = 1';
352
353 $res = $ilDB->query($query);
354 $row = $ilDB->fetchAssoc($res);
355
356 if(!is_array($row) || !count($row)) return null;
357
358 $row['top_name'] = trim($row['top_name']);
359 $row['top_description'] = nl2br($row['top_description']);
360
361 return $row;
362 }
363 }
364
370 public function getOneThread()
371 {
372 global $ilDB;
373
374 $data_type = array();
375 $data_value = array();
376
377 $query = 'SELECT * FROM frm_threads WHERE ';
378
379 if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
380 {
381 $query .= $this->getMDB2Query();
382 $data_type = $data_type + $this->getMDB2DataType();
383 $data_value = $data_value + $this->getMDB2DataValue();
384
385 $sql_res = $ilDB->queryf($query, $data_type, $data_value);
386 $result = $ilDB->fetchAssoc($sql_res);
387 $result["thr_subject"] = trim($result["thr_subject"]);
388 }
389
390 return $result;
391 }
392
399 public function getOnePost($post)
400 {
401 global $ilDB;
402
403 $res = $ilDB->queryf('
404 SELECT frm_posts.*, usr_data.lastname FROM frm_posts, usr_data
405 WHERE pos_pk = %s
406 AND pos_display_user_id = usr_id',
407 array('integer'), array($post));
408
409 $row = $ilDB->fetchAssoc($res);
410
411
412 $row["pos_date"] = $this->convertDate($row["pos_date"]);
413 $row["pos_message"] = nl2br($row["pos_message"]);
414
415 return $row;
416 }
417
418 public function _lookupPostMessage($a_id)
419 {
420 global $ilDB;
421
422 $res = $ilDB->queryf('
423 SELECT * FROM frm_posts WHERE pos_pk = %s',
424 array('integer'), array($a_id));
425
426 while($row = $ilDB->fetchObject($res))
427 {
428 return $row->pos_message;
429 }
430 return '';
431 }
432
448 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)
449 {
450 global $ilDB;
451
452 $objNewPost = new ilForumPost();
453 $objNewPost->setForumId($forum_id);
454 $objNewPost->setThreadId($thread_id);
455 $objNewPost->setSubject($subject);
456 $objNewPost->setMessage($message);
457 $objNewPost->setDisplayUserId($display_user_id);
458 $objNewPost->setUserAlias($alias);
459 $objNewPost->setPosAuthorId($author_id);
460
461 self::_isModerator($this->getForumRefId(), $author_id) ? $is_moderator = true : $is_moderator = false;
462 $objNewPost->setIsAuthorModerator($is_moderator);
463
464 if ($date == "")
465 {
466 $objNewPost->setCreateDate(date("Y-m-d H:i:s"));
467 }
468 else
469 {
470 if (strpos($date, "-") > 0) // in mysql format
471 {
472 $objNewPost->setCreateDate($date);
473 }
474 else // a timestamp
475 {
476 $objNewPost->setCreateDate(date("Y-m-d H:i:s", $date));
477 }
478 }
479 if($status == 1)
480 {
481 $objNewPost->setPostActivationDate($objNewPost->getCreateDate());
482 }
483
484 $objNewPost->setImportName($this->getImportName());
485 $objNewPost->setNotification($notify);
486 $objNewPost->setStatus($status);
487 $objNewPost->insert();
488
489 // entry in tree-table
490 if ($parent_pos == 0)
491 {
492 $this->addPostTree($objNewPost->getThreadId(), $objNewPost->getId(), $objNewPost->getCreateDate());
493 }
494 else
495 {
496 $this->insertPostNode($objNewPost->getId(), $parent_pos, $objNewPost->getThreadId(), $objNewPost->getCreateDate());
497 }
498
499 // string last post
500 $lastPost = $objNewPost->getForumId()."#".$objNewPost->getThreadId()."#".$objNewPost->getId();
501
502 // update thread
503 $result = $ilDB->manipulateF('
504 UPDATE frm_threads
505 SET thr_num_posts = thr_num_posts + 1,
506 thr_last_post = %s
507 WHERE thr_pk = %s',
508 array('text', 'integer'),
509 array($lastPost, $objNewPost->getThreadId()));
510
511 // update forum
512 $result = $ilDB->manipulateF('
513 UPDATE frm_data
514 SET top_num_posts = top_num_posts + 1,
515 top_last_post = %s
516 WHERE top_pk = %s',
517 array('text', 'integer'),
518 array($lastPost, $objNewPost->getForumId()));
519
520 // MARK READ
522 $forum_obj->markPostRead($objNewPost->getPosAuthorId(), $objNewPost->getThreadId(), $objNewPost->getId());
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($display_user_id);
535 $news_item->setVisibility(NEWS_USERS);
536 $news_item->create();
537 }
538
539 return $objNewPost->getId();
540 }
541
556 public function generateThread($forum_id, $author_id, $display_user_id, $subject, $message, $notify, $notify_posts, $alias = '', $date = '', $status = 1)
557 {
558 global $ilDB;
559
560 $objNewThread = new ilForumTopic();
561 $objNewThread->setForumId($forum_id);
562 $objNewThread->setDisplayUserId($display_user_id);
563 $objNewThread->setSubject($subject);
564 $objNewThread->setThrAuthorId($author_id);
565
566 if ($date == "")
567 {
568 $objNewThread->setCreateDate(date("Y-m-d H:i:s"));
569 }
570 else
571 {
572 if (strpos($date, "-") > 0) // in mysql format
573 {
574 $objNewThread->setCreateDate($date);
575 }
576 else // a timestamp
577 {
578 $objNewThread->setCreateDate(date("Y-m-d H:i:s", $date));
579 }
580 }
581 $objNewThread->setImportName($this->getImportName());
582 $objNewThread->setUserAlias($alias);
583 $objNewThread->insert();
584
585 if ($notify_posts == 1)
586 {
587 $objNewThread->enableNotification($author_id);
588 }
589
590 // update forum
591 $statement = $ilDB->manipulateF('
592 UPDATE frm_data
593 SET top_num_threads = top_num_threads + 1
594 WHERE top_pk = %s',
595 array('integer'), array($forum_id));
596
597 return $this->generatePost($forum_id, $objNewThread->getId(), $author_id, $display_user_id, $message, 0, $notify, $subject, $alias, $objNewThread->getCreateDate(), $status, 0);
598 }
599
608 public function moveThreads($thread_ids = array(), $src_ref_id = 0, $dest_top_frm_fk = 0)
609 {
610 global $ilDB;
611
612 $src_top_frm_fk = ilObject::_lookupObjectId($src_ref_id);
613
614 if (is_numeric($src_top_frm_fk) && $src_top_frm_fk > 0 && is_numeric($dest_top_frm_fk) && $dest_top_frm_fk > 0)
615 {
616
617 $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($src_top_frm_fk));
618
619 $oldFrmData = $this->getOneTopic();
620
621 $this->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($dest_top_frm_fk));
622
623 $newFrmData = $this->getOneTopic();
624
625 if ($oldFrmData['top_pk'] && $newFrmData['top_pk'])
626 {
627 $moved_posts = 0;
628 $moved_threads = 0;
629 $visits = 0;
630 foreach ($thread_ids as $id)
631 {
632 $objTmpThread = new ilForumTopic($id);
633
634 $numPosts = $objTmpThread->movePosts($src_top_frm_fk, $oldFrmData['top_pk'], $dest_top_frm_fk, $newFrmData['top_pk']);
635 if (($last_post_string = $objTmpThread->getLastPostString()) != '')
636 {
637 $last_post_string = explode('#', $last_post_string);
638 $last_post_string[0] = $newFrmData['top_pk'];
639 $last_post_string = implode('#', $last_post_string);
640 $objTmpThread->setLastPostString($last_post_string);
641 }
642
643 $visits += $objTmpThread->getVisits();
644
645 $moved_posts += $numPosts;
646 ++$moved_threads;
647
648 $objTmpThread->setForumId($newFrmData['top_pk']);
649 $objTmpThread->update();
650
651 unset($objTmpThread);
652 }
653
654 // update frm_data source forum
655 $ilDB->setLimit(1);
656 $res = $ilDB->queryf('
657 SELECT pos_thr_fk, pos_pk
658 FROM frm_posts
659 WHERE pos_top_fk = %s
660 ORDER BY pos_date DESC',
661 array('integer'), array($oldFrmData['top_pk']));
662
663 $row = $ilDB->fetchObject($res);
664 $last_post_src = $oldFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
665
666 $statement = $ilDB->manipulateF('
667 UPDATE frm_data
668 SET top_num_posts = top_num_posts - %s,
669 top_num_threads = top_num_threads - %s,
670 visits = visits - %s,
671 top_last_post = %s
672 WHERE top_pk = %s',
673 array('integer', 'integer', 'integer', 'text', 'integer'),
674 array( $moved_posts,
675 $moved_threads,
676 $visits,
677 $last_post_src,
678 $oldFrmData['top_pk']));
679
680 // update frm_data destination forum
681
682 $ilDB->setLimit(1);
683 $res = $ilDB->queryf('
684 SELECT pos_thr_fk, pos_pk
685 FROM frm_posts
686 WHERE pos_top_fk = %s
687 ORDER BY pos_date DESC',
688 array('integer'), array($newFrmData['top_kp']));
689
690 $row = $ilDB->fetchObject($res);
691 $last_post_dest = $newFrmData['top_pk'] . '#' . $row->pos_thr_fk . '#' . $row->pos_pk;
692
693 $statement = $ilDB->manipulateF('
694 UPDATE frm_data
695 SET top_num_posts = top_num_posts + %s,
696 top_num_threads = top_num_threads + %s,
697 visits = visits + %s,
698 top_last_post = %s
699 WHERE top_pk = %s',
700 array('integer', 'integer', 'integer', 'text', 'integer'),
701 array($moved_posts, $moved_threads, $visits, $last_post_dest, $newFrmData['top_pk']));
702
703 /*
704 // update news items
705 include_once("./Services/News/classes/class.ilNewsItem.php");
706 $objNewsItem = new ilNewsItem();
707 $news_items = $objNewsItem->getNewsForRefId($src_ref_id);
708 foreach ($news_items as $news_item)
709 {
710 $tmpObjNewsItem = new ilNewsItem($news_item['id']);
711 if ($tmpObjNewsItem->getContextObjId() == $src_top_frm_fk)
712 {
713 $tmpObjNewsItem->setContextObjId($dest_top_frm_fk);
714 $tmpObjNewsItem->update();
715 }
716 unset($tmpObjNewsItem);
717 }
718 */
719 }
720 }
721}
722
723
731 public function postCensorship($message, $pos_pk, $cens = 0)
732 {
733 global $ilDB;
734
735 $cens_date = date("Y-m-d H:i:s");
736
737 $ilDB->manipulateF('
738 UPDATE frm_posts
739 SET pos_cens_com = %s,
740 pos_cens_date = %s,
741 pos_cens = %s,
742 update_user = %s
743 WHERE pos_pk = %s',
744 array('text', 'timestamp', 'integer', 'integer', 'integer'),
745 array($message, $cens_date, $cens, $_SESSION['AccountId'], $pos_pk));
746
747 // Change news item accordingly
748 include_once("./Services/News/classes/class.ilNewsItem.php");
749 $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
750 "frm", $pos_pk, "pos");
751 if ($news_id > 0)
752 {
753 if ($cens > 0) // censor
754 {
755 $news_item = new ilNewsItem($news_id);
756 //$news_item->setTitle($subject);
757 $news_item->setContent(nl2br($this->prepareText($message, 0)));
758 $news_item->update();
759 }
760 else // revoke censorship
761 {
762 // get original message
763 $res = $ilDB->queryf('
764 SELECT * FROM frm_posts
765 WHERE pos_pk = %s',
766 array('integer'), array($pos_pk));
767
768 $rec = $ilDB->fetchAssoc($res);
769
770 $news_item = new ilNewsItem($news_id);
771 //$news_item->setTitle($subject);
772 $news_item->setContent(nl2br($this->prepareText($rec["pos_message"], 0)));
773 $news_item->update();
774 }
775 }
776
777 require_once 'Modules/Forum/classes/class.ilForumPost.php';
778 $GLOBALS['ilAppEventHandler']->raise(
779 'Modules/Forum',
780 'censoredPost',
781 array(
782 'ref_id' => $this->getForumRefId(),
783 'post' => new ilForumPost($pos_pk)
784 )
785 );
786
787 return true;
788 }
789
796 public function deletePost($post)
797 {
798 global $ilDB;
799
800 include_once "./Modules/Forum/classes/class.ilObjForum.php";
801
802 $p_node = $this->getPostNode($post);
803
804 $GLOBALS['ilAppEventHandler']->raise(
805 'Modules/Forum',
806 'deletedPost',
807 array(
808 'ref_id' => $this->getForumRefId(),
809 'post' => new ilForumPost($post),
810 'thread_deleted' => ($p_node["parent"] == 0)? true : false
811 )
812 );
813
814 // delete tree and get id's of all posts to delete
815 $del_id = $this->deletePostTree($p_node);
816
817 // Delete User read entries
818 foreach($del_id as $post_id)
819 {
820 ilObjForum::_deleteReadEntries($post_id);
821 }
822
823 // DELETE ATTACHMENTS ASSIGNED TO POST
824 $this->__deletePostFiles($del_id);
825
826 $dead_pos = count($del_id);
827 $dead_thr = 0;
828
829 // if deletePost is thread opener ...
830 if ($p_node["parent"] == 0)
831 {
832 // delete thread access data
833 include_once './Modules/Forum/classes/class.ilObjForum.php';
834
835 ilObjForum::_deleteAccessEntries($p_node['tree']);
836
837 // delete thread
838 $dead_thr = $p_node["tree"];
839
840 $statement = $ilDB->manipulateF('
841 DELETE FROM frm_threads
842 WHERE thr_pk = %s',
843 array('integer'), array($p_node['tree']));
844
845 // update num_threads
846 $statement = $ilDB->manipulateF('
847 UPDATE frm_data
848 SET top_num_threads = top_num_threads - 1
849 WHERE top_frm_fk = %s',
850 array('integer'), array($this->id));
851
852 // delete all related news
853 $posset = $ilDB->queryf('
854 SELECT * FROM frm_posts
855 WHERE pos_thr_fk = %s',
856 array('integer'), array($p_node['tree']));
857
858 while ($posrec = $ilDB->fetchAssoc($posset))
859 {
860 include_once("./Services/News/classes/class.ilNewsItem.php");
861 $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
862 "frm", $posrec["pos_pk"], "pos");
863 if ($news_id > 0)
864 {
865 $news_item = new ilNewsItem($news_id);
866 $news_item->delete();
867 }
868
869 try
870 {
871 include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
872 $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $posrec['pos_pk']);
873 foreach($mobs as $mob)
874 {
876 {
877 ilObjMediaObject::_removeUsage($mob, 'frm:html', $posrec['pos_pk']);
878 $mob_obj = new ilObjMediaObject($mob);
879 $mob_obj->delete();
880 }
881 }
882 }
883 catch(Exception $e)
884 {
885 }
886 }
887
888
889 // delete all posts of this thread
890 $statement = $ilDB->manipulateF('
891 DELETE FROM frm_posts
892 WHERE pos_thr_fk = %s',
893 array('integer'), array($p_node['tree']));
894
895 }
896 else
897 {
898
899 // delete this post and its sub-posts
900 for ($i = 0; $i < $dead_pos; $i++)
901 {
902 $statement = $ilDB->manipulateF('
903 DELETE FROM frm_posts
904 WHERE pos_pk = %s',
905 array('integer'), array($del_id[$i]));
906
907 // delete related news item
908 include_once("./Services/News/classes/class.ilNewsItem.php");
909 $news_id = ilNewsItem::getFirstNewsIdForContext($this->id,
910 "frm", $del_id[$i], "pos");
911 if ($news_id > 0)
912 {
913 $news_item = new ilNewsItem($news_id);
914 $news_item->delete();
915 }
916
917 try
918 {
919 include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
920 $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $del_id[$i]);
921 foreach($mobs as $mob)
922 {
924 {
925 ilObjMediaObject::_removeUsage($mob, 'frm:html', $del_id[$i]);
926 $mob_obj = new ilObjMediaObject($mob);
927 $mob_obj->delete();
928 }
929 }
930 }
931 catch(Exception $e)
932 {
933 }
934 }
935
936 // update num_posts in frm_threads
937 $statement = $ilDB->manipulateF('
938 UPDATE frm_threads
939 SET thr_num_posts = thr_num_posts - %s
940 WHERE thr_pk = %s',
941 array('integer', 'integer'),
942 array($dead_pos, $p_node['tree']));
943
944
945 // get latest post of thread and update last_post
946 $res1 = $ilDB->queryf('
947 SELECT * FROM frm_posts
948 WHERE pos_thr_fk = %s
949 ORDER BY pos_date DESC',
950 array('integer'), array($p_node['tree']));
951
952 if ($res1->numRows() == 0)
953 {
954 $lastPost_thr = "";
955 }
956 else
957 {
958 $z = 0;
959
960 while ($selData = $ilDB->fetchAssoc($res1))
961 {
962 if ($z > 0)
963 {
964 break;
965 }
966
967 $lastPost_thr = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
968 $z ++;
969 }
970 }
971
972 $statement = $ilDB->manipulateF('
973 UPDATE frm_threads
974 SET thr_last_post = %s
975 WHERE thr_pk = %s',
976 array('text', 'integer'), array($lastPost_thr, $p_node['tree']));
977 }
978
979 // update num_posts in frm_data
980 $statement = $ilDB->manipulateF('
981 UPDATE frm_data
982 SET top_num_posts = top_num_posts - %s
983 WHERE top_frm_fk = %s',
984 array('integer', 'integer'), array($dead_pos, $this->id));
985
986
987 // get latest post of forum and update last_post
988 $res2 = $ilDB->queryf('
989 SELECT * FROM frm_posts, frm_data
990 WHERE pos_top_fk = top_pk
991 AND top_frm_fk = %s
992 ORDER BY pos_date DESC',
993 array('integer'), array($this->id));
994
995 if ($res2->numRows() == 0)
996 {
997 $lastPost_top = "";
998 }
999 else
1000 {
1001 $z = 0;
1002
1003 while ($selData = $ilDB->fetchAssoc($res2))
1004 {
1005 if ($z > 0)
1006 {
1007 break;
1008 }
1009
1010 $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
1011 $z ++;
1012 }
1013 }
1014
1015 $statement = $ilDB->manipulateF('
1016 UPDATE frm_data
1017 SET top_last_post = %s
1018 WHERE top_frm_fk = %s',
1019 array('text', 'integer'), array($lastPost_top, $this->id));
1020
1021 return $dead_thr;
1022 }
1023
1031 public function getAllThreads($a_topic_id, array $params = array(), $limit = 0, $offset = 0)
1032 {
1038 global $ilDB, $ilUser, $ilSetting;
1039
1040 $frm_overview_setting = (int)$ilSetting::_lookupValue('frma','forum_overview');
1041 $frm_props = ilForumProperties::getInstance($this->getForumId());
1042
1043 $excluded_ids_condition = '';
1044 if(isset($params['excluded_ids']) && is_array($params['excluded_ids']) && $params['excluded_ids'])
1045 {
1046 $excluded_ids_condition = ' AND ' . $ilDB->in('thr_pk', $params['excluded_ids'], true, 'integer'). ' ';
1047 }
1048
1049 if(!in_array(strtolower($params['order_column']), array('lp_date', 'rating')))
1050 {
1051 $params['order_column'] = 'post_date';
1052 }
1053 if(!in_array(strtolower($params['order_direction']), array('asc', 'desc')))
1054 {
1055 $params['order_direction'] = 'desc';
1056 }
1057
1058 // Count all threads for the passed forum
1059 $query = "SELECT COUNT(thr_pk) cnt
1060 FROM frm_threads
1061 WHERE thr_top_fk = %s {$excluded_ids_condition}";
1062 $res = $ilDB->queryF($query, array('integer'), array($a_topic_id));
1063 $data = $ilDB->fetchAssoc($res);
1064 $cnt = (int) $data['cnt'];
1065
1066 $threads = array();
1067
1068 $data = array();
1069 $data_types = array();
1070
1071 $active_query = '';
1072 $active_inner_query = '';
1073 $is_post_activation_enabled = $frm_props->isPostActivationEnabled();
1074 if($is_post_activation_enabled && !$params['is_moderator'])
1075 {
1076 $active_query = ' AND (pos_status = %s OR pos_author_id = %s) ';
1077 $active_inner_query = ' AND (ipos.pos_status = %s OR ipos.pos_author_id = %s) ';
1078 }
1079
1080 $optional_fields = '';
1081 if($frm_props->isIsThreadRatingEnabled())
1082 {
1083 $optional_fields = ',avg_rating';
1084 }
1085 if($frm_props->getThreadSorting() == 1)
1086 {
1087 $optional_fields = ',thread_sorting';
1088 }
1089
1090 $additional_sort = '';
1091 if($frm_props->getThreadSorting())
1092 {
1093 $additional_sort .= ' ,thread_sorting ASC ';
1094 }
1095
1096 $dynamic_columns = array(
1097 ' ,post_date ' . $params['order_direction']
1098 );
1099 if($frm_props->isIsThreadRatingEnabled())
1100 {
1101 $dynamic_columns[] = ' ,avg_rating ' . $params['order_direction'];
1102 }
1103 if('rating' == strtolower($params['order_column']))
1104 {
1105 $dynamic_columns = array_reverse($dynamic_columns);
1106 }
1107 $additional_sort .= implode(' ', $dynamic_columns);
1108
1109 if(!$ilUser->isAnonymous())
1110 {
1111 $query = "SELECT
1112 (CASE WHEN COUNT(DISTINCT(notification_id)) > 0 THEN 1 ELSE 0 END) usr_notification_is_enabled,
1113 MAX(pos_date) post_date,
1114 COUNT(DISTINCT(pos_pk)) num_posts,
1115 COUNT(DISTINCT(pos_pk)) - COUNT(DISTINCT(postread.post_id)) num_unread_posts, ";
1116
1117 // new posts query
1118 if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1119 {
1120 $query .= "
1121 (SELECT COUNT(DISTINCT(ipos.pos_pk))
1122 FROM frm_posts ipos
1123 LEFT JOIN frm_user_read iread ON iread.post_id = ipos.pos_pk AND iread.usr_id = %s
1124 LEFT JOIN frm_thread_access iacc ON (iacc.thread_id = ipos.pos_thr_fk AND iacc.usr_id = %s)
1125 WHERE ipos.pos_thr_fk = thr_pk
1126
1127 AND (ipos.pos_update > iacc.access_old_ts
1128 OR
1129 (iacc.access_old IS NULL AND (ipos.pos_update > " . $ilDB->quote(date('Y-m-d H:i:s', NEW_DEADLINE), 'timestamp') . "))
1130 )
1131
1132 AND ipos.pos_author_id != %s
1133 AND iread.usr_id IS NULL $active_inner_query
1134 ) num_new_posts, ";
1135 }
1136
1137 $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
1138 {$optional_fields}
1139 FROM frm_threads
1140
1141 LEFT JOIN frm_notification
1142 ON frm_notification.thread_id = thr_pk
1143 AND frm_notification.user_id = %s
1144
1145 LEFT JOIN frm_posts
1146 ON pos_thr_fk = thr_pk $active_query
1147
1148 LEFT JOIN frm_user_read postread
1149 ON postread.post_id = pos_pk
1150 AND postread.usr_id = %s";
1151
1152 $query .= " WHERE thr_top_fk = %s
1153 {$excluded_ids_condition}
1154 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
1155 {$optional_fields}
1156 ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1157
1158
1159 // data_types for new posts query and $active_inner_query
1160 if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1161 {
1162 $data_types[] = 'integer';
1163 $data_types[] = 'integer';
1164 $data_types[] = 'integer';
1165 if($is_post_activation_enabled && !$params['is_moderator'])
1166 {
1167 array_push($data_types, 'integer', 'integer');
1168 }
1169 }
1170 $data_types[] = 'integer';
1171 if($is_post_activation_enabled && !$params['is_moderator'])
1172 {
1173 array_push($data_types, 'integer', 'integer');
1174 }
1175 $data_types[] = 'integer';
1176 $data_types[] = 'integer';
1177
1178 // data_values for new posts query and $active_inner_query
1179 if($frm_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1180 {
1181 $data[] = $ilUser->getId();
1182 $data[] = $ilUser->getId();
1183 $data[] = $ilUser->getId();
1184 if($is_post_activation_enabled && !$params['is_moderator'])
1185 {
1186 array_push($data, '1', $ilUser->getId());
1187 }
1188 }
1189 $data[] = $ilUser->getId();
1190 if($is_post_activation_enabled && !$params['is_moderator'])
1191 {
1192 array_push($data, '1', $ilUser->getId());
1193 }
1194 $data[] = $ilUser->getId();
1195 $data[] = $a_topic_id;
1196 }
1197 else
1198 {
1199 $query = "SELECT
1200 0 usr_notification_is_enabled,
1201 MAX(pos_date) post_date,
1202 COUNT(DISTINCT(pos_pk)) num_posts,
1203 COUNT(DISTINCT(pos_pk)) num_unread_posts,
1204 COUNT(DISTINCT(pos_pk)) num_new_posts,
1205 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
1206 {$optional_fields}
1207 FROM frm_threads
1208
1209 LEFT JOIN frm_posts
1210 ON pos_thr_fk = thr_pk $active_query";
1211
1212 $query .= " WHERE thr_top_fk = %s
1213 {$excluded_ids_condition}
1214 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
1215 {$optional_fields}
1216 ORDER BY is_sticky DESC {$additional_sort}, thr_date DESC";
1217
1218 if($is_post_activation_enabled && !$params['is_moderator'])
1219 {
1220 array_push($data_types, 'integer', 'integer');
1221 }
1222 $data_types[] = 'integer';
1223 if($is_post_activation_enabled && !$params['is_moderator'])
1224 {
1225 array_push($data, '1', $ilUser->getId());
1226 }
1227 $data[] = $a_topic_id;
1228 }
1229
1230 if($limit || $offset)
1231 {
1232 $ilDB->setLimit($limit, $offset);
1233 }
1234 $res = $ilDB->queryF($query, $data_types, $data);
1235 while($row = $ilDB->fetchAssoc($res))
1236 {
1237 $thread = new ilForumTopic($row['thr_pk'], $params['is_moderator'], true);
1238 $thread->assignData($row);
1239 $threads[] = $thread;
1240 }
1241
1242 return array(
1243 'items' => $threads,
1244 'cnt' => $cnt
1245 );
1246 }
1247
1248 public function getUserStatistic($is_moderator = false)
1249 {
1250 global $ilDB, $ilUser;
1251
1252 $statistic = array();
1253
1254 $data_types = array();
1255 $data = array();
1256
1257 $query = "SELECT COUNT(f.pos_display_user_id) ranking, u.login, p.value, u.lastname, u.firstname
1258 FROM frm_posts f
1259 INNER JOIN frm_posts_tree t
1260 ON f.pos_pk = t.pos_fk
1261 INNER JOIN frm_threads th
1262 ON t.thr_fk = th.thr_pk
1263 INNER JOIN usr_data u
1264 ON u.usr_id = f.pos_display_user_id
1265 INNER JOIN frm_data d
1266 ON d.top_pk = f.pos_top_fk
1267 LEFT JOIN usr_pref p
1268 ON p.usr_id = u.usr_id AND p.keyword = %s
1269 WHERE 1 = 1";
1270
1271 array_push($data_types, 'text');
1272 array_push($data, 'public_profile');
1273
1274 if (!$is_moderator)
1275 {
1276 $query .= ' AND (pos_status = %s
1277 OR (pos_status = %s
1278 AND pos_author_id = %s ))';
1279
1280 array_push($data_types,'integer', 'integer', 'integer');
1281 array_push($data, '1', '0', $ilUser->getId());
1282 }
1283
1284 $query .= ' AND d.top_frm_fk = %s
1285 GROUP BY pos_display_user_id, u.login, p.value,u.lastname, u.firstname';
1286
1287 array_push($data_types,'integer');
1288 array_push($data, $this->getForumId());
1289
1290
1291
1292 $res = $ilDB->queryf($query, $data_types, $data);
1293
1294 $counter = 0;
1295 while ($row = $ilDB->fetchAssoc($res))
1296 {
1297 $statistic[$counter][] = $row['ranking'];
1298 $statistic[$counter][] = $row['login'];
1299
1300 $lastname ='';
1301 $firstname = '';
1302 if(!$ilUser->isAnonymous() && in_array($row['value'], array('y', 'g')) ||
1303 $ilUser->isAnonymous() && 'g' == $row['value'])
1304 {
1305 $lastname = $row['lastname'];
1306 $firstname = $row['firstname'];
1307 }
1308
1309 $statistic[$counter][] = $lastname;
1310 $statistic[$counter][] = $firstname;
1311
1312 ++$counter;
1313 }
1314
1315 return is_array($statistic) ? $statistic : array();
1316 }
1317
1318
1326 public function getFirstPostByThread($a_thread_id)
1327 {
1328 global $ilDB;
1329
1330 $res = $ilDB->queryf('
1331 SELECT * FROM frm_posts_tree
1332 WHERE thr_fk = %s
1333 AND parent_pos = %s',
1334 array('integer', 'integer'), array($a_thread_id, '0'));
1335
1336 $row = $ilDB->fetchObject($res);
1337
1338 return $row->pos_fk ? $row->pos_fk : 0;
1339 }
1340
1347 public function getModerators()
1348 {
1349 global $rbacreview;
1350
1351 return $this->_getModerators($this->getForumRefId());
1352 }
1353
1361 function _getModerators($a_ref_id)
1362 {
1363 global $rbacreview;
1364
1365 $role_arr = $rbacreview->getRolesOfRoleFolder($a_ref_id);
1366
1367 foreach ($role_arr as $role_id)
1368 {
1369 //$roleObj = $this->ilias->obj_factory->getInstanceByObjId($role_id);
1370 $title = ilObject::_lookupTitle($role_id);
1371 if ($title == "il_frm_moderator_".$a_ref_id)
1372 {
1373 #return $rbacreview->assignedUsers($roleObj->getId());
1374 return $title = $rbacreview->assignedUsers($role_id);
1375 }
1376 }
1377
1378 return array();
1379 }
1380
1389 public static function _isModerator($a_ref_id, $a_usr_id)
1390 {
1391 if(!self::$moderators_by_ref_id_map[$a_ref_id])
1392 {
1393 self::$moderators_by_ref_id_map[$a_ref_id] = ilForum::_getModerators($a_ref_id);
1394 }
1395 return in_array($a_usr_id, self::$moderators_by_ref_id_map[$a_ref_id]);
1396 }
1397
1405 public function countUserArticles($a_user_id)
1406 {
1407 global $ilDB;
1408
1409 $res = $ilDB->queryf('
1410 SELECT * FROM frm_data
1411 INNER JOIN frm_posts ON pos_top_fk = top_pk
1412 WHERE top_frm_fk = %s
1413 AND pos_author_id = %s',
1414 array('integer', 'integer'),
1415 array($this->getForumId(), $a_user_id));
1416
1417 return $res->numRows();
1418 }
1419
1420 public function countActiveUserArticles($a_user_id)
1421 {
1422 global $ilDB, $ilUser;
1423
1424 $res = $ilDB->queryf('
1425 SELECT * FROM frm_data
1426 INNER JOIN frm_posts ON pos_top_fk = top_pk
1427 WHERE top_frm_fk = %s
1428 AND (pos_status = %s
1429 OR (pos_status = %s
1430 AND pos_author_id = %s
1431 )
1432 )
1433 AND pos_author_id = %s',
1434 array('integer', 'integer', 'integer', 'integer', 'integer'),
1435 array($this->getForumId(),'1', '0', $ilUser->getId(), $a_user_id));
1436
1437 return $res->numRows();
1438 }
1439
1446 public function convertDate($date)
1447 {
1449 }
1450
1458 public function addPostTree($a_tree_id, $a_node_id = -1, $a_date = '')
1459 {
1460 global $ilDB;
1461
1462 $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1463
1464 if ($a_node_id <= 0)
1465 {
1466 $a_node_id = $a_tree_id;
1467 }
1468
1469 $nextId = $ilDB->nextId('frm_posts_tree');
1470
1471 $statement = $ilDB->manipulateF('
1472 INSERT INTO frm_posts_tree
1473 ( fpt_pk,
1474 thr_fk,
1475 pos_fk,
1476 parent_pos,
1477 lft,
1478 rgt,
1479 depth,
1480 fpt_date
1481 )
1482 VALUES(%s, %s, %s, %s, %s, %s, %s, %s )',
1483 array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1484 array($nextId, $a_tree_id, $a_node_id, '0', '1', '2', '1', $a_date));
1485
1486 return true;
1487 }
1488
1496 public function insertPostNode($a_node_id, $a_parent_id, $tree_id, $a_date = '')
1497 {
1498 global $ilDB;
1499
1500 $a_date = $a_date ? $a_date : date("Y-m-d H:i:s");
1501
1502 // get left value
1503 $sql_res = $ilDB->queryf('
1504 SELECT * FROM frm_posts_tree
1505 WHERE pos_fk = %s
1506 AND thr_fk = %s',
1507 array('integer', 'integer'),
1508 array($a_parent_id, $tree_id));
1509
1510 $res = $ilDB->fetchObject($sql_res);
1511
1512 $left = $res->lft;
1513
1514 $lft = $left + 1;
1515 $rgt = $left + 2;
1516
1517 // spread tree
1518 $statement = $ilDB->manipulateF('
1519 UPDATE frm_posts_tree
1520 SET lft = CASE
1521 WHEN lft > %s
1522 THEN lft + 2
1523 ELSE lft
1524 END,
1525 rgt = CASE
1526 WHEN rgt > %s
1527 THEN rgt + 2
1528 ELSE rgt
1529 END
1530 WHERE thr_fk = %s',
1531 array('integer', 'integer', 'integer'),
1532 array($left, $left, $tree_id));
1533
1534 $depth = $this->getPostDepth($a_parent_id, $tree_id) + 1;
1535
1536 // insert node
1537 $nextId = $ilDB->nextId('frm_posts_tree');
1538 $statement = $ilDB->manipulateF('
1539 INSERT INTO frm_posts_tree
1540 ( fpt_pk,
1541 thr_fk,
1542 pos_fk,
1543 parent_pos,
1544 lft,
1545 rgt,
1546 depth,
1547 fpt_date
1548 )
1549 VALUES(%s,%s,%s, %s, %s, %s,%s, %s)',
1550 array('integer','integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'timestamp'),
1551 array( $nextId,
1552 $tree_id,
1553 $a_node_id,
1554 $a_parent_id,
1555 $lft,
1556 $rgt,
1557 $depth,
1558 $a_date)
1559 );
1560
1561 }
1562
1570 public function getPostDepth($a_node_id, $tree_id)
1571 {
1572 global $ilDB;
1573
1574 if ($tree_id)
1575 {
1576 $sql_res = $ilDB->queryf('
1577 SELECT depth FROM frm_posts_tree
1578 WHERE pos_fk = %s
1579 AND thr_fk = %s',
1580 array('integer', 'integer'),
1581 array($a_node_id, $tree_id));
1582
1583 $res = $ilDB->fetchObject($sql_res);
1584
1585 return $res->depth;
1586 }
1587 else
1588 {
1589 return 0;
1590 }
1591 }
1592
1599 public function getFirstPostNode($tree_id)
1600 {
1601 global $ilDB;
1602
1603 $res = $ilDB->queryf('
1604 SELECT * FROM frm_posts, frm_posts_tree
1605 WHERE pos_pk = pos_fk
1606 AND parent_pos = %s
1607 AND thr_fk = %s',
1608 array('integer', 'integer'),
1609 array('0', $tree_id));
1610
1611 $row = $ilDB->fetchObject($res);
1612
1613 return $this->fetchPostNodeData($row);
1614 }
1615
1622 public function getPostNode($post_id)
1623 {
1624 global $ilDB;
1625
1626 $res = $ilDB->queryf('
1627 SELECT * FROM frm_posts, frm_posts_tree
1628 WHERE pos_pk = pos_fk
1629 AND pos_pk = %s',
1630 array('integer'),
1631 array($post_id));
1632
1633 $row = $ilDB->fetchObject($res);
1634
1635 return $this->fetchPostNodeData($row);
1636 }
1637
1644 public function fetchPostNodeData($a_row)
1645 {
1646 global $lng;
1647
1648 require_once('./Services/User/classes/class.ilObjUser.php');
1649
1650 if (ilObject::_exists($a_row->pos_display_user_id))
1651 {
1652 $tmp_user = new ilObjUser($a_row->pos_display_user_id);
1653 $fullname = $tmp_user->getFullname();
1654 $loginname = $tmp_user->getLogin();
1655 }
1656
1657 $fullname = $fullname ? $fullname : ($a_row->import_name ? $a_row->import_name : $lng->txt("unknown"));
1658
1659 $data = array(
1660 "pos_pk" => $a_row->pos_pk,
1661 "child" => $a_row->pos_pk,
1662 "author" => $a_row->pos_display_user_id,
1663 "alias" => $a_row->pos_usr_alias,
1664 "title" => $fullname,
1665 "loginname" => $loginname,
1666 "type" => "post",
1667 "message" => $a_row->pos_message,
1668 "subject" => $a_row->pos_subject,
1669 "pos_cens_com" => $a_row->pos_cens_com,
1670 "pos_cens" => $a_row->pos_cens,
1671 // "date" => $a_row->date,
1672 "date" => $a_row->fpt_date,
1673 "create_date" => $a_row->pos_date,
1674 "update" => $a_row->pos_update,
1675 "update_user" => $a_row->update_user,
1676 "tree" => $a_row->thr_fk,
1677 "parent" => $a_row->parent_pos,
1678 "lft" => $a_row->lft,
1679 "rgt" => $a_row->rgt,
1680 "depth" => $a_row->depth,
1681 "id" => $a_row->fpt_pk,
1682 "notify" => $a_row->notify,
1683 "import_name" => $a_row->import_name,
1684 "pos_status" => $a_row->pos_status
1685 );
1686
1687 // why this line? data should be stored without slashes in db
1688 //$data["message"] = stripslashes($data["message"]);
1689
1690 return $data ? $data : array();
1691 }
1692
1699 public function deletePostTree($a_node)
1700 {
1701 global $ilDB;
1702
1703 // GET LEFT AND RIGHT VALUES
1704 $res = $ilDB->queryf('
1705 SELECT * FROM frm_posts_tree
1706 WHERE thr_fk = %s
1707 AND pos_fk = %s
1708 AND parent_pos = %s',
1709 array('integer', 'integer', 'integer'),
1710 array($a_node['tree'], $a_node['pos_pk'], $a_node['parent']));
1711
1712 while($row = $ilDB->fetchObject($res))
1713 {
1714 $a_node["lft"] = $row->lft;
1715 $a_node["rgt"] = $row->rgt;
1716 }
1717
1718 $diff = $a_node["rgt"] - $a_node["lft"] + 1;
1719
1720 // get data of posts
1721 $result = $ilDB->queryf('
1722 SELECT * FROM frm_posts_tree
1723 WHERE lft BETWEEN %s AND %s
1724 AND thr_fk = %s',
1725 array('integer', 'integer', 'integer'),
1726 array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1727
1728 $del_id = array();
1729
1730 while ($treeData = $ilDB->fetchAssoc($result))
1731 {
1732 $del_id[] = $treeData["pos_fk"];
1733 }
1734
1735 // delete subtree
1736 $statement = $ilDB->manipulateF('
1737 DELETE FROM frm_posts_tree
1738 WHERE lft BETWEEN %s AND %s
1739 AND thr_fk = %s',
1740 array('integer', 'integer', 'integer'),
1741 array($a_node['lft'], $a_node['rgt'], $a_node['tree']));
1742
1743
1744 // close gaps
1745 $statement = $ilDB->manipulateF('
1746 UPDATE frm_posts_tree
1747 SET lft = CASE
1748 WHEN lft > %s
1749 THEN lft - %s
1750 ELSE lft
1751 END,
1752 rgt = CASE
1753 WHEN rgt > %s
1754 THEN rgt - %s
1755 ELSE rgt
1756 END
1757 WHERE thr_fk = %s',
1758 array('integer', 'integer', 'integer', 'integer', 'integer'),
1759 array($a_node['lft'], $diff, $a_node['lft'], $diff, $a_node['tree']));
1760
1761 return $del_id;
1762
1763 }
1764
1770 public function updateVisits($ID)
1771 {
1772
1773 global $ilDB;
1774
1775 $checkTime = time() - (60*60);
1776
1777 if ($_SESSION["frm_visit_".$this->dbTable."_".$ID] < $checkTime)
1778 {
1779
1780 $_SESSION["frm_visit_".$this->dbTable."_".$ID] = time();
1781 $query = 'UPDATE '.$this->dbTable.' SET visits = visits + 1 WHERE ';
1782
1783 $data_type = array();
1784 $data_value = array();
1785
1786 if($this->getMDB2Query() != '' && $this->getMDB2DataType() != '' && $this->getMDB2DataValue() != '')
1787 {
1788 $query .= $this->getMDB2Query();
1789 $data_type = $data_type + $this->getMDB2DataType();
1790 $data_value = $data_value + $this->getMDB2DataValue();
1791
1792 $res = $ilDB->queryf($query, $data_type, $data_value);
1793 }
1794 }
1795 }
1796
1804 public function prepareText($text, $edit=0, $quote_user = '', $type = '')
1805 {
1806 global $lng;
1807
1808 if($type == 'export')
1809 {
1810 $this->replQuote1 = "<blockquote class=\"quote\"><hr size=\"1\" color=\"#000000\">";
1811 $this->replQuote2 = "<hr size=\"1\" color=\"#000000\"/></blockquote>";
1812 }
1813
1814 if($edit == 1)
1815 {
1816 // add login name of quoted users
1817 $lname = ($quote_user != "")
1818 ? '="'.$quote_user.'"'
1819 : "";
1820
1821 $text = "[quote$lname]".$text."[/quote]";
1822 }
1823 else
1824 {
1825 // check for quotation
1826 $startZ = substr_count ($text, "[quote"); // also count [quote="..."]
1827 $endZ = substr_count ($text, "[/quote]");
1828
1829 if ($startZ > 0 || $endZ > 0)
1830 {
1831 // add missing opening and closing tags
1832 if ($startZ > $endZ)
1833 {
1834 $diff = $startZ - $endZ;
1835
1836 for ($i = 0; $i < $diff; $i++)
1837 {
1838 if ($type == 'export') $text .= $this->txtQuote2;
1839 else $text .= "[/quote]";
1840 }
1841 }
1842 elseif ($startZ < $endZ)
1843 {
1844 $diff = $endZ - $startZ;
1845
1846 for ($i = 0; $i < $diff; $i++)
1847 {
1848 if ($type == 'export') $text = $this->txtQuote1.$text;
1849 else $text = "[quote]".$text;
1850 }
1851 }
1852
1853 if($edit == 0)
1854 {
1855 $ws= "[ \t\r\f\v\n]*";
1856
1857 $text = eregi_replace("\[(quote$ws=$ws\"([^\"]*)\"$ws)\]",
1858 $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote")." (\\2)".'</div>', $text);
1859
1860 $text = str_replace("[quote]",
1861 $this->replQuote1.'<div class="ilForumQuoteHead">'.$lng->txt("quote").'</div>', $text);
1862
1863 $text = str_replace("[/quote]", $this->replQuote2, $text);
1864 }
1865 }
1866 }
1867
1868 if($type != 'export')
1869 {
1870 if($edit == 0)
1871 {
1872 $text = ilUtil::insertLatexImages($text, "<span class\=\"latex\">", "<\/span>");
1873 $text = ilUtil::insertLatexImages($text, "\[tex\]", "\[\/tex\]");
1874 }
1875
1876 // workaround for preventing template engine
1877 // from hiding text that is enclosed
1878 // in curly brackets (e.g. "{a}")
1879 $text = str_replace("{", "&#123;", $text);
1880 $text = str_replace("}", "&#125;", $text);
1881 }
1882
1883 return $text;
1884 }
1885
1886
1893 public function getModeratorFromPost($pos_pk)
1894 {
1895 global $ilDB;
1896
1897 $res = $ilDB->queryf('
1898 SELECT frm_data.* FROM frm_data, frm_posts
1899 WHERE pos_pk = %s
1900 AND pos_top_fk = top_pk',
1901 array('integer'), array($pos_pk));
1902
1903 $row = $ilDB->fetchAssoc($res);
1904
1905 return $row;
1906
1907 }
1908
1909 function __deletePostFiles($a_ids)
1910 {
1911 if(!is_array($a_ids))
1912 {
1913 return false;
1914 }
1915 include_once "./Modules/Forum/classes/class.ilFileDataForum.php";
1916
1917 $tmp_file_obj =& new ilFileDataForum($this->getForumId());
1918 foreach($a_ids as $pos_id)
1919 {
1920 $tmp_file_obj->setPosId($pos_id);
1921 $files = $tmp_file_obj->getFilesOfPost();
1922 foreach($files as $file)
1923 {
1924 $tmp_file_obj->unlinkFile($file["name"]);
1925 }
1926 }
1927 unset($tmp_file_obj);
1928 return true;
1929 }
1930
1931 function getImportName()
1932 {
1933 return $this->import_name;
1934 }
1935 function setImportName($a_import_name)
1936 {
1937 $this->import_name = $a_import_name;
1938 }
1939
1946 function enableForumNotification($user_id)
1947 {
1948 global $ilDB;
1949
1950 if (!$this->isForumNotificationEnabled($user_id))
1951 {
1952 /* Remove all notifications of threads that belong to the forum */
1953
1954 $res = $ilDB->queryf('
1955 SELECT frm_notification.thread_id FROM frm_data, frm_notification, frm_threads
1956 WHERE frm_notification.user_id = %s
1957 AND frm_notification.thread_id = frm_threads.thr_pk
1958 AND frm_threads.thr_top_fk = frm_data.top_pk
1959 AND frm_data.top_frm_fk = %s
1960 GROUP BY frm_notification.thread_id',
1961 array('integer', 'integer'),
1962 array($user_id, $this->id));
1963
1964 if (is_object($res) && $res->numRows() > 0)
1965 {
1966 $thread_data = array();
1967 $thread_data_types = array();
1968
1969 $query = ' DELETE FROM frm_notification
1970 WHERE user_id = %s
1971 AND thread_id IN (';
1972
1973 array_push($thread_data, $user_id);
1974 array_push($thread_data_types, 'integer');
1975
1976 $counter = 1;
1977
1978 while($row = $ilDB->fetchAssoc($res))
1979 {
1980 if($counter < $res->numRows())
1981 {
1982 $query .= '%s, ';
1983 array_push($thread_data, $row['thread_id']);
1984 array_push($thread_data_types, 'integer');
1985 }
1986
1987 if($counter == $res->numRows())
1988 {
1989 $query .= '%s)';
1990 array_push($thread_data, $row['thread_id']);
1991 array_push($thread_data_types, 'integer');
1992
1993 }
1994 $counter++;
1995 }
1996
1997 $statement = $ilDB->manipulateF($query, $thread_data_types, $thread_data);
1998 }
1999
2000 /* Insert forum notification */
2001
2002 $nextId = $ilDB->nextId('frm_notification');
2003
2004 $statement = $ilDB->manipulateF('
2005 INSERT INTO frm_notification
2006 ( notification_id,
2007 user_id,
2008 frm_id
2009 )
2010 VALUES(%s, %s, %s)',
2011 array('integer','integer', 'integer'),
2012 array($nextId, $user_id, $this->id));
2013
2014 }
2015
2016 return true;
2017 }
2018
2025 function disableForumNotification($user_id)
2026 {
2027 global $ilDB;
2028
2029 $statement = $ilDB->manipulateF('
2030 DELETE FROM frm_notification
2031 WHERE user_id = %s
2032 AND frm_id = %s',
2033 array('integer', 'integer'),
2034 array($user_id, $this->id));
2035
2036 return true;
2037 }
2038
2046 {
2047 global $ilDB;
2048
2049 $result = $ilDB->queryf('SELECT COUNT(*) cnt FROM frm_notification WHERE user_id = %s AND frm_id = %s',
2050 array('integer', 'integer'), array($user_id, $this->id));
2051
2052 while($record = $ilDB->fetchAssoc($result))
2053 {
2054 return (bool)$record['cnt'];
2055 }
2056
2057 return false;
2058 }
2059
2067 function enableThreadNotification($user_id, $thread_id)
2068 {
2069 global $ilDB;
2070
2071 if (!$this->isThreadNotificationEnabled($user_id, $thread_id))
2072 {
2073 $nextId = $ilDB->nextId('frm_notification');
2074 $statement = $ilDB->manipulateF('
2075 INSERT INTO frm_notification
2076 ( notification_id,
2077 user_id,
2078 thread_id
2079 )
2080 VALUES (%s, %s, %s)',
2081 array('integer', 'integer', 'integer'), array($nextId, $user_id, $thread_id));
2082
2083 }
2084
2085 return true;
2086 }
2087
2095 function isThreadNotificationEnabled($user_id, $thread_id)
2096 {
2097 global $ilDB;
2098
2099 $result = $ilDB->queryf('
2100 SELECT COUNT(*) cnt FROM frm_notification
2101 WHERE user_id = %s
2102 AND thread_id = %s',
2103 array('integer', 'integer'),
2104 array($user_id, $thread_id));
2105
2106
2107 while($record = $ilDB->fetchAssoc($result))
2108 {
2109 return (bool)$record['cnt'];
2110 }
2111
2112 return false;
2113 }
2114
2124 public static function _getThreads($a_obj_id,$a_sort_mode = self::SORT_DATE)
2125 {
2126 global $ilDB;
2127
2128 switch($a_sort_mode)
2129 {
2130 case self::SORT_DATE:
2131 $sort = 'thr_date';
2132 break;
2133
2134 case self::SORT_TITLE:
2135 default:
2136 $sort = 'thr_subject';
2137 break;
2138 }
2139
2140 $res = $ilDB->queryf('
2141 SELECT * FROM frm_threads
2142 JOIN frm_data ON top_pk = thr_top_fk
2143 WHERE top_frm_fk = %s
2144 ORDER BY %s',
2145 array('integer', 'text'), array($a_obj_id, $sort));
2146
2147 while($row = $ilDB->fetchObject($res))
2148 {
2149 $threads[$row->thr_pk] = $row->thr_subject;
2150 }
2151 return $threads ? $threads : array();
2152 }
2153
2154 public static function _lookupObjIdForForumId($a_for_id)
2155 {
2156 global $ilDB;
2157
2158 $res = $ilDB->queryf('
2159 SELECT top_frm_fk FROM frm_data
2160 WHERE top_pk = %s',
2161 array('integer'), array($a_for_id));
2162
2163 if ($fdata = $ilDB->fetchAssoc($res))
2164 {
2165 return $fdata["top_frm_fk"];
2166 }
2167 return false;
2168 }
2169
2170 public static function updateLastPostByObjId($a_obj_id)
2171 {
2172 global $ilDB;
2173 // get latest post of forum and update last_post
2174 $ilDB->setLimit(1);
2175 $res2 = $ilDB->queryf('
2176 SELECT pos_top_fk, pos_thr_fk, pos_pk FROM frm_posts, frm_data
2177 WHERE pos_top_fk = top_pk
2178 AND top_frm_fk = %s
2179 ORDER BY pos_date DESC',
2180 array('integer'), array($a_obj_id));
2181
2182 if ($res2->numRows() == 0)
2183 {
2184 $lastPost_top = "";
2185 }
2186 else
2187 {
2188 $z = 0;
2189
2190 while ($selData = $ilDB->fetchAssoc($res2))
2191 {
2192 if ($z > 0)
2193 {
2194 break;
2195 }
2196
2197 $lastPost_top = $selData["pos_top_fk"]."#".$selData["pos_thr_fk"]."#".$selData["pos_pk"];
2198 $z ++;
2199 }
2200 }
2201
2202 $ilDB->update('frm_data',
2203 array('top_last_post' => array('text', $lastPost_top)),
2204 array('top_frm_fk' => array('integer', $a_obj_id))
2205 );
2206
2207 }
2208
2215 public static function mergeThreads($obj_id, $source_id, $target_id)
2216 {
2217 // selected source & target objects
2218 $source_thread_obj = new ilForumTopic((int)$source_id);
2219 $target_thread_obj = new ilForumTopic((int)$target_id);
2220
2221 if($source_thread_obj->getForumId() != $target_thread_obj->getForumId())
2222 {
2223 throw new ilException('not_allowed_to_merge_into_another_forum');
2224 }
2225 // use the "older" thread as target
2226 if($source_thread_obj->getCreateDate() > $target_thread_obj->getCreateDate())
2227 {
2228 $merge_thread_source = $source_thread_obj;
2229 $merge_thread_target = $target_thread_obj;
2230 }
2231 else
2232 {
2233 $merge_thread_source = $target_thread_obj;
2234 $merge_thread_target = $source_thread_obj;
2235 }
2236
2237 $thread_subject = $target_thread_obj->getSubject();
2238
2239 // remember if the threads are open or closed and then close both threads !
2240 $targed_was_closed = $merge_thread_target->isClosed();
2241
2242 $merge_thread_source->close();
2243
2244 if($targed_was_closed == false)
2245 {
2246 $merge_thread_target->close();
2247 }
2248
2249 $source_all_posts = $merge_thread_source->getAllPosts();
2250 $source_root_node = $merge_thread_source->getFirstPostNode();
2251 $target_root_node = $merge_thread_target->getFirstPostNode();
2252
2253 $add_difference = $target_root_node->getRgt();
2254
2255// update target root node rgt
2256 include_once 'Modules/Forum/classes/class.ilForumPostsTree.php';
2257// $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt() + 1);
2258 $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt());
2259 ilForumPostsTree::updateTargetRootRgt($target_root_node->getId(), $new_target_rgt);
2260
2261 $new_target_root = $target_root_node->getId();
2262
2263 // get source post tree and update posts tree
2264 foreach($source_all_posts as $post)
2265 {
2266 $post_obj = new ilForumPost($post->pos_pk);
2267
2268 $posts_tree_obj = new ilForumPostsTree();
2269 $posts_tree_obj->setPosFk($post->pos_pk);
2270
2271 if($post_obj->getParentId() == 0)
2272 {
2273 $posts_tree_obj->setParentPos($new_target_root);
2274
2275 //$posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference));
2276 $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2277 $posts_tree_obj->setLft($target_root_node->getRgt());
2278
2279 $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2280 $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2281
2282 $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2283
2284 $posts_tree_obj->mergeParentPos();
2285 }
2286 else
2287 {
2288 $posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference) - 1);
2289 $posts_tree_obj->setLft(($post_obj->getLft() + $add_difference) - 1);
2290
2291 $posts_tree_obj->setDepth(($post_obj->getDepth() + 1));
2292 $posts_tree_obj->setSourceThreadId($merge_thread_source->getId());
2293
2294 $posts_tree_obj->setParentPos($post_obj->getParentId());
2295 $posts_tree_obj->setTargetThreadId($merge_thread_target->getId());
2296
2297 $posts_tree_obj->merge();
2298 }
2299 }
2300
2301// update frm_posts pos_thr_fk = target_thr_id
2302 include_once 'Modules/Forum/classes/class.ilForumPost.php';
2303 ilForumPost::mergePosts($merge_thread_source->getId(), $merge_thread_target->getId());
2304
2305// check notifications
2306 include_once 'Modules/Forum/classes/class.ilForumNotification.php';
2307 ilForumNotification::mergeThreadNotificiations($merge_thread_source->getId(), $merge_thread_target->getId());
2308
2309// delete frm_thread_access entries
2310 include_once './Modules/Forum/classes/class.ilObjForum.php';
2311 ilObjForum::_deleteAccessEntries($merge_thread_source->getId());
2312
2313// update frm_user_read
2314 ilObjForum::mergeForumUserRead($merge_thread_source->getId(), $merge_thread_target->getId());
2315
2316// update visits, thr_num_posts, last_post, subject
2317 $post_date_source = $merge_thread_source->getLastPost()->getCreateDate();
2318 $post_date_target = $merge_thread_target->getLastPost()->getCreateDate();
2319
2320 $target_last_post = $merge_thread_target->getLastPostString();
2321 $exp = explode('#', $target_last_post);
2322
2323 if($post_date_source > $post_date_target)
2324 {
2325 $exp[2] = $merge_thread_source->getLastPost()->getId();
2326 }
2327 else
2328 {
2329 $exp[2] = $merge_thread_target->getLastPost()->getId();
2330 }
2331 $new_thr_last_post = implode('#', $exp);
2332
2333 $num_posts_source = (int)$merge_thread_source->getNumPosts();
2334 $num_visits_source = (int)$merge_thread_source->getVisits();
2335 $num_posts_target = (int)$merge_thread_target->getNumPosts();
2336 $num_visits_target = (int)$merge_thread_source->getVisits();
2337
2338 $frm_topic_obj = new ilForumTopic(0, false, true);
2339 $frm_topic_obj->setNumPosts(($num_posts_source + $num_posts_target));
2340 $frm_topic_obj->setVisits(($num_visits_source + $num_visits_target));
2341 $frm_topic_obj->setLastPostString($new_thr_last_post);
2342 $frm_topic_obj->setSubject($thread_subject);
2343 $frm_topic_obj->setId($merge_thread_target->getId());
2344
2345 $frm_topic_obj->updateMergedThread();
2346
2347// update frm_data: top_last_post , top_num_threads
2349
2350// reopen target if was not "closed" before merging
2351 if(!$targed_was_closed)
2352 {
2353 $merge_thread_target->reopen();
2354 }
2355
2356// delete source thread
2357 ilForumTopic::deleteByThreadId($merge_thread_source->getId());
2358 }
2359} // END class.Forum
$result
print $file
$_SESSION["AccountId"]
const IL_CAL_DATETIME
const NEWS_USERS
const NEWS_NOTICE
static formatDate(ilDateTime $date)
Format a date @access public.
@classDescription Date and time handling
Base class for ILIAS Exception handling.
This class handles all operations on files for the forum object.
static mergeThreadNotificiations($merge_source_thread_id, $merge_target_thread_id)
static mergePosts($source_thread_id, $target_thread_id)
static updateTargetRootRgt($root_node_id, $rgt)
static getInstance($a_obj_id=0)
static deleteByThreadId($thr_id)
Class Forum core functions for forum.
countUserArticles($a_user_id)
get number of articles from given user-ID
getOnePost($post)
get one post-dataset
disableForumNotification($user_id)
Disable a user's notification about new posts in this forum.
generateThread($forum_id, $author_id, $display_user_id, $subject, $message, $notify, $notify_posts, $alias='', $date='', $status=1)
generate new dataset in frm_threads
getFirstPostNode($tree_id)
get data of the first node from frm_posts_tree and frm_posts @access public
fetchPostNodeData($a_row)
get data of parent node from frm_posts_tree and frm_posts @access private
countActiveUserArticles($a_user_id)
deletePostTree($a_node)
delete node and the whole subtree under this node @access public
__deletePostFiles($a_ids)
const SORT_DATE
addPostTree($a_tree_id, $a_node_id=-1, $a_date='')
create a new post-tree
enableThreadNotification($user_id, $thread_id)
Enable a user's notification about new posts in a thread.
__construct()
Constructor @access public.
setForumId($a_obj_id)
set object id which refers to ILIAS obj_id
updateVisits($ID)
update page hits of given forum- or thread-ID @access public
getPostNode($post_id)
get data of given node from frm_posts_tree and frm_posts @access public
static $moderators_by_ref_id_map
setImportName($a_import_name)
getPostDepth($a_node_id, $tree_id)
Return depth of an object @access private.
_getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)
static _isModerator($a_ref_id, $a_usr_id)
checks whether a user is moderator of a given forum object
getOneTopic()
get one topic-dataset by WhereCondition
prepareText($text, $edit=0, $quote_user='', $type='')
prepares given string @access public
getForumId()
get forum id @access public
postCensorship($message, $pos_pk, $cens=0)
update dataset in frm_posts with censorship info
const SORT_TITLE
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
static updateLastPostByObjId($a_obj_id)
static _getLanguageInstanceByUsrId($usr_id)
Get the ilLanguage instance for the passed user id.
convertDate($date)
converts the date format
setPageHits($pageHits)
isThreadNotificationEnabled($user_id, $thread_id)
Check whether a user's notification about new posts in a thread is enabled (result > 0) or not (resul...
moveThreads($thread_ids=array(), $src_ref_id=0, $dest_top_frm_fk=0)
Moves all chosen threads and their posts to a new forum.
getMDB2Query()
get content of additional condition
deletePost($post)
delete post and sub-posts
getOneThread()
get one thread-dataset by WhereCondition
setOrderField($orderField)
set database field for sorting results
getDbTable()
get name of database table
getPageHits()
get number of max.
getModeratorFromPost($pos_pk)
get one post-dataset
setMDB2WhereCondition($query_string, $data_type, $data_value)
set content for additional condition
const DEFAULT_PAGE_HITS
static _getThreads($a_obj_id, $a_sort_mode=self::SORT_DATE)
Get thread infos of object.
getUserStatistic($is_moderator=false)
static mergeThreads($obj_id, $source_id, $target_id)
isForumNotificationEnabled($user_id)
Check whether a user's notification about new posts in this forum is enabled (result > 0) or not (res...
getMDB2DataType()
get content of additional condition
static _lookupObjIdForForumId($a_for_id)
getFirstPostByThread($a_thread_id)
Get first post of thread.
setForumRefId($a_ref_id)
set reference id which refers to ILIAS obj_id
enableForumNotification($user_id)
Enable a user's notification about new posts in this forum.
setLanguage($lng)
getMDB2DataValue()
/* get content of additional condition
getModerators()
get all users assigned to local role il_frm_moderator_<frm_ref_id>
getForumRefId()
get forum ref_id @access public
setDbTable($dbTable)
set database table
getOrderField()
get name of orderField
_lookupPostMessage($a_id)
insertPostNode($a_node_id, $a_parent_id, $tree_id, $a_date='')
insert node under parent node @access public
language handling
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.
static mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
Class ilObjMediaObject.
_getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
static _exists($a_id)
checks wether a lm content object with specified id exists or not
_removeUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Remove usage of mob in another container.
static _lookupLanguage($a_usr_id)
getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
static _lookupTitle($a_id)
lookup object title
static _lookupObjectId($a_ref_id)
lookup object id
static _exists($a_id, $a_reference=false, $a_type=null)
checks if an object exists in object_data@access 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...
static insertLatexImages($a_text, $a_start="\[tex\]", $a_end="\[\/tex\]")
replace [text]...[/tex] tags with formula image code
$data
$text
$params
Definition: example_049.php:96
$target_id
Definition: goto.php:88
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
redirection script todo: (a better solution should control the processing via a xml file)
global $ilSetting
Definition: privfeed.php:40
$ref_id
Definition: sahs_server.php:39
global $ilDB
$mobs
global $ilUser
Definition: imgupload.php:15