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