ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
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
198 {
199 $this->post_subject = $post_subject;
200 }
201
205 public function getPostMessage()
206 {
207 return $this->post_message;
208 }
209
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
246 {
247 $this->post_update = $post_update;
248 }
249
253 public function getUpdateUserId()
254 {
256 }
257
262 {
263 $this->update_user_id = $update_user_id;
264 }
265
269 public function getPostUserAlias()
270 {
272 }
273
278 {
279 $this->post_user_alias = $post_user_alias;
280 }
281
285 public function getPostAuthorId()
286 {
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)) {
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,
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();
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 (!self::$instances[$user_id]) {
446 self::readDrafts($user_id);
447 }
448
449 return self::$instances[$user_id]['draft_ids'];
450 }
451
457 public static function getInstancesByUserIdAndThreadId($user_id, $thread_id) : array
458 {
459 if (!self::$instances[$user_id]) {
460 self::readDrafts($user_id);
461 }
462
463 if (isset(self::$instances[$user_id][$thread_id])) {
464 return self::$instances[$user_id][$thread_id];
465 }
466
467 return [];
468 }
469
474 public static function newInstanceByDraftId($draft_id)
475 {
476 global $DIC;
477 $ilDB = $DIC->database();
478
479 $res = $ilDB->queryF(
480 'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
481 array('integer'),
482 array($draft_id)
483 );
484
485 $tmp_obj = new ilForumPostDraft();
486 while ($row = $ilDB->fetchAssoc($res)) {
487 self::populateWithDatabaseRecord($tmp_obj, $row);
488 }
489 return $tmp_obj;
490 }
491
497 public static function newInstanceByHistorytId($history_id)
498 {
499 global $DIC;
500 $ilDB = $DIC->database();
501
502 $res = $ilDB->queryF(
503 'SELECT * FROM frm_drafts_history WHERE history_id = %s',
504 array('integer'),
505 array($history_id)
506 );
507
508 while ($row = $ilDB->fetchAssoc($res)) {
509 $tmp_obj = new ilForumPostDraft();
510 self::populateWithDatabaseRecord($tmp_obj, $row);
511 return $tmp_obj;
512 }
513
514 throw new ilException(sprintf("Could not find history object for id %s", $history_id));
515 }
516
517 public function saveDraft()
518 {
519 $draft_id = $this->db->nextId('frm_posts_drafts');
520 $post_date = date("Y-m-d H:i:s");
521
522 $this->db->insert('frm_posts_drafts', array(
523 'draft_id' => array('integer', $draft_id),
524 'post_id' => array('integer', $this->getPostId()),
525 'thread_id' => array('integer', $this->getThreadId()),
526 'forum_id' => array('integer', $this->getForumId()),
527 'post_author_id' => array('integer', $this->getPostAuthorId()),
528 'post_subject' => array('text', $this->getPostSubject()),
529 'post_message' => array('clob', $this->getPostMessage()),
530 'notify' => array('integer', $this->getNotify()),
531 'post_notify' => array('integer', $this->getPostNotify()),
532 'post_date' => array('timestamp', $post_date),
533 'post_update' => array('timestamp', $post_date),
534// 'update_user_id' => array('integer', $this->getUpdateUserId()),
535 'post_user_alias' => array('text', $this->getPostUserAlias()),
536 'pos_display_usr_id' => array('integer', $this->getPostDisplayUserId())
537 ));
538 $this->setDraftId($draft_id);
539 return $draft_id;
540 }
541
542 public function updateDraft()
543 {
544 $this->db->update(
545 'frm_posts_drafts',
546 array(
547 'post_subject' => array('text', $this->getPostSubject()),
548 'post_message' => array('clob', $this->getPostMessage()),
549 'post_user_alias' => array('text', $this->getPostUserAlias())
550 ),
551 array('draft_id' => array('integer', $this->getDraftId()))
552 );
553 }
554
555 public function deleteDraft()
556 {
557 $this->db->manipulateF(
558 'DELETE FROM frm_posts_drafts WHERE draft_id = %s',
559 array('integer'),
560 array($this->getDraftId())
561 );
562 }
563
567 public static function deleteMobsOfDraft($draft_id)
568 {
569 // delete mobs of draft
570 $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft_id);
571 foreach ($oldMediaObjects as $oldMob) {
572 if (ilObjMediaObject::_exists($oldMob)) {
573 ilObjMediaObject::_removeUsage($oldMob, 'frm~d:html', $draft_id);
574 $mob_obj = new ilObjMediaObject($oldMob);
575 $mob_obj->delete();
576 }
577 }
578 }
579
583 public function deleteDraftsByPostIds(array $post_ids = array())
584 {
585 $draft_ids = array();
586 $res = $this->db->query('SELECT draft_id FROM frm_posts_drafts WHERE ' . $this->db->in('post_id', $post_ids, false, 'integer'));
587 while ($row = $this->db->fetchAssoc($res)) {
588 $draft_ids[] = $row['draft_id'];
589 }
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 function deleteDraftsByDraftIds(array $draft_ids = array())
606 {
607 foreach ($draft_ids as $draft_id) {
608 self::deleteMobsOfDraft($draft_id);
609
610 // delete attachments of draft
611 $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
612 $objFileDataForumDrafts->delete();
613 }
614 $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
615 $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
616 }
617
621 public static function deleteDraftsByUserId($user_id)
622 {
623 global $DIC;
624 $ilDB = $DIC->database();
625
626 $res = $ilDB->queryF(
627 'SELECT draft_id FROM frm_posts_drafts WHERE post_author_id = %s',
628 array('integer'),
629 array($user_id)
630 );
631
632 $draft_ids = array();
633 while ($row = $ilDB->fetchAssoc($res)) {
634 $draft_ids[] = $row['draft_id'];
635 }
636
637 foreach ($draft_ids as $draft_id) {
638 self::deleteMobsOfDraft($draft_id);
639
640 // delete attachments of draft
641 $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
642 $objFileDataForumDrafts->delete();
643 }
644
645 $ilDB->manipulate('DELETE FROM frm_drafts_history WHERE ' . $ilDB->in('draft_id', $draft_ids, false, 'integer'));
646 $ilDB->manipulateF(
647 'DELETE FROM frm_posts_drafts WHERE post_author_id = %s',
648 array('integer'),
649 array($user_id)
650 );
651 }
652
656 public static function isSavePostDraftAllowed()
657 {
658 if (!isset(self::$drafts_settings_cache['save_post_drafts'])) {
659 global $DIC;
660 self::$drafts_settings_cache['save_post_drafts'] = (bool) $DIC->settings()->get('save_post_drafts', false);
661 }
662 return self::$drafts_settings_cache['save_post_drafts'];
663 }
664
668 public static function isAutoSavePostDraftAllowed()
669 {
670 if (!self::isSavePostDraftAllowed()) {
671 // feature is globally deactivated
672 return false;
673 }
674 if (!isset(self::$drafts_settings_cache['autosave_drafts'])) {
675 global $DIC;
676
677 self::$drafts_settings_cache['autosave_drafts'] = (bool) $DIC->settings()->get('autosave_drafts', false);
678 self::$drafts_settings_cache['autosave_drafts_ival'] = (int) $DIC->settings()->get('autosave_drafts_ival', 30);
679 }
680 return self::$drafts_settings_cache['autosave_drafts'];
681 }
682
683 public static function lookupAutosaveInterval()
684 {
685 if (self::isAutoSavePostDraftAllowed()) {
686 return self::$drafts_settings_cache['autosave_drafts_ival'];
687 }
688 return 0;
689 }
690
695 public static function getDraftsStatisticsByRefId($ref_id)
696 {
697 global $DIC;
698 $ilDB = $DIC->database();
699 $ilUser = $DIC->user();
700
701 if (!isset(self::$forum_statistics_cache[$ref_id][$ilUser->getId()])) {
702 $forumId = ilObjForum::lookupForumIdByRefId($ref_id);
703
704 $res = $ilDB->queryF(
705 '
706 SELECT COUNT(draft_id) num_drafts, thread_id FROM frm_posts_drafts
707 WHERE forum_id = %s AND post_author_id = %s
708 GROUP BY thread_id',
709 array('integer', 'integer'),
710 array($forumId, $ilUser->getId())
711 );
712
713 $num_drafts_total = 0;
714
715 while ($row = $ilDB->fetchAssoc($res)) {
716 $num_drafts_total += $row['num_drafts'];
717 self::$forum_statistics_cache[$ref_id][$ilUser->getId()][$row['thread_id']] = $row['num_drafts'];
718 }
719
720 self::$forum_statistics_cache[$ref_id][$ilUser->getId()]['total'] = $num_drafts_total;
721 }
722 return self::$forum_statistics_cache[$ref_id][$ilUser->getId()];
723 }
724
729 public static function moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
730 {
731 global $DIC;
732 $ilDB = $DIC->database();
733
734 $ilDB->update(
735 'frm_posts_drafts',
736 array('thread_id' => array('integer', $target_thread_id)),
737 array('thread_id' => array('integer', $source_thread_id))
738 );
739 }
740
746 public static function moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
747 {
748 global $DIC;
749 $ilDB = $DIC->database();
750
751 $source_forum_id = ilObjForum::lookupForumIdByRefId($source_ref_id);
752 $target_forum_id = ilObjForum::lookupForumIdByRefId($target_ref_id);
753
754 $ilDB->manipulateF(
755 '
756 UPDATE frm_posts_drafts
757 SET forum_id = %s
758 WHERE forum_id = %s
759 AND ' . $ilDB->in('thread_id', $thread_ids, false, 'integer'),
760 array('integer', 'integer'),
761 array($target_forum_id, $source_forum_id)
762 );
763 }
764
770 public static function getThreadDraftData($post_author_id, $forum_id)
771 {
772 global $DIC;
773 $ilDB = $DIC->database();
774
775 $res = $ilDB->queryF(
776 'SELECT * FROM frm_posts_drafts
777 WHERE post_author_id = %s
778 AND forum_id = %s
779 AND thread_id = %s
780 AND post_id = %s
781 ORDER BY post_date DESC',
782 array('integer', 'integer', 'integer', 'integer'),
783 array($post_author_id, $forum_id, 0, 0)
784 );
785 $draft_data = array();
786 while ($row = $ilDB->fetchAssoc($res)) {
787 $tmp_obj = new self;
788 self::populateWithDatabaseRecord($tmp_obj, $row);
789 $draft_data[] = array('subject' => $tmp_obj->getPostSubject(), 'post_update' => $tmp_obj->getPostUpdate(), 'draft_id' => $tmp_obj->getDraftId());
790 }
791 return $draft_data;
792 }
793
797 public static function createDraftBackup($draft_id)
798 {
799 global $DIC;
800 $ilDB = $DIC->database();
801
802 $res = $ilDB->queryF(
803 'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
804 array('integer'),
805 array((int) $draft_id)
806 );
807
808 while ($row = $ilDB->fetchAssoc($res)) {
809 $tmp_obj = new self;
810 self::populateWithDatabaseRecord($tmp_obj, $row);
811 }
812
813 $history_obj = new ilForumDraftsHistory();
814 $history_obj->deleteHistoryByDraftIds(array($draft_id));
815
816 $history_obj->setDraftId($draft_id);
817 $history_obj->setPostSubject($tmp_obj->getPostSubject());
818 $history_obj->setPostMessage($tmp_obj->getPostMessage());
819 $history_obj->addDraftToHistory();
820
822 $tmp_obj->getPostMessage(),
823 self::MEDIAOBJECT_TYPE,
824 $draft_id,
826 $history_obj->getHistoryId()
827 );
828 }
829}
An exception for terminatinating execution or to throw for unit testing.
Base class for ILIAS Exception handling.
This class handles all operations on files for the drafts of a forum object.
Class ilForumDraftHistory.
Class ilForumPostDraft.
setPostUserAlias($post_user_alias)
static moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
deleteDraftsByDraftIds(array $draft_ids=array())
__construct($user_id=0, $post_id=0, $draft_id=0)
ilForumPostDraft constructor.
static newInstanceByDraftId($draft_id)
static populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
static getInstancesByUserIdAndThreadId($user_id, $thread_id)
static newInstanceByHistorytId($history_id)
static readDrafts($user_id)
setPostMessage($post_message)
static moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
static deleteDraftsByUserId($user_id)
static getDraftInstancesByUserId($user_id)
static createDraftBackup($draft_id)
static getThreadDraftData($post_author_id, $forum_id)
setPostSubject($post_subject)
setPostAuthorId($post_author_id)
deleteDraftsByPostIds(array $post_ids=array())
setPostDisplayUserId($post_display_user_id)
static deleteMobsOfDraft($draft_id)
static getSortedDrafts(int $usrId, int $threadId, int $sorting=ilForumProperties::VIEW_DATE_ASC)
static getDraftsStatisticsByRefId($ref_id)
setUpdateUserId($update_user_id)
static moveMediaObjects($post_message, $source_type, $source_id, $target_type, $target_id, $direction=0)
static lookupForumIdByRefId($ref_id)
Class ilObjMediaObject.
static _getMobsOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
get mobs of 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 _exists($a_id, $a_reference=false, $a_type=null)
checks wether a lm content object with specified id exists or not
foreach($_POST as $key=> $value) $res
global $ilDB
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46