ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilForumTopic.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 private static array $possibleOrderDirections = ['ASC', 'DESC'];
28
29 private int $forum_id = 0;
30 private int $frm_obj_id = 0;
31 private int $display_user_id = 0;
32 private ?string $user_alias = null;
33 private string $subject = '';
34 private ?string $createdate = null;
35 private ?string $changedate = null;
36 private int $num_posts = 0;
37 private ?string $last_post_string = null;
38 private int $visits = 0;
39 private ?string $import_name = null;
40 private bool $is_sticky = false;
41 private bool $is_closed = false;
42 private string $orderField = '';
43 private ?ilForumPost $last_post = null;
44 private readonly ilDBInterface $db;
45 private int $thr_author_id = 0;
46 private float $average_rating = 0.0;
47 private string $orderDirection = 'DESC';
48 private readonly ilObjUser $user;
49 private int $num_unread_posts = 0;
50 private bool $user_notification_enabled = false;
51
59 public function __construct(
60 private int $id = 0,
61 private readonly bool $is_moderator = false,
62 bool $preventImplicitRead = false
63 ) {
64 global $DIC;
65 $this->db = $DIC->database();
66 $this->user = $DIC->user();
67
68 if (!$preventImplicitRead) {
69 $this->read();
70 }
71 }
72
73 public function assignData(array $data): void
74 {
75 $this->setId((int) $data['thr_pk']);
76 $this->setForumId((int) $data['thr_top_fk']);
77 $this->setSubject($data['thr_subject']);
78 $this->setDisplayUserId((int) $data['thr_display_user_id']);
79 $this->setUserAlias($data['thr_usr_alias']);
80 $this->setLastPostString($data['thr_last_post']);
81 $this->setCreateDate($data['thr_date']);
82 $this->setChangeDate($data['thr_update']);
83 $this->setVisits((int) $data['visits']);
84 $this->setImportName($data['import_name']);
85 $this->setSticky((bool) $data['is_sticky']);
86 $this->setClosed((bool) $data['is_closed']);
87 $this->setAverageRating(isset($data['avg_rating']) ? (float) $data['avg_rating'] : 0);
88 $this->setThrAuthorId((int) $data['thr_author_id']);
89
90 // Aggregated values
91 if (isset($data['num_posts'])) {
92 $this->setNumPosts((int) $data['num_posts']);
93 }
94 if (isset($data['num_unread_posts'])) {
95 $this->setNumUnreadPosts((int) $data['num_unread_posts']);
96 }
97 if (isset($data['usr_notification_is_enabled'])) {
98 $this->setUserNotificationEnabled((bool) $data['usr_notification_is_enabled']);
99 }
100 }
101
102 public function insert(): bool
103 {
104 if ($this->forum_id !== 0) {
105 $nextId = $this->db->nextId('frm_threads');
106
107 $this->db->insert(
108 'frm_threads',
109 [
110 'thr_pk' => ['integer', $nextId],
111 'thr_top_fk' => ['integer', $this->forum_id],
112 'thr_subject' => ['text', $this->subject],
113 'thr_display_user_id' => ['integer', $this->display_user_id],
114 'thr_usr_alias' => ['text', $this->user_alias],
115 'thr_num_posts' => ['integer', $this->num_posts],
116 'thr_last_post' => ['text', $this->last_post_string],
117 'thr_date' => ['timestamp', $this->createdate],
118 'thr_update' => ['timestamp', null],
119 'import_name' => ['text', $this->import_name],
120 'is_sticky' => ['integer', (int) $this->is_sticky],
121 'is_closed' => ['integer', (int) $this->is_closed],
122 'avg_rating' => ['text', (string) $this->average_rating],
123 'thr_author_id' => ['integer', $this->thr_author_id]
124 ]
125 );
126
127 $this->id = $nextId;
128
129 return true;
130 }
131
132 return false;
133 }
134
135 public function update(): bool
136 {
137 if ($this->id !== 0) {
138 $this->db->manipulateF(
139 '
140 UPDATE frm_threads
141 SET thr_top_fk = %s,
142 thr_subject = %s,
143 thr_update = %s,
144 thr_num_posts = %s,
145 thr_last_post = %s,
146 avg_rating = %s
147 WHERE thr_pk = %s',
148 ['integer', 'text', 'timestamp', 'integer', 'text', 'text', 'integer'],
149 [
150 $this->forum_id,
151 $this->subject,
152 date('Y-m-d H:i:s'),
153 $this->num_posts,
154 $this->last_post_string,
155 (string) $this->average_rating,
156 $this->id
157 ]
158 );
159
160 return true;
161 }
162
163 return false;
164 }
165
166 private function read(): bool
167 {
168 if ($this->id !== 0) {
169 $res = $this->db->queryF(
170 '
171 SELECT frm_threads.*, top_frm_fk frm_obj_id
172 FROM frm_threads
173 INNER JOIN frm_data ON top_pk = thr_top_fk
174 WHERE thr_pk = %s',
175 ['integer'],
176 [$this->id]
177 );
178
179 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
180
181 if (is_object($row)) {
182 $this->forum_id = (int) $row->thr_top_fk;
183 $this->display_user_id = (int) $row->thr_display_user_id;
184 $this->user_alias = $row->thr_usr_alias;
185 $this->subject = html_entity_decode((string) $row->thr_subject);
186 $this->createdate = $row->thr_date;
187 $this->changedate = $row->thr_update;
188 $this->import_name = $row->import_name;
189 $this->num_posts = (int) $row->thr_num_posts;
190 $this->last_post_string = $row->thr_last_post;
191 $this->visits = (int) $row->visits;
192 $this->is_sticky = (bool) $row->is_sticky;
193 $this->is_closed = (bool) $row->is_closed;
194 $this->frm_obj_id = (int) $row->frm_obj_id;
195 $this->average_rating = (float) $row->avg_rating;
196 $this->thr_author_id = (int) $row->thr_author_id;
197
198 return true;
199 }
200 $this->id = 0;
201 return false;
202 }
203
204 return false;
205 }
206
207 public function reload(): bool
208 {
209 return $this->read();
210 }
211
212 public function getPostRootId(): int
213 {
214 $this->db->setLimit(1);
215 $res = $this->db->queryF(
216 'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos = %s AND depth = %s ORDER BY rgt DESC',
217 ['integer', 'integer', 'integer'],
218 [$this->id, 0, 1]
219 );
220
221 if (($row = $this->db->fetchObject($res)) !== null) {
222 return (int) $row->pos_fk ?: 0;
223 }
224 return 0;
225 }
226
227 public function updateVisits(): void
228 {
229 $checkTime = time() - (60 * 60);
230
231 if (ilSession::get('frm_visit_frm_threads_' . $this->id) < $checkTime) {
232 ilSession::set('frm_visit_frm_threads_' . $this->id, time());
233
234 $this->db->manipulateF(
235 'UPDATE frm_threads SET visits = visits + 1 WHERE thr_pk = %s',
236 ['integer'],
237 [$this->id]
238 );
239 }
240 }
241
242 public function countPosts(bool $ignoreRoot = false): int
243 {
244 $res = $this->db->queryF(
245 '
246 SELECT COUNT(*) cnt
247 FROM frm_posts
248 INNER JOIN frm_posts_tree ON frm_posts_tree.pos_fk = pos_pk
249 WHERE pos_thr_fk = %s' . ($ignoreRoot ? ' AND parent_pos != 0 ' : ''),
250 ['integer'],
251 [$this->id]
252 );
253
254 $row = $this->db->fetchAssoc($res);
255 if (is_array($row)) {
256 return (int) $row['cnt'];
257 }
258
259 return 0;
260 }
261
262 public function countActivePosts(bool $ignoreRoot = false): int
263 {
264 $res = $this->db->queryF(
265 '
266 SELECT COUNT(*) cnt
267 FROM frm_posts
268 INNER JOIN frm_posts_tree ON frm_posts_tree.pos_fk = pos_pk
269 WHERE (pos_status = %s
270 OR (pos_status = %s AND pos_display_user_id = %s))
271 AND pos_thr_fk = %s' . ($ignoreRoot ? ' AND parent_pos != 0 ' : ''),
272 ['integer', 'integer', 'integer', 'integer'],
273 ['1', '0', $this->user->getId(), $this->id]
274 );
275
276 $row = $this->db->fetchAssoc($res);
277 if (is_array($row)) {
278 return (int) $row['cnt'];
279 }
280
281 return 0;
282 }
283
284 public function getPostRootNode(bool $isModerator = false, bool $preventImplicitRead = false): ilForumPost
285 {
286 $this->db->setLimit(1);
287 $res = $this->db->queryF(
288 '
289 SELECT *
290 FROM frm_posts
291 INNER JOIN frm_posts_tree ON pos_fk = pos_pk
292 WHERE parent_pos = %s
293 AND thr_fk = %s
294 ORDER BY rgt DESC',
295 ['integer', 'integer'],
296 [0, $this->id]
297 );
298
299 if ($row = $this->db->fetchAssoc($res)) {
300 $post = new ilForumPost((int) $row['pos_pk'], $isModerator, $preventImplicitRead);
301 $post->assignData($row);
302 return $post;
303 }
304
305 throw new OutOfBoundsException(sprintf('Could not find first posting by id: %s', $this->id));
306 }
307
308 public function getFirstVisiblePostNode(bool $isModerator = false, bool $preventImplicitRead = false): ilForumPost
309 {
310 $this->db->setLimit(1);
311 $res = $this->db->queryF(
312 '
313 SELECT *
314 FROM frm_posts
315 INNER JOIN frm_posts_tree ON pos_fk = pos_pk
316 WHERE parent_pos != %s
317 AND thr_fk = %s
318 AND depth = %s
319 ORDER BY rgt DESC',
320 ['integer', 'integer', 'integer'],
321 [0, $this->id, 2]
322 );
323
324 if ($row = $this->db->fetchAssoc($res)) {
325 $post = new ilForumPost((int) $row['pos_pk'], $isModerator, $preventImplicitRead);
326 $post->assignData($row);
327 return $post;
328 }
329
330 throw new OutOfBoundsException(sprintf('Could not find first posting by id: %s', $this->id));
331 }
332
333 public function getLastPost(): ilForumPost
334 {
335 if ($this->id !== 0) {
336 $this->db->setLimit(1);
337 $res = $this->db->queryF(
338 'SELECT pos_pk FROM frm_posts WHERE pos_thr_fk = %s ORDER BY pos_date DESC',
339 ['integer'],
340 [$this->id]
341 );
342
343 if (($row = $this->db->fetchObject($res)) !== null) {
344 return new ilForumPost((int) $row->pos_pk);
345 }
346 }
347
348 throw new OutOfBoundsException(sprintf('Could not find last posting by id: %s', $this->id));
349 }
350
352 {
353 if ($this->id !== 0) {
354 $this->db->setLimit(1);
355 $res = $this->db->queryF(
356 '
357 SELECT pos_pk
358 FROM frm_posts
359 WHERE pos_thr_fk = %s
360 AND (pos_status = %s OR (pos_status = %s AND pos_display_user_id = %s))
361 ORDER BY pos_date DESC',
362 ['integer', 'integer', 'integer', 'integer'],
363 [$this->id, '1', '0', $this->user->getId()]
364 );
365
366 if (($row = $this->db->fetchObject($res)) !== null) {
367 return new ilForumPost((int) $row->pos_pk);
368 }
369 }
370
371 throw new OutOfBoundsException(sprintf('Could not find last active posting by id: %s', $this->id));
372 }
373
377 public function getAllPostIds(): array
378 {
379 $posts = [];
380
381 if ($this->id !== 0) {
382 $res = $this->db->queryF('SELECT pos_pk FROM frm_posts WHERE pos_thr_fk = %s', ['integer'], [$this->id]);
383
384 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
385 $posts[(int) $row->pos_pk] = (int) $row->pos_pk;
386 }
387 }
388
389 return $posts;
390 }
391
398 public function getPostTree(ilForumPost $a_post_node): array
399 {
400 $posts = [];
401 $data = [];
402 $data_types = [];
403
404 if ($a_post_node->getLft() > 1) {
405 $dummy_root_condition = 'lft >= %s AND lft < %s';
406 } else {
407 $dummy_root_condition = 'lft > %s AND lft < %s';
408 }
409
410 $query = '
411 SELECT is_author_moderator, pos_author_id, pos_pk, fpt_date, rgt, pos_top_fk, pos_thr_fk,
412 pos_display_user_id, pos_usr_alias, pos_subject,
413 pos_status, pos_message, pos_date, pos_update, rcid,
414 update_user, pos_cens, pos_cens_com, notify,
415 import_name, fpt_pk, parent_pos, lft, depth,
416 (CASE
417 WHEN fur.post_id IS NULL ' .
418 ($this->user->getId() === ANONYMOUS_USER_ID ? ' AND 1 = 2 ' : '') . '
419 THEN 0
420 ELSE 1
421 END) post_read,
422 firstname, lastname, title, login
423
424 FROM frm_posts_tree
425
426 INNER JOIN frm_posts
427 ON pos_fk = pos_pk
428
429 LEFT JOIN usr_data
430 ON pos_display_user_id = usr_id
431
432 LEFT JOIN frm_user_read fur
433 ON fur.thread_id = pos_thr_fk
434 AND fur.post_id = pos_pk
435 AND fur.usr_id = %s
436
437 WHERE ' . $dummy_root_condition . '
438 AND thr_fk = %s';
439 $data_types[] = 'integer';
440 $data_types[] = 'integer';
441 $data_types[] = 'integer';
442 $data_types[] = 'integer';
443 $data[] = $this->user->getId();
444 $data[] = $a_post_node->getLft();
445 $data[] = $a_post_node->getRgt();
446 $data[] = $a_post_node->getThreadId();
447
448 if ($this->orderField !== '') {
449 $query .= " ORDER BY " . $this->orderField . " " . $this->getOrderDirection();
450 }
451
452 $res = $this->db->queryF($query, $data_types, $data);
453
454 $usr_ids = [];
455 while ($row = $this->db->fetchAssoc($res)) {
456 $post = new ilForumPost((int) $row['pos_pk'], false, true);
457 $post->assignData($row);
458
459 if (!$this->is_moderator && !$post->isActivated() && $post->getPosAuthorId() !== $this->user->getId()) {
460 continue;
461 }
462
463 if ((int) $row['pos_display_user_id'] !== 0) {
464 $usr_ids[(int) $row['pos_display_user_id']] = (int) $row['pos_display_user_id'];
465 }
466 if ((int) $row['update_user'] !== 0) {
467 $usr_ids[(int) $row['update_user']] = (int) $row['update_user'];
468 }
469
470 $posts[] = $post;
471 }
472
474
475 return $posts;
476 }
477
487 public function movePosts(int $old_obj_id, int $old_pk, int $new_obj_id, int $new_pk): int
488 {
489 if ($this->id === 0) {
490 return 0;
491 }
492
493 $post_ids = $this->getAllPostIds();
494 $postsMoved = [];
495 try {
496 foreach ($post_ids as $post_id) {
497 $file_obj = new ilFileDataForum($old_obj_id, $post_id);
498 $moved = $file_obj->moveFilesOfPost($new_obj_id);
499
500 if ($moved) {
501 $postsMoved[] = [
502 'from' => $old_obj_id,
503 'to' => $new_obj_id,
504 'position_id' => $post_id
505 ];
506 }
507
508 unset($file_obj);
509 }
510 } catch (ilFileUtilsException $exception) {
511 foreach ($postsMoved as $postedInformation) {
512 $file_obj = new ilFileDataForum($postedInformation['to'], $postedInformation['position_id']);
513 $file_obj->moveFilesOfPost($postedInformation['from']);
514 }
515
516 throw $exception;
517 }
518
519 $current_id = $this->id;
520
521 $ilAtomQuery = $this->db->buildAtomQuery();
522 $ilAtomQuery->addTableLock('frm_user_read');
523
524 $ilAtomQuery->addQueryCallable(static function (ilDBInterface $ilDB) use ($new_obj_id, $current_id): void {
525 $ilDB->manipulateF(
526 'DELETE FROM frm_user_read WHERE obj_id = %s AND thread_id =%s',
527 ['integer', 'integer'],
528 [$new_obj_id, $current_id]
529 );
530
531 $ilDB->manipulateF(
532 'UPDATE frm_user_read SET obj_id = %s WHERE thread_id = %s',
533 ['integer', 'integer'],
534 [$new_obj_id, $current_id]
535 );
536 });
537
538 $ilAtomQuery->run();
539
540 $this->db->manipulateF(
541 'UPDATE frm_posts SET pos_top_fk = %s WHERE pos_thr_fk = %s',
542 ['integer', 'integer'],
543 [$new_pk, $this->id]
544 );
545
546 $res = $this->db->queryF(
547 'SELECT * FROM frm_posts WHERE pos_thr_fk = %s',
548 ['integer'],
549 [$this->id]
550 );
551
552 $old_obj_id = ilForum::_lookupObjIdForForumId($old_pk);
553 $new_obj_id = ilForum::_lookupObjIdForForumId($new_pk);
554
555 while ($post = $this->db->fetchAssoc($res)) {
557 $old_obj_id,
558 'frm',
559 (int) $post['pos_pk'],
560 'pos'
561 );
562 $news_item = new ilNewsItem($news_id);
563 $news_item->setContextObjId($new_obj_id);
564 $news_item->update();
565 }
566
567 return count($post_ids);
568 }
569
570 public function getNestedSetPostChildren(?int $pos_id = null, ?int $num_levels = null): array
571 {
572 $data = null;
573 $objProperties = ilForumProperties::getInstance($this->getFrmObjId());
574 $is_post_activation_enabled = $objProperties->isPostActivationEnabled();
575
576 if ($pos_id !== null) {
577 $res = $this->db->queryF(
578 "
579 SELECT lft, rgt, depth
580 FROM frm_posts_tree
581 WHERE pos_fk = %s
582 AND thr_fk = %s",
583 ['integer', 'integer'],
584 [$pos_id, $this->id]
585 );
586
587 $data = $this->db->fetchAssoc($res);
588 }
589
590 $query = '
591 SELECT fpt.depth,
592 fpt.rgt,
593 fpt.parent_pos,
594 fp.pos_pk,
595 fp.pos_subject,
596 fp.pos_usr_alias,
597 fp.pos_date,
598 fp.pos_update,
599 fp.pos_status,
600 fp.pos_display_user_id,
601 fp.pos_usr_alias,
602 fp.import_name,
603 fp.pos_author_id,
604 fp.is_author_moderator,
605 fur.post_id,
606 (CASE
607 WHEN fur.post_id IS NULL ' .
608 ($this->user->getId() === ANONYMOUS_USER_ID ? ' AND 1 = 2 ' : '') . '
609 THEN 0
610 ELSE 1
611 END) post_read,
612 COUNT(fpt2.pos_fk) children
613
614 FROM frm_posts_tree fpt
615
616 INNER JOIN frm_posts fp
617 ON fp.pos_pk = fpt.pos_fk
618
619 LEFT JOIN frm_posts_tree fpt2
620 ON fpt2.lft BETWEEN fpt.lft AND fpt.rgt
621 AND fpt.thr_fk = fpt2.thr_fk
622 AND fpt.pos_fk != fpt2.pos_fk ';
623
624 $query .= '
625 LEFT JOIN frm_user_read fur
626 ON fur.thread_id = fp.pos_thr_fk
627 AND fur.post_id = fp.pos_pk
628 AND fur.usr_id = ' . $this->db->quote($this->user->getId(), 'integer') . '
629
630 LEFT JOIN usr_data ud
631 ON ud.usr_id = fp.pos_display_user_id
632
633 WHERE fpt.thr_fk = ' . $this->db->quote($this->id, 'integer');
634
635 if ($data) {
636 $query .= ' AND fpt.lft > ' . $this->db->quote($data['lft'], 'integer') .
637 ' AND fpt.lft < ' . $this->db->quote($data['rgt'], 'integer') . ' ';
638 }
639 if ($is_post_activation_enabled && !$this->is_moderator) {
640 $query .= ' AND (fp.pos_status = 1 OR fp.pos_status = 0 AND fp.pos_display_user_id = ' . $this->db->quote(
641 $this->user->getId(),
642 'integer'
643 ) . ') ';
644 }
645
646 if ($data && is_numeric($num_levels)) {
647 $query .= ' AND fpt.depth <= ' . $this->db->quote((int) $data['depth'] + $num_levels, 'integer') . ' ';
648 }
649
650 $query .= ' GROUP BY fpt.depth,
651 fpt.rgt,
652 fpt.parent_pos,
653 fp.pos_pk,
654 fp.pos_subject,
655 fp.pos_usr_alias,
656 fp.pos_date,
657 fp.pos_update,
658 fp.pos_status,
659 fp.pos_display_user_id,
660 fp.pos_usr_alias,
661 fp.import_name,
662 fp.pos_author_id,
663 fp.is_author_moderator,
664 fur.post_id
665 ORDER BY fpt.rgt DESC
666 ';
667
668 $queryCounter = '
669 SELECT pos_fk
670 FROM frm_posts_tree fpt
671 INNER JOIN frm_posts fp
672 ON fp.pos_pk = fpt.pos_fk
673 WHERE fpt.thr_fk = ' . $this->db->quote($this->id, 'integer');
674
675 if ($is_post_activation_enabled && !$this->is_moderator) {
676 $queryCounter .= ' AND (fp.pos_status = 1 OR fp.pos_status = 0 AND fp.pos_display_user_id = ' . $this->db->quote(
677 $this->user->getId(),
678 'integer'
679 ) . ') ';
680 }
681 $queryCounter .= ' ORDER BY fpt.rgt DESC';
682
683 $resCounter = $this->db->query($queryCounter);
684 $counter = [];
685 $i = 0;
686 while ($row = $this->db->fetchAssoc($resCounter)) {
687 $counter[(int) $row['pos_fk']] = $i++;
688 }
689
690 $res = $this->db->query($query);
691 $children = [];
692 $usr_ids = [];
693 while ($row = $this->db->fetchAssoc($res)) {
694 if ((int) $row['pos_display_user_id'] !== 0) {
695 $usr_ids[] = (int) $row['pos_display_user_id'];
696 }
697
698 $row['counter'] = $counter[$row['pos_pk']];
699 $casted_row = [];
700 $casted_row['depth'] = (int) $row['depth'];
701 $casted_row['rgt'] = (int) $row['rgt'];
702 $casted_row['parent_pos'] = (int) $row['parent_pos'];
703 $casted_row['pos_pk'] = (int) $row['pos_pk'];
704 $casted_row['pos_subject'] = (string) $row['pos_subject'];
705 $casted_row['pos_usr_alias'] = (string) $row['pos_usr_alias'];
706 $casted_row['pos_date'] = (string) $row['pos_date'];
707 $casted_row['pos_update'] = (string) $row['pos_update'];
708 $casted_row['pos_status'] = (int) $row['pos_status'];
709 $casted_row['pos_display_user_id'] = (int) $row['pos_display_user_id'];
710 $casted_row['import_name'] = (string) $row['import_name'];
711 $casted_row['pos_author_id'] = (int) $row['pos_author_id'];
712 $casted_row['is_author_moderator'] = (int) $row['is_author_moderator'];
713 $casted_row['post_id'] = (int) $row['post_id'];
714 $casted_row['post_read'] = (int) $row['post_read'];
715 $casted_row['children'] = (int) $row['children'];
716
717 $children[] = $casted_row;
718 }
719
721
722 return $children;
723 }
724
725 public function isNotificationEnabled(int $a_user_id): bool
726 {
727 if ($this->id && $a_user_id) {
728 $result = $this->db->queryF(
729 'SELECT COUNT(notification_id) cnt FROM frm_notification WHERE user_id = %s AND thread_id = %s',
730 ['integer', 'integer'],
731 [$a_user_id, $this->id]
732 );
733
734 if ($row = $this->db->fetchAssoc($result)) {
735 return (int) $row['cnt'] > 0;
736 }
737
738 return false;
739 }
740
741 return false;
742 }
743
744 public function enableNotification(int $a_user_id): void
745 {
746 if ($this->id && $a_user_id && !$this->isNotificationEnabled($a_user_id)) {
747 $nextId = $this->db->nextId('frm_notification');
748 $this->db->manipulateF(
749 '
750 INSERT INTO frm_notification
751 ( notification_id,
752 user_id,
753 thread_id
754 )
755 VALUES(%s, %s, %s)',
756 ['integer', 'integer', 'integer'],
757 [$nextId, $a_user_id, $this->id]
758 );
759 }
760 }
761
762 public function disableNotification(int $a_user_id): void
763 {
764 if ($this->id && $a_user_id) {
765 $this->db->manipulateF(
766 'DELETE FROM frm_notification WHERE user_id = %s AND thread_id = %s',
767 ['integer', 'integer'],
768 [$a_user_id, $this->id]
769 );
770 }
771 }
772
773 public function makeSticky(): bool
774 {
775 if ($this->id && !$this->is_sticky) {
776 $this->db->manipulateF(
777 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
778 ['integer', 'integer'],
779 [1, $this->id]
780 );
781
782 $this->is_sticky = true;
783 return true;
784 }
785
786 return false;
787 }
788
789 public function unmakeSticky(): bool
790 {
791 if ($this->id && $this->is_sticky) {
792 $this->db->manipulateF(
793 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
794 ['integer', 'integer'],
795 [0, $this->id]
796 );
797
798 $this->is_sticky = false;
799 return true;
800 }
801
802 return false;
803 }
804
805 public function close(): void
806 {
807 if ($this->id && !$this->is_closed) {
808 $this->db->manipulateF(
809 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
810 ['integer', 'integer'],
811 [1, $this->id]
812 );
813 $this->is_closed = true;
814 }
815 }
816
817 public function reopen(): void
818 {
819 if ($this->id && $this->is_closed) {
820 $this->db->manipulateF(
821 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
822 ['integer', 'integer'],
823 [0, $this->id]
824 );
825
826 $this->is_closed = false;
827 }
828 }
829
830 public function getAverageRating(): float
831 {
833 }
834
835 public function setAverageRating(float $average_rating): void
836 {
837 $this->average_rating = $average_rating;
838 }
839
840 public function setId(int $a_id): void
841 {
842 $this->id = $a_id;
843 }
844
845 public function getId(): int
846 {
847 return $this->id;
848 }
849
850 public function setForumId(int $a_forum_id): void
851 {
852 $this->forum_id = $a_forum_id;
853 }
854
855 public function getForumId(): int
856 {
857 return $this->forum_id;
858 }
859
860 public function setDisplayUserId(int $a_user_id): void
861 {
862 $this->display_user_id = $a_user_id;
863 }
864
865 public function getDisplayUserId(): int
866 {
868 }
869
870 public function setUserAlias(?string $a_user_alias): void
871 {
872 $this->user_alias = $a_user_alias;
873 }
874
875 public function getUserAlias(): ?string
876 {
877 return $this->user_alias;
878 }
879
880 public function setSubject(string $a_subject): void
881 {
882 $this->subject = $a_subject;
883 }
884
885 public function getSubject(): string
886 {
887 return $this->subject;
888 }
889
890 public function setCreateDate(?string $a_createdate): void
891 {
892 $this->createdate = $a_createdate;
893 }
894
895 public function getCreateDate(): ?string
896 {
897 return $this->createdate;
898 }
899
900 public function setChangeDate(?string $a_changedate): void
901 {
902 $this->changedate = $a_changedate;
903 }
904
905 public function getChangeDate(): ?string
906 {
907 return $this->changedate;
908 }
909
910 public function setImportName(?string $a_import_name): void
911 {
912 $this->import_name = $a_import_name;
913 }
914
915 public function getImportName(): ?string
916 {
917 return $this->import_name;
918 }
919
920 public function setLastPostString(?string $a_last_post): void
921 {
922 $this->last_post_string = $a_last_post;
923 }
924
925 public function getLastPostString(): ?string
926 {
928 }
929
930 public function setVisits(int $a_visits): void
931 {
932 $this->visits = $a_visits;
933 }
934
935 public function getVisits(): int
936 {
937 return $this->visits;
938 }
939
940 public function setSticky(bool $a_sticky): void
941 {
942 $this->is_sticky = $a_sticky;
943 }
944
945 public function isSticky(): bool
946 {
947 return $this->is_sticky;
948 }
949
950 public function setClosed(bool $a_closed): void
951 {
952 $this->is_closed = $a_closed;
953 }
954
955 public function isClosed(): bool
956 {
957 return $this->is_closed;
958 }
959
960 public function setOrderField(string $a_order_field): void
961 {
962 $this->orderField = $a_order_field;
963 }
964
965 public function getOrderField(): string
966 {
967 return $this->orderField;
968 }
969
970 public function getFrmObjId(): int
971 {
972 return $this->frm_obj_id;
973 }
974
975 public function setThrAuthorId(int $thr_author_id): void
976 {
977 $this->thr_author_id = $thr_author_id;
978 }
979
980 public function getThrAuthorId(): int
981 {
983 }
984
985 public static function lookupTitle(int $a_topic_id): string
986 {
987 global $DIC;
988 $ilDB = $DIC->database();
989
990 $res = $ilDB->queryF(
991 'SELECT thr_subject FROM frm_threads WHERE thr_pk = %s',
992 ['integer'],
993 [$a_topic_id]
994 );
995
996 if ($row = $ilDB->fetchObject($res)) {
997 return (string) $row->thr_subject;
998 }
999
1000 return '';
1001 }
1002
1003 public function updateThreadTitle(): void
1004 {
1005 $this->db->update(
1006 'frm_threads',
1007 ['thr_subject' => ['text', $this->getSubject()]],
1008 ['thr_pk' => ['integer', $this->getId()]]
1009 );
1010
1011 try {
1012 $first_node = $this->getPostRootNode();
1013 $first_node->setSubject($this->getSubject());
1014 $first_node->update();
1015 } catch (OutOfBoundsException) {
1016 }
1017 }
1018
1019 public function setNumPosts(int $a_num_posts): ilForumTopic
1020 {
1021 $this->num_posts = $a_num_posts;
1022 return $this;
1023 }
1024
1025 public function getNumPosts(): int
1026 {
1027 return $this->num_posts;
1028 }
1029
1031 {
1032 $this->num_unread_posts = $num_unread_posts;
1033 return $this;
1034 }
1035
1036 public function getNumUnreadPosts(): int
1037 {
1039 }
1040
1041 public function setUserNotificationEnabled(bool $status): ilForumTopic
1042 {
1043 $this->user_notification_enabled = $status;
1044 return $this;
1045 }
1046
1047 public function isUserNotificationEnabled(): bool
1048 {
1050 }
1051
1052 public function setOrderDirection(string $direction): ilForumTopic
1053 {
1054 if (!in_array(strtoupper($direction), self::$possibleOrderDirections, true)) {
1055 $direction = current(self::$possibleOrderDirections);
1056 }
1057
1058 $this->orderDirection = $direction;
1059 return $this;
1060 }
1061
1062 public function getOrderDirection(): string
1063 {
1064 return $this->orderDirection;
1065 }
1066
1067 public static function lookupForumIdByTopicId(int $a_topic_id): int
1068 {
1069 global $DIC;
1070 $ilDB = $DIC->database();
1071
1072 $res = $ilDB->queryF(
1073 'SELECT thr_top_fk FROM frm_threads WHERE thr_pk = %s',
1074 ['integer'],
1075 [$a_topic_id]
1076 );
1077
1078 $row = $ilDB->fetchAssoc($res);
1079
1080 return (int) $row['thr_top_fk'];
1081 }
1082
1083 public function updateMergedThread(): void
1084 {
1085 $this->db->update(
1086 'frm_threads',
1087 [
1088 'thr_num_posts' => ['integer', $this->getNumPosts()],
1089 'visits' => ['integer', $this->getVisits()],
1090 'thr_last_post' => ['text', $this->getLastPostString()],
1091 'thr_subject' => ['text', $this->getSubject()]
1092 ],
1093 ['thr_pk' => ['integer', $this->getId()]]
1094 );
1095 }
1096
1097 public static function lookupCreationDate(int $thread_id): ?string
1098 {
1099 global $DIC;
1100 $ilDB = $DIC->database();
1101
1102 $res = $ilDB->queryF(
1103 'SELECT thr_date FROM frm_threads WHERE thr_pk = %s',
1104 ['integer'],
1105 [$thread_id]
1106 );
1107
1108 $date = null;
1109 $row = $ilDB->fetchAssoc($res);
1110 if (is_array($row)) {
1111 $date = $row['thr_date'];
1112 }
1113
1114 return $date;
1115 }
1116
1118 {
1119 return $this->last_post;
1120 }
1121
1123 {
1124 $this->last_post = $post;
1125 }
1126}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance(int $a_obj_id=0)
setDisplayUserId(int $a_user_id)
isNotificationEnabled(int $a_user_id)
static lookupTitle(int $a_topic_id)
countPosts(bool $ignoreRoot=false)
ilForumPost $last_post
disableNotification(int $a_user_id)
enableNotification(int $a_user_id)
setLastPostString(?string $a_last_post)
setSticky(bool $a_sticky)
assignData(array $data)
getPostTree(ilForumPost $a_post_node)
Fetches and returns an array of posts from the post tree, starting with the node object passed by the...
__construct(private int $id=0, private readonly bool $is_moderator=false, bool $preventImplicitRead=false)
Returns an object of a forum topic.
static array $possibleOrderDirections
countActivePosts(bool $ignoreRoot=false)
getFirstVisiblePostNode(bool $isModerator=false, bool $preventImplicitRead=false)
setChangeDate(?string $a_changedate)
readonly ilObjUser $user
setNumUnreadPosts(int $num_unread_posts)
setAverageRating(float $average_rating)
setClosed(bool $a_closed)
setUserAlias(?string $a_user_alias)
setCreateDate(?string $a_createdate)
getPostRootNode(bool $isModerator=false, bool $preventImplicitRead=false)
static lookupCreationDate(int $thread_id)
setOrderDirection(string $direction)
readonly ilDBInterface $db
bool $user_notification_enabled
setThrAuthorId(int $thr_author_id)
setSubject(string $a_subject)
static lookupForumIdByTopicId(int $a_topic_id)
movePosts(int $old_obj_id, int $old_pk, int $new_obj_id, int $new_pk)
Moves all posts within the current thread to a new forum.
getNestedSetPostChildren(?int $pos_id=null, ?int $num_levels=null)
setImportName(?string $a_import_name)
setForumId(int $a_forum_id)
setUserNotificationEnabled(bool $status)
setVisits(int $a_visits)
setOrderField(string $a_order_field)
setNumPosts(int $a_num_posts)
setLastPostForThreadOverview(ilForumPost $post)
static _lookupObjIdForForumId(int $a_for_id)
A news item can be created by different sources.
static getFirstNewsIdForContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Get first new id of news set related to a certain context.
User class.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$post
Definition: ltitoken.php:46
global $DIC
Definition: shib_login.php:26
$counter