ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 */
3require_once './Modules/Forum/classes/class.ilFileDataForumDrafts.php';
9{
10 const MEDIAOBJECT_TYPE = 'frm~d:html';
14 protected $draft_id = 0;
18 protected $post_id = 0;
22 protected $forum_id = 0;
26 protected $thread_id = 0;
30 protected $post_subject = '';
34 protected $post_message = '';
38 protected $post_date = '0000-00-00 00:00:00';
42 protected $post_update = '0000-00-00 00:00:00';
46 protected $update_user_id = 0;
50 protected $post_user_alias = '';
54 protected $post_author_id = 0;
58 protected $post_display_user_id = 0;
59
63 protected $notify = 0;
64 protected $post_notify = 0;
65
69 private static $instances = array();
70
75 protected static $forum_statistics_cache = array();
76
80 protected static $drafts_settings_cache = array();
81
86 protected static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
87 {
88 $draft->setDraftId($row['draft_id']);
89 $draft->setForumId($row['forum_id']);
90 $draft->setPostAuthorId($row['post_author_id']);
91 $draft->setPostDate($row['post_date']);
92 $draft->setPostDisplayUserId($row['pos_display_usr_id']);
93 $draft->setPostId($row['post_id']);
94 $draft->setPostMessage($row['post_message']);
95 $draft->setPostSubject($row['post_subject']);
96 $draft->setPostUpdate($row['post_update']);
97 $draft->setPostUserAlias($row['post_user_alias']);
98 $draft->setThreadId($row['thread_id']);
99 $draft->setUpdateUserId($row['update_user_id']);
100 $draft->setNotify($row['notify']);
101 $draft->setPostNotify($row['post_notify']);
102 }
103
107 public function getPostNotify()
108 {
109 return $this->post_notify;
110 }
111
116 {
117 $this->post_notify = $post_notify;
118 }
119
123 public function getDraftId()
124 {
125 return $this->draft_id;
126 }
127
131 public function setDraftId($draft_id)
132 {
133 $this->draft_id = $draft_id;
134 }
135
139 public function getPostId()
140 {
141 return $this->post_id;
142 }
143
147 public function setPostId($post_id)
148 {
149 $this->post_id = $post_id;
150 }
151
155 public function getForumId()
156 {
157 return $this->forum_id;
158 }
159
163 public function setForumId($forum_id)
164 {
165 $this->forum_id = $forum_id;
166 }
167
171 public function getThreadId()
172 {
173 return $this->thread_id;
174 }
175
179 public function setThreadId($thread_id)
180 {
181 $this->thread_id = $thread_id;
182 }
183
187 public function getPostSubject()
188 {
189 return $this->post_subject;
190 }
191
196 {
197 $this->post_subject = $post_subject;
198 }
199
203 public function getPostMessage()
204 {
205 return $this->post_message;
206 }
207
212 {
213 $this->post_message = $post_message;
214 }
215
219 public function getPostDate()
220 {
221 return $this->post_date;
222 }
223
227 public function setPostDate($post_date)
228 {
229 $this->post_date = $post_date;
230 }
231
235 public function getPostUpdate()
236 {
237 return $this->post_update;
238 }
239
244 {
245 $this->post_update = $post_update;
246 }
247
251 public function getUpdateUserId()
252 {
254 }
255
260 {
261 $this->update_user_id = $update_user_id;
262 }
263
267 public function getPostUserAlias()
268 {
270 }
271
276 {
277 $this->post_user_alias = $post_user_alias;
278 }
279
283 public function getPostAuthorId()
284 {
286 }
287
292 {
293 $this->post_author_id = $post_author_id;
294 }
295
299 public function getPostDisplayUserId()
300 {
302 }
303
308 {
309 $this->post_display_user_id = $post_display_user_id;
310 }
311
315 public function getNotify()
316 {
317 return $this->notify;
318 }
319
323 public function setNotify($notify)
324 {
325 $this->notify = $notify;
326 }
327
333 public function __construct($user_id = 0, $post_id = 0, $draft_id = 0)
334 {
335 global $DIC;
336
337 $this->db = $DIC->database();
338
339 if ($user_id && $post_id && $draft_id) {
340 $this->setPostAuthorId($user_id);
341 $this->setPostId($post_id);
342 $this->setDraftId($draft_id);
343 $this->readDraft();
344 }
345 }
346
350 protected function readDraft()
351 {
352 $res = $this->db->queryF(
353 'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND post_id = %s AND draft_id = %s',
354 array('integer', 'integer','integer'),
355 array($this->getPostAuthorId(), $this->getPostId(), $this->getDraftId())
356 );
357
358 while ($row = $this->db->fetchAssoc($res)) {
360 }
361 }
362
366 protected static function readDrafts($user_id)
367 {
368 global $DIC;
369 $ilDB = $DIC->database();
370
371 $res = $ilDB->queryF(
372 'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s',
373 array('integer'),
374 array($user_id)
375 );
376
377 self::$instances[$user_id] = array();
378 while ($row = $ilDB->fetchAssoc($res)) {
379 $tmp_obj = new ilForumPostDraft();
381 self::$instances[$user_id][$row['thread_id']][$tmp_obj->getPostId()][] = $tmp_obj;
382 self::$instances[$user_id]['draft_ids'][$tmp_obj->getDraftId()] = $tmp_obj;
383 }
384 unset($tmp_obj);
385 }
386
391 public static function getDraftInstancesByUserId($user_id)
392 {
393 if (!self::$instances[$user_id]) {
394 self::readDrafts($user_id);
395 }
396
397 return self::$instances[$user_id]['draft_ids'];
398 }
399
405 public static function getInstancesByUserIdAndThreadId($user_id, $thread_id)
406 {
407 if (!self::$instances[$user_id]) {
408 self::readDrafts($user_id);
409 }
410
411 if (isset(self::$instances[$user_id][$thread_id])) {
412 return self::$instances[$user_id][$thread_id];
413 }
414
415 return [];
416 }
417
422 public static function newInstanceByDraftId($draft_id)
423 {
424 global $DIC;
425 $ilDB = $DIC->database();
426
427 $res = $ilDB->queryF(
428 'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
429 array('integer'),
430 array($draft_id)
431 );
432
433 $tmp_obj = new ilForumPostDraft();
434 while ($row = $ilDB->fetchAssoc($res)) {
436 }
437 return $tmp_obj;
438 }
439
445 public static function newInstanceByHistorytId($history_id)
446 {
447 global $DIC;
448 $ilDB = $DIC->database();
449
450 $res = $ilDB->queryF(
451 'SELECT * FROM frm_drafts_history WHERE history_id = %s',
452 array('integer'),
453 array($history_id)
454 );
455
456 while ($row = $ilDB->fetchAssoc($res)) {
457 $tmp_obj = new ilForumPostDraft();
459 return $tmp_obj;
460 }
461
462 throw new ilException(sprintf("Could not find history object for id %s", $history_id));
463 }
464
465 public function saveDraft()
466 {
467 $draft_id = $this->db->nextId('frm_posts_drafts');
468 $post_date = date("Y-m-d H:i:s");
469
470 $this->db->insert('frm_posts_drafts', array(
471 'draft_id' => array('integer', $draft_id),
472 'post_id' => array('integer', $this->getPostId()),
473 'thread_id' => array('integer', $this->getThreadId()),
474 'forum_id' => array('integer', $this->getForumId()),
475 'post_author_id' => array('integer', $this->getPostAuthorId()),
476 'post_subject' => array('text', $this->getPostSubject()),
477 'post_message' => array('clob', $this->getPostMessage()),
478 'notify' => array('integer', $this->getNotify()),
479 'post_notify' => array('integer', $this->getPostNotify()),
480 'post_date' => array('timestamp', $post_date),
481 'post_update' => array('timestamp', $post_date),
482// 'update_user_id' => array('integer', $this->getUpdateUserId()),
483 'post_user_alias' => array('text', $this->getPostUserAlias()),
484 'pos_display_usr_id' => array('integer', $this->getPostDisplayUserId())
485 ));
486 $this->setDraftId($draft_id);
487 return $draft_id;
488 }
489
490 public function updateDraft()
491 {
492 $this->db->update(
493 'frm_posts_drafts',
494 array(
495 'post_subject' => array('text', $this->getPostSubject()),
496 'post_message' => array('clob', $this->getPostMessage()),
497 'notify' => array('integer', $this->getNotify()),
498 'post_notify' => array('integer', $this->getPostNotify()),
499 'post_update' => array('timestamp', date("Y-m-d H:i:s")),
500 'update_user_id' => array('integer', $this->getUpdateUserId()),
501 'post_user_alias' => array('text', $this->getPostUserAlias()),
502 'pos_display_usr_id' => array('integer', $this->getPostDisplayUserId())
503 ),
504 array('draft_id' => array('integer', $this->getDraftId()))
505 );
506 }
507
508 public function deleteDraft()
509 {
510 $this->db->manipulateF(
511 'DELETE FROM frm_posts_drafts WHERE draft_id = %s',
512 array('integer'),
513 array($this->getDraftId())
514 );
515 }
516
520 public static function deleteMobsOfDraft($draft_id)
521 {
522 require_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
523 // delete mobs of draft
524 $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft_id);
525 foreach ($oldMediaObjects as $oldMob) {
526 if (ilObjMediaObject::_exists($oldMob)) {
527 ilObjMediaObject::_removeUsage($oldMob, 'frm~d:html', $draft_id);
528 $mob_obj = new ilObjMediaObject($oldMob);
529 $mob_obj->delete();
530 }
531 }
532 }
533
537 public function deleteDraftsByPostIds(array $post_ids = array())
538 {
539 $draft_ids = array();
540 $res = $this->db->query('SELECT draft_id FROM frm_posts_drafts WHERE ' . $this->db->in('post_id', $post_ids, false, 'integer'));
541 while ($row = $this->db->fetchAssoc($res)) {
542 $draft_ids[] = $row['draft_id'];
543 }
544
545 foreach ($draft_ids as $draft_id) {
547
548 // delete attachments of draft
549 $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
550 $objFileDataForumDrafts->delete();
551 }
552 $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
553 $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
554 }
555
559 public function deleteDraftsByDraftIds(array $draft_ids = array())
560 {
561 foreach ($draft_ids as $draft_id) {
563
564 // delete attachments of draft
565 $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
566 $objFileDataForumDrafts->delete();
567 }
568 $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
569 $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in('draft_id', $draft_ids, false, 'integer'));
570 }
571
575 public static function deleteDraftsByUserId($user_id)
576 {
577 global $DIC;
578 $ilDB = $DIC->database();
579
580 $res = $ilDB->queryF(
581 'SELECT draft_id FROM frm_posts_drafts WHERE post_author_id = %s',
582 array('integer'),
583 array($user_id)
584 );
585
586 $draft_ids = array();
587 while ($row = $ilDB->fetchAssoc($res)) {
588 $draft_ids[] = $row['draft_id'];
589 }
590
591 foreach ($draft_ids as $draft_id) {
593
594 // delete attachments of draft
595 $objFileDataForumDrafts = new ilFileDataForumDrafts(0, $draft_id);
596 $objFileDataForumDrafts->delete();
597 }
598
599 $ilDB->manipulate('DELETE FROM frm_drafts_history WHERE ' . $ilDB->in('draft_id', $draft_ids, false, 'integer'));
600 $ilDB->manipulateF(
601 'DELETE FROM frm_posts_drafts WHERE post_author_id = %s',
602 array('integer'),
603 array($user_id)
604 );
605 }
606
610 public static function isSavePostDraftAllowed()
611 {
612 if (!isset(self::$drafts_settings_cache['save_post_drafts'])) {
613 global $DIC;
614 self::$drafts_settings_cache['save_post_drafts'] = (bool) $DIC->settings()->get('save_post_drafts', false);
615 }
616 return self::$drafts_settings_cache['save_post_drafts'];
617 }
618
622 public static function isAutoSavePostDraftAllowed()
623 {
624 if (!self::isSavePostDraftAllowed()) {
625 // feature is globally deactivated
626 return false;
627 }
628 if (!isset(self::$drafts_settings_cache['autosave_drafts'])) {
629 global $DIC;
630
631 self::$drafts_settings_cache['autosave_drafts'] = (bool) $DIC->settings()->get('autosave_drafts', false);
632 self::$drafts_settings_cache['autosave_drafts_ival'] = (int) $DIC->settings()->get('autosave_drafts_ival', 30);
633 }
634 return self::$drafts_settings_cache['autosave_drafts'];
635 }
636
637 public static function lookupAutosaveInterval()
638 {
639 if (self::isAutoSavePostDraftAllowed()) {
640 return self::$drafts_settings_cache['autosave_drafts_ival'];
641 }
642 return 0;
643 }
644
649 public static function getDraftsStatisticsByRefId($ref_id)
650 {
651 global $DIC;
652 $ilDB = $DIC->database();
653 $ilUser = $DIC->user();
654
655 if (!isset(self::$forum_statistics_cache[$ref_id][$ilUser->getId()])) {
656 $forumId = ilObjForum::lookupForumIdByRefId($ref_id);
657
658 $res = $ilDB->queryF(
659 '
660 SELECT COUNT(draft_id) num_drafts, thread_id FROM frm_posts_drafts
661 WHERE forum_id = %s AND post_author_id = %s
662 GROUP BY thread_id',
663 array('integer', 'integer'),
664 array($forumId, $ilUser->getId())
665 );
666
667 $num_drafts_total = 0;
668
669 while ($row = $ilDB->fetchAssoc($res)) {
670 $num_drafts_total += $row['num_drafts'];
671 self::$forum_statistics_cache[$ref_id][$ilUser->getId()][$row['thread_id']] = $row['num_drafts'];
672 }
673
674 self::$forum_statistics_cache[$ref_id][$ilUser->getId()]['total'] = $num_drafts_total;
675 }
676 return self::$forum_statistics_cache[$ref_id][$ilUser->getId()];
677 }
678
683 public static function moveDraftsByMergedThreads($source_thread_id, $target_thread_id)
684 {
685 global $DIC;
686 $ilDB = $DIC->database();
687
688 $ilDB->update(
689 'frm_posts_drafts',
690 array('thread_id' => array('integer', $target_thread_id)),
691 array('thread_id' => array('integer', $source_thread_id))
692 );
693 }
694
700 public static function moveDraftsByMovedThread($thread_ids, $source_ref_id, $target_ref_id)
701 {
702 global $DIC;
703 $ilDB = $DIC->database();
704
705 $source_forum_id = ilObjForum::lookupForumIdByRefId($source_ref_id);
706 $target_forum_id = ilObjForum::lookupForumIdByRefId($target_ref_id);
707
708 $ilDB->manipulateF(
709 '
710 UPDATE frm_posts_drafts
711 SET forum_id = %s
712 WHERE forum_id = %s
713 AND ' . $ilDB->in('thread_id', $thread_ids, false, 'integer'),
714 array('integer', 'integer'),
715 array($target_forum_id, $source_forum_id)
716 );
717 }
718
725 {
726 global $DIC;
727 $ilDB = $DIC->database();
728
729 $res = $ilDB->queryF(
730 'SELECT * FROM frm_posts_drafts
731 WHERE post_author_id = %s
732 AND forum_id = %s
733 AND thread_id = %s
734 AND post_id = %s
735 ORDER BY post_date DESC',
736 array('integer', 'integer', 'integer', 'integer'),
737 array($post_author_id, $forum_id, 0, 0)
738 );
739 $draft_data = array();
740 while ($row = $ilDB->fetchAssoc($res)) {
741 $tmp_obj = new self;
743 $draft_data[] = array('subject'=> $tmp_obj->getPostSubject(), 'post_update' => $tmp_obj->getPostUpdate(), 'draft_id' => $tmp_obj->getDraftId());
744 }
745 return $draft_data;
746 }
747
751 public static function createDraftBackup($draft_id)
752 {
753 global $DIC;
754 $ilDB = $DIC->database();
755
756 $res = $ilDB->queryF(
757 'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
758 array('integer'),
759 array((int) $draft_id)
760 );
761
762 while ($row = $ilDB->fetchAssoc($res)) {
763 $tmp_obj = new self;
765 }
766
767 $history_obj = new ilForumDraftsHistory();
768 $history_obj->deleteHistoryByDraftIds(array($draft_id));
769
770 $history_obj->setDraftId($draft_id);
771 $history_obj->setPostSubject($tmp_obj->getPostSubject());
772 $history_obj->setPostMessage($tmp_obj->getPostMessage());
773 $history_obj->addDraftToHistory();
774
776 $tmp_obj->getPostMessage(),
777 self::MEDIAOBJECT_TYPE,
778 $draft_id,
780 $history_obj->getHistoryId()
781 );
782 }
783}
sprintf('%.4f', $callTime)
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
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 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
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
global $ilDB
$ilUser
Definition: imgupload.php:18