ILIAS  release_8 Revision v8.24
class.ilForumPost.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 private int $id;
28 private int $forum_id = 0;
29 private int $thread_id = 0;
30 private int $display_user_id = 0;
31 private ?string $user_alias = null;
32 private string $subject = '';
33 private string $message = '';
34 private ?string $createdate = null;
35 private ?string $changedate = null;
36 private int $user_id_update = 0;
37 private bool $censored = false;
38 private ?string $censorship_comment = null;
39 private ?string $censored_date = null;
40 private bool $notification = false;
41 private ?string $import_name = null;
42 private bool $status = true;
43 private int $tree_id = 0;
44 private int $parent_id = 0;
45 private int $lft = 0;
46 private int $rgt = 0;
47 private int $depth = 0;
48 private ?ilForumTopic $objThread = null;
50 private bool $is_moderator = false;
51 private ?bool $is_author_moderator = false;
52 private bool $post_read = false;
53 private int $pos_author_id = 0;
54 private ?string $post_activation_date = null;
55
56 public function __construct(int $a_id = 0, bool $a_is_moderator = false, bool $preventImplicitRead = false)
57 {
58 global $DIC;
59
60 $this->db = $DIC->database();
61 $this->id = $a_id;
62
63 if (!$preventImplicitRead) {
64 $this->read();
65 }
66 }
67
68 public function insert(): void
69 {
70 if ($this->forum_id && $this->thread_id) {
71 $this->id = $this->db->nextId('frm_posts');
72
73 $this->db->insert('frm_posts', [
74 'pos_pk' => ['integer', $this->id],
75 'pos_top_fk' => ['integer', $this->forum_id],
76 'pos_thr_fk' => ['integer', $this->thread_id],
77 'pos_display_user_id' => ['integer', $this->display_user_id],
78 'pos_usr_alias' => ['text', $this->user_alias],
79 'pos_subject' => ['text', $this->subject],
80 'pos_message' => ['clob', $this->message],
81 'pos_date' => ['timestamp', $this->createdate],
82 'pos_update' => ['timestamp', $this->createdate],
83 'update_user' => ['integer', $this->user_id_update],
84 'pos_cens' => ['integer', (int) $this->censored],
85 'notify' => ['integer', (int) $this->notification],
86 'import_name' => ['text', $this->import_name],
87 'pos_status' => ['integer', (int) $this->status],
88 'pos_author_id' => ['integer', $this->pos_author_id],
89 'is_author_moderator' => ['integer', $this->is_author_moderator],
90 'pos_activation_date' => ['timestamp', $this->createdate]
91 ]);
92 }
93 }
94
95 public function update(): bool
96 {
97 if ($this->id) {
98 $this->db->update(
99 'frm_posts',
100 [
101 'pos_top_fk' => ['integer', $this->forum_id],
102 'pos_thr_fk' => ['integer', $this->thread_id],
103 'pos_subject' => ['text', $this->subject],
104 'pos_message' => ['clob', $this->message],
105 'pos_update' => ['timestamp', $this->changedate],
106 'update_user' => ['integer', $this->user_id_update],
107 'pos_cens' => ['integer', (int) $this->censored],
108 'pos_cens_date' => ['timestamp', $this->censored_date],
109 'pos_cens_com' => ['text', $this->censorship_comment],
110 'notify' => ['integer', (int) $this->notification],
111 'pos_status' => ['integer', (int) $this->status]
112 ],
113 [
114 'pos_pk' => ['integer', $this->id]
115 ]
116 );
117
118 if ($this->objThread->getFirstVisiblePostId() === $this->id) {
119 $this->objThread->setSubject($this->subject);
120 $this->objThread->update();
121 $this->objThread->reload();
122 }
123
124 return true;
125 }
126
127 return false;
128 }
129
130 private function read(): void
131 {
132 if ($this->id) {
133 $res = $this->db->queryF(
134 '
135 SELECT * FROM frm_posts
136 INNER JOIN frm_posts_tree ON pos_fk = pos_pk
137 WHERE pos_pk = %s',
138 ['integer'],
139 [$this->id]
140 );
141 $row = $this->db->fetchObject($res);
142
143 if (is_object($row)) {
144 $this->id = (int) $row->pos_pk;
145 $this->forum_id = (int) $row->pos_top_fk;
146 $this->thread_id = (int) $row->pos_thr_fk;
147 $this->display_user_id = (int) $row->pos_display_user_id;
148 $this->user_alias = $row->pos_usr_alias;
149 $this->subject = (string) $row->pos_subject;
150 $this->message = (string) $row->pos_message;
151 $this->createdate = $row->pos_date;
152 $this->changedate = $row->pos_update;
153 $this->user_id_update = (int) $row->update_user;
154 $this->censored = (bool) $row->pos_cens;
155 $this->censored_date = $row->pos_cens_date;
156 $this->censorship_comment = $row->pos_cens_com;
157 $this->notification = (bool) $row->notify;
158 $this->import_name = $row->import_name;
159 $this->status = (bool) $row->pos_status;
160 $this->tree_id = (int) $row->fpt_pk;
161 $this->parent_id = (int) $row->parent_pos;
162 $this->lft = (int) $row->lft;
163 $this->rgt = (int) $row->rgt;
164 $this->depth = (int) $row->depth;
165 $this->pos_author_id = (int) $row->pos_author_id;
166 $this->is_author_moderator = (bool) $row->is_author_moderator;
167 $this->post_activation_date = $row->pos_activation_date;
168
169 $this->objThread = new ilForumTopic($this->thread_id, $this->is_moderator);
170 }
171 }
172 }
173
174 public function isAnyParentDeactivated(): bool
175 {
176 if ($this->id) {
177 $res = $this->db->queryF(
178 '
179 SELECT * FROM frm_posts_tree
180 INNER JOIN frm_posts ON pos_pk = pos_fk
181 WHERE pos_status = %s
182 AND lft < %s AND rgt > %s
183 AND thr_fk = %s',
184 ['integer', 'integer', 'integer', 'integer'],
185 [0, $this->lft, $this->rgt, $this->thread_id]
186 );
187
188 return $res->numRows() > 0;
189 }
190
191 return false;
192 }
193
194 public function reload(): void
195 {
196 $this->read();
197 }
198
199 public function activatePost(): void
200 {
201 if ($this->id) {
202 $now = date('Y-m-d H:i:s');
203 $this->db->update(
204 'frm_posts',
205 [
206 'pos_status' => ['integer', 1],
207 'pos_activation_date' => ['timestamp', $now]
208 ],
209 ['pos_pk' => ['integer', $this->id]]
210 );
211
212 $this->activateParentPosts();
213 $this->setPostActivationDate($now);
214 $this->setStatus(true);
215 }
216 }
217
218 public function activatePostAndChildPosts(): bool
219 {
220 if ($this->id) {
221 $query = "SELECT pos_pk FROM frm_posts_tree treea "
222 . "INNER JOIN frm_posts_tree treeb ON treeb.thr_fk = treea.thr_fk "
223 . "AND treeb.lft BETWEEN treea.lft AND treea.rgt "
224 . "INNER JOIN frm_posts ON pos_pk = treeb.pos_fk "
225 . "WHERE treea.pos_fk = %s";
226 $result = $this->db->queryF(
227 $query,
228 ['integer'],
229 [$this->id]
230 );
231
232 $now = date('Y-m-d H:i:s');
233 while ($row = $this->db->fetchAssoc($result)) {
234 $this->db->update(
235 'frm_posts',
236 [
237 'pos_status' => ['integer', 1],
238 'pos_activation_date' => ['timestamp', $now]
239 ],
240 ['pos_pk' => ['integer', $row['pos_pk']]]
241 );
242 }
243
244 $this->activateParentPosts();
245
246 return true;
247 }
248
249 return false;
250 }
251
252 public function activateParentPosts(): bool
253 {
254 if ($this->id) {
255 $query = "SELECT pos_pk FROM frm_posts "
256 . "INNER JOIN frm_posts_tree ON pos_fk = pos_pk "
257 . "WHERE lft < %s AND rgt > %s AND thr_fk = %s";
258 $result = $this->db->queryF(
259 $query,
260 ['integer', 'integer', 'integer'],
261 [$this->lft, $this->rgt, $this->thread_id]
262 );
263
264 $now = date('Y-m-d H:i:s');
265 while ($row = $this->db->fetchAssoc($result)) {
266 $this->db->update(
267 'frm_posts',
268 [
269 'pos_status' => ['integer', 1],
270 'pos_activation_date' => ['timestamp', $now]
271 ],
272 ['pos_pk' => ['integer', $row['pos_pk']]]
273 );
274 }
275
276 return true;
277 }
278
279 return false;
280 }
281
282 public function isPostRead(): bool
283 {
284 return $this->getIsRead();
285 }
286
287 public function isRead(int $a_user_id = 0): bool
288 {
289 if ($a_user_id && $this->id) {
290 $res = $this->db->queryF(
291 'SELECT * FROM frm_user_read WHERE usr_id = %s AND post_id = %s',
292 ['integer', 'integer'],
293 [$a_user_id, $this->id]
294 );
295
296 return $res->numRows() > 0;
297 }
298
299 return false;
300 }
301
302 public function hasReplies(): bool
303 {
304 if ($this->id && $this->rgt && $this->lft) {
305 $res = $this->db->queryF(
306 'SELECT * FROM frm_posts_tree WHERE lft > %s AND rgt < %s AND thr_fk = %s',
307 ['integer', 'integer', 'integer'],
308 [$this->lft, $this->rgt, $this->thread_id]
309 );
310
311 return $res->numRows() > 0;
312 }
313
314 return false;
315 }
316
317 public function isOwner(int $a_user_id = 0): bool
318 {
319 if ($this->pos_author_id && $a_user_id) {
320 return $this->pos_author_id === $a_user_id;
321 }
322
323 return false;
324 }
325
326 public function setId(int $a_id): void
327 {
328 $this->id = $a_id;
329 }
330
331 public function getId(): int
332 {
333 return $this->id;
334 }
335
336 public function setForumId(int $a_forum_id): void
337 {
338 $this->forum_id = $a_forum_id;
339 }
340
341 public function getForumId(): int
342 {
343 return $this->forum_id;
344 }
345
346 public function setThreadId(int $a_thread_id): void
347 {
348 $this->thread_id = $a_thread_id;
349 }
350
351 public function getThreadId(): int
352 {
353 return $this->thread_id;
354 }
355
356 public function setDisplayUserId(int $a_user_id): void
357 {
358 $this->display_user_id = $a_user_id;
359 }
360
361 public function getDisplayUserId(): int
362 {
364 }
365
366 public function setUserAlias(?string $a_user_alias): void
367 {
368 $this->user_alias = $a_user_alias;
369 }
370
371 public function getUserAlias(): ?string
372 {
373 return $this->user_alias;
374 }
375
376 public function setSubject(string $a_subject): void
377 {
378 $this->subject = $a_subject;
379 }
380
381 public function getSubject(): string
382 {
383 return $this->subject;
384 }
385
386 public function setMessage(string $a_message): void
387 {
388 $this->message = $a_message;
389 }
390
391 public function getMessage(): string
392 {
393 return $this->message;
394 }
395
396 public function setCreateDate(?string $a_createdate): void
397 {
398 $this->createdate = $a_createdate;
399 }
400
401 public function getCreateDate(): ?string
402 {
403 return $this->createdate;
404 }
405
406 public function setChangeDate(?string $a_changedate): void
407 {
408 $this->changedate = $a_changedate;
409 }
410
411 public function getChangeDate(): ?string
412 {
413 return $this->changedate;
414 }
415
416 public function setUpdateUserId(int $a_user_id_update): void
417 {
418 $this->user_id_update = $a_user_id_update;
419 }
420
421 public function getUpdateUserId(): int
422 {
424 }
425
426 public function setCensorship(bool $a_censorship): void
427 {
428 $this->censored = $a_censorship;
429 }
430
431 public function isCensored(): bool
432 {
433 return $this->censored;
434 }
435
436 public function setCensorshipComment(?string $a_comment): void
437 {
438 $this->censorship_comment = $a_comment;
439 }
440
441 public function getCensorshipComment(): ?string
442 {
444 }
445
446 public function setNotification(bool $a_notification): void
447 {
448 $this->notification = $a_notification;
449 }
450
451 public function isNotificationEnabled(): bool
452 {
453 return $this->notification;
454 }
455
456 public function setImportName(?string $a_import_name): void
457 {
458 $this->import_name = $a_import_name;
459 }
460
461 public function getImportName(): ?string
462 {
463 return $this->import_name;
464 }
465
466 public function setStatus(bool $a_status): void
467 {
468 $this->status = $a_status;
469 }
470
471 public function isActivated(): bool
472 {
473 return $this->status;
474 }
475
476 public function setTreeId(int $a_tree_id): void
477 {
478 $this->tree_id = $a_tree_id;
479 }
480
481 public function getTreeId(): int
482 {
483 return $this->tree_id;
484 }
485
486 public function setParentId(int $a_parent_id): void
487 {
488 $this->parent_id = $a_parent_id;
489 }
490
491 public function setIsRead(bool $a_is_read): void
492 {
493 $this->post_read = $a_is_read;
494 }
495
496 public function getIsRead(): bool
497 {
498 return $this->post_read;
499 }
500
501 public function getParentId(): int
502 {
503 return $this->parent_id;
504 }
505
506 public function setLft(int $a_lft): void
507 {
508 $this->lft = $a_lft;
509 }
510
511 public function getLft(): int
512 {
513 return $this->lft;
514 }
515
516 public function setRgt(int $a_rgt): void
517 {
518 $this->rgt = $a_rgt;
519 }
520
521 public function getRgt(): int
522 {
523 return $this->rgt;
524 }
525
526 public function setDepth(int $a_depth): void
527 {
528 $this->depth = $a_depth;
529 }
530
531 public function getDepth(): int
532 {
533 return $this->depth;
534 }
535
536 public function setThread(ilForumTopic $thread): void
537 {
538 $this->objThread = $thread;
539 }
540
541 public function getThread(): ?ilForumTopic
542 {
543 return $this->objThread;
544 }
545
546 public function setPosAuthorId(int $pos_author_id): void
547 {
548 $this->pos_author_id = $pos_author_id;
549 }
550
551 public function getPosAuthorId(): int
552 {
554 }
555
556 public function isAuthorModerator(): ?bool
557 {
559 }
560
561 public function setIsAuthorModerator(?bool $is_author_moderator): void
562 {
563 $this->is_author_moderator = $is_author_moderator;
564 }
565
566 public function getCensoredDate(): ?string
567 {
569 }
570
571 public function getPostActivationDate(): ?string
572 {
574 }
575
576 public function setPostActivationDate(?string $post_activation_date): void
577 {
578 $this->post_activation_date = $post_activation_date;
579 }
580
581 public function setCensoredDate(?string $censored_date): void
582 {
583 $this->censored_date = $censored_date;
584 }
585
586 public function assignData(array $row): void
587 {
588 $this->setUserAlias((string) $row['pos_usr_alias']);
589 $this->setSubject((string) $row['pos_subject']);
590 $this->setCreateDate($row['pos_date']);
591 $this->setMessage((string) $row['pos_message']);
592 $this->setForumId((int) $row['pos_top_fk']);
593 $this->setThreadId((int) $row['pos_thr_fk']);
594 $this->setChangeDate($row['pos_update']);
595 $this->setUpdateUserId((int) $row['update_user']);
596 $this->setCensorship((bool) $row['pos_cens']);
597 $this->setCensoredDate($row['pos_cens_date'] ?? null);
598 $this->setCensorshipComment($row['pos_cens_com']);
599 $this->setNotification((bool) $row['notify']);
600 $this->setImportName($row['import_name']);
601 $this->setStatus((bool) $row['pos_status']);
602 $this->setTreeId((int) $row['fpt_pk']);
603 $this->setParentId((int) $row['parent_pos']);
604 $this->setLft((int) $row['lft']);
605 $this->setRgt((int) $row['rgt']);
606 $this->setDepth((int) $row['depth']);
607 $this->setIsRead(isset($row['post_read']) && (int) $row['post_read']);
608 $this->setDisplayUserId((int) $row['pos_display_user_id']);
609 $this->setPosAuthorId((int) $row['pos_author_id']);
610 $this->setIsAuthorModerator((bool) $row['is_author_moderator']);
611 }
612
618 public static function mergePosts(int $sourceThreadId, int $targetThreadId, array $excludedPostIds = []): void
619 {
620 global $DIC;
621 $ilDB = $DIC->database();
622
623 $conditions = ['pos_thr_fk = ' . $ilDB->quote($sourceThreadId, 'integer')];
624 if ($excludedPostIds !== []) {
625 $conditions[] = $ilDB->in('pos_pk', $excludedPostIds, true, 'integer');
626 }
627
628 $ilDB->manipulateF(
629 'UPDATE frm_posts SET pos_thr_fk = %s WHERE ' . implode(' AND ', $conditions),
630 ['integer',],
631 [$targetThreadId,]
632 );
633 }
634
635 public static function lookupNotificationStatusByPostId(int $post_id): bool
636 {
637 global $DIC;
638
639 $res = $DIC->database()->queryF(
640 'SELECT notify FROM frm_posts WHERE pos_pk = %s',
641 ['integer'],
642 [$post_id]
643 );
644
645 $row = $DIC->database()->fetchAssoc($res);
646 return (bool) $row['notify'];
647 }
648
649 public static function lookupPostMessage(int $post_id): string
650 {
651 global $DIC;
652 $ilDB = $DIC->database();
653
654 $res = $ilDB->queryF(
655 'SELECT pos_message FROM frm_posts WHERE pos_pk = %s',
656 ['integer'],
657 [$post_id]
658 );
659
660 if ($row = $ilDB->fetchObject($res)) {
661 return $row->pos_message ?: '';
662 }
663
664 return '';
665 }
666}
setUpdateUserId(int $a_user_id_update)
setIsAuthorModerator(?bool $is_author_moderator)
static lookupNotificationStatusByPostId(int $post_id)
setForumId(int $a_forum_id)
setThread(ilForumTopic $thread)
ilDBInterface $db
setStatus(bool $a_status)
setDisplayUserId(int $a_user_id)
string $post_activation_date
setRgt(int $a_rgt)
setParentId(int $a_parent_id)
setImportName(?string $a_import_name)
setSubject(string $a_subject)
static lookupPostMessage(int $post_id)
string $censorship_comment
setCensorshipComment(?string $a_comment)
setMessage(string $a_message)
setPosAuthorId(int $pos_author_id)
setPostActivationDate(?string $post_activation_date)
setLft(int $a_lft)
isRead(int $a_user_id=0)
setIsRead(bool $a_is_read)
setCensorship(bool $a_censorship)
setNotification(bool $a_notification)
isOwner(int $a_user_id=0)
setUserAlias(?string $a_user_alias)
ilForumTopic $objThread
__construct(int $a_id=0, bool $a_is_moderator=false, bool $preventImplicitRead=false)
setTreeId(int $a_tree_id)
setChangeDate(?string $a_changedate)
static mergePosts(int $sourceThreadId, int $targetThreadId, array $excludedPostIds=[])
setCreateDate(?string $a_createdate)
setDepth(int $a_depth)
assignData(array $row)
setId(int $a_id)
setThreadId(int $a_thread_id)
setCensoredDate(?string $censored_date)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query