ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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
50 $this->http_wrapper = $http_wrapper ?? $DIC->http()->wrapper();
51 $this->refinery = $refinery ?? $DIC->refinery();
52 }
53
54 public function fetchData(ilForum $forum, ForumDto $topicData): ThreadsPage
55 {
56 $sortation = $this->getThreadSortation();
57 $page = $this->getThreadPage();
59
60 $params = [
61 'is_moderator' => $this->is_moderator,
62 'excluded_ids' => [],
63 'order_column' => $sortation->field(),
64 'order_direction' => $sortation->direction()
65 ];
66
67 $data = $forum->getAllThreads(
68 $topicData->getTopPk(),
69 $params,
70 $limit,
71 $page * $limit
72 );
73 if ($data['items'] === [] && $page > 0) {
74 ilSession::set($this->buildSessionKey($this->forum_ref_id, self::KEY_THREAD_PAGE), 0);
75 $data = $forum->getAllThreads(
76 $topicData->getTopPk(),
77 $params,
78 $limit,
79 $page * $limit
80 );
81 }
82
83 $items = $data['items'];
84
85 $thread_ids = [];
86 $user_ids = [];
87 foreach ($items as $thread) {
89 $thread_ids[] = $thread->getId();
90 if ($thread->getDisplayUserId() > 0) {
91 $user_ids[$thread->getDisplayUserId()] = $thread->getDisplayUserId();
92 }
93 }
94
95 $user_ids = array_merge(
96 ilObjForum::getUserIdsOfLastPostsByRefIdAndThreadIds($this->forum_ref_id, $thread_ids),
97 $user_ids
98 );
99
101
102 return new ThreadsPage($items);
103 }
104
106 {
107 $query_thread_sortation = $this->getKeyValueFromQuery(self::KEY_THREAD_SORTATION);
108
109 if ($query_thread_sortation !== null) {
110 $this->setSessionKeyValue($this->forum_ref_id, self::KEY_THREAD_SORTATION, $query_thread_sortation);
111
112 return ThreadSortation::tryFrom($query_thread_sortation) ?? ThreadSortation::DEFAULT_SORTATION;
113 }
114
115 $session_thread_sortation = $this->getKeyValueFromSession(
116 $this->forum_ref_id,
117 self::KEY_THREAD_SORTATION,
118 ThreadSortation::DEFAULT_SORTATION->value
119 );
120
121 return ThreadSortation::tryFrom(
122 $session_thread_sortation
123 ) ?? ThreadSortation::DEFAULT_SORTATION;
124 }
125
126 public function getThreadPage(): int
127 {
128 $query_thread_page = $this->getKeyValueFromQuery(self::KEY_THREAD_PAGE, null);
129
130 if ($query_thread_page !== null) {
131 $this->setSessionKeyValue($this->forum_ref_id, self::KEY_THREAD_PAGE, $query_thread_page);
132 return $query_thread_page;
133 }
134
135 return (int) $this->getKeyValueFromSession(
136 $this->forum_ref_id,
137 self::KEY_THREAD_PAGE,
138 $query_thread_page
139 );
140 }
141
142 private function getKeyValueFromQuery(string $key, ?int $default = null): ?int
143 {
144 return $this->http_wrapper->query()->retrieve(
145 $key,
146 $this->refinery->byTrying([
147 $this->refinery->kindlyTo()->int(),
148 $this->refinery->always($default)
149 ])
150 );
151 }
152
153 private function getKeyValueFromSession(int $refId, string $key, mixed $default = null): mixed
154 {
155 $session_value = ilSession::get($this->buildSessionKey($refId, $key));
156 if ($session_value === null) {
157 $session_value = $default;
158 }
159
160 return $session_value;
161 }
162
163 private function setSessionKeyValue(int $refId, string $key, mixed $value): void
164 {
165 ilSession::set($this->buildSessionKey($refId, $key), $value);
166 }
167
168 private function buildSessionKey(int $refId, string $key): string
169 {
170 return "frm_{$refId}_$key";
171 }
172}
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:56