ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_once 'Services/Object/classes/class.ilObject.php';
5 require_once 'Modules/Forum/classes/class.ilForum.php';
6 require_once 'Modules/Forum/classes/class.ilFileDataForum.php';
7 require_once 'Modules/Forum/classes/class.ilForumProperties.php';
8 
18 class 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  public static 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  public static 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  public static 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($a_update_user_id = 0)
492  {
496  global $ilDB;
497 
498  if(!$a_update_user_id)
499  {
500  $a_update_user_id = $GLOBALS['DIC']['ilUser']->getId();
501  }
502 
503 
504  if(parent::update())
505  {
506 
507  $statement = $ilDB->manipulateF('
508  UPDATE frm_data
509  SET top_name = %s,
510  top_description = %s,
511  top_update = %s,
512  update_user = %s
513  WHERE top_frm_fk =%s',
514  array('text', 'text', 'timestamp', 'integer', 'integer'),
515  array(
516  $this->getTitle(),
517  $this->getDescription(),
518  date("Y-m-d H:i:s"),
519  (int) $a_update_user_id,
520  (int)$this->getId()
521  ));
522 
523  return true;
524  }
525 
526  return false;
527  }
528 
535  public function cloneObject($a_target_id, $a_copy_id = 0, $a_omit_tree = false)
536  {
540  global $ilDB;
541 
543  $new_obj = parent::cloneObject($a_target_id, $a_copy_id, $a_omit_tree);
544  $this->cloneAutoGeneratedRoles($new_obj);
545 
546  ilForumProperties::getInstance($this->getId())->copy($new_obj->getId());
547  $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
548  $topData = $this->Forum->getOneTopic();
549 
550  $ilDB->update('frm_data', array(
551  'top_name' => array('text', $topData['top_name']),
552  'top_description' => array('text', $topData['top_description']),
553  'top_num_posts' => array('integer', $topData['top_num_posts']),
554  'top_num_threads' => array('integer', $topData['top_num_threads']),
555  'top_last_post' => array('text', $topData['top_last_post']),
556  'top_date' => array('timestamp', $topData['top_date']),
557  'visits' => array('integer', $topData['visits']),
558  'top_update' => array('timestamp', $topData['top_update']),
559  'update_user' => array('integer', $topData['update_user']),
560  'top_usr_id' => array('integer', $topData['top_usr_id'])
561  ), array(
562  'top_frm_fk' => array('integer', $new_obj->getId())
563  ));
564 
565  // read options
566  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
567 
568  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
569  $options = $cwo->getOptions($this->getRefId());
570 
571  $options['threads'] = $this->Forum->_getThreads($this->getId());
572 
573  // Generate starting threads
574  include_once('Modules/Forum/classes/class.ilFileDataForum.php');
575 
576  $new_frm = $new_obj->Forum;
577  $new_frm->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($new_obj->getId()));
578 
579  $new_frm->setForumId($new_obj->getId());
580  $new_frm->setForumRefId($new_obj->getRefId());
581 
582  $new_topic = $new_frm->getOneTopic();
583  foreach($options['threads'] as $thread_id => $thread_subject)
584  {
585  $this->Forum->setMDB2WhereCondition('thr_pk = %s ', array('integer'), array($thread_id));
586 
587  $old_thread = $this->Forum->getOneThread();
588 
589  $old_post_id = $this->Forum->getFirstPostByThread($old_thread['thr_pk']);
590  $old_post = $this->Forum->getOnePost($old_post_id);
591 
592  $newThread = new ilForumTopic(0, true, true);
593  $newThread->setSticky($old_thread['is_sticky']);
594  $newThread->setForumId($new_topic['top_pk']);
595  $newThread->setThrAuthorId($old_thread['thr_author_id']);
596  $newThread->setDisplayUserId($old_thread['thr_display_user_id']);
597  $newThread->setSubject($old_thread['thr_subject']);
598  $newThread->setUserAlias($old_thread['thr_usr_alias']);
599  $newThread->setCreateDate($old_thread['thr_date']);
600 
601  $newPostId = $new_frm->generateThread(
602  $newThread,
603  ilForum::_lookupPostMessage($old_post_id),
604  $old_post['notify'],
605  0
606  );
607 
608  $old_forum_files = new ilFileDataForum($this->getId(), $old_post_id);
609  $old_forum_files->ilClone($new_obj->getId(), $newPostId);
610  }
611 
612  return $new_obj;
613  }
614 
621  public function cloneAutoGeneratedRoles($new_obj)
622  {
627  global $ilLog, $rbacadmin;
628 
629  $moderator = ilObjForum::_lookupModeratorRole($this->getRefId());
630  $new_moderator = ilObjForum::_lookupModeratorRole($new_obj->getRefId());
631 
632  if(!$moderator || !$new_moderator || !$this->getRefId() || !$new_obj->getRefId())
633  {
634  $ilLog->write(__METHOD__ . ' : Error cloning auto generated role: il_frm_moderator');
635  }
636  $rbacadmin->copyRolePermissions($moderator, $this->getRefId(), $new_obj->getRefId(), $new_moderator, true);
637  $ilLog->write(__METHOD__ . ' : Finished copying of role il_frm_moderator.');
638 
639  include_once './Modules/Forum/classes/class.ilForumModerators.php';
640  $obj_mods = new ilForumModerators($this->getRefId());
641 
642  $old_mods = $obj_mods->getCurrentModerators();
643  foreach($old_mods as $user_id)
644  {
645  // The object owner is already member of the moderator role when this method is called
646  // Since the new static caches are introduced with ILIAS 5.0, a database error occurs if we try to assign the user here.
647  if($this->getOwner() != $user_id)
648  {
649  $rbacadmin->assignUser($new_moderator, $user_id);
650  }
651  }
652  }
653 
659  public function delete()
660  {
664  global $ilDB;
665 
666  // always call parent delete function first!!
667  if(!parent::delete())
668  {
669  return false;
670  }
671 
672  // delete attachments
673  $tmp_file_obj = new ilFileDataForum($this->getId());
674  $tmp_file_obj->delete();
675  unset($tmp_file_obj);
676 
677  $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
678 
679  $topData = $this->Forum->getOneTopic();
680 
681  $threads = $this->Forum->getAllThreads($topData['top_pk']);
682  foreach($threads['items'] as $thread)
683  {
684  $thread_ids_to_delete[$thread->getId()] = $thread->getId();
685  }
686 
687  // delete tree
688  $ilDB->manipulate('DELETE FROM frm_posts_tree WHERE '. $ilDB->in('thr_fk', $thread_ids_to_delete, false, 'integer'));
689 
690  // delete posts
691  $ilDB->manipulate('DELETE FROM frm_posts WHERE '. $ilDB->in('pos_thr_fk', $thread_ids_to_delete, false, 'integer'));
692 
693  // delete threads
694  $ilDB->manipulate('DELETE FROM frm_threads WHERE '. $ilDB->in('thr_pk', $thread_ids_to_delete, false, 'integer'));
695 
696  $obj_id = array($this->getId());
697  // delete forum
698  $ilDB->manipulateF('DELETE FROM frm_data WHERE top_frm_fk = %s',
699  array('integer'), $obj_id);
700 
701  // delete settings
702  $ilDB->manipulateF('DELETE FROM frm_settings WHERE obj_id = %s',
703  array('integer'), $obj_id);
704 
705  // delete read infos
706  $ilDB->manipulateF('DELETE FROM frm_user_read WHERE obj_id = %s',
707  array('integer'), $obj_id);
708 
709  // delete thread access entries
710  $ilDB->manipulateF('DELETE FROM frm_thread_access WHERE obj_id = %s',
711  array('integer'), $obj_id);
712 
713  //delete thread notifications
714  $ilDB->manipulate('DELETE FROM frm_notification WHERE '. $ilDB->in('thread_id', $thread_ids_to_delete, false, 'integer'));
715 
716  //delete forum notifications
717  $ilDB->manipulateF('DELETE FROM frm_notification WHERE frm_id = %s', array('integer'), $obj_id);
718 
719  // delete posts_deleted entries
720  $ilDB->manipulateF('DELETE FROM frm_posts_deleted WHERE obj_id = %s', array('integer'), $obj_id);
721 
722  //delete drafts
723  $this->deleteDraftsByForumId((int)$topData['top_pk']);
724 
725  return true;
726  }
727 
731  private function deleteDraftsByForumId($forum_id)
732  {
733  global $ilDB;
734  $res = $ilDB->queryF('SELECT draft_id FROM frm_posts_drafts WHERE forum_id = %s',
735  array('integer'), array((int)$forum_id));
736 
737  $draft_ids = array();
738  while($row = $ilDB->fetchAssoc($res))
739  {
740  $draft_ids[] = $row['draft_id'];
741  }
742 
743  if(count($draft_ids) > 0)
744  {
745  require_once 'Modules/Forum/classes/class.ilForumDraftsHistory.php';
746  $historyObj = new ilForumDraftsHistory();
747  $historyObj->deleteHistoryByDraftIds($draft_ids);
748 
749  require_once 'Modules/Forum/classes/class.ilForumPostDraft.php';
750  $draftObj = new ilForumPostDraft();
751  $draftObj->deleteDraftsByDraftIds($draft_ids);
752  }
753  }
754 
760  public function initDefaultRoles()
761  {
762  include_once './Services/AccessControl/classes/class.ilObjRole.php';
764  'il_frm_moderator_'.$this->getRefId(),
765  "Moderator of forum obj_no.".$this->getId(),
766  'il_frm_moderator',
767  $this->getRefId()
768  );
769  return array();
770  }
771 
779  public static function _lookupModeratorRole($a_ref_id)
780  {
784  global $ilDB;
785 
786  $mod_title = 'il_frm_moderator_' . $a_ref_id;
787 
788  $res = $ilDB->queryf('
789  SELECT * FROM object_data WHERE title = %s',
790  array('text'), array($mod_title));
791 
792  while($row = $ilDB->fetchObject($res))
793  {
794  return $row->obj_id;
795  }
796  return 0;
797  }
798 
799 
800  public function createSettings()
801  {
802  // news settings (public notifications yes/no)
803  include_once("./Services/News/classes/class.ilNewsItem.php");
804  $default_visibility = ilNewsItem::_getDefaultVisibilityForRefId($_GET["ref_id"]);
805  if($default_visibility == "public")
806  {
807  ilBlockSetting::_write("news", "public_notifications", 1, 0, $this->getId());
808  }
809 
810  return true;
811  }
812 
813  public function saveData($a_roles = array())
814  {
819  global $ilUser, $ilDB;
820 
821  $nextId = $ilDB->nextId('frm_data');
822 
823  $top_data = array(
824  'top_frm_fk' => $this->getId(),
825  'top_name' => $this->getTitle(),
826  'top_description' => $this->getDescription(),
827  'top_num_posts' => 0,
828  'top_num_threads' => 0,
829  'top_last_post' => NULL,
830  'top_mods' => !is_numeric($a_roles[0]) ? 0 : $a_roles[0],
831  'top_usr_id' => $ilUser->getId(),
832  'top_date' => ilUtil::now()
833  );
834 
835  $ilDB->manipulateF('
836  INSERT INTO frm_data
837  (
838  top_pk,
839  top_frm_fk,
840  top_name,
841  top_description,
842  top_num_posts,
843  top_num_threads,
844  top_last_post,
845  top_mods,
846  top_date,
847  top_usr_id
848  )
849  VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
850  array('integer', 'integer', 'text', 'text', 'integer', 'integer', 'text', 'integer', 'timestamp', 'integer'),
851  array(
852  $nextId,
853  $top_data['top_frm_fk'],
854  $top_data['top_name'],
855  $top_data['top_description'],
856  $top_data['top_num_posts'],
857  $top_data['top_num_threads'],
858  $top_data['top_last_post'],
859  $top_data['top_mods'],
860  $top_data['top_date'],
861  $top_data['top_usr_id']
862  ));
863  }
864 
865  public function setThreadSorting($a_thr_pk, $a_sorting_value)
866  {
867  global $ilDB;
868 
869  $ilDB->update('frm_threads',
870  array('thread_sorting' => array('integer',$a_sorting_value)),
871  array('thr_pk' => array('integer', $a_thr_pk)));
872  }
873 
874 
880  public static function lookupForumIdByObjId($obj_id)
881  {
882  if(array_key_exists($obj_id, self::$obj_id_to_forum_id_cache))
883  {
884  return (int)self::$obj_id_to_forum_id_cache[$obj_id];
885  }
886 
887  self::preloadForumIdsByObjIds(array($obj_id));
888 
889  return (int)self::$obj_id_to_forum_id_cache[$obj_id];
890  }
891 
897  public static function lookupForumIdByRefId($ref_id)
898  {
899  if(array_key_exists($ref_id, self::$ref_id_to_forum_id_cache))
900  {
901  return (int)self::$ref_id_to_forum_id_cache[$ref_id];
902  }
903 
904  self::preloadForumIdsByRefIds(array($ref_id));
905 
906  return (int)self::$ref_id_to_forum_id_cache[$ref_id];
907  }
908 
913  public static function preloadForumIdsByObjIds(array $obj_ids)
914  {
918  global $ilDB;
919 
920  if(count($obj_ids) == 1)
921  {
922  $in = " objr.obj_id = " . $ilDB->quote(current($obj_ids), 'integer') . " ";
923  }
924  else
925  {
926  $in = $ilDB->in('objr.obj_id', $obj_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($obj_ids as $obj_id)
938  {
939  self::$obj_id_to_forum_id_cache[$obj_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 
953  public static function preloadForumIdsByRefIds(array $ref_ids)
954  {
958  global $ilDB;
959 
960  if(count($ref_ids) == 1)
961  {
962  $in = " objr.ref_id = " . $ilDB->quote(current($ref_ids), 'integer') . " ";
963  }
964  else
965  {
966  $in = $ilDB->in('objr.ref_id', $ref_ids, false, 'integer');
967  }
968  $query = "
969  SELECT frmd.top_pk, objr.ref_id, objr.obj_id
970  FROM object_reference objr
971  INNER JOIN frm_data frmd ON frmd.top_frm_fk = objr.obj_id
972  WHERE $in
973  ";
974  $res = $ilDB->query($query);
975 
976  // Prepare cache array
977  foreach($ref_ids as $ref_id)
978  {
979  self::$ref_id_to_forum_id_cache[$ref_id] = null;
980  }
981 
982  while($row = $ilDB->fetchAssoc($res))
983  {
984  self::$obj_id_to_forum_id_cache[$row['obj_id']] = $row['top_pk'];
985  self::$ref_id_to_forum_id_cache[$row['ref_id']] = $row['top_pk'];
986  }
987  }
988 
994  public static function lookupStatisticsByRefId($ref_id)
995  {
1002  global $ilAccess, $ilUser, $ilDB, $ilSetting;
1003 
1004  if(isset(self::$forum_statistics_cache[$ref_id]))
1005  {
1006  return self::$forum_statistics_cache[$ref_id];
1007  }
1008 
1009  $statistics = array(
1010  'num_posts' => 0,
1011  'num_unread_posts' => 0,
1012  'num_new_posts' => 0
1013  );
1014 
1015  $forumId = self::lookupForumIdByRefId($ref_id);
1016  if(!$forumId)
1017  {
1018  self::$forum_statistics_cache[$ref_id] = $statistics;
1019  return self::$forum_statistics_cache[$ref_id];
1020  }
1021 
1022  $objProperties = ilForumProperties::getInstance(ilObject::_lookupObjectId($ref_id));
1023  $is_post_activation_enabled = $objProperties->isPostActivationEnabled();
1024 
1025  $act_clause = '';
1026 
1027  if($is_post_activation_enabled && !$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1028  {
1029  $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1030  }
1031 
1032  $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));
1033 
1034  if(!$ilUser->isAnonymous())
1035  {
1036  $query = "
1037  (SELECT COUNT(frm_posts.pos_pk) cnt
1038  FROM frm_posts
1039  INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
1040  WHERE frm_threads.thr_top_fk = %s $act_clause)
1041 
1042  UNION ALL
1043 
1044  (SELECT COUNT(DISTINCT(frm_user_read.post_id)) cnt
1045  FROM frm_user_read
1046  INNER JOIN frm_posts ON frm_user_read.post_id = frm_posts.pos_pk
1047  INNER JOIN frm_threads ON frm_threads.thr_pk = frm_posts.pos_thr_fk
1048  WHERE frm_user_read.usr_id = %s AND frm_posts.pos_top_fk = %s $act_clause)
1049  ";
1050 
1051  $types = array('integer', 'integer', 'integer');
1052  $values = array($forumId, $ilUser->getId(), $forumId);
1053 
1054  $forum_overview_setting = (int)$ilSetting::_lookupValue('frma', 'forum_overview');
1055  if($forum_overview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
1056  {
1057  $news_types = array('integer', 'integer', 'integer', 'timestamp', 'integer');
1058  $news_values = array($ilUser->getId(), $ilUser->getId(), $forumId, $new_deadline, $ilUser->getId());
1059 
1060  $query .= "
1061  UNION ALL
1062 
1063  (SELECT COUNT(frm_posts.pos_pk) cnt
1064  FROM frm_posts
1065  LEFT JOIN frm_user_read ON (post_id = frm_posts.pos_pk AND frm_user_read.usr_id = %s)
1066  LEFT JOIN frm_thread_access ON (frm_thread_access.thread_id = frm_posts.pos_thr_fk AND frm_thread_access.usr_id = %s)
1067  WHERE frm_posts.pos_top_fk = %s
1068  AND ( (frm_posts.pos_update > frm_thread_access.access_old_ts)
1069  OR (frm_thread_access.access_old IS NULL AND frm_posts.pos_update > %s)
1070  )
1071  AND frm_posts.pos_author_id != %s
1072  AND frm_user_read.usr_id IS NULL $act_clause)";
1073 
1074  $types = array_merge($types, $news_types);
1075  $values = array_merge($values, $news_values);
1076  }
1077 
1078  $mapping = array_keys($statistics);
1079  $res = $ilDB->queryF(
1080  $query,
1081  $types,
1082  $values
1083  );
1084  for($i = 0; $i <= 2; $i++)
1085  {
1086  $row = $ilDB->fetchAssoc($res);
1087 
1088  $statistics[$mapping[$i]] = (int)$row['cnt'];
1089 
1090  if($i == 1)
1091  {
1092  // unread = all - read
1093  $statistics[$mapping[$i]] = $statistics[$mapping[$i - 1]] - $statistics[$mapping[$i]];
1094  }
1095  }
1096  }
1097  else
1098  {
1099  $query = "
1100  SELECT COUNT(frm_posts.pos_pk) cnt
1101  FROM frm_posts
1102  INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
1103  WHERE frm_threads.thr_top_fk = %s $act_clause
1104  ";
1105  $types = array('integer');
1106  $values = array($forumId);
1107  $res = $ilDB->queryF(
1108  $query,
1109  $types,
1110  $values
1111  );
1112  $row = $ilDB->fetchAssoc($res);
1113 
1114  $statistics = array(
1115  'num_posts' => $row['cnt'],
1116  'num_unread_posts' => $row['cnt'],
1117  'num_new_posts' => $row['cnt']
1118  );
1119  }
1120 
1121  self::$forum_statistics_cache[$ref_id] = $statistics;
1122 
1123  return self::$forum_statistics_cache[$ref_id];
1124  }
1125 
1131  public static function lookupLastPostByRefId($ref_id)
1132  {
1138  global $ilAccess, $ilUser, $ilDB;
1139 
1140  if(isset(self::$forum_last_post_cache[$ref_id]))
1141  {
1142  return self::$forum_last_post_cache[$ref_id];
1143  }
1144 
1145  $forumId = self::lookupForumIdByRefId($ref_id);
1146  if(!$forumId)
1147  {
1148  self::$forum_last_post_cache[$ref_id] = array();
1149  return self::$forum_last_post_cache[$ref_id];
1150  }
1151 
1152  $act_clause = '';
1153  if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1154  {
1155  $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1156  }
1157 
1158  $ilDB->setLimit(1, 0);
1159  $query = "
1160  SELECT *
1161  FROM frm_posts
1162  WHERE pos_top_fk = %s $act_clause
1163  ORDER BY pos_date DESC
1164  ";
1165  $res = $ilDB->queryF(
1166  $query,
1167  array('integer'),
1168  array($forumId)
1169  );
1170 
1171  $data = $ilDB->fetchAssoc($res);
1172 
1173  self::$forum_last_post_cache[$ref_id] = is_array($data) ? $data : array();
1174 
1175  return self::$forum_last_post_cache[$ref_id];
1176  }
1177 
1184  public static function getUserIdsOfLastPostsByRefIdAndThreadIds($ref_id, array $thread_ids)
1185  {
1191  global $ilUser, $ilAccess, $ilDB;
1192 
1193  $act_clause = '';
1194  $act_inner_clause = '';
1195  if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1196  {
1197  $act_clause .= " AND (t1.pos_status = " . $ilDB->quote(1, "integer") . " OR t1.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1198  $act_inner_clause .= " AND (t3.pos_status = " . $ilDB->quote(1, "integer") . " OR t3.pos_author_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1199  }
1200 
1201  $in = $ilDB->in("t1.pos_thr_fk", $thread_ids, false, 'integer');
1202  $inner_in = $ilDB->in("t3.pos_thr_fk", $thread_ids, false, 'integer');
1203 //@todo fix this query 'group by ... '
1204  $query = "
1205  SELECT t1.pos_display_user_id, t1.update_user
1206  FROM frm_posts t1
1207  INNER JOIN (
1208  SELECT t3.pos_thr_fk, MAX(t3.pos_date) pos_date
1209  FROM frm_posts t3
1210  WHERE $inner_in $act_inner_clause
1211  GROUP BY t3.pos_thr_fk
1212  ) t2 ON t2.pos_thr_fk = t1.pos_thr_fk AND t2.pos_date = t1.pos_date
1213  WHERE $in $act_clause
1214  GROUP BY t1.pos_thr_fk, t1.pos_display_user_id, t1.update_user
1215  ";
1217  $usr_ids = array();
1218 
1219  $res = $ilDB->query($query);
1220  while($row = $ilDB->fetchAssoc($res))
1221  {
1222  if((int)$row['pos_display_user_id'])
1223  {
1224  $usr_ids[] = (int)$row['pos_display_user_id'];
1225  }
1226  if((int)$row['update_user'])
1227  {
1228  $usr_ids[] = (int)$row['update_user'];
1229  }
1230  }
1231 
1232  return array_unique($usr_ids);
1233  }
1234 
1235  public static function mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
1236  {
1237  global $ilDB;
1238 
1239  $ilDB->update('frm_user_read',
1240  array('thread_id' => array('integer', $merge_target_thread_id)),
1241  array('thread_id' => array('integer',$merge_source_thread_id)));
1242  }
1243 }
static $forum_last_post_cache
Class ilForumPostDraft.
static mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
Class Forum core functions for forum.
static _write($a_type, $a_setting, $a_value, $a_user=0, $a_block_id=0)
Write setting to database.
static _lookupPostMessage($a_id)
setThreadSorting($a_thr_pk, $a_sorting_value)
static lookupForumIdByObjId($obj_id)
cloneObject($a_target_id, $a_copy_id=0, $a_omit_tree=false)
Clone object permissions, put in tree ...
$_GET["client_id"]
Class ilObject Basic functions for all objects.
static lookupForumIdByRefId($ref_id)
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static $obj_id_to_forum_id_cache
initDefaultRoles()
init default roles settings public
deleteDraftsByForumId($forum_id)
getOwner()
get object owner
static now()
Return current timestamp in Y-m-d H:i:s format.
static createDefaultRole($a_title, $a_description, $a_tpl_name, $a_ref_id)
Class ilForumDraftHistory.
static _lookupObjectId($a_ref_id)
lookup object id
static getInstance($a_obj_id=0)
static _getInstance($a_copy_id)
Get instance of copy wizard options.
if(!is_array($argv)) $options
getId()
get object id public
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
getTitle()
get object title public
getDescription()
get object description
$ilUser
Definition: imgupload.php:18
redirection script todo: (a better solution should control the processing via a xml file) ...
Create styles array
The data for the language used.
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
static $forum_statistics_cache
global $ilSetting
Definition: privfeed.php:17
This class handles all operations on files for the forum object.
global $ilBench
Definition: ilias.php:18
global $ilDB
static _getDefaultVisibilityForRefId($a_ref_id)
Get default visibility for reference id.
getRefId()
get reference id public
__construct($a_id=0, $a_call_by_reference=true)
Constructor public.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static $ref_id_to_forum_id_cache
update()
update object in db
Class ilObjForum.
Class ilForumModerators.
setPermissions($a_parent_ref)
set permissions of object
getDiskUsage()
Gets the disk usage of the object in bytes.