ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
84  function getDiskUsage()
85  {
86  require_once("./Modules/File/classes/class.ilObjFileAccess.php");
87  return ilObjForumAccess::_lookupDiskUsage($this->id);
88  }
89 
90  public static function _lookupThreadSubject($a_thread_id)
91  {
96  global $ilDB;
97 
98  $res = $ilDB->queryf('
99  SELECT thr_subject FROM frm_threads WHERE thr_pk = %s',
100  array('integer'), array($a_thread_id));
101 
102  while($row = $ilDB->fetchObject($res))
103  {
104  return $row->thr_subject;
105  }
106  return '';
107  }
108 
109  // METHODS FOR UN-READ STATUS
110  public function getCountUnread($a_usr_id, $a_thread_id = 0)
111  {
112 // return $this->_getCountUnread($this->getId(),$a_usr_id,$a_thread_id);
113 // }
114 //
115 // function _getCountUnread($a_frm_id, $a_usr_id,$a_thread_id = 0)
116 // {
117 
118  $a_frm_id = $this->getId();
119 
123  global $ilBench, $ilDB;
124 
125  $ilBench->start("Forum", 'getCountRead');
126  if(!$a_thread_id)
127  {
128  // Get topic_id
129  $res = $ilDB->queryf('
130  SELECT top_pk FROM frm_data WHERE top_frm_fk = %s',
131  array('integer'), array($a_frm_id));
132 
133 
134  while($row = $ilDB->fetchObject($res))
135  {
136  $topic_id = $row->top_pk;
137  }
138 
139  // Get number of posts
140  $res = $ilDB->queryf('
141  SELECT COUNT(pos_pk) num_posts FROM frm_posts
142  WHERE pos_top_fk = %s',
143  array('integer'), array($topic_id));
144 
145  while($row = $ilDB->fetchObject($res))
146  {
147  $num_posts = $row->num_posts;
148  }
149 
150  $res = $ilDB->queryf('
151  SELECT COUNT(post_id) count_read FROM frm_user_read
152  WHERE obj_id = %s
153  AND usr_id = %s',
154  array('integer', 'integer'), array($a_frm_id, $a_usr_id));
155 
156  while($row = $ilDB->fetchObject($res))
157  {
158  $count_read = $row->count_read;
159  }
160  $unread = $num_posts - $count_read;
161 
162  $ilBench->stop("Forum", 'getCountRead');
163  return $unread > 0 ? $unread : 0;
164  }
165  else
166  {
167  $res = $ilDB->queryf('
168  SELECT COUNT(pos_pk) num_posts FROM frm_posts
169  WHERE pos_thr_fk = %s',
170  array('integer'), array($a_thread_id));
171 
172  $row = $ilDB->fetchObject($res);
173  $num_posts = $row->num_posts;
174 
175  $res = $ilDB->queryf('
176  SELECT COUNT(post_id) count_read FROM frm_user_read
177  WHERE obj_id = %s
178  AND usr_id = %s
179  AND thread_id = %s',
180  array('integer', 'integer', 'integer'), array($a_frm_id, $a_frm_id, $a_thread_id));
181 
182  $row = $ilDB->fetchObject($res);
183  $count_read = $row->count_read;
184 
185  $unread = $num_posts - $count_read;
186 
187  $ilBench->stop("Forum", 'getCountRead');
188  return $unread > 0 ? $unread : 0;
189  }
190  $ilBench->stop("Forum", 'getCountRead');
191  return false;
192  }
193 
194 
195  public function markThreadRead($a_usr_id, $a_thread_id)
196  {
200  global $ilDB;
201 
202  // Get all post ids
203  $res = $ilDB->queryf('
204  SELECT * FROM frm_posts WHERE pos_thr_fk = %s',
205  array('integer'), array($a_thread_id));
206 
207  while($row = $ilDB->fetchObject($res))
208  {
209  $this->markPostRead($a_usr_id, $a_thread_id, $row->pos_pk);
210  }
211  return true;
212  }
213 
214  public function markAllThreadsRead($a_usr_id)
215  {
219  global $ilDB;
220 
221  $res = $ilDB->queryf('
222  SELECT * FROM frm_data, frm_threads
223  WHERE top_frm_fk = %s
224  AND top_pk = thr_top_fk',
225  array('integer'), array($this->getId()));
226 
227  while($row = $ilDB->fetchObject($res))
228  {
229  $this->markThreadRead($a_usr_id, $row->thr_pk);
230  }
231 
232  return true;
233  }
234 
235 
236  public function markPostRead($a_usr_id, $a_thread_id, $a_post_id)
237  {
241  global $ilDB;
242 
243  // CHECK IF ENTRY EXISTS
244  $res = $ilDB->queryf('
245  SELECT * FROM frm_user_read
246  WHERE usr_id = %s
247  AND obj_id = %s
248  AND thread_id = %s
249  AND post_id = %s',
250  array('integer', 'integer', 'integer', 'integer'),
251  array($a_usr_id, $this->getId(), $a_thread_id, $a_post_id));
252 
253  if($ilDB->numRows($res))
254  {
255  return true;
256  }
257 
258  $res = $ilDB->manipulateF('
259  INSERT INTO frm_user_read
260  ( usr_id,
261  obj_id,
262  thread_id,
263  post_id
264  )
265  VALUES (%s,%s,%s,%s)',
266  array('integer', 'integer', 'integer', 'integer'),
267  array($a_usr_id, $this->getId(), $a_thread_id, $a_post_id));
268 
269  return true;
270  }
271 
272  public function markPostUnread($a_user_id, $a_post_id)
273  {
277  global $ilDB;
278 
279  $res = $ilDB->manipulateF('
280  DELETE FROM frm_user_read
281  WHERE usr_id = %s
282  AND post_id = %s',
283  array('integer', 'integer'),
284  array($a_user_id, $a_post_id));
285  }
286 
287  public function isRead($a_usr_id, $a_post_id)
288  {
292  global $ilDB;
293 
294  $res = $ilDB->queryf('
295  SELECT * FROM frm_user_read
296  WHERE usr_id = %s
297  AND post_id = %s',
298  array('integer', 'integer'),
299  array($a_usr_id, $a_post_id));
300 
301  return $ilDB->numRows($res) ? true : false;
302  }
303 
304  public function updateLastAccess($a_usr_id, $a_thread_id)
305  {
309  global $ilDB;
310 
311  $res = $ilDB->queryf('
312  SELECT * FROM frm_thread_access
313  WHERE usr_id = %s
314  AND obj_id = %s
315  AND thread_id = %s',
316  array('integer', 'integer', 'integer'),
317  array($a_usr_id, $this->getId(), $a_thread_id));
318  $data = $ilDB->fetchAssoc($res);
319 
320  $ilDB->replace(
321  'frm_thread_access',
322  array(
323  'usr_id' => array('integer', $a_usr_id),
324  'obj_id' => array('integer', $this->getId()),
325  'thread_id' => array('integer', $a_thread_id)
326  ),
327  array(
328  'access_last' => array('integer', time()),
329  'access_old' => array('integer', isset($data['access_old']) ? $data['access_old'] : 0),
330  'access_old_ts' => array('timestamp', $data['access_old_ts'])
331  )
332  );
333 
334  return true;
335  }
336 
341  public static function _updateOldAccess($a_usr_id)
342  {
347  global $ilDB, $ilias;
348 
349  $ilDB->manipulateF('
350  UPDATE frm_thread_access
351  SET access_old = access_last
352  WHERE usr_id = %s',
353  array('integer'), array($a_usr_id));
354 
355  $set = $ilDB->query("SELECT * FROM frm_thread_access " .
356  " WHERE usr_id = " . $ilDB->quote($a_usr_id, "integer")
357  );
358  while($rec = $ilDB->fetchAssoc($set))
359  {
360  $ilDB->manipulate("UPDATE frm_thread_access SET " .
361  " access_old_ts = " . $ilDB->quote(date('Y-m-d H:i:s', $rec["access_old"]), "timestamp") .
362  " WHERE usr_id = " . $ilDB->quote($rec["usr_id"], "integer") .
363  " AND obj_id = " . $ilDB->quote($rec["obj_id"], "integer") .
364  " AND thread_id = " . $ilDB->quote($rec["thread_id"], "integer")
365  );
366  }
367 
368  $new_deadline = time() - 60 * 60 * 24 * 7 * ($ilias->getSetting('frm_store_new') ?
369  $ilias->getSetting('frm_store_new') :
370  8);
371 
372  $ilDB->manipulateF('
373  DELETE FROM frm_thread_access WHERE access_last < %s',
374  array('integer'), array($new_deadline));
375  }
376 
377  function _deleteUser($a_usr_id)
378  {
382  global $ilDB;
383 
384  $data = array($a_usr_id);
385 
386  $res = $ilDB->manipulateF('
387  DELETE FROM frm_user_read WHERE usr_id = %s',
388  array('integer'), $data
389  );
390 
391  $res = $ilDB->manipulateF('
392  DELETE FROM frm_thread_access WHERE usr_id = %s',
393  array('integer'), $data
394  );
395 
396  // delete notifications of deleted user
397  $ilDB->manipulateF('
398  DELETE FROM frm_notification WHERE user_id = %s',
399  array('integer'), $data);
400 
401  return true;
402  }
403 
404 
405  function _deleteReadEntries($a_post_id)
406  {
410  global $ilDB;
411 
412  $statement = $ilDB->manipulateF('
413  DELETE FROM frm_user_read WHERE post_id = %s',
414  array('integer'), array($a_post_id));
415 
416  return true;
417  }
418 
419  function _deleteAccessEntries($a_thread_id)
420  {
424  global $ilDB;
425 
426  $statement = $ilDB->manipulateF('
427  DELETE FROM frm_thread_access WHERE thread_id = %s',
428  array('integer'), array($a_thread_id));
429 
430  return true;
431  }
432 
437  function update()
438  {
442  global $ilDB;
443 
444  if(parent::update())
445  {
446 
447  $statement = $ilDB->manipulateF('
448  UPDATE frm_data
449  SET top_name = %s,
450  top_description = %s,
451  top_update = %s,
452  update_user = %s
453  WHERE top_frm_fk =%s',
454  array('text', 'text', 'timestamp', 'integer', 'integer'),
455  array(
456  $this->getTitle(),
457  $this->getDescription(),
458  date("Y-m-d H:i:s"),
459  (int)$_SESSION["AccountId"],
460  (int)$this->getId()
461  ));
462 
463  return true;
464  }
465 
466  return false;
467  }
468 
475  public function cloneObject($a_target_id, $a_copy_id = 0)
476  {
480  global $ilDB;
481 
482  $new_obj = parent::cloneObject($a_target_id, $a_copy_id);
483  $this->cloneAutoGeneratedRoles($new_obj);
484 
485  ilForumProperties::getInstance($this->getId())->copy($new_obj->getId());
486  $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
487 
488  $topData = $this->Forum->getOneTopic();
489 
490  $nextId = $ilDB->nextId('frm_data');
491 
492  $statement = $ilDB->insert('frm_data', array(
493  'top_pk' => array('integer', $nextId),
494  'top_frm_fk' => array('integer', $new_obj->getId()),
495  'top_name' => array('text', $topData['top_name']),
496  'top_description' => array('text', $topData['top_description']),
497  'top_num_posts' => array('integer', $topData['top_num_posts']),
498  'top_num_threads' => array('integer', $topData['top_num_threads']),
499  'top_last_post' => array('text', $topData['top_last_post']),
500  'top_mods' => array('integer', !is_numeric($topData['top_mods']) ? 0 : $topData['top_mods']),
501  'top_date' => array('timestamp', $topData['top_date']),
502  'visits' => array('integer', $topData['visits']),
503  'top_update' => array('timestamp', $topData['top_update']),
504  'update_user' => array('integer', $topData['update_user']),
505  'top_usr_id' => array('integer', $topData['top_usr_id'])
506  ));
507 
508  // read options
509  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
510 
511  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
512  $options = $cwo->getOptions($this->getRefId());
513 
514  $options['threads'] = $this->Forum->_getThreads($this->getId());
515 
516  // Generate starting threads
517  include_once('Modules/Forum/classes/class.ilFileDataForum.php');
518 
519  $new_frm = $new_obj->Forum;
520  $new_frm->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($new_obj->getId()));
521 
522  $new_frm->setForumId($new_obj->getId());
523  $new_frm->setForumRefId($new_obj->getRefId());
524 
525  $new_topic = $new_frm->getOneTopic();
526  foreach($options['threads'] as $thread_id=> $thread_subject)
527  {
528  $this->Forum->setMDB2WhereCondition('thr_pk = %s ', array('integer'), array($thread_id));
529 
530  $old_thread = $this->Forum->getOneThread();
531 
532  $old_post_id = $this->Forum->getFirstPostByThread($old_thread['thr_pk']);
533  $old_post = $this->Forum->getOnePost($old_post_id);
534 
535  // Now create new thread and first post
536  $new_post = $new_frm->generateThread($new_topic['top_pk'],
537  $old_thread['thr_usr_id'],
538  $old_thread['thr_subject'],
539  ilForum::_lookupPostMessage($old_post_id),
540  $old_post['notify'],
541  0,
542  $old_thread['thr_usr_alias'],
543  $old_thread['thr_date']);
544  // Copy attachments
545  $old_forum_files = new ilFileDataForum($this->getId(), $old_post_id);
546  $old_forum_files->ilClone($new_obj->getId(), $new_post);
547  }
548 
549  return $new_obj;
550  }
551 
558  public function cloneAutoGeneratedRoles($new_obj)
559  {
564  global $ilLog, $rbacadmin, $rbacreview;
565 
566  $moderator = ilObjForum::_lookupModeratorRole($this->getRefId());
567  $new_moderator = ilObjForum::_lookupModeratorRole($new_obj->getRefId());
568  $source_rolf = $rbacreview->getRoleFolderIdOfObject($this->getRefId());
569  $target_rolf = $rbacreview->getRoleFolderIdOfObject($new_obj->getRefId());
570 
571  if(!$moderator || !$new_moderator || !$source_rolf || !$target_rolf)
572  {
573  $ilLog->write(__METHOD__ . ' : Error cloning auto generated role: il_frm_moderator');
574  }
575  $rbacadmin->copyRolePermissions($moderator, $source_rolf, $target_rolf, $new_moderator, true);
576  $ilLog->write(__METHOD__ . ' : Finished copying of role il_frm_moderator.');
577 
578  include_once './Modules/Forum/classes/class.ilForumModerators.php';
579  $obj_mods = new ilForumModerators($this->getRefId());
580 
581  $old_mods = array();
582  $old_mods = $obj_mods->getCurrentModerators();
583 
584  foreach($old_mods as $user_id)
585  {
586  $rbacadmin->assignUser($new_moderator, $user_id);
587  }
588  }
589 
595  public function delete()
596  {
600  global $ilDB;
601 
602  // always call parent delete function first!!
603  if(!parent::delete())
604  {
605  return false;
606  }
607 
608  // delete attachments
609  $tmp_file_obj =& new ilFileDataForum($this->getId());
610  $tmp_file_obj->delete();
611  unset($tmp_file_obj);
612 
613  $this->Forum->setMDB2WhereCondition('top_frm_fk = %s ', array('integer'), array($this->getId()));
614 
615  $topData = $this->Forum->getOneTopic();
616 
617  $threads = $this->Forum->getAllThreads($topData['top_pk']);
618  foreach($threads['items'] as $thread)
619  {
620  $data = array($thread->getId());
621 
622  // delete tree
623  $statement = $ilDB->manipulateF('
624  DELETE FROM frm_posts_tree WHERE thr_fk = %s',
625  array('integer'), $data);
626 
627  // delete posts
628  $statement = $ilDB->manipulateF('
629  DELETE FROM frm_posts WHERE pos_thr_fk = %s',
630  array('integer'), $data);
631 
632  // delete threads
633  $statement = $ilDB->manipulateF('
634  DELETE FROM frm_threads WHERE thr_pk = %s',
635  array('integer'), $data);
636 
637  }
638 
639  $data = array($this->getId());
640  // delete forum
641  $statement = $ilDB->manipulateF('
642  DELETE FROM frm_data WHERE top_frm_fk = %s',
643  array('integer'), $data);
644 
645  // delete settings
646  $statement = $ilDB->manipulateF('
647  DELETE FROM frm_settings WHERE obj_id = %s',
648  array('integer'), $data);
649 
650  // delete read infos
651  $statement = $ilDB->manipulateF('
652  DELETE FROM frm_user_read WHERE obj_id = %s',
653  array('integer'), $data);
654 
655  // delete thread access entries
656  $statement = $ilDB->manipulateF('
657  DELETE FROM frm_thread_access WHERE obj_id = %s',
658  array('integer'), $data);
659 
660  //delete notifications
661  $ilDB->manipulateF('
662  DELETE FROM frm_notification WHERE frm_id = %s',
663  array('integer'), $data);
664 
665  return true;
666  }
667 
673  public function initDefaultRoles()
674  {
680  global $rbacadmin, $rbacreview, $ilDB;
681 
682  // Create a local role folder
683  $rolf_obj = $this->createRoleFolder();
684 
685  // CREATE Moderator role
686  $role_obj = $rolf_obj->createRole("il_frm_moderator_" . $this->getRefId(), "Moderator of forum obj_no." . $this->getId());
687  $roles[] = $role_obj->getId();
688 
689  // SET PERMISSION TEMPLATE OF NEW LOCAL ADMIN ROLE
690  $statement = $ilDB->queryf('
691  SELECT obj_id FROM object_data
692  WHERE type = %s
693  AND title = %s',
694  array('text', 'text'),
695  array('rolt', 'il_frm_moderator'));
696 
697  $res = $ilDB->fetchObject($statement);
698 
699  $rbacadmin->copyRoleTemplatePermissions($res->obj_id, ROLE_FOLDER_ID, $rolf_obj->getRefId(), $role_obj->getId());
700 
701  // SET OBJECT PERMISSIONS OF COURSE OBJECT
702  $ops = $rbacreview->getOperationsOfRole($role_obj->getId(), "frm", $rolf_obj->getRefId());
703  $rbacadmin->grantPermission($role_obj->getId(), $ops, $this->getRefId());
704 
705  return $roles ? $roles : array();
706  }
707 
715  public static function _lookupModeratorRole($a_ref_id)
716  {
720  global $ilDB;
721 
722  $mod_title = 'il_frm_moderator_' . $a_ref_id;
723 
724  $res = $ilDB->queryf('
725  SELECT * FROM object_data WHERE title = %s',
726  array('text'), array($mod_title));
727 
728  while($row = $ilDB->fetchObject($res))
729  {
730  return $row->obj_id;
731  }
732  return 0;
733  }
734 
735 
736  public function createSettings()
737  {
738  // news settings (public notifications yes/no)
739  include_once("./Services/News/classes/class.ilNewsItem.php");
740  $default_visibility = ilNewsItem::_getDefaultVisibilityForRefId($_GET["ref_id"]);
741  if($default_visibility == "public")
742  {
743  ilBlockSetting::_write("news", "public_notifications", 1, 0, $this->getId());
744  }
745 
746  return true;
747  }
748 
749  public function saveData($a_roles = array())
750  {
755  global $ilUser, $ilDB;
756 
757  $nextId = $ilDB->nextId('frm_data');
758 
759  $top_data = array(
760  'top_frm_fk' => $this->getId(),
761  'top_name' => $this->getTitle(),
762  'top_description' => $this->getDescription(),
763  'top_num_posts' => 0,
764  'top_num_threads' => 0,
765  'top_last_post' => NULL,
766  'top_mods' => !is_numeric($a_roles[0]) ? 0 : $a_roles[0],
767  'top_usr_id' => $ilUser->getId(),
768  'top_date' => ilUtil::now()
769  );
770 
771  $statement = $ilDB->manipulateF('
772  INSERT INTO frm_data
773  (
774  top_pk,
775  top_frm_fk,
776  top_name,
777  top_description,
778  top_num_posts,
779  top_num_threads,
780  top_last_post,
781  top_mods,
782  top_date,
783  top_usr_id
784  )
785  VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
786  array('integer', 'integer', 'text', 'text', 'integer', 'integer', 'text', 'integer', 'timestamp', 'integer'),
787  array(
788  $nextId,
789  $top_data['top_frm_fk'],
790  $top_data['top_name'],
791  $top_data['top_description'],
792  $top_data['top_num_posts'],
793  $top_data['top_num_threads'],
794  $top_data['top_last_post'],
795  $top_data['top_mods'],
796  $top_data['top_date'],
797  $top_data['top_usr_id']
798  ));
799  }
800 
801  public function setThreadSorting($a_thr_pk, $a_sorting_value)
802  {
803  global $ilDB;
804 
805  $ilDB->update('frm_threads',
806  array('thread_sorting' => array('integer',$a_sorting_value)),
807  array('thr_pk' => array('integer', $a_thr_pk)));
808  }
809 
810 
816  public static function lookupForumIdByObjId($obj_id)
817  {
818  if(array_key_exists($obj_id, self::$obj_id_to_forum_id_cache))
819  {
820  return (int)self::$obj_id_to_forum_id_cache[$obj_id];
821  }
822 
823  self::preloadForumIdsByObjIds(array($obj_id));
824 
825  return (int)self::$obj_id_to_forum_id_cache[$obj_id];
826  }
827 
833  public static function lookupForumIdByRefId($ref_id)
834  {
835  if(array_key_exists($ref_id, self::$ref_id_to_forum_id_cache))
836  {
837  return (int)self::$ref_id_to_forum_id_cache[$ref_id];
838  }
839 
840  self::preloadForumIdsByRefIds(array($ref_id));
841 
842  return (int)self::$ref_id_to_forum_id_cache[$ref_id];
843  }
844 
849  public static function preloadForumIdsByObjIds(array $obj_ids)
850  {
854  global $ilDB;
855 
856  if(count($obj_ids) == 1)
857  {
858  $in = " objr.obj_id = " . $ilDB->quote(current($obj_ids), 'integer') . " ";
859  }
860  else
861  {
862  $in = $ilDB->in('objr.obj_id', $obj_ids, false, 'integer');
863  }
864  $query = "
865  SELECT frmd.top_pk, objr.ref_id, objr.obj_id
866  FROM object_reference objr
867  INNER JOIN frm_data frmd ON frmd.top_frm_fk = objr.obj_id
868  WHERE $in
869  ";
870  $res = $ilDB->query($query);
871 
872  // Prepare cache array
873  foreach($obj_ids as $obj_id)
874  {
875  self::$obj_id_to_forum_id_cache[$obj_id] = null;
876  }
877 
878  while($row = $ilDB->fetchAssoc($res))
879  {
880  self::$obj_id_to_forum_id_cache[$row['obj_id']] = $row['top_pk'];
881  self::$ref_id_to_forum_id_cache[$row['ref_id']] = $row['top_pk'];
882  }
883  }
884 
889  public static function preloadForumIdsByRefIds(array $ref_ids)
890  {
894  global $ilDB;
895 
896  if(count($ref_ids) == 1)
897  {
898  $in = " objr.ref_id = " . $ilDB->quote(current($ref_ids), 'integer') . " ";
899  }
900  else
901  {
902  $in = $ilDB->in('objr.ref_id', $ref_ids, false, 'integer');
903  }
904  $query = "
905  SELECT frmd.top_pk, objr.ref_id, objr.obj_id
906  FROM object_reference objr
907  INNER JOIN frm_data frmd ON frmd.top_frm_fk = objr.obj_id
908  WHERE $in
909  ";
910  $res = $ilDB->query($query);
911 
912  // Prepare cache array
913  foreach($ref_ids as $ref_id)
914  {
915  self::$ref_id_to_forum_id_cache[$ref_id] = null;
916  }
917 
918  while($row = $ilDB->fetchAssoc($res))
919  {
920  self::$obj_id_to_forum_id_cache[$row['obj_id']] = $row['top_pk'];
921  self::$ref_id_to_forum_id_cache[$row['ref_id']] = $row['top_pk'];
922  }
923  }
924 
930  public static function lookupStatisticsByRefId($ref_id)
931  {
938  global $ilAccess, $ilUser, $ilDB, $ilSetting;
939 
940  if(isset(self::$forum_statistics_cache[$ref_id]))
941  {
942  return self::$forum_statistics_cache[$ref_id];
943  }
944 
945  $statistics = array(
946  'num_posts' => 0,
947  'num_unread_posts' => 0,
948  'num_new_posts' => 0
949  );
950 
951  $forumId = self::lookupForumIdByRefId($ref_id);
952  if(!$forumId)
953  {
954  self::$forum_statistics_cache[$ref_id] = $statistics;
955  return self::$forum_statistics_cache[$ref_id];
956  }
957 
958  $act_clause = '';
959  if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
960  {
961  $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_usr_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
962  }
963 
964  $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));
965 
966  if(!$ilUser->isAnonymous())
967  {
968  $query = "
969  (SELECT COUNT(frm_posts.pos_pk) cnt
970  FROM frm_posts
971  INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
972  WHERE frm_threads.thr_top_fk = %s $act_clause)
973 
974  UNION ALL
975 
976  (SELECT COUNT(DISTINCT(frm_user_read.post_id)) cnt
977  FROM frm_user_read
978  INNER JOIN frm_posts ON frm_user_read.post_id = frm_posts.pos_pk
979  INNER JOIN frm_threads ON frm_threads.thr_pk = frm_posts.pos_thr_fk
980  WHERE frm_user_read.usr_id = %s AND frm_posts.pos_top_fk = %s $act_clause)
981  ";
982 
983  $types = array('integer', 'integer', 'integer');
984  $values = array($forumId, $ilUser->getId(), $forumId);
985 
986  $forum_oveerview_setting = ilSetting::_lookupValue('frma', 'forum_overview');
987  if($forum_oveerview_setting == ilForumProperties::FORUM_OVERVIEW_WITH_NEW_POSTS)
988  {
989  $news_types = array('integer', 'integer', 'integer', 'timestamp', 'integer');
990  $news_values = array($ilUser->getId(), $ilUser->getId(), $forumId, $new_deadline, $ilUser->getId());
991 
992  $query .= "
993  UNION ALL
994 
995  (SELECT COUNT(frm_posts.pos_pk) cnt
996  FROM frm_posts
997  LEFT JOIN frm_user_read ON (post_id = frm_posts.pos_pk AND frm_user_read.usr_id = %s)
998  LEFT JOIN frm_thread_access ON (frm_thread_access.thread_id = frm_posts.pos_thr_fk AND frm_thread_access.usr_id = %s)
999  WHERE frm_posts.pos_top_fk = %s
1000  AND ( (frm_posts.pos_update > frm_thread_access.access_old_ts)
1001  OR (frm_thread_access.access_old IS NULL AND frm_posts.pos_update > %s)
1002  )
1003  AND frm_posts.pos_usr_id != %s
1004  AND frm_user_read.usr_id IS NULL $act_clause)";
1005 
1006  $types = array_merge($types, $news_types);
1007  $values = array_merge($values, $news_values);
1008  }
1009 
1010  $mapping = array_keys($statistics);
1011  $res = $ilDB->queryF(
1012  $query,
1013  $types,
1014  $values
1015  );
1016  for($i = 0; $i <= 2; $i++)
1017  {
1018  $row = $ilDB->fetchAssoc($res);
1019 
1020  $statistics[$mapping[$i]] = (int)$row['cnt'];
1021 
1022  if($i == 1)
1023  {
1024  // unread = all - read
1025  $statistics[$mapping[$i]] = $statistics[$mapping[$i - 1]] - $statistics[$mapping[$i]];
1026  }
1027  }
1028  }
1029  else
1030  {
1031  $query = "
1032  SELECT COUNT(frm_posts.pos_pk) cnt
1033  FROM frm_posts
1034  INNER JOIN frm_threads ON frm_posts.pos_thr_fk = frm_threads.thr_pk
1035  WHERE frm_threads.thr_top_fk = %s $act_clause
1036  ";
1037  $types = array('integer');
1038  $values = array($forumId);
1039  $res = $ilDB->queryF(
1040  $query,
1041  $types,
1042  $values
1043  );
1044  $row = $ilDB->fetchAssoc($res);
1045 
1046  $statistics = array(
1047  'num_posts' => $row['cnt'],
1048  'num_unread_posts' => $row['cnt'],
1049  'num_new_posts' => $row['cnt']
1050  );
1051  }
1052 
1053  self::$forum_statistics_cache[$ref_id] = $statistics;
1054 
1055  return self::$forum_statistics_cache[$ref_id];
1056  }
1057 
1063  public static function lookupLastPostByRefId($ref_id)
1064  {
1070  global $ilAccess, $ilUser, $ilDB;
1071 
1072  if(isset(self::$forum_last_post_cache[$ref_id]))
1073  {
1074  return self::$forum_last_post_cache[$ref_id];
1075  }
1076 
1077  $forumId = self::lookupForumIdByRefId($ref_id);
1078  if(!$forumId)
1079  {
1080  self::$forum_last_post_cache[$ref_id] = array();
1081  return self::$forum_last_post_cache[$ref_id];
1082  }
1083 
1084  $act_clause = '';
1085  if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1086  {
1087  $act_clause .= " AND (frm_posts.pos_status = " . $ilDB->quote(1, "integer") . " OR frm_posts.pos_usr_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1088  }
1089 
1090  $ilDB->setLimit(1, 0);
1091  $query = "
1092  SELECT *
1093  FROM frm_posts
1094  WHERE pos_top_fk = %s $act_clause
1095  ORDER BY pos_date DESC
1096  ";
1097  $res = $ilDB->queryF(
1098  $query,
1099  array('integer'),
1100  array($forumId)
1101  );
1102 
1103  $data = $ilDB->fetchAssoc($res);
1104 
1105  self::$forum_last_post_cache[$ref_id] = is_array($data) ? $data : array();
1106 
1107  return self::$forum_last_post_cache[$ref_id];
1108  }
1109 
1116  public static function getUserIdsOfLastPostsByRefIdAndThreadIds($ref_id, array $thread_ids)
1117  {
1123  global $ilUser, $ilAccess, $ilDB;
1124 
1125  $act_clause = '';
1126  $act_inner_clause = '';
1127  if(!$ilAccess->checkAccess('moderate_frm', '', $ref_id))
1128  {
1129  $act_clause .= " AND (t1.pos_status = " . $ilDB->quote(1, "integer") . " OR t1.pos_usr_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1130  $act_inner_clause .= " AND (t3.pos_status = " . $ilDB->quote(1, "integer") . " OR t3.pos_usr_id = " . $ilDB->quote($ilUser->getId(), "integer") . ") ";
1131  }
1132 
1133  $in = $ilDB->in("t1.pos_thr_fk", $thread_ids, false, 'integer');
1134  $inner_in = $ilDB->in("t3.pos_thr_fk", $thread_ids, false, 'integer');
1135 
1136  $query = "
1137  SELECT t1.pos_usr_id, t1.update_user
1138  FROM frm_posts t1
1139  INNER JOIN (
1140  SELECT t3.pos_thr_fk, MAX(t3.pos_date) pos_date
1141  FROM frm_posts t3
1142  WHERE $inner_in $act_inner_clause
1143  GROUP BY t3.pos_thr_fk
1144  ) t2 ON t2.pos_thr_fk = t1.pos_thr_fk AND t2.pos_date = t1.pos_date
1145  WHERE $in $act_clause
1146  ";
1147 
1148  $usr_ids = array();
1149 
1150  $res = $ilDB->query($query);
1151  while($row = $ilDB->fetchAssoc($res))
1152  {
1153  if((int)$row['pos_usr_id'])
1154  {
1155  $usr_ids[] = (int)$row['pos_usr_id'];
1156  }
1157  if((int)$row['update_user'])
1158  {
1159  $usr_ids[] = (int)$row['update_user'];
1160  }
1161  }
1162 
1163  return array_unique($usr_ids);
1164  }
1165 
1166  public static function mergeForumUserRead($merge_source_thread_id, $merge_target_thread_id)
1167  {
1168  global $ilDB;
1169 
1170  $ilDB->update('frm_user_read',
1171  array('thread_id' => array('integer', $merge_target_thread_id)),
1172  array('thread_id' => array('integer',$merge_source_thread_id)));
1173  }
1174 }
< a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">< img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/></a >< br/>< strong > Enter Code *if($_SERVER['REQUEST_METHOD']=='POST' &&@ $_POST['do']=='contact') $_SESSION['ctform']['success']
static $forum_last_post_cache
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.
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)
static $obj_id_to_forum_id_cache
static now()
Return current timestamp in Y-m-d H:i:s format.
static _lookupValue($a_module, $a_keyword)
manipulateF($a_query, $a_types, $a_values)
Formatted manupulate (for DELETE, UPDATE, INSERT).
fetchAssoc($a_set)
Fetch row as associative array from result set.
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
getTitle()
get object title public
getDescription()
get object description
redirection script todo: (a better solution should control the processing via a xml file) ...
initDefaultRoles()
init default roles settings Purpose of this function is to create a local role folder and local roles...
static $forum_statistics_cache
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
global $ilUser
Definition: imgupload.php:15
global $ilSetting
Definition: privfeed.php:40
This class handles all operations on files for the forum object.
global $ilBench
Definition: ilias.php:18
static _getDefaultVisibilityForRefId($a_ref_id)
Get default visibility for reference id.
_lookupPostMessage($a_id)
getRefId()
get reference id public
__construct($a_id=0, $a_call_by_reference=true)
Constructor public.
static $ref_id_to_forum_id_cache
update()
update object in db
createRoleFolder()
creates a local role folder
Class ilObjForum.
Class ilForumModerators.
getDiskUsage()
Gets the disk usage of the object in bytes.