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