ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilForumPostDraft.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  public const MEDIAOBJECT_TYPE = 'frm~d:html';
28 
30  private static array $instances = [];
31  private static array $forum_statistics_cache = [];
32  private static array $drafts_settings_cache = [];
33 
34  public const NO_RCID = '-';
35  private readonly ilDBInterface $db;
36  private int $draft_id = 0;
37  private int $post_id = 0;
38  private int $forum_id = 0;
39  private int $thread_id = 0;
40  private string $post_subject = '';
41  private string $post_message = '';
42  private string $post_date = '0000-00-00 00:00:00';
43  private string $post_update = '0000-00-00 00:00:00';
44  private int $update_user_id = 0;
45  private string $post_user_alias = '';
46  private int $post_author_id = 0;
47  private int $post_display_user_id = 0;
48  private bool $notify = false;
49  private bool $post_notify = false;
50  private string $rcid = '';
51 
52  public function __construct(int $user_id = 0, int $post_id = 0, int $draft_id = 0)
53  {
54  global $DIC;
55 
56  $this->db = $DIC->database();
57 
58  if ($user_id && $post_id && $draft_id) {
59  $this->setPostAuthorId($user_id);
60  $this->setPostId($post_id);
61  $this->setDraftId($draft_id);
62  $this->readDraft();
63  }
64  }
65 
66  private static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row): void
67  {
68  $draft->setDraftId((int) $row['draft_id']);
69  $draft->setForumId((int) $row['forum_id']);
70  $draft->setThreadId((int) $row['thread_id']);
71  $draft->setPostId((int) $row['post_id']);
72  $draft->setPostAuthorId((int) $row['post_author_id']);
73  $draft->setPostDisplayUserId((int) $row['pos_display_usr_id']);
74  $draft->setUpdateUserId((int) $row['update_user_id']);
75  $draft->setPostSubject((string) $row['post_subject']);
76  $draft->setPostMessage((string) $row['post_message']);
77  $draft->setPostDate((string) $row['post_date']);
78  $draft->setPostUpdate((string) $row['post_update']);
79  $draft->setPostUserAlias((string) $row['post_user_alias']);
80  $draft->setNotificationStatus((bool) $row['notify']);
81  $draft->setPostNotificationStatus((bool) $row['post_notify']);
82  $draft->setRCID((string) ($row['rcid']));
83  }
84 
85  public function getRCID(): string
86  {
87  return $this->rcid;
88  }
89 
90  public function setRCID(string $rcid): void
91  {
92  $this->rcid = $rcid;
93  }
94 
95  public function isPostNotificationEnabled(): bool
96  {
97  return $this->post_notify;
98  }
99 
100  public function setPostNotificationStatus(bool $post_notify): void
101  {
102  $this->post_notify = $post_notify;
103  }
104 
105  public function isNotificationEnabled(): bool
106  {
107  return $this->notify;
108  }
109 
110  public function setNotificationStatus(bool $notify): void
111  {
112  $this->notify = $notify;
113  }
114 
115  public function getDraftId(): int
116  {
117  return $this->draft_id;
118  }
119 
120  public function setDraftId(int $draft_id): void
121  {
122  $this->draft_id = $draft_id;
123  }
124 
125  public function getPostId(): int
126  {
127  return $this->post_id;
128  }
129 
130  public function setPostId(int $post_id): void
131  {
132  $this->post_id = $post_id;
133  }
134 
135  public function getForumId(): int
136  {
137  return $this->forum_id;
138  }
139 
140  public function setForumId(int $forum_id): void
141  {
142  $this->forum_id = $forum_id;
143  }
144 
145  public function getThreadId(): int
146  {
147  return $this->thread_id;
148  }
149 
150  public function setThreadId(int $thread_id): void
151  {
152  $this->thread_id = $thread_id;
153  }
154 
155  public function getPostSubject(): string
156  {
157  return $this->post_subject;
158  }
159 
160  public function setPostSubject(string $post_subject): void
161  {
162  $this->post_subject = $post_subject;
163  }
164 
165  public function getPostMessage(): string
166  {
167  return $this->post_message;
168  }
169 
170  public function setPostMessage(string $post_message): void
171  {
172  $this->post_message = $post_message;
173  }
174 
175  public function getPostDate(): string
176  {
177  return $this->post_date;
178  }
179 
180  public function setPostDate(string $post_date): void
181  {
182  $this->post_date = $post_date;
183  }
184 
185  public function getPostUpdate(): string
186  {
187  return $this->post_update;
188  }
189 
190  public function setPostUpdate(string $post_update): void
191  {
192  $this->post_update = $post_update;
193  }
194 
195  public function getUpdateUserId(): int
196  {
197  return $this->update_user_id;
198  }
199 
200  public function setUpdateUserId(int $update_user_id): void
201  {
202  $this->update_user_id = $update_user_id;
203  }
204 
205  public function getPostUserAlias(): string
206  {
207  return $this->post_user_alias;
208  }
209 
210  public function setPostUserAlias(string $post_user_alias): void
211  {
212  $this->post_user_alias = $post_user_alias;
213  }
214 
215  public function getPostAuthorId(): int
216  {
217  return $this->post_author_id;
218  }
219 
220  public function setPostAuthorId(int $post_author_id): void
221  {
222  $this->post_author_id = $post_author_id;
223  }
224 
225  public function getPostDisplayUserId(): int
226  {
228  }
229 
230  public function setPostDisplayUserId(int $post_display_user_id): void
231  {
232  $this->post_display_user_id = $post_display_user_id;
233  }
234 
235  protected function readDraft(): void
236  {
237  $res = $this->db->queryF(
238  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND draft_id = %s',
239  ['integer', 'integer'],
240  [$this->getPostAuthorId(), $this->getDraftId()]
241  );
242 
243  if ($row = $this->db->fetchAssoc($res)) {
244  self::populateWithDatabaseRecord($this, $row);
245  }
246  }
247 
248  protected static function readDrafts(int $user_id): void
249  {
250  global $DIC;
251  $ilDB = $DIC->database();
252 
253  $res = $ilDB->queryF(
254  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s',
255  ['integer'],
256  [$user_id]
257  );
258 
259  self::$instances[$user_id] = [
260  'draft_ids' => [],
261  ];
262 
263  while ($row = $ilDB->fetchAssoc($res)) {
264  $draft = new ilForumPostDraft();
265  self::populateWithDatabaseRecord($draft, $row);
266  self::$instances[$user_id][$row['thread_id']][$draft->getPostId()][] = $draft;
267  self::$instances[$user_id]['draft_ids'][$draft->getDraftId()] = $draft;
268  }
269  }
270 
274  public static function getSortedDrafts(
275  int $usrId,
276  int $threadId,
277  int $sorting = ilForumProperties::VIEW_DATE_ASC
278  ): array {
279  global $DIC;
280  $ilDB = $DIC->database();
281 
282  $drafts = [];
283 
284  $orderColumn = ' ';
285  $orderDirection = ' ';
286 
287  if ($sorting !== ilForumProperties::VIEW_TREE) {
288  $orderColumn = ' ORDER BY post_date ';
289  $orderDirection = 'ASC';
290  if ($sorting === ilForumProperties::VIEW_DATE_DESC) {
291  $orderDirection = 'DESC';
292  }
293  }
294 
295  $res = $ilDB->queryF(
296  'SELECT * FROM frm_posts_drafts WHERE post_author_id = %s AND thread_id = %s' .
297  $orderColumn . $orderDirection,
298  ['integer', 'integer'],
299  [$usrId, $threadId]
300  );
301 
302  while ($row = $ilDB->fetchAssoc($res)) {
303  $draft = new ilForumPostDraft();
304  self::populateWithDatabaseRecord($draft, $row);
305  $drafts[] = $draft;
306  self::$instances[$usrId][$threadId][$draft->getPostId()][] = $draft;
307  }
308 
309  if (ilForumProperties::VIEW_TREE === $sorting) {
310  return self::$instances[$usrId][$threadId] ?? [];
311  }
312 
313  return $drafts;
314  }
315 
319  public static function getDraftInstancesByUserId(int $user_id): array
320  {
321  if (!isset(self::$instances[$user_id])) {
322  self::readDrafts($user_id);
323  }
324 
325  return self::$instances[$user_id]['draft_ids'];
326  }
327 
328  public static function newInstanceByDraftId(int $draft_id): ilForumPostDraft
329  {
330  global $DIC;
331  $ilDB = $DIC->database();
332 
333  $res = $ilDB->queryF(
334  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
335  ['integer'],
336  [$draft_id]
337  );
338 
339  $tmp_obj = new ilForumPostDraft();
340  while ($row = $ilDB->fetchAssoc($res)) {
341  self::populateWithDatabaseRecord($tmp_obj, $row);
342  }
343  return $tmp_obj;
344  }
345 
346  public function saveDraft(): int
347  {
348  $draft_id = $this->db->nextId('frm_posts_drafts');
349  $post_date = date("Y-m-d H:i:s");
350 
351  $this->db->insert('frm_posts_drafts', [
352  'draft_id' => ['integer', $draft_id],
353  'post_id' => ['integer', $this->getPostId()],
354  'thread_id' => ['integer', $this->getThreadId()],
355  'forum_id' => ['integer', $this->getForumId()],
356  'post_author_id' => ['integer', $this->getPostAuthorId()],
357  'post_subject' => ['text', $this->getPostSubject()],
358  'post_message' => ['clob', $this->getPostMessage()],
359  'notify' => ['integer', (int) $this->isNotificationEnabled()],
360  'post_notify' => ['integer', (int) $this->isPostNotificationEnabled()],
361  'post_date' => ['timestamp', $post_date],
362  'post_update' => ['timestamp', $post_date],
363  'post_user_alias' => ['text', $this->getPostUserAlias()],
364  'pos_display_usr_id' => ['integer', $this->getPostDisplayUserId()]
365  ]);
366  $this->setDraftId($draft_id);
367  return $draft_id;
368  }
369 
370  public function update(): void
371  {
372  $this->updateDraft();
373  }
374 
375  public function updateDraft(): void
376  {
377  $this->db->update(
378  'frm_posts_drafts',
379  [
380  'post_subject' => ['text', $this->getPostSubject()],
381  'post_message' => ['clob', $this->getPostMessage()],
382  'post_user_alias' => ['text', $this->getPostUserAlias()],
383  'post_update' => ['timestamp', date("Y-m-d H:i:s")],
384  'update_user_id' => ['integer', $this->getUpdateUserId()],
385  'rcid' => ['text', $this->getRCID()]
386  ],
387  ['draft_id' => ['integer', $this->getDraftId()]]
388  );
389  }
390 
391  public function deleteDraft(): void
392  {
393  $this->db->manipulateF(
394  'DELETE FROM frm_posts_drafts WHERE draft_id = %s',
395  ['integer'],
396  [$this->getDraftId()]
397  );
398  }
399 
400  public static function deleteMobsOfDraft(int $draft_id): void
401  {
402  $oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft_id);
403  foreach ($oldMediaObjects as $oldMob) {
404  if (ilObjMediaObject::_exists($oldMob)) {
405  ilObjMediaObject::_removeUsage($oldMob, 'frm~d:html', $draft_id);
406  $mob_obj = new ilObjMediaObject($oldMob);
407  $mob_obj->delete();
408  }
409  }
410  }
411 
415  public function deleteDraftsByPostIds(array $post_ids = []): void
416  {
417  $draft_ids = [];
418  $res = $this->db->query('SELECT draft_id FROM frm_posts_drafts WHERE ' . $this->db->in(
419  'post_id',
420  $post_ids,
421  false,
422  'integer'
423  ));
424  while ($row = $this->db->fetchAssoc($res)) {
425  $draft_ids[] = (int) $row['draft_id'];
426  }
427 
428  foreach ($draft_ids as $draft_id) {
429  self::deleteMobsOfDraft($draft_id);
430  }
431 
432  $objFileDataForumDrafts = new ilFileDataForumDrafts();
433  $objFileDataForumDrafts->delete($draft_ids);
434 
435  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in(
436  'draft_id',
437  $draft_ids,
438  false,
439  'integer'
440  ));
441  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in(
442  'draft_id',
443  $draft_ids,
444  false,
445  'integer'
446  ));
447  }
448 
452  public function deleteDraftsByDraftIds(array $draft_ids = []): void
453  {
454  foreach ($draft_ids as $draft_id) {
455  self::deleteMobsOfDraft($draft_id);
456  }
457 
458  $objFileDataForumDrafts = new ilFileDataForumDrafts();
459  $objFileDataForumDrafts->delete($draft_ids);
460 
461  $this->db->manipulate('DELETE FROM frm_drafts_history WHERE ' . $this->db->in(
462  'draft_id',
463  $draft_ids,
464  false,
465  'integer'
466  ));
467  $this->db->manipulate('DELETE FROM frm_posts_drafts WHERE ' . $this->db->in(
468  'draft_id',
469  $draft_ids,
470  false,
471  'integer'
472  ));
473  }
474 
475  public static function deleteDraftsByUserId(int $user_id): void
476  {
477  global $DIC;
478  $ilDB = $DIC->database();
479 
480  $res = $ilDB->queryF(
481  'SELECT draft_id FROM frm_posts_drafts WHERE post_author_id = %s',
482  ['integer'],
483  [$user_id]
484  );
485 
486  $draft_ids = [];
487  while ($row = $ilDB->fetchAssoc($res)) {
488  $draft_ids[] = (int) $row['draft_id'];
489  }
490 
491  foreach ($draft_ids as $draft_id) {
492  self::deleteMobsOfDraft($draft_id);
493  }
494 
495  $objFileDataForumDrafts = new ilFileDataForumDrafts();
496  $objFileDataForumDrafts->delete($draft_ids);
497 
498  $ilDB->manipulate('DELETE FROM frm_drafts_history WHERE ' . $ilDB->in(
499  'draft_id',
500  $draft_ids,
501  false,
502  'integer'
503  ));
504  $ilDB->manipulateF(
505  'DELETE FROM frm_posts_drafts WHERE post_author_id = %s',
506  ['integer'],
507  [$user_id]
508  );
509  }
510 
511  public static function isSavePostDraftAllowed(): bool
512  {
513  if (!isset(self::$drafts_settings_cache['save_post_drafts'])) {
514  global $DIC;
515  self::$drafts_settings_cache['save_post_drafts'] = (bool) $DIC->settings()->get('save_post_drafts', '0');
516  }
517 
518  return self::$drafts_settings_cache['save_post_drafts'];
519  }
520 
521  public static function isAutoSavePostDraftAllowed(): bool
522  {
523  if (!self::isSavePostDraftAllowed()) {
524  return false;
525  }
526 
527  if (!isset(self::$drafts_settings_cache['autosave_drafts'])) {
528  global $DIC;
529 
530  self::$drafts_settings_cache['autosave_drafts'] = (bool) $DIC->settings()->get('autosave_drafts', '0');
531  self::$drafts_settings_cache['autosave_drafts_ival'] = (int) $DIC->settings()->get(
532  'autosave_drafts_ival',
533  '30'
534  );
535  }
536 
537  return self::$drafts_settings_cache['autosave_drafts'];
538  }
539 
540  public static function lookupAutosaveInterval(): int
541  {
542  if (self::isAutoSavePostDraftAllowed()) {
543  return (int) self::$drafts_settings_cache['autosave_drafts_ival'];
544  }
545  return 0;
546  }
547 
548  public static function getDraftsStatisticsByRefId(int $ref_id): array
549  {
550  global $DIC;
551  $ilDB = $DIC->database();
552  $ilUser = $DIC->user();
553 
554  if (!isset(self::$forum_statistics_cache[$ref_id][$ilUser->getId()])) {
555  $forumId = ilObjForum::lookupForumIdByRefId($ref_id);
556 
557  $res = $ilDB->queryF(
558  '
559  SELECT COUNT(draft_id) num_drafts, thread_id FROM frm_posts_drafts
560  LEFT JOIN frm_posts ON pos_pk = post_id
561  WHERE forum_id = %s AND post_author_id = %s AND (frm_posts.pos_pk IS NOT NULL OR post_id = 0)
562  GROUP BY thread_id',
563  ['integer', 'integer'],
564  [$forumId, $ilUser->getId()]
565  );
566 
567  $num_drafts_total = 0;
568 
569  while ($row = $ilDB->fetchAssoc($res)) {
570  $num_drafts_total += $row['num_drafts'];
571  self::$forum_statistics_cache[$ref_id][$ilUser->getId()][(int) $row['thread_id']] = (int) $row['num_drafts'];
572  }
573 
574  self::$forum_statistics_cache[$ref_id][$ilUser->getId()]['total'] = $num_drafts_total;
575  }
576  return self::$forum_statistics_cache[$ref_id][$ilUser->getId()];
577  }
578 
579  public static function moveDraftsByMergedThreads(int $source_thread_id, int $target_thread_id): void
580  {
581  global $DIC;
582  $ilDB = $DIC->database();
583 
584  $ilDB->update(
585  'frm_posts_drafts',
586  ['thread_id' => ['integer', $target_thread_id]],
587  ['thread_id' => ['integer', $source_thread_id]]
588  );
589  }
590 
591  public static function getThreadDraftData(int $post_author_id, int $forum_id): array
592  {
593  global $DIC;
594  $ilDB = $DIC->database();
595 
596  $res = $ilDB->queryF(
597  'SELECT * FROM frm_posts_drafts
598  WHERE post_author_id = %s
599  AND forum_id = %s
600  AND thread_id = %s
601  AND post_id = %s
602  ORDER BY post_date DESC',
603  ['integer', 'integer', 'integer', 'integer'],
604  [$post_author_id, $forum_id, 0, 0]
605  );
606  $draft_data = [];
607  while ($row = $ilDB->fetchAssoc($res)) {
608  $tmp_obj = new self();
609  self::populateWithDatabaseRecord($tmp_obj, $row);
610  $draft_data[] = ['subject' => $tmp_obj->getPostSubject(),
611  'post_update' => $tmp_obj->getPostUpdate(),
612  'draft_id' => $tmp_obj->getDraftId()
613  ];
614  }
615  return $draft_data;
616  }
617 
618  public static function createDraftBackup(int $draft_id): void
619  {
620  global $DIC;
621  $ilDB = $DIC->database();
622 
623  $res = $ilDB->queryF(
624  'SELECT * FROM frm_posts_drafts WHERE draft_id = %s',
625  ['integer'],
626  [$draft_id]
627  );
628 
629  $tmp_obj = new self();
630  while ($row = $ilDB->fetchAssoc($res)) {
631  self::populateWithDatabaseRecord($tmp_obj, $row);
632  }
633 
634  $history_obj = new ilForumDraftsHistory();
635  $history_obj->deleteHistoryByDraftIds([$draft_id]);
636 
637  $history_obj->setDraftId($draft_id);
638  $history_obj->setPostSubject($tmp_obj->getPostSubject());
639  $history_obj->setPostMessage($tmp_obj->getPostMessage());
640  $history_obj->addDraftToHistory();
641 
643  $tmp_obj->getPostMessage(),
644  self::MEDIAOBJECT_TYPE,
645  $draft_id,
647  $history_obj->getHistoryId()
648  );
649  }
650 }
setThreadId(int $thread_id)
setPostMessage(string $post_message)
$res
Definition: ltiservices.php:66
setPostSubject(string $post_subject)
setPostUpdate(string $post_update)
static moveMediaObjects(string $post_message, string $source_type, int $source_id, string $target_type, int $target_id, int $direction=0)
deleteDraftsByDraftIds(array $draft_ids=[])
readonly ilDBInterface $db
static readDrafts(int $user_id)
setPostDisplayUserId(int $post_display_user_id)
static array $drafts_settings_cache
static deleteMobsOfDraft(int $draft_id)
static lookupForumIdByRefId(int $ref_id)
static createDraftBackup(int $draft_id)
Class ilForumDraftHistory.
static populateWithDatabaseRecord(ilForumPostDraft $draft, array $row)
$ref_id
Definition: ltiauth.php:65
setUpdateUserId(int $update_user_id)
static getDraftsStatisticsByRefId(int $ref_id)
setNotificationStatus(bool $notify)
global $DIC
Definition: shib_login.php:22
static getThreadDraftData(int $post_author_id, int $forum_id)
__construct(int $user_id=0, int $post_id=0, int $draft_id=0)
setPostUserAlias(string $post_user_alias)
static _exists(int $id, bool $reference=false, ?string $type=null)
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static _removeUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Remove usage of mob in another container.
setPostDate(string $post_date)
static deleteDraftsByUserId(int $user_id)
setPostNotificationStatus(bool $post_notify)
deleteDraftsByPostIds(array $post_ids=[])
static getSortedDrafts(int $usrId, int $threadId, int $sorting=ilForumProperties::VIEW_DATE_ASC)
static moveDraftsByMergedThreads(int $source_thread_id, int $target_thread_id)
static array $forum_statistics_cache
static newInstanceByDraftId(int $draft_id)
static getDraftInstancesByUserId(int $user_id)
setPostAuthorId(int $post_author_id)