ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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;
63  protected $post_notify = 0;
64 
68  private static $instances = array();
69 
74  protected static $forum_statistics_cache = array();
75 
79  protected static $drafts_settings_cache = array();
80 
85  protected static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
86  {
87  $draft->setDraftId($row['draft_id']);
88  $draft->setForumId($row['forum_id']);
89  $draft->setPostAuthorId($row['post_author_id']);
90  $draft->setPostDate($row['post_date']);
91  $draft->setPostDisplayUserId($row['pos_display_usr_id']);
92  $draft->setPostId($row['post_id']);
93  $draft->setPostMessage($row['post_message']);
94  $draft->setPostSubject($row['post_subject']);
95  $draft->setPostUpdate($row['post_update']);
96  $draft->setPostUserAlias($row['post_user_alias']);
97  $draft->setThreadId($row['thread_id']);
98  $draft->setUpdateUserId($row['update_user_id']);
99  $draft->setNotify($row['notify']);
100  $draft->setPostNotify($row['post_notify']);
101  }
102 
106  public function getPostNotify()
107  {
108  return $this->post_notify;
109  }
110 
114  public function setPostNotify($post_notify)
115  {
116  $this->post_notify = $post_notify;
117  }
118 
122  public function getDraftId()
123  {
124  return $this->draft_id;
125  }
126 
130  public function setDraftId($draft_id)
131  {
132  $this->draft_id = $draft_id;
133  }
134 
138  public function getPostId()
139  {
140  return $this->post_id;
141  }
142 
146  public function setPostId($post_id)
147  {
148  $this->post_id = $post_id;
149  }
150 
154  public function getForumId()
155  {
156  return $this->forum_id;
157  }
158 
162  public function setForumId($forum_id)
163  {
164  $this->forum_id = $forum_id;
165  }
166 
170  public function getThreadId()
171  {
172  return $this->thread_id;
173  }
174 
178  public function setThreadId($thread_id)
179  {
180  $this->thread_id = $thread_id;
181  }
182 
186  public function getPostSubject()
187  {
188  return $this->post_subject;
189  }
190 
194  public function setPostSubject($post_subject)
195  {
196  $this->post_subject = $post_subject;
197  }
198 
202  public function getPostMessage()
203  {
204  return $this->post_message;
205  }
206 
210  public function setPostMessage($post_message)
211  {
212  $this->post_message = $post_message;
213  }
214 
218  public function getPostDate()
219  {
220  return $this->post_date;
221  }
222 
226  public function setPostDate($post_date)
227  {
228  $this->post_date = $post_date;
229  }
230 
234  public function getPostUpdate()
235  {
236  return $this->post_update;
237  }
238 
242  public function setPostUpdate($post_update)
243  {
244  $this->post_update = $post_update;
245  }
246 
250  public function getUpdateUserId()
251  {
252  return $this->update_user_id;
253  }
254 
259  {
260  $this->update_user_id = $update_user_id;
261  }
262 
266  public function getPostUserAlias()
267  {
268  return $this->post_user_alias;
269  }
270 
275  {
276  $this->post_user_alias = $post_user_alias;
277  }
278 
282  public function getPostAuthorId()
283  {
284  return $this->post_author_id;
285  }
286 
291  {
292  $this->post_author_id = $post_author_id;
293  }
294 
298  public function getPostDisplayUserId()
299  {
301  }
302 
307  {
308  $this->post_display_user_id = $post_display_user_id;
309  }
310 
314  public function getNotify()
315  {
316  return $this->notify;
317  }
318 
322  public function setNotify($notify)
323  {
324  $this->notify = $notify;
325  }
326 
332  public function __construct($user_id = 0, $post_id = 0, $draft_id = 0)
333  {
334  global $DIC;
335 
336  $this->db = $DIC->database();
337 
338  if ($user_id && $post_id && $draft_id) {
339  $this->setPostAuthorId($user_id);
340  $this->setPostId($post_id);
341  $this->setDraftId($draft_id);
342  $this->readDraft();
343  }
344  }
345 
349  protected function readDraft()
350  {
351  $res = $this->db->queryF(
352  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND post_id = %s AND draft_id = %s',
353  array('integer', 'integer','integer'),
354  array($this->getPostAuthorId(), $this->getPostId(), $this->getDraftId())
355  );
356 
357  while ($row = $this->db->fetchAssoc($res)) {
358  self::populateWithDatabaseRecord($this, $row);
359  }
360  }
361 
365  protected static function readDrafts($user_id)
366  {
367  global $DIC;
368  $ilDB = $DIC->database();
369 
370  $res = $ilDB->queryF(
371  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s',
372  array('integer'),
373  array($user_id)
374  );
375 
376  self::$instances[$user_id] = array();
377  while ($row = $ilDB->fetchAssoc($res)) {
378  $tmp_obj = new ilForumPostDraft();
379  self::populateWithDatabaseRecord($tmp_obj, $row);
380  self::$instances[$user_id][$row['thread_id']][$tmp_obj->getPostId()][] = $tmp_obj;
381  self::$instances[$user_id]['draft_ids'][$tmp_obj->getDraftId()] = $tmp_obj;
382  }
383  unset($tmp_obj);
384  }
385 
390  public static function getDraftInstancesByUserId($user_id)
391  {
392  if (!self::$instances[$user_id]) {
393  self::readDrafts($user_id);
394  }
395 
396  return self::$instances[$user_id]['draft_ids'];
397  }
398 
404  public static function getInstancesByUserIdAndThreadId($user_id, $thread_id) : array
405  {
406  if (!self::$instances[$user_id]) {
407  self::readDrafts($user_id);
408  }
409 
410  if (isset(self::$instances[$user_id][$thread_id])) {
411  return self::$instances[$user_id][$thread_id];
412  }
413 
414  return [];
415  }
416 
421  public static function newInstanceByDraftId($draft_id)
422  {
423  global $DIC;
424  $ilDB = $DIC->database();
425 
426  $res = $ilDB->queryF(
427  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
428  array('integer'),
429  array($draft_id)
430  );
431 
432  $tmp_obj = new ilForumPostDraft();
433  while ($row = $ilDB->fetchAssoc($res)) {
434  self::populateWithDatabaseRecord($tmp_obj, $row);
435  }
436  return $tmp_obj;
437  }
438 
444  public static function newInstanceByHistorytId($history_id)
445  {
446  global $DIC;
447  $ilDB = $DIC->database();
448 
449  $res = $ilDB->queryF(
450  'SELECT * FROM frm_drafts_history WHERE history_id = %s',
451  array('integer'),
452  array($history_id)
453  );
454 
455  while ($row = $ilDB->fetchAssoc($res)) {
456  $tmp_obj = new ilForumPostDraft();
457  self::populateWithDatabaseRecord($tmp_obj, $row);
458  return $tmp_obj;
459  }
460 
461  throw new ilException(sprintf("Could not find history object for id %s", $history_id));
462  }
463 
464  public function saveDraft()
465  {
466  $draft_id = $this->db->nextId('frm_posts_drafts');
467  $post_date = date("Y-m-d H:i:s");
468 
469  $this->db->insert('frm_posts_drafts', array(
470  'draft_id' => array('integer', $draft_id),
471  'post_id' => array('integer', $this->getPostId()),
472  'thread_id' => array('integer', $this->getThreadId()),
473  'forum_id' => array('integer', $this->getForumId()),
474  'post_author_id' => array('integer', $this->getPostAuthorId()),
475  'post_subject' => array('text', $this->getPostSubject()),
476  'post_message' => array('clob', $this->getPostMessage()),
477  'notify' => array('integer', $this->getNotify()),
478  'post_notify' => array('integer', $this->getPostNotify()),
479  'post_date' => array('timestamp', $post_date),
480  'post_update' => array('timestamp', $post_date),
481 // 'update_user_id' => array('integer', $this->getUpdateUserId()),
482  'post_user_alias' => array('text', $this->getPostUserAlias()),
483  'pos_display_usr_id' => array('integer', $this->getPostDisplayUserId())
484  ));
485  $this->setDraftId($draft_id);
486  return $draft_id;
487  }
488 
489  public function updateDraft()
490  {
491  $this->db->update(
492  'frm_posts_drafts',
493  array(
494  'post_subject' => array('text', $this->getPostSubject()),
495  'post_message' => array('clob', $this->getPostMessage()),
496  'post_user_alias' => array('text', $this->getPostUserAlias())
497  ),
498  array('draft_id' => array('integer', $this->getDraftId()))
499  );
500  }
501 
502  public function deleteDraft()
503  {
504  $this->db->manipulateF(
505  'DELETE FROM frm_posts_drafts WHERE draft_id = %s',
506  array('integer'),
507  array($this->getDraftId())
508  );
509  }
510 
514  public static function deleteMobsOfDraft($draft_id)
515  {
516  // delete mobs of draft
517  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft_id);
518  foreach ($oldMediaObjects as $oldMob) {
519  if (ilObjMediaObject::_exists($oldMob)) {
520  ilObjMediaObject::_removeUsage($oldMob, 'frm~d:html', $draft_id);
521  $mob_obj = new ilObjMediaObject($oldMob);
522  $mob_obj->delete();
523  }
524  }
525  }
526 
530  public function deleteDraftsByPostIds(array $post_ids = array())
531  {
532  $draft_ids = array();
533  $res = $this->db->query('SELECT draft_id FROM frm_posts_drafts WHERE ' . $this->db->in('post_id', $post_ids, false, 'integer'));
534  while ($row = $this->db->fetchAssoc($res)) {
535  $draft_ids[] = $row['draft_id'];
536  }
537 
538  foreach ($draft_ids as $draft_id) {
539  self::deleteMobsOfDraft($draft_id);
540 
541  // delete attachments of draft
542  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
543  $objFileDataForumDrafts->delete();
544  }
545  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
546  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
547  }
548 
552  public function deleteDraftsByDraftIds(array $draft_ids = array())
553  {
554  foreach ($draft_ids as $draft_id) {
555  self::deleteMobsOfDraft($draft_id);
556 
557  // delete attachments of draft
558  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
559  $objFileDataForumDrafts->delete();
560  }
561  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
562  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
563  }
564 
568  public static function deleteDraftsByUserId($user_id)
569  {
570  global $DIC;
571  $ilDB = $DIC->database();
572 
573  $res = $ilDB->queryF(
574  'SELECT draft_id FROM frm_posts_drafts WHERE post_author_id = %s',
575  array('integer'),
576  array($user_id)
577  );
578 
579  $draft_ids = array();
580  while ($row = $ilDB->fetchAssoc($res)) {
581  $draft_ids[] = $row['draft_id'];
582  }
583 
584  foreach ($draft_ids as $draft_id) {
585  self::deleteMobsOfDraft($draft_id);
586 
587  // delete attachments of draft
588  $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
589  $objFileDataForumDrafts->delete();
590  }
591 
592  $ilDB->manipulate('DELETE FROM frm_drafts_history WHERE ' . $ilDB->in('draft_id', $draft_ids, false, 'integer'));
593  $ilDB->manipulateF(
594  'DELETE FROM frm_posts_drafts WHERE post_author_id = %s',
595  array('integer'),
596  array($user_id)
597  );
598  }
599 
603  public static function isSavePostDraftAllowed()
604  {
605  if (!isset(self::$drafts_settings_cache['save_post_drafts'])) {
606  global $DIC;
607  self::$drafts_settings_cache['save_post_drafts'] = (bool) $DIC->settings()->get('save_post_drafts', false);
608  }
609  return self::$drafts_settings_cache['save_post_drafts'];
610  }
611 
615  public static function isAutoSavePostDraftAllowed()
616  {
617  if (!self::isSavePostDraftAllowed()) {
618  // feature is globally deactivated
619  return false;
620  }
621  if (!isset(self::$drafts_settings_cache['autosave_drafts'])) {
622  global $DIC;
623 
624  self::$drafts_settings_cache['autosave_drafts'] = (bool) $DIC->settings()->get('autosave_drafts', false);
625  self::$drafts_settings_cache['autosave_drafts_ival'] = (int) $DIC->settings()->get('autosave_drafts_ival', 30);
626  }
627  return self::$drafts_settings_cache['autosave_drafts'];
628  }
629 
630  public static function lookupAutosaveInterval()
631  {
632  if (self::isAutoSavePostDraftAllowed()) {
633  return self::$drafts_settings_cache['autosave_drafts_ival'];
634  }
635  return 0;
636  }
637 
642  public static function getDraftsStatisticsByRefId($ref_id)
643  {
644  global $DIC;
645  $ilDB = $DIC->database();
646  $ilUser = $DIC->user();
647 
648  if (!isset(self::$forum_statistics_cache[$ref_id][$ilUser->getId()])) {
649  $forumId = ilObjForum::lookupForumIdByRefId($ref_id);
650 
651  $res = $ilDB->queryF(
652  '
653  SELECT COUNT(draft_id) num_drafts, thread_id FROM frm_posts_drafts
654  WHERE forum_id = %s AND post_author_id = %s
655  GROUP BY thread_id',
656  array('integer', 'integer'),
657  array($forumId, $ilUser->getId())
658  );
659 
660  $num_drafts_total = 0;
661 
662  while ($row = $ilDB->fetchAssoc($res)) {
663  $num_drafts_total += $row['num_drafts'];
664  self::$forum_statistics_cache[$ref_id][$ilUser->getId()][$row['thread_id']] = $row['num_drafts'];
665  }
666 
667  self::$forum_statistics_cache[$ref_id][$ilUser->getId()]['total'] = $num_drafts_total;
668  }
669  return self::$forum_statistics_cache[$ref_id][$ilUser->getId()];
670  }
671 
676  public static function moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
677  {
678  global $DIC;
679  $ilDB = $DIC->database();
680 
681  $ilDB->update(
682  'frm_posts_drafts',
683  array('thread_id' => array('integer', $target_thread_id)),
684  array('thread_id' => array('integer', $source_thread_id))
685  );
686  }
687 
693  public static function moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
694  {
695  global $DIC;
696  $ilDB = $DIC->database();
697 
698  $source_forum_id = ilObjForum::lookupForumIdByRefId($source_ref_id);
699  $target_forum_id = ilObjForum::lookupForumIdByRefId($target_ref_id);
700 
701  $ilDB->manipulateF(
702  '
703  UPDATE frm_posts_drafts
704  SET forum_id = %s
705  WHERE forum_id = %s
706  AND ' . $ilDB->in('thread_id', $thread_ids, false, 'integer'),
707  array('integer', 'integer'),
708  array($target_forum_id, $source_forum_id)
709  );
710  }
711 
718  {
719  global $DIC;
720  $ilDB = $DIC->database();
721 
722  $res = $ilDB->queryF(
723  'SELECT * FROM frm_posts_drafts
724  WHERE post_author_id = %s
725  AND forum_id = %s
726  AND thread_id = %s
727  AND post_id = %s
728  ORDER BY post_date DESC',
729  array('integer', 'integer', 'integer', 'integer'),
730  array($post_author_id, $forum_id, 0, 0)
731  );
732  $draft_data = array();
733  while ($row = $ilDB->fetchAssoc($res)) {
734  $tmp_obj = new self;
735  self::populateWithDatabaseRecord($tmp_obj, $row);
736  $draft_data[] = array('subject' => $tmp_obj->getPostSubject(), 'post_update' => $tmp_obj->getPostUpdate(), 'draft_id' => $tmp_obj->getDraftId());
737  }
738  return $draft_data;
739  }
740 
744  public static function createDraftBackup($draft_id)
745  {
746  global $DIC;
747  $ilDB = $DIC->database();
748 
749  $res = $ilDB->queryF(
750  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
751  array('integer'),
752  array((int) $draft_id)
753  );
754 
755  while ($row = $ilDB->fetchAssoc($res)) {
756  $tmp_obj = new self;
757  self::populateWithDatabaseRecord($tmp_obj, $row);
758  }
759 
760  $history_obj = new ilForumDraftsHistory();
761  $history_obj->deleteHistoryByDraftIds(array($draft_id));
762 
763  $history_obj->setDraftId($draft_id);
764  $history_obj->setPostSubject($tmp_obj->getPostSubject());
765  $history_obj->setPostMessage($tmp_obj->getPostMessage());
766  $history_obj->addDraftToHistory();
767 
769  $tmp_obj->getPostMessage(),
770  self::MEDIAOBJECT_TYPE,
771  $draft_id,
773  $history_obj->getHistoryId()
774  );
775  }
776 }
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
global $DIC
Definition: saml.php:7
deleteDraftsByDraftIds(array $draft_ids=array())
setPostUserAlias($post_user_alias)
static getInstancesByUserIdAndThreadId($user_id, $thread_id)
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)
setPostMessage($post_message)
static newInstanceByDraftId($draft_id)
$ilUser
Definition: imgupload.php:18
Class ilObjMediaObject.
setPostSubject($post_subject)
setPostAuthorId($post_author_id)
static moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
$row
static newInstanceByHistorytId($history_id)
static deleteDraftsByUserId($user_id)
global $ilDB
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)