ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilObjForum.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 'Services/Object/classes/class.ilObject.php';
5require_once 'Modules/Forum/classes/class.ilForum.php';
6require_once 'Modules/Forum/classes/class.ilFileDataForum.php';
7require_once 'Modules/Forum/classes/class.ilForumProperties.php';
8
18class ilObjForum extends ilObject
19{
25 public $Forum;
26
27// private $objProperties = null;
28
33 protected static $obj_id_to_forum_id_cache = array();
34
39 protected static $ref_id_to_forum_id_cache = array();
40
45 protected static $forum_statistics_cache = array();
46
51 protected static $forum_last_post_cache = array();
52
59 public function __construct($a_id = 0, $a_call_by_reference = true)
60 {
61 $this->type = 'frm';
62 parent::__construct($a_id, $a_call_by_reference);
63
64 /*
65 * this constant is used for the information if a single post is marked as new
66 * All threads/posts created before this date are never marked as new
67 * Default is 8 weeks
68 *
69 */
70 $new_deadline = time() - 60 * 60 * 24 * 7 * ($this->ilias->getSetting('frm_store_new') ?
71 $this->ilias->getSetting('frm_store_new') :
72 8);
73 define('NEW_DEADLINE', $new_deadline);
74
75 // TODO: needs to rewrite scripts that are using Forum outside this class
76 $this->Forum = new ilForum();
77 }
78
82 public function create()
83 {
84 $id = parent::create();
85
86 require_once 'Modules/Forum/classes/class.ilForumProperties.php';
87 $properties = ilForumProperties::getInstance($this->getId());
88 $properties->setDefaultView(1);
89 $properties->setAnonymisation(0);
90 $properties->setStatisticsStatus(0);
91 $properties->setPostActivation(0);
92 $properties->setThreadSorting(0);
93 $properties->insert();
94
95 $this->createSettings();
96
97 $this->saveData();
98
99 return $id;
100 }
101
105 public function setPermissions($a_ref_id)
106 {
110 global $rbacadmin;
111
112 parent::setPermissions($a_ref_id);
113
114 // ...finally assign moderator role to creator of forum object
115 $roles = array(ilObjForum::_lookupModeratorRole($this->getRefId()));
116 $rbacadmin->assignUser($roles[0], $this->getOwner(), 'n');
117 $this->updateModeratorRole($roles[0]);
118 }
119
123 public function updateModeratorRole($role_id)
124 {
128 global $ilDB;
129
130 $ilDB->manipulate('UPDATE frm_data SET top_mods = ' . $ilDB->quote($role_id, 'integer') . ' WHERE top_frm_fk = ' . $ilDB->quote($this->getId(), 'integer'));
131 }
132
138 function getDiskUsage()
139 {
140 require_once("./Modules/File/classes/class.ilObjFileAccess.php");
141 return ilObjForumAccess::_lookupDiskUsage($this->id);
142 }
143
144 public static function _lookupThreadSubject($a_thread_id)
145 {
150 global $ilDB;
151
152 $res = $ilDB->queryf('
153 SELECT thr_subject FROM frm_threads WHERE thr_pk = %s',
154 array('integer'), array($a_thread_id));
155
156 while($row = $ilDB->fetchObject($res))
157 {
158 return $row->thr_subject;
159 }
160 return '';
161 }
162
163 // METHODS FOR UN-READ STATUS
164 public function getCountUnread($a_usr_id, $a_thread_id = 0)
165 {
166// return $this->_getCountUnread($this->getId(),$a_usr_id,$a_thread_id);
167// }
168//
169// function _getCountUnread($a_frm_id, $a_usr_id,$a_thread_id = 0)
170// {
171
172 $a_frm_id = $this->getId();
173
177 global $ilBench, $ilDB;
178
179 $ilBench->start("Forum", 'getCountRead');
180 if(!$a_thread_id)
181 {
182 // Get topic_id
183 $res = $ilDB->queryf('
184 SELECT top_pk FROM frm_data WHERE top_frm_fk = %s',
185 array('integer'), array($a_frm_id));
186
187
188 while($row = $ilDB->fetchObject($res))
189 {
190 $topic_id = $row->top_pk;
191 }
192
193 // Get number of posts
194 $res = $ilDB->queryf('
195 SELECT COUNT(pos_pk) num_posts FROM frm_posts
196 WHERE pos_top_fk = %s',
197 array('integer'), array($topic_id));
198
199 while($row = $ilDB->fetchObject($res))
200 {
201 $num_posts = $row->num_posts;
202 }
203
204 $res = $ilDB->queryf('
205 SELECT COUNT(post_id) count_read FROM frm_user_read
206 WHERE obj_id = %s
207 AND usr_id = %s',
208 array('integer', 'integer'), array($a_frm_id, $a_usr_id));
209
210 while($row = $ilDB->fetchObject($res))
211 {
212 $count_read = $row->count_read;
213 }
214 $unread = $num_posts - $count_read;
215
216 $ilBench->stop("Forum", 'getCountRead');
217 return $unread > 0 ? $unread : 0;
218 }
219 else
220 {
221 $res = $ilDB->queryf('
222 SELECT COUNT(pos_pk) num_posts FROM frm_posts
223 WHERE pos_thr_fk = %s',
224 array('integer'), array($a_thread_id));
225
226 $row = $ilDB->fetchObject($res);
227 $num_posts = $row->num_posts;
228
229 $res = $ilDB->queryf('
230 SELECT COUNT(post_id) count_read FROM frm_user_read
231 WHERE obj_id = %s
232 AND usr_id = %s
233 AND thread_id = %s',
234 array('integer', 'integer', 'integer'), array($a_frm_id, $a_frm_id, $a_thread_id));
235
236 $row = $ilDB->fetchObject($res);
237 $count_read = $row->count_read;
238
239 $unread = $num_posts - $count_read;
240
241 $ilBench->stop("Forum", 'getCountRead');
242 return $unread > 0 ? $unread : 0;
243 }
244 $ilBench->stop("Forum", 'getCountRead');
245 return false;
246 }
247
248
249 public function markThreadRead($a_usr_id, $a_thread_id)
250 {
254 global $ilDB;
255
256 // Get all post ids
257 $res = $ilDB->queryf('
258 SELECT * FROM frm_posts WHERE pos_thr_fk = %s',
259 array('integer'), array($a_thread_id));
260
261 while($row = $ilDB->fetchObject($res))
262 {
263 $this->markPostRead($a_usr_id, $a_thread_id, $row->pos_pk);
264 }
265 return true;
266 }
267
268 public function markAllThreadsRead($a_usr_id)
269 {
273 global $ilDB;
274
275 $res = $ilDB->queryf('
276 SELECT * FROM frm_data, frm_threads
277 WHERE top_frm_fk = %s
278 AND top_pk = thr_top_fk',
279 array('integer'), array($this->getId()));
280
281 while($row = $ilDB->fetchObject($res))
282 {
283 $this->markThreadRead($a_usr_id, $row->thr_pk);
284 }
285
286 return true;
287 }
288
289
290 public function markPostRead($a_usr_id, $a_thread_id, $a_post_id)
291 {
295 global $ilDB;
296
297 // CHECK IF ENTRY EXISTS
298 $res = $ilDB->queryf('
299 SELECT * FROM frm_user_read
300 WHERE usr_id = %s
301 AND obj_id = %s
302 AND thread_id = %s
303 AND post_id = %s',
304 array('integer', 'integer', 'integer', 'integer'),
305 array($a_usr_id, $this->getId(), $a_thread_id, $a_post_id));
306
307 if($ilDB->numRows($res))
308 {
309 return true;
310 }
311
312 $res = $ilDB->manipulateF('
313 INSERT INTO frm_user_read
314 ( usr_id,
315 obj_id,
316 thread_id,
317 post_id
318 )
319 VALUES (%s,%s,%s,%s)',
320 array('integer', 'integer', 'integer', 'integer'),
321 array($a_usr_id, $this->getId(), $a_thread_id, $a_post_id));
322
323 return true;
324 }
325
326 public function markPostUnread($a_user_id, $a_post_id)
327 {
331 global $ilDB;
332
333 $res = $ilDB->manipulateF('
334 DELETE FROM frm_user_read
335 WHERE usr_id = %s
336 AND post_id = %s',
337 array('integer', 'integer'),
338 array($a_user_id, $a_post_id));
339 }
340
341 public function isRead($a_usr_id, $a_post_id)
342 {
346 global $ilDB;
347
348 $res = $ilDB->queryf('
349 SELECT * FROM frm_user_read
350 WHERE usr_id = %s
351 AND post_id = %s',
352 array('integer', 'integer'),
353 array($a_usr_id, $a_post_id));
354
355 return $ilDB->numRows($res) ? true : false;
356 }
357
358 public function updateLastAccess($a_usr_id, $a_thread_id)
359 {
363 global $ilDB;
364
365 $res = $ilDB->queryf('
366 SELECT * FROM frm_thread_access
367 WHERE usr_id = %s
368 AND obj_id = %s
369 AND thread_id = %s',
370 array('integer', 'integer', 'integer'),
371 array($a_usr_id, $this->getId(), $a_thread_id));
372 $data = $ilDB->fetchAssoc($res);
373
374 $ilDB->replace(
375 'frm_thread_access',
376 array(
377 'usr_id' => array('integer', $a_usr_id),
378 'obj_id' => array('integer', $this->getId()),
379 'thread_id' => array('integer', $a_thread_id)
380 ),
381 array(
382 'access_last' => array('integer', time()),
383 'access_old' => array('integer', isset($data['access_old']) ? $data['access_old'] : 0),
384 'access_old_ts' => array('timestamp', $data['access_old_ts'])
385 )
386 );
387
388 return true;
389 }
390
395 public static function _updateOldAccess($a_usr_id)
396 {
401 global $ilDB, $ilias;
402
403 $ilDB->manipulateF('
404 UPDATE frm_thread_access
405 SET access_old = access_last
406 WHERE usr_id = %s',
407 array('integer'), array($a_usr_id));
408
409 $set = $ilDB->query("SELECT * FROM frm_thread_access " .
410 " WHERE usr_id = " . $ilDB->quote($a_usr_id, "integer")
411 );
412 while($rec = $ilDB->fetchAssoc($set))
413 {
414 $ilDB->manipulate("UPDATE frm_thread_access SET " .
415 " access_old_ts = " . $ilDB->quote(date('Y-m-d H:i:s', $rec["access_old"]), "timestamp") .
416 " WHERE usr_id = " . $ilDB->quote($rec["usr_id"], "integer") .
417 " AND obj_id = " . $ilDB->quote($rec["obj_id"], "integer") .
418 " AND thread_id = " . $ilDB->quote($rec["thread_id"], "integer")
419 );
420 }
421
422 $new_deadline = time() - 60 * 60 * 24 * 7 * ($ilias->getSetting('frm_store_new') ?
423 $ilias->getSetting('frm_store_new') :
424 8);
425
426 $ilDB->manipulateF('
427 DELETE FROM frm_thread_access WHERE access_last < %s',
428 array('integer'), array($new_deadline));
429 }
430
431 function _deleteUser($a_usr_id)
432 {
436 global $ilDB;
437
438 $data = array($a_usr_id);
439
440 $res = $ilDB->manipulateF('
441 DELETE FROM frm_user_read WHERE usr_id = %s',
442 array('integer'), $data
443 );
444
445 $res = $ilDB->manipulateF('
446 DELETE FROM frm_thread_access WHERE usr_id = %s',
447 array('integer'), $data
448 );
449
450 // delete notifications of deleted user
451 $ilDB->manipulateF('
452 DELETE FROM frm_notification WHERE user_id = %s',
453 array('integer'), $data);
454
455 return true;
456 }
457
458
459 function _deleteReadEntries($a_post_id)
460 {
464 global $ilDB;
465
466 $statement = $ilDB->manipulateF('
467 DELETE FROM frm_user_read WHERE post_id = %s',
468 array('integer'), array($a_post_id));
469
470 return true;
471 }
472
473 function _deleteAccessEntries($a_thread_id)
474 {
478 global $ilDB;
479
480 $statement = $ilDB->manipulateF('
481 DELETE FROM frm_thread_access WHERE thread_id = %s',
482 array('integer'), array($a_thread_id));
483
484 return true;
485 }
486
491 function update()
492 {
496 global $ilDB;
497
498 if(parent::update())
499 {
500
501 $statement = $ilDB->manipulateF('
502 UPDATE frm_data
503 SET top_name = %s,
504 top_description = %s,
505 top_update = %s,
506 update_user = %s
507 WHERE top_frm_fk =%s',
508 array('text', 'text', 'timestamp', 'integer', 'integer'),
509 array(
510 $this->getTitle(),
511 $this->getDescription(),
512 date("Y-m-d H:i:s"),
513 (int)$_SESSION["AccountId"],
514 (int)$this->getId()
515 ));
516
517 return true;
518 }
519
520 return false;
521 }
522
529 public function cloneObject($a_target_id, $a_copy_id = 0)
530 {
534 global $ilDB;
535
536 $new_obj = parent::cloneObject($a_target_id, $a_copy_id);
537 $this->cloneAutoGeneratedRoles($new_obj);
538
539 ilForumProperties::getInstance($this->getId())->copy($new_obj->getId());
540 $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
541 $topData = $this->Forum->getOneTopic();
542
543 $ilDB->update('frm_data', array(
544 'top_name' => array('text', $topData['top_name']),
545 'top_description' => array('text', $topData['top_description']),
546 'top_num_posts' => array('integer', $topData['top_num_posts']),
547 'top_num_threads' => array('integer', $topData['top_num_threads']),
548 'top_last_post' => array('text', $topData['top_last_post']),
549 'top_date' => array('timestamp', $topData['top_date']),
550 'visits' => array('integer', $topData['visits']),
551 'top_update' => array('timestamp', $topData['top_update']),
552 'update_user' => array('integer', $topData['update_user']),
553 'top_usr_id' => array('integer', $topData['top_usr_id'])
554 ), array(
555 'top_frm_fk' => array('integer', $new_obj->getId())
556 ));
557
558 // read options
559 include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
560
561 $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
562 $options = $cwo->getOptions($this->getRefId());
563
564 $options['threads'] = $this->Forum->_getThreads($this->getId());
565
566 // Generate starting threads
567 include_once('Modules/Forum/classes/class.ilFileDataForum.php');
568
569 $new_frm = $new_obj->Forum;
570 $new_frm->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($new_obj->getId()));
571
572 $new_frm->setForumId($new_obj->getId());
573 $new_frm->setForumRefId($new_obj->getRefId());
574
575 $new_topic = $new_frm->getOneTopic();
576 foreach($options['threads'] as $thread_id=> $thread_subject)
577 {
578 $this->Forum->setMDB2WhereCondition('thr_pk = %s ', array('integer'), array($thread_id));
579
580 $old_thread = $this->Forum->getOneThread();
581
582 $old_post_id = $this->Forum->getFirstPostByThread($old_thread['thr_pk']);
583 $old_post = $this->Forum->getOnePost($old_post_id);
584
585 // Now create new thread and first post
586
587 $new_post = $new_frm->generateThread(
588 $new_topic['top_pk'],
589 $old_thread['thr_author_id'],
590 $old_thread['thr_display_user_id'],
591 $old_thread['thr_subject'],
592 ilForum::_lookupPostMessage($old_post_id),
593 $old_post['notify'],
594 0,
595 $old_thread['thr_usr_alias'],
596 $old_thread['thr_date']);
597 // Copy attachments
598 $old_forum_files = new ilFileDataForum($this->getId(), $old_post_id);
599 $old_forum_files->ilClone($new_obj->getId(), $new_post);
600 }
601
602 return $new_obj;
603 }
604
611 public function cloneAutoGeneratedRoles($new_obj)
612 {
617 global $ilLog, $rbacadmin;
618
619 $moderator = ilObjForum::_lookupModeratorRole($this->getRefId());
620 $new_moderator = ilObjForum::_lookupModeratorRole($new_obj->getRefId());
621
622 if(!$moderator || !$new_moderator || !$this->getRefId() || !$new_obj->getRefId())
623 {
624 $ilLog->write(__METHOD__ . ' : Error cloning auto generated role: il_frm_moderator');
625 }
626 $rbacadmin->copyRolePermissions($moderator, $this->getRefId(), $new_obj->getRefId(), $new_moderator, true);
627 $ilLog->write(__METHOD__ . ' : Finished copying of role il_frm_moderator.');
628
629 include_once './Modules/Forum/classes/class.ilForumModerators.php';
630 $obj_mods = new ilForumModerators($this->getRefId());
631
632 $old_mods = $obj_mods->getCurrentModerators();
633 foreach($old_mods as $user_id)
634 {
635 // The object owner is already member of the moderator role when this method is called
636 // Since the new static caches are introduced with ILIAS 5.0, a database error occurs if we try to assign the user here.
637 if($this->getOwner() != $user_id)
638 {
639 $rbacadmin->assignUser($new_moderator, $user_id);
640 }
641 }
642 }
643
649 public function delete()
650 {
654 global $ilDB;
655
656 // always call parent delete function first!!
657 if(!parent::delete())
658 {
659 return false;
660 }
661
662 // delete attachments
663 $tmp_file_obj =& new ilFileDataForum($this->getId());
664 $tmp_file_obj->delete();
665 unset($tmp_file_obj);
666
667 $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
668
669 $topData = $this->Forum->getOneTopic();
670
671 $threads = $this->Forum->getAllThreads($topData['top_pk']);
672 foreach($threads['items'] as $thread)
673 {
674 $thread_ids_to_delete[$thread->getId()] = $thread->getId();
675 }
676
677 // delete tree
678 $ilDB->manipulate('DELETE FROM frm_posts_tree WHERE '. $ilDB->in('thr_fk', $thread_ids_to_delete, false, 'integer'));
679
680 // delete posts
681 $ilDB->manipulate('DELETE FROM frm_posts WHERE '. $ilDB->in('pos_thr_fk', $thread_ids_to_delete, false, 'integer'));
682
683 // delete threads
684 $ilDB->manipulate('DELETE FROM frm_threads WHERE '. $ilDB->in('thr_pk', $thread_ids_to_delete, false, 'integer'));
685
686 $obj_id = array($this->getId());
687 // delete forum
688 $ilDB->manipulateF('DELETE FROM frm_data WHERE top_frm_fk = %s',
689 array('integer'), $obj_id);
690
691 // delete settings
692 $ilDB->manipulateF('DELETE FROM frm_settings WHERE obj_id = %s',
693 array('integer'), $obj_id);
694
695 // delete read infos
696 $ilDB->manipulateF('DELETE FROM frm_user_read WHERE obj_id = %s',
697 array('integer'), $obj_id);
698
699 // delete thread access entries
700 $ilDB->manipulateF('DELETE FROM frm_thread_access WHERE obj_id = %s',
701 array('integer'), $obj_id);
702
703 //delete thread notifications
704 $ilDB->manipulate('DELETE FROM frm_notification WHERE '. $ilDB->in('thread_id', $thread_ids_to_delete, false, 'integer'));
705
706 //delete forum notifications
707 $ilDB->manipulateF('DELETE FROM frm_notification WHERE frm_id = %s', array('integer'), $obj_id);
708
709 // delete posts_deleted entries
710 $ilDB->manipulateF('DELETE FROM frm_posts_deleted WHERE obj_id = %s', array('integer'), $obj_id);
711
712 return true;
713 }
714
720 public function initDefaultRoles()
721 {
722 include_once './Services/AccessControl/classes/class.ilObjRole.php';
724 'il_frm_moderator_'.$this->getRefId(),
725 "Moderator of forum obj_no.".$this->getId(),
726 'il_frm_moderator',
727 $this->getRefId()
728 );
729 return array();
730 }
731
739 public static function _lookupModeratorRole($a_ref_id)
740 {
744 global $ilDB;
745
746 $mod_title = 'il_frm_moderator_' . $a_ref_id;
747
748 $res = $ilDB->queryf('
749 SELECT * FROM object_data WHERE title = %s',
750 array('text'), array($mod_title));
751
752 while($row = $ilDB->fetchObject($res))
753 {
754 return $row->obj_id;
755 }
756 return 0;
757 }
758
759
760 public function createSettings()
761 {
762 // news settings (public notifications yes/no)
763 include_once("./Services/News/classes/class.ilNewsItem.php");
764 $default_visibility = ilNewsItem::_getDefaultVisibilityForRefId($_GET["ref_id"]);
765 if($default_visibility == "public")
766 {
767 ilBlockSetting::_write("news", "public_notifications", 1, 0, $this->getId());
768 }
769
770 return true;
771 }
772
773 public function saveData($a_roles = array())
774 {
779 global $ilUser, $ilDB;
780
781 $nextId = $ilDB->nextId('frm_data');
782
783 $top_data = array(
784 'top_frm_fk' => $this->getId(),
785 'top_name' => $this->getTitle(),
786 'top_description' => $this->getDescription(),
787 'top_num_posts' => 0,
788 'top_num_threads' => 0,
789 'top_last_post' => NULL,
790 'top_mods' => !is_numeric($a_roles[0]) ? 0 : $a_roles[0],
791 'top_usr_id' => $ilUser->getId(),
792 'top_date' => ilUtil::now()
793 );
794
795 $ilDB->manipulateF('
796 INSERT INTO frm_data
797 (
798 top_pk,
799 top_frm_fk,
800 top_name,
801 top_description,
802 top_num_posts,
803 top_num_threads,
804 top_last_post,
805 top_mods,
806 top_date,
807 top_usr_id
808 )
809 VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
810 array('integer', 'integer', 'text', 'text', 'integer', 'integer', 'text', 'integer', 'timestamp', 'integer'),
811 array(
812 $nextId,
813 $top_data['top_frm_fk'],
814 $top_data['top_name'],
815 $top_data['top_description'],
816 $top_data['top_num_posts'],
817 $top_data['top_num_threads'],
818 $top_data['top_last_post'],
819 $top_data['top_mods'],
820 $top_data['top_date'],
821 $top_data['top_usr_id']
822 ));
823 }
824
825 public function setThreadSorting($a_thr_pk, $a_sorting_value)
826 {
827 global $ilDB;
828
829 $ilDB->update('frm_threads',
830 array('thread_sorting' => array('integer',$a_sorting_value)),
831 array('thr_pk' => array('integer', $a_thr_pk)));
832 }
833
834
840 public static function lookupForumIdByObjId($obj_id)
841 {
842 if(array_key_exists($obj_id, self::$obj_id_to_forum_id_cache))
843 {
844 return (int)self::$obj_id_to_forum_id_cache[$obj_id];
845 }
846
847 self::preloadForumIdsByObjIds(array($obj_id));
848
849 return (int)self::$obj_id_to_forum_id_cache[$obj_id];
850 }
851
857 public static function lookupForumIdByRefId($ref_id)
858 {
859 if(array_key_exists($ref_id, self::$ref_id_to_forum_id_cache))
860 {
861 return (int)self::$ref_id_to_forum_id_cache[$ref_id];
862 }
863
864 self::preloadForumIdsByRefIds(array($ref_id));
865
866 return (int)self::$ref_id_to_forum_id_cache[$ref_id];
867 }
868
873 public static function preloadForumIdsByObjIds(array $obj_ids)
874 {
878 global $ilDB;
879
880 if(count($obj_ids) == 1)
881 {
882 $in = " objr.obj_id = " . $ilDB->quote(current($obj_ids), 'integer') . " ";
883 }
884 else
885 {
886 $in = $ilDB->in('objr.obj_id', $obj_ids, false, 'integer');
887 }
888 $query = "
889 SELECT frmd.top_pk, objr.ref_id, objr.obj_id
890 FROM object_reference objr
891 INNER JOIN frm_data frmd ON frmd.top_frm_fk = objr.obj_id
892 WHERE $in
893 ";
894 $res = $ilDB->query($query);
895
896 // Prepare cache array
897 foreach($obj_ids as $obj_id)
898 {
899 self::$obj_id_to_forum_id_cache[$obj_id] = null;
900 }
901
902 while($row = $ilDB->fetchAssoc($res))
903 {
904 self::$obj_id_to_forum_id_cache[$row['obj_id']] = $row['top_pk'];
905 self::$ref_id_to_forum_id_cache[$row['ref_id']] = $row['top_pk'];
906 }
907 }
908
913 public static function preloadForumIdsByRefIds(array $ref_ids)
914 {
918 global $ilDB;
919
920 if(count($ref_ids) == 1)
921 {
922 $in = " objr.ref_id = " . $ilDB->quote(current($ref_ids), 'integer') . " ";
923 }
924 else
925 {
926 $in = $ilDB->in('objr.ref_id', $ref_ids, false, 'integer');
927 }
928 $query = "
929 SELECT frmd.top_pk, objr.ref_id, objr.obj_id
930 FROM object_reference objr
931 INNER JOIN frm_data frmd ON frmd.top_frm_fk = objr.obj_id
932 WHERE $in
933 ";
934 $res = $ilDB->query($query);
935
936 // Prepare cache array
937 foreach($ref_ids as $ref_id)
938 {
939 self::$ref_id_to_forum_id_cache[$ref_id] = null;
940 }
941
942 while($row = $ilDB->fetchAssoc($res))
943 {
944 self::$obj_id_to_forum_id_cache[$row['obj_id']] = $row['top_pk'];
945 self::$ref_id_to_forum_id_cache[$row['ref_id']] = $row['top_pk'];
946 }
947 }
948
954 public static function lookupStatisticsByRefId($ref_id)
955 {
962 global $ilAccess, $ilUser, $ilDB, $ilSetting;
963
964 if(isset(self::$forum_statistics_cache[$ref_id]))
965 {
966 return self::$forum_statistics_cache[$ref_id];
967 }
968
969 $statistics = array(
970 'num_posts' => 0,
971 'num_unread_posts' => 0,
972 'num_new_posts' => 0
973 );
974
976 if(!$forumId)
977 {
978 self::$forum_statistics_cache[$ref_id] = $statistics;
979 return self::$forum_statistics_cache[$ref_id];
980 }
981
983 $is_post_activation_enabled = $objProperties->isPostActivationEnabled();
984
985 $act_clause = '';
986
987 if($is_post_activation_enabled && !$ilAccess->checkAccess('moderate_frm', '', $ref_id))
988 {
989 $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
990 }
991
992 $new_deadline = date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 7 * ($ilSetting->get('frm_store_new') ? $ilSetting->get('frm_store_new') : 8));
993
994 if(!$ilUser->isAnonymous())
995 {
996 $query = "
997 (SELECT COUNT(frm_posts.pos_pk) cnt
998 FROM frm_posts
999 INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
1000 WHERE frm_threads.thr_top_fk = %s $act_clause)
1001
1002 UNION ALL
1003
1004 (SELECT COUNT(DISTINCT(frm_user_read.post_id)) cnt
1005 FROM frm_user_read
1006 INNER JOIN frm_posts ON frm_user_read.post_id = frm_posts.pos_pk
1007 INNER JOIN frm_threads ON frm_threads.thr_pk = frm_posts.pos_thr_fk
1008 WHERE frm_user_read.usr_id = %s AND frm_posts.pos_top_fk = %s $act_clause)
1009 ";
1010
1011 $types = array('integer', 'integer', 'integer');
1012 $values = array($forumId, $ilUser->getId(), $forumId);
1013
1014 $forum_overview_setting = (int)$ilSetting::_lookupValue('frma', 'forum_overview');
1015 if($forum_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1016 {
1017 $news_types = array('integer', 'integer', 'integer', 'timestamp', 'integer');
1018 $news_values = array($ilUser->getId(), $ilUser->getId(), $forumId, $new_deadline, $ilUser->getId());
1019
1020 $query .= "
1021 UNION ALL
1022
1023 (SELECT COUNT(frm_posts.pos_pk) cnt
1024 FROM frm_posts
1025 LEFT JOIN frm_user_read ON (post_id = frm_posts.pos_pk AND frm_user_read.usr_id = %s)
1026 LEFT JOIN frm_thread_access ON (frm_thread_access.thread_id = frm_posts.pos_thr_fk AND frm_thread_access.usr_id = %s)
1027 WHERE frm_posts.pos_top_fk = %s
1028 AND ( (frm_posts.pos_update > frm_thread_access.access_old_ts)
1029 OR (frm_thread_access.access_old IS NULL AND frm_posts.pos_update > %s)
1030 )
1031 AND frm_posts.pos_author_id != %s
1032 AND frm_user_read.usr_id IS NULL $act_clause)";
1033
1034 $types = array_merge($types, $news_types);
1035 $values = array_merge($values, $news_values);
1036 }
1037
1038 $mapping = array_keys($statistics);
1039 $res = $ilDB->queryF(
1040 $query,
1041 $types,
1042 $values
1043 );
1044 for($i = 0; $i <= 2; $i++)
1045 {
1046 $row = $ilDB->fetchAssoc($res);
1047
1048 $statistics[$mapping[$i]] = (int)$row['cnt'];
1049
1050 if($i == 1)
1051 {
1052 // unread = all - read
1053 $statistics[$mapping[$i]] = $statistics[$mapping[$i - 1]] - $statistics[$mapping[$i]];
1054 }
1055 }
1056 }
1057 else
1058 {
1059 $query = "
1060 SELECT COUNT(frm_posts.pos_pk) cnt
1061 FROM frm_posts
1062 INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
1063 WHERE frm_threads.thr_top_fk = %s $act_clause
1064 ";
1065 $types = array('integer');
1066 $values = array($forumId);
1067 $res = $ilDB->queryF(
1068 $query,
1069 $types,
1070 $values
1071 );
1072 $row = $ilDB->fetchAssoc($res);
1073
1074 $statistics = array(
1075 'num_posts' => $row['cnt'],
1076 'num_unread_posts' => $row['cnt'],
1077 'num_new_posts' => $row['cnt']
1078 );
1079 }
1080
1081 self::$forum_statistics_cache[$ref_id] = $statistics;
1082
1083 return self::$forum_statistics_cache[$ref_id];
1084 }
1085
1091 public static function lookupLastPostByRefId($ref_id)
1092 {
1098 global $ilAccess, $ilUser, $ilDB;
1099
1100 if(isset(self::$forum_last_post_cache[$ref_id]))
1101 {
1102 return self::$forum_last_post_cache[$ref_id];
1103 }
1104
1106 if(!$forumId)
1107 {
1108 self::$forum_last_post_cache[$ref_id] = array();
1109 return self::$forum_last_post_cache[$ref_id];
1110 }
1111
1112 $act_clause = '';
1113 if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1114 {
1115 $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1116 }
1117
1118 $ilDB->setLimit(1, 0);
1119 $query = "
1120 SELECT *
1121 FROM frm_posts
1122 WHERE pos_top_fk = %s $act_clause
1123 ORDER BY pos_date DESC
1124 ";
1125 $res = $ilDB->queryF(
1126 $query,
1127 array('integer'),
1128 array($forumId)
1129 );
1130
1131 $data = $ilDB->fetchAssoc($res);
1132
1133 self::$forum_last_post_cache[$ref_id] = is_array($data) ? $data : array();
1134
1135 return self::$forum_last_post_cache[$ref_id];
1136 }
1137
1144 public static function getUserIdsOfLastPostsByRefIdAndThreadIds($ref_id, array $thread_ids)
1145 {
1151 global $ilUser, $ilAccess, $ilDB;
1152
1153 $act_clause = '';
1154 $act_inner_clause = '';
1155 if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1156 {
1157 $act_clause .= " AND (t1.pos_status = " . $ilDB->quote(1, "integer") . " OR t1.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1158 $act_inner_clause .= " AND (t3.pos_status = " . $ilDB->quote(1, "integer") . " OR t3.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1159 }
1160
1161 $in = $ilDB->in("t1.pos_thr_fk", $thread_ids, false, 'integer');
1162 $inner_in = $ilDB->in("t3.pos_thr_fk", $thread_ids, false, 'integer');
1163//@todo fix this query 'group by ... '
1164 $query = "
1165 SELECT t1.pos_display_user_id, t1.update_user
1166 FROM frm_posts t1
1167 INNER JOIN (
1168 SELECT t3.pos_thr_fk, MAX(t3.pos_date) pos_date
1169 FROM frm_posts t3
1170 WHERE $inner_in $act_inner_clause
1171 GROUP BY t3.pos_thr_fk
1172 ) t2 ON t2.pos_thr_fk = t1.pos_thr_fk AND t2.pos_date = t1.pos_date
1173 WHERE $in $act_clause
1174 GROUP BY t1.pos_thr_fk, t1.pos_display_user_id, t1.update_user
1175 ";
1177 $usr_ids = array();
1178
1179 $res = $ilDB->query($query);
1180 while($row = $ilDB->fetchAssoc($res))
1181 {
1182 if((int)$row['pos_display_user_id'])
1183 {
1184 $usr_ids[] = (int)$row['pos_display_user_id'];
1185 }
1186 if((int)$row['update_user'])
1187 {
1188 $usr_ids[] = (int)$row['update_user'];
1189 }
1190 }
1191
1192 return array_unique($usr_ids);
1193 }
1194
1195 public static function mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
1196 {
1197 global $ilDB;
1198
1199 $ilDB->update('frm_user_read',
1200 array('thread_id' => array('integer', $merge_target_thread_id)),
1201 array('thread_id' => array('integer',$merge_source_thread_id)));
1202 }
1203}
$_GET["client_id"]
$_SESSION["AccountId"]
static _write($a_type, $a_setting, $a_value, $a_user=0, $a_block_id=0)
Write setting to database.
static _getInstance($a_copy_id)
Get instance of copy wizard options.
This class handles all operations on files for the forum object.
Class ilForumModerators.
static getInstance($a_obj_id=0)
Class Forum core functions for forum.
_lookupPostMessage($a_id)
static _getDefaultVisibilityForRefId($a_ref_id)
Get default visibility for reference id.
Class ilObjForum.
static $obj_id_to_forum_id_cache
__construct($a_id=0, $a_call_by_reference=true)
Constructor @access public.
static $forum_last_post_cache
initDefaultRoles()
init default roles settings @access public
static $forum_statistics_cache
setThreadSorting($a_thr_pk, $a_sorting_value)
getDiskUsage()
Gets the disk usage of the object in bytes.
static lookupForumIdByRefId($ref_id)
static $ref_id_to_forum_id_cache
static mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
static lookupForumIdByObjId($obj_id)
static createDefaultRole($a_title, $a_description, $a_tpl_name, $a_ref_id)
Class ilObject Basic functions for all objects.
getOwner()
get object owner
cloneObject($a_target_id, $a_copy_id=0, $a_omit_tree=false)
Clone object permissions, put in tree ...
update()
update object in db
static _lookupObjectId($a_ref_id)
lookup object id
setPermissions($a_parent_ref)
set permissions of object
getRefId()
get reference id @access public
getDescription()
get object description
getId()
get object id @access public
getTitle()
get object title @access public
static now()
Return current timestamp in Y-m-d H:i:s format.
$data
global $ilBench
Definition: ilias.php:18
redirection script todo: (a better solution should control the processing via a xml file)
global $ilSetting
Definition: privfeed.php:40
global $ilDB
if(!is_array($argv)) $options
global $ilUser
Definition: imgupload.php:15