ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ForumThreadTableSessionStorage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ForumDto;
24use ilForum;
27use ilForumTopic;
30use ilObjForum;
31use ilSession;
33
35{
36 public const string KEY_THREAD_SORTATION = 'thread_sortation';
37 public const string KEY_THREAD_PAGE = 'thread_page';
38
39 private readonly WrapperFactory $http_wrapper;
40 private readonly Factory $refinery;
41
42 public function __construct(
43 private readonly int $forum_ref_id,
44 private readonly bool $is_moderator,
46 ?Factory $refinery = null
47 ) {
48 global $DIC;
49 $this->http_wrapper = $http_wrapper ?? $DIC->http()->wrapper();
50 $this->refinery = $refinery ?? $DIC->refinery();
51 }
52
53 public function fetchData(ilForum $forum, ForumDto $topicData): ThreadsPage
54 {
55 $sortation = $this->getThreadSortation();
56 $page = $this->getThreadPage();
58
59 $params = [
60 'is_moderator' => $this->is_moderator,
61 'excluded_ids' => [],
62 'order_column' => $sortation->field(),
63 'order_direction' => $sortation->direction()
64 ];
65
66 $data = $forum->getAllThreads(
67 $topicData->getTopPk(),
68 $params,
69 $limit,
70 $page * $limit
71 );
72 if ($data['items'] === [] && $page > 0) {
73 ilSession::set($this->buildSessionKey($this->forum_ref_id, self::KEY_THREAD_PAGE), 0);
74 $data = $forum->getAllThreads(
75 $topicData->getTopPk(),
76 $params,
77 $limit,
78 $page * $limit
79 );
80 }
81
82 $items = $data['items'];
83
84 $thread_ids = [];
85 $user_ids = [];
86 foreach ($items as $thread) {
88 $thread_ids[] = $thread->getId();
89 if ($thread->getDisplayUserId() > 0) {
90 $user_ids[$thread->getDisplayUserId()] = $thread->getDisplayUserId();
91 }
92 }
93
94 $user_ids = array_merge(
95 ilObjForum::getUserIdsOfLastPostsByRefIdAndThreadIds($this->forum_ref_id, $thread_ids),
96 $user_ids
97 );
98
100
101 return new ThreadsPage($items);
102 }
103
105 {
106 $query_thread_sortation = $this->getKeyValueFromQuery(self::KEY_THREAD_SORTATION);
107
108 if ($query_thread_sortation !== null) {
109 $this->setSessionKeyValue($this->forum_ref_id, self::KEY_THREAD_SORTATION, $query_thread_sortation);
110 return ThreadSortation::tryFrom($query_thread_sortation);
111 }
112
113 $session_thread_sortation = $this->getKeyValueFromSession(
114 $this->forum_ref_id,
115 self::KEY_THREAD_SORTATION,
116 ThreadSortation::DEFAULT_SORTATION->value
117 );
118
119 return ThreadSortation::tryFrom(
120 $session_thread_sortation
121 );
122 }
123
124 public function getThreadPage(): int
125 {
126 $query_thread_page = $this->getKeyValueFromQuery(self::KEY_THREAD_PAGE, null);
127
128 if ($query_thread_page !== null) {
129 $this->setSessionKeyValue($this->forum_ref_id, self::KEY_THREAD_PAGE, $query_thread_page);
130 return $query_thread_page;
131 }
132
133 return (int) $this->getKeyValueFromSession(
134 $this->forum_ref_id,
135 self::KEY_THREAD_PAGE,
136 $query_thread_page
137 );
138 }
139
140 private function getKeyValueFromQuery(string $key, ?int $default = null): ?int
141 {
142 return $this->http_wrapper->query()->retrieve(
143 $key,
144 $this->refinery->byTrying([
145 $this->refinery->kindlyTo()->int(),
146 $this->refinery->always($default)
147 ])
148 );
149 }
150
151 private function getKeyValueFromSession(int $refId, string $key, mixed $default = null): mixed
152 {
153 $session_value = ilSession::get($this->buildSessionKey($refId, $key));
154 if ($session_value === null) {
155 $session_value = $default;
156 }
157
158 return $session_value;
159 }
160
161 private function setSessionKeyValue(int $refId, string $key, mixed $value): void
162 {
163 ilSession::set($this->buildSessionKey($refId, $key), $value);
164 }
165
166 private function buildSessionKey(int $refId, string $key): string
167 {
168 return "frm_{$refId}_$key";
169 }
170}
ThreadSortation
Builds data types.
Definition: Factory.php:36
getKeyValueFromSession(int $refId, string $key, mixed $default=null)
__construct(private readonly int $forum_ref_id, private readonly bool $is_moderator, ?WrapperFactory $http_wrapper=null, ?Factory $refinery=null)
Class Forum core functions for forum.
getAllThreads(int $a_topic_id, array $params=[], int $limit=0, int $offset=0)
Class ilObjForum.
static getUserIdsOfLastPostsByRefIdAndThreadIds(int $ref_id, array $thread_ids)
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
global $DIC
Definition: shib_login.php:26
$refId
Definition: xapitoken.php:58