ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilForumPostDraft.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
8 {
9  const MEDIAOBJECT_TYPE = 'frm~d:html';
13  protected $draft_id = 0;
17  protected $post_id = 0;
21  protected $forum_id = 0;
25  protected $thread_id = 0;
29  protected $post_subject = '';
33  protected $post_message = '';
37  protected $post_date = '0000-00-00 00:00:00';
41  protected $post_update = '0000-00-00 00:00:00';
45  protected $update_user_id = 0;
49  protected $post_user_alias = '';
53  protected $post_author_id = 0;
57  protected $post_display_user_id = 0;
58 
62  protected $notify = 0;
66  protected $post_notify = 0;
67 
71  private static $instances = array();
72 
77  protected static $forum_statistics_cache = array();
78 
82  protected static $drafts_settings_cache = array();
83 
88  protected static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
89  {
90  $draft->setDraftId($row['draft_id']);
91  $draft->setForumId($row['forum_id']);
92  $draft->setPostAuthorId($row['post_author_id']);
93  $draft->setPostDate($row['post_date']);
94  $draft->setPostDisplayUserId($row['pos_display_usr_id']);
95  $draft->setPostId($row['post_id']);
96  $draft->setPostMessage($row['post_message']);
97  $draft->setPostSubject($row['post_subject']);
98  $draft->setPostUpdate($row['post_update']);
99  $draft->setPostUserAlias($row['post_user_alias']);
100  $draft->setThreadId($row['thread_id']);
101  $draft->setUpdateUserId($row['update_user_id']);
102  $draft->setNotify($row['notify']);
103  $draft->setPostNotify($row['post_notify']);
104  }
105 
109  public function getPostNotify()
110  {
111  return $this->post_notify;
112  }
113 
117  public function setPostNotify($post_notify)
118  {
119  $this->post_notify = $post_notify;
120  }
121 
125  public function getDraftId()
126  {
127  return $this->draft_id;
128  }
129 
133  public function setDraftId($draft_id)
134  {
135  $this->draft_id = $draft_id;
136  }
137 
141  public function getPostId()
142  {
143  return $this->post_id;
144  }
145 
149  public function setPostId($post_id)
150  {
151  $this->post_id = $post_id;
152  }
153 
157  public function getForumId()
158  {
159  return $this->forum_id;
160  }
161 
165  public function setForumId($forum_id)
166  {
167  $this->forum_id = $forum_id;
168  }
169 
173  public function getThreadId()
174  {
175  return $this->thread_id;
176  }
177 
181  public function setThreadId($thread_id)
182  {
183  $this->thread_id = $thread_id;
184  }
185 
189  public function getPostSubject()
190  {
191  return $this->post_subject;
192  }
193 
197  public function setPostSubject($post_subject)
198  {
199  $this->post_subject = $post_subject;
200  }
201 
205  public function getPostMessage()
206  {
207  return $this->post_message;
208  }
209 
213  public function setPostMessage($post_message)
214  {
215  $this->post_message = $post_message;
216  }
217 
221  public function getPostDate()
222  {
223  return $this->post_date;
224  }
225 
229  public function setPostDate($post_date)
230  {
231  $this->post_date = $post_date;
232  }
233 
237  public function getPostUpdate()
238  {
239  return $this->post_update;
240  }
241 
245  public function setPostUpdate($post_update)
246  {
247  $this->post_update = $post_update;
248  }
249 
253  public function getUpdateUserId()
254  {
255  return $this->update_user_id;
256  }
257 
262  {
263  $this->update_user_id = $update_user_id;
264  }
265 
269  public function getPostUserAlias()
270  {
271  return $this->post_user_alias;
272  }
273 
278  {
279  $this->post_user_alias = $post_user_alias;
280  }
281 
285  public function getPostAuthorId()
286  {
287  return $this->post_author_id;
288  }
289 
294  {
295  $this->post_author_id = $post_author_id;
296  }
297 
301  public function getPostDisplayUserId()
302  {
304  }
305 
310  {
311  $this->post_display_user_id = $post_display_user_id;
312  }
313 
317  public function getNotify()
318  {
319  return $this->notify;
320  }
321 
325  public function setNotify($notify)
326  {
327  $this->notify = $notify;
328  }
329 
335  public function __construct($user_id = 0, $post_id = 0, $draft_id = 0)
336  {
337  global $DIC;
338 
339  $this->db = $DIC->database();
340 
341  if ($user_id && $post_id && $draft_id) {
342  $this->setPostAuthorId($user_id);
343  $this->setPostId($post_id);
344  $this->setDraftId($draft_id);
345  $this->readDraft();
346  }
347  }
348 
352  protected function readDraft()
353  {
354  $res = $this->db->queryF(
355  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND post_id = %s AND draft_id = %s',
356  array('integer', 'integer','integer'),
357  array($this->getPostAuthorId(), $this->getPostId(), $this->getDraftId())
358  );
359 
360  while ($row = $this->db->fetchAssoc($res)) {
361  self::populateWithDatabaseRecord($this, $row);
362  }
363  }
364 
368  protected static function readDrafts($user_id)
369  {
370  global $DIC;
371  $ilDB = $DIC->database();
372 
373  $res = $ilDB->queryF(
374  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s',
375  ['integer'],
376  [$user_id]
377  );
378 
379  self::$instances[$user_id] = [
380  'draft_ids' => [],
381  ];
382 
383  while ($row = $ilDB->fetchAssoc($res)) {
384  $tmp_obj = new ilForumPostDraft();
385  self::populateWithDatabaseRecord($tmp_obj, $row);
386  self::$instances[$user_id][$row['thread_id']][$tmp_obj->getPostId()][] = $tmp_obj;
387  self::$instances[$user_id]['draft_ids'][$tmp_obj->getDraftId()] = $tmp_obj;
388  }
389  }
390 
397  public static function getSortedDrafts(
398  int $usrId,
399  int $threadId,
400  int $sorting = ilForumProperties::VIEW_DATE_ASC
401  ) : array {
402  global $DIC;
403  $ilDB = $DIC->database();
404 
405  $drafts = [];
406 
407  $orderColumn = ' ';
408  $orderDirection = ' ';
409 
410  if ($sorting !== ilForumProperties::VIEW_TREE) {
411  $orderColumn = ' ORDER BY post_date ';
412  $orderDirection = 'ASC';
413  if ($sorting === ilForumProperties::VIEW_DATE_DESC) {
414  $orderDirection = 'DESC';
415  }
416  }
417 
418  $res = $ilDB->queryF(
419  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND thread_id = %s' .
420  $orderColumn . $orderDirection,
421  ['integer', 'integer'],
422  [$usrId, $threadId]
423  );
424 
425  while ($row = $ilDB->fetchAssoc($res)) {
426  $draft = new ilForumPostDraft();
427  self::populateWithDatabaseRecord($draft, $row);
428  $drafts[] = $draft;
429  self::$instances[$usrId][$threadId][$draft->getPostId()][] = $draft;
430  }
431 
432  if (ilForumProperties::VIEW_TREE === $sorting) {
433  return self::$instances[$usrId][$threadId] ?? [];
434  }
435 
436  return $drafts;
437  }
438 
443  public static function getDraftInstancesByUserId($user_id) : array
444  {
445  if (!isset(self::$instances[$user_id])) {
446  self::readDrafts($user_id);
447  }
448 
449  return self::$instances[$user_id]['draft_ids'];
450  }
451 
456  public static function newInstanceByDraftId($draft_id)
457  {
458  global $DIC;
459  $ilDB = $DIC->database();
460 
461  $res = $ilDB->queryF(
462  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
463  array('integer'),
464  array($draft_id)
465  );
466 
467  $tmp_obj = new ilForumPostDraft();
468  while ($row = $ilDB->fetchAssoc($res)) {
469  self::populateWithDatabaseRecord($tmp_obj, $row);
470  }
471  return $tmp_obj;
472  }
473 
479  public static function newInstanceByHistorytId($history_id)
480  {
481  global $DIC;
482  $ilDB = $DIC->database();
483 
484  $res = $ilDB->queryF(
485  'SELECT * FROM frm_drafts_history WHERE history_id = %s',
486  array('integer'),
487  array($history_id)
488  );
489 
490  while ($row = $ilDB->fetchAssoc($res)) {
491  $tmp_obj = new ilForumPostDraft();
492  self::populateWithDatabaseRecord($tmp_obj, $row);
493  return $tmp_obj;
494  }
495 
496  throw new ilException(sprintf("Could not find history object for id %s", $history_id));
497  }
498 
499  public function saveDraft()
500  {
501  $draft_id = $this->db->nextId('frm_posts_drafts');
502  $post_date = date("Y-m-d H:i:s");
503 
504  $this->db->insert('frm_posts_drafts', array(
505  'draft_id' => array('integer', $draft_id),
506  'post_id' => array('integer', $this->getPostId()),
507  'thread_id' => array('integer', $this->getThreadId()),
508  'forum_id' => array('integer', $this->getForumId()),
509  'post_author_id' => array('integer', $this->getPostAuthorId()),
510  'post_subject' => array('text', $this->getPostSubject()),
511  'post_message' => array('clob', $this->getPostMessage()),
512  'notify' => array('integer', $this->getNotify()),
513  'post_notify' => array('integer', $this->getPostNotify()),
514  'post_date' => array('timestamp', $post_date),
515  'post_update' => array('timestamp', $post_date),
516 // 'update_user_id' => array('integer', $this->getUpdateUserId()),
517  'post_user_alias' => array('text', $this->getPostUserAlias()),
518  'pos_display_usr_id' => array('integer', $this->getPostDisplayUserId())
519  ));
520  $this->setDraftId($draft_id);
521  return $draft_id;
522  }
523 
524  public function updateDraft()
525  {
526  $this->db->update(
527  'frm_posts_drafts',
528  array(
529  'post_subject' => array('text', $this->getPostSubject()),
530  'post_message' => array('clob', $this->getPostMessage()),
531  'post_user_alias' => array('text', $this->getPostUserAlias()),
532  'post_update' => array('timestamp', date("Y-m-d H:i:s")),
533  'update_user_id' => array('integer', $this->getUpdateUserId()),
534  ),
535  array('draft_id' => array('integer', $this->getDraftId()))
536  );
537  }
538 
539  public function deleteDraft()
540  {
541  $this->db->manipulateF(
542  'DELETE FROM frm_posts_drafts WHERE draft_id = %s',
543  array('integer'),
544  array($this->getDraftId())
545  );
546  }
547 
551  public static function deleteMobsOfDraft($draft_id)
552  {
553  // delete mobs of draft
554  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft_id);
555  foreach ($oldMediaObjects as $oldMob) {
556  if (ilObjMediaObject::_exists($oldMob)) {
557  ilObjMediaObject::_removeUsage($oldMob, 'frm~d:html', $draft_id);
558  $mob_obj = new ilObjMediaObject($oldMob);
559  $mob_obj->delete();
560  }
561  }
562  }
563 
567  public function deleteDraftsByPostIds(array $post_ids = array())
568  {
569  $draft_ids = array();
570  $res = $this->db->query('SELECT draft_id FROM frm_posts_drafts WHERE ' . $this->db->in('post_id', $post_ids, false, 'integer'));
571  while ($row = $this->db->fetchAssoc($res)) {
572  $draft_ids[] = $row['draft_id'];
573  }
574 
575  foreach ($draft_ids as $draft_id) {
576  self::deleteMobsOfDraft($draft_id);
577 
578  // delete attachments of draft
579  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
580  $objFileDataForumDrafts->delete();
581  }
582  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
583  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
584  }
585 
589  public function deleteDraftsByDraftIds(array $draft_ids = array())
590  {
591  foreach ($draft_ids as $draft_id) {
592  self::deleteMobsOfDraft($draft_id);
593 
594  // delete attachments of draft
595  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
596  $objFileDataForumDrafts->delete();
597  }
598  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
599  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
600  }
601 
605  public static function deleteDraftsByUserId($user_id)
606  {
607  global $DIC;
608  $ilDB = $DIC->database();
609 
610  $res = $ilDB->queryF(
611  'SELECT draft_id FROM frm_posts_drafts WHERE post_author_id = %s',
612  array('integer'),
613  array($user_id)
614  );
615 
616  $draft_ids = array();
617  while ($row = $ilDB->fetchAssoc($res)) {
618  $draft_ids[] = $row['draft_id'];
619  }
620 
621  foreach ($draft_ids as $draft_id) {
622  self::deleteMobsOfDraft($draft_id);
623 
624  // delete attachments of draft
625  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
626  $objFileDataForumDrafts->delete();
627  }
628 
629  $ilDB->manipulate('DELETE FROM frm_drafts_history WHERE ' . $ilDB->in('draft_id', $draft_ids, false, 'integer'));
630  $ilDB->manipulateF(
631  'DELETE FROM frm_posts_drafts WHERE post_author_id = %s',
632  array('integer'),
633  array($user_id)
634  );
635  }
636 
640  public static function isSavePostDraftAllowed()
641  {
642  if (!isset(self::$drafts_settings_cache['save_post_drafts'])) {
643  global $DIC;
644  self::$drafts_settings_cache['save_post_drafts'] = (bool) $DIC->settings()->get('save_post_drafts', false);
645  }
646  return self::$drafts_settings_cache['save_post_drafts'];
647  }
648 
652  public static function isAutoSavePostDraftAllowed()
653  {
654  if (!self::isSavePostDraftAllowed()) {
655  // feature is globally deactivated
656  return false;
657  }
658  if (!isset(self::$drafts_settings_cache['autosave_drafts'])) {
659  global $DIC;
660 
661  self::$drafts_settings_cache['autosave_drafts'] = (bool) $DIC->settings()->get('autosave_drafts', false);
662  self::$drafts_settings_cache['autosave_drafts_ival'] = (int) $DIC->settings()->get('autosave_drafts_ival', 30);
663  }
664  return self::$drafts_settings_cache['autosave_drafts'];
665  }
666 
667  public static function lookupAutosaveInterval()
668  {
669  if (self::isAutoSavePostDraftAllowed()) {
670  return self::$drafts_settings_cache['autosave_drafts_ival'];
671  }
672  return 0;
673  }
674 
679  public static function getDraftsStatisticsByRefId($ref_id)
680  {
681  global $DIC;
682  $ilDB = $DIC->database();
683  $ilUser = $DIC->user();
684 
685  if (!isset(self::$forum_statistics_cache[$ref_id][$ilUser->getId()])) {
686  $forumId = ilObjForum::lookupForumIdByRefId($ref_id);
687 
688  $res = $ilDB->queryF(
689  '
690  SELECT COUNT(draft_id) num_drafts, thread_id FROM frm_posts_drafts
691  WHERE forum_id = %s AND post_author_id = %s
692  GROUP BY thread_id',
693  array('integer', 'integer'),
694  array($forumId, $ilUser->getId())
695  );
696 
697  $num_drafts_total = 0;
698 
699  while ($row = $ilDB->fetchAssoc($res)) {
700  $num_drafts_total += $row['num_drafts'];
701  self::$forum_statistics_cache[$ref_id][$ilUser->getId()][$row['thread_id']] = $row['num_drafts'];
702  }
703 
704  self::$forum_statistics_cache[$ref_id][$ilUser->getId()]['total'] = $num_drafts_total;
705  }
706  return self::$forum_statistics_cache[$ref_id][$ilUser->getId()];
707  }
708 
713  public static function moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
714  {
715  global $DIC;
716  $ilDB = $DIC->database();
717 
718  $ilDB->update(
719  'frm_posts_drafts',
720  array('thread_id' => array('integer', $target_thread_id)),
721  array('thread_id' => array('integer', $source_thread_id))
722  );
723  }
724 
730  public static function moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
731  {
732  global $DIC;
733  $ilDB = $DIC->database();
734 
735  $source_forum_id = ilObjForum::lookupForumIdByRefId($source_ref_id);
736  $target_forum_id = ilObjForum::lookupForumIdByRefId($target_ref_id);
737 
738  $ilDB->manipulateF(
739  '
740  UPDATE frm_posts_drafts
741  SET forum_id = %s
742  WHERE forum_id = %s
743  AND ' . $ilDB->in('thread_id', $thread_ids, false, 'integer'),
744  array('integer', 'integer'),
745  array($target_forum_id, $source_forum_id)
746  );
747  }
748 
755  {
756  global $DIC;
757  $ilDB = $DIC->database();
758 
759  $res = $ilDB->queryF(
760  'SELECT * FROM frm_posts_drafts
761  WHERE post_author_id = %s
762  AND forum_id = %s
763  AND thread_id = %s
764  AND post_id = %s
765  ORDER BY post_date DESC',
766  array('integer', 'integer', 'integer', 'integer'),
767  array($post_author_id, $forum_id, 0, 0)
768  );
769  $draft_data = array();
770  while ($row = $ilDB->fetchAssoc($res)) {
771  $tmp_obj = new self;
772  self::populateWithDatabaseRecord($tmp_obj, $row);
773  $draft_data[] = array('subject' => $tmp_obj->getPostSubject(), 'post_update' => $tmp_obj->getPostUpdate(), 'draft_id' => $tmp_obj->getDraftId());
774  }
775  return $draft_data;
776  }
777 
781  public static function createDraftBackup($draft_id)
782  {
783  global $DIC;
784  $ilDB = $DIC->database();
785 
786  $res = $ilDB->queryF(
787  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
788  array('integer'),
789  array((int) $draft_id)
790  );
791 
792  while ($row = $ilDB->fetchAssoc($res)) {
793  $tmp_obj = new self;
794  self::populateWithDatabaseRecord($tmp_obj, $row);
795  }
796 
797  $history_obj = new ilForumDraftsHistory();
798  $history_obj->deleteHistoryByDraftIds(array($draft_id));
799 
800  $history_obj->setDraftId($draft_id);
801  $history_obj->setPostSubject($tmp_obj->getPostSubject());
802  $history_obj->setPostMessage($tmp_obj->getPostMessage());
803  $history_obj->addDraftToHistory();
804 
806  $tmp_obj->getPostMessage(),
807  self::MEDIAOBJECT_TYPE,
808  $draft_id,
810  $history_obj->getHistoryId()
811  );
812  }
813 }
static getDraftInstancesByUserId($user_id)
static getThreadDraftData($post_author_id, $forum_id)
Class ilForumPostDraft.
static _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of object
deleteDraftsByDraftIds(array $draft_ids=array())
setPostUserAlias($post_user_alias)
static lookupForumIdByRefId($ref_id)
setPostDisplayUserId($post_display_user_id)
__construct($user_id=0, $post_id=0, $draft_id=0)
ilForumPostDraft constructor.
static moveMediaObjects($post_message, $source_type, $source_id, $target_type, $target_id, $direction=0)
This class handles all operations on files for the drafts of a forum object.
static _removeUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Remove usage of mob in another container.
static readDrafts($user_id)
Class ilForumDraftHistory.
static populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
static createDraftBackup($draft_id)
static moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
foreach($_POST as $key=> $value) $res
static deleteMobsOfDraft($draft_id)
deleteDraftsByPostIds(array $post_ids=array())
setUpdateUserId($update_user_id)
global $DIC
Definition: goto.php:24
setPostMessage($post_message)
static newInstanceByDraftId($draft_id)
Class ilObjMediaObject.
setPostSubject($post_subject)
setPostAuthorId($post_author_id)
static moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
static newInstanceByHistorytId($history_id)
static getSortedDrafts(int $usrId, int $threadId, int $sorting=ilForumProperties::VIEW_DATE_ASC)
static deleteDraftsByUserId($user_id)
global $ilDB
$ilUser
Definition: imgupload.php:18
static _exists($a_id, $a_reference=false, $a_type=null)
checks wether a lm content object with specified id exists or not
static getDraftsStatisticsByRefId($ref_id)