ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ForumNotificationTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ilStr;
24use ilForum;
25use Generator;
26use ilObjUser;
27use ilLanguage;
28use ilUIService;
35use ILIAS\UI\Factory as UIFactory;
36use ILIAS\Data\Factory as DataFactory;
41use Psr\Http\Message\ServerRequestInterface;
42use ILIAS\UI\Component\Table\Data as DataTable;
46use ilUtil;
48
50{
59 private ?array $records = null;
60 private FilterComponent $filter_component;
61 private DataTable $table_component;
62
63 public function __construct(
64 private readonly ServerRequestInterface $http_request,
65 private readonly ilLanguage $lng,
66 private readonly UIFactory $ui_factory,
67 private readonly DataFactory $data_factory,
68 private readonly int $ref_id,
69 private readonly ilParticipants $participants,
70 private readonly ilForumNotification $forumNotificationObj,
71 private readonly ilUIService $ui_service,
72 private readonly string $action
73 ) {
74 }
75
79 public function getRows(
80 DataRowBuilder $row_builder,
81 array $visible_column_ids,
83 Order $order,
84 mixed $additional_viewcontrol_data,
85 mixed $filter_data,
86 mixed $additional_parameters
87 ): Generator {
88 $records = $this->getRecords($range, $order, $filter_data);
89 foreach ($records as $record) {
90 yield $row_builder->buildDataRow((string) $record['user_id'], $record);
91 }
92 }
93
97 public function initRecords(?array $filter_data): void
98 {
99 if ($this->records === null) {
100 $this->records = $this->getUserNotificationTableData($this->getFilteredUserIds($filter_data));
101 }
102 }
103
110 public function getComponents(): array
111 {
112 return [$this->getFilterComponent(), $this->getTableComponent()];
113 }
114
115 public function getTableComponent(): DataTable
116 {
117 if (!isset($this->table_component)) {
118 $query_params_namespace = ['frm', 'notifications', 'table'];
119 $table_uri = $this->data_factory->uri(ilUtil::_getHttpPath() . '/' . $this->action);
120 $url_builder = new URLBuilder($table_uri);
121 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
122 $query_params_namespace,
123 'action',
124 'usr_ids'
125 );
126
127 $this->table_component = $this->ui_factory->table()
128 ->data(
129 $this,
130 $this->lng->txt(''),
131 $this->getColumns()
132 )
133 ->withFilter(
134 $this->ui_service->filter()->getData(
135 $this->getFilterComponent()
136 )
137 )
138 ->withActions(
139 $this->getActions(
140 $url_builder,
141 $action_parameter_token,
142 $row_id_token
143 )
144 )
145 ->withId('forum_notification_table')
146 ->withRange(new Range(0, 50))
147 ->withRequest($this->http_request);
148 }
149
150 return $this->table_component;
151 }
152
153 public function getFilterComponent(): FilterComponent
154 {
155 if (!isset($this->filter_component)) {
156 $filter_inputs = [];
157 $is_input_initially_rendered = [];
158 $field_factory = $this->ui_factory->input()->field();
159
160 foreach ($this->getFilterFields($field_factory) as $filter_id => $filter) {
161 [$filter_inputs[$filter_id], $is_input_initially_rendered[$filter_id]] = $filter;
162 }
163
164 $this->filter_component = $this->ui_service->filter()->standard(
165 'forum_notification_filter',
166 $this->action,
167 $filter_inputs,
168 $is_input_initially_rendered,
169 true,
170 true
171 );
172 }
173
174 return $this->filter_component;
175 }
176
180 public function getFilterFields(Factory $field_factory): array
181 {
182 $options = [
183 'member' => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
184 'tutor' => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
185 'admin' => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
186 'moderators' => $this->lng->txt('frm_moderators'),
187 ];
188
189 return [
190 'role' => [
191 $field_factory->select($this->lng->txt('roles'), $options),
192 true
193 ]
194 ];
195 }
196
200 public function getTotalRowCount(
201 mixed $additional_viewcontrol_data,
202 mixed $filter_data,
203 mixed $additional_parameters
204 ): ?int {
205 $this->initRecords($filter_data);
206
207 return \count((array) $this->records);
208 }
209
214 public function getFilteredUserIds(?array $filter_data): array
215 {
216 $moderator_ids = ilForum::_getModerators($this->ref_id);
217
218 $admin_ids = $this->participants->getAdmins();
219 $member_ids = $this->participants->getMembers();
220 $tutor_ids = $this->participants->getTutors();
221
222 $filter = (string) ($filter_data['role'] ?? '');
223 switch ($filter) {
224 case 'member':
225 $user_ids = $member_ids;
226 break;
227
228 case 'tutor':
229 $user_ids = $tutor_ids;
230 break;
231
232 case 'admin':
233 $user_ids = $admin_ids;
234 break;
235
236 case 'moderators':
237 $user_ids = $moderator_ids;
238 break;
239
240 default:
241 $user_ids = array_merge($admin_ids, $member_ids, $tutor_ids, $moderator_ids);
242 break;
243 }
244
245 return array_unique($user_ids);
246 }
247
259 private function getRecords(Range $range, Order $order, ?array $filter_data): array
260 {
261 $this->initRecords($filter_data);
262 $records = $this->records;
263
264 if ($order) {
265 $records = $this->orderRecords($records, $order);
266 }
267
268 if ($range) {
269 $records = $this->limitRecords($records, $range);
270 }
271
272 return $records;
273 }
274
286 private function getUserNotificationTableData(array $user_ids): array
287 {
288 $icons = [
289 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small'),
290 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
291 ];
292
293 $counter = 0;
294 $users = [];
295 $moderator_ids = ilForum::_getModerators($this->ref_id);
296 foreach ($user_ids as $user_id) {
297 $forced_events = $this->forumNotificationObj->getForcedEventsObjectByUserId($user_id);
298 $member_const = 'IL_' . strtoupper($this->participants->getType()) . '_MEMBER';
299 $tutor_const = 'IL_' . strtoupper($this->participants->getType()) . '_TUTOR';
300 $admin_const = 'IL_' . strtoupper($this->participants->getType()) . '_ADMIN';
301 $member_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$member_const});
302 $tutor_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$tutor_const});
303 $admin_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$admin_const});
304
305 $types = implode(', ', array_map(function (int $role_id) use ($admin_id, $tutor_id, $member_id) {
306 return match ($role_id) {
307 $member_id => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
308 $tutor_id => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
309 $admin_id => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
310 default => ''
311 };
312 }, $this->participants->getAssignedRoles($user_id)));
313 if (\in_array($user_id, $moderator_ids, true)) {
314 $types .= ', ' . $this->lng->txt('frm_moderators');
315 }
316
317 $users[$counter]['user_id'] = $user_id;
318 $users[$counter]['login'] = ilObjUser::_lookupLogin($user_id);
320 $users[$counter]['firstname'] = $name['firstname'];
321 $users[$counter]['lastname'] = $name['lastname'];
322 $users[$counter]['user_toggle_noti'] = $icons[(int) $forced_events->getUserToggle()];
323 $users[$counter]['role'] = $types;
324
325 $counter++;
326 }
327
328 return $users;
329 }
330
334 public function getActions(
335 URLBuilder $url_builder,
336 URLBuilderToken $action_parameter_token,
337 URLBuilderToken $row_id_token
338 ): array {
339 return [
340 'enableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
341 $this->lng->txt('enable_hide_user_toggle'),
342 $url_builder->withParameter($action_parameter_token, 'enableHideUserToggleNoti'),
343 $row_id_token
344 ),
345 'disableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
346 $this->lng->txt('disable_hide_user_toggle'),
347 $url_builder->withParameter($action_parameter_token, 'disableHideUserToggleNoti'),
348 $row_id_token
349 ),
350 'notificationSettings' => $this->ui_factory->table()->action()->single(
351 $this->lng->txt('notification_settings'),
352 $url_builder->withParameter($action_parameter_token, 'notificationSettings'),
353 $row_id_token
354 )->withAsync(true),
355 ];
356 }
357
376 private function limitRecords(array $records, Range $range): array
377 {
378 return \array_slice($records, $range->getStart(), $range->getLength());
379 }
380
399 private function orderRecords(array $records, Order $order): array
400 {
401 [$order_field, $order_direction] = $order->join(
402 [],
403 fn($ret, $key, $value) => [$key, $value]
404 );
405 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
406 $left[$order_field] ?? '',
407 $right[$order_field] ?? ''
408 ));
409
410 if ($order_direction === Order::DESC) {
411 $records = array_reverse($records);
412 }
413
414 return $records;
415 }
416
420 private function getColumns(): array
421 {
422 return [
423 'login' => $this->ui_factory->table()->column()->text($this->lng->txt('login')),
424 'firstname' => $this->ui_factory->table()->column()->text($this->lng->txt('firstname')),
425 'lastname' => $this->ui_factory->table()->column()->text($this->lng->txt('lastname')),
426 'user_toggle_noti' => $this->ui_factory->table()->column()->statusIcon(
427 $this->lng->txt('allow_user_toggle_noti')
428 )
429 ->withIsSortable(false),
430 'role' => $this->ui_factory->table()->column()->text($this->lng->txt('role')),
431 ];
432 }
433}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
getRecords(Range $range, Order $order, ?array $filter_data)
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
__construct(private readonly ServerRequestInterface $http_request, private readonly ilLanguage $lng, private readonly UIFactory $ui_factory, private readonly DataFactory $data_factory, private readonly int $ref_id, private readonly ilParticipants $participants, private readonly ilForumNotification $forumNotificationObj, private readonly ilUIService $ui_service, private readonly string $action)
return true
Class ilForumNotification.
Class Forum core functions for forum.
static _getModerators(int $a_ref_id)
language handling
User class.
static _lookupName(int $a_user_id)
static _lookupLogin(int $a_user_id)
Base class for course and group participants.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilStr.php:20
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
Filter service.
Util class various functions, usage as namespace.
static _getHttpPath()
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This interface must be implemented by all Inputs that support Filter Containers.
Definition: FilterInput.php:36
This describes a standard filter.
Definition: Standard.php:27
This is what a factory for input fields looks like.
Definition: Factory.php:31
select(string $label, array $options, ?string $byline=null)
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
buildDataRow(string $id, array $record)
This describes a Data Table.
Definition: Data.php:33
$ref_id
Definition: ltiauth.php:66
global $lng
Definition: privfeed.php:31
$counter