ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 ->withRequest($this->http_request);
147 }
148
149 return $this->table_component;
150 }
151
152 public function getFilterComponent(): FilterComponent
153 {
154 if (!isset($this->filter_component)) {
155 $filter_inputs = [];
156 $is_input_initially_rendered = [];
157 $field_factory = $this->ui_factory->input()->field();
158
159 foreach ($this->getFilterFields($field_factory) as $filter_id => $filter) {
160 [$filter_inputs[$filter_id], $is_input_initially_rendered[$filter_id]] = $filter;
161 }
162
163 $this->filter_component = $this->ui_service->filter()->standard(
164 'forum_notification_filter',
165 $this->action,
166 $filter_inputs,
167 $is_input_initially_rendered,
168 true,
169 true
170 );
171 }
172
173 return $this->filter_component;
174 }
175
179 public function getFilterFields(Factory $field_factory): array
180 {
181 $options = [
182 'member' => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
183 'tutor' => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
184 'admin' => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
185 'moderators' => $this->lng->txt('frm_moderators'),
186 ];
187
188 return [
189 'role' => [
190 $field_factory->select($this->lng->txt('roles'), $options),
191 true
192 ]
193 ];
194 }
195
199 public function getTotalRowCount(
200 mixed $additional_viewcontrol_data,
201 mixed $filter_data,
202 mixed $additional_parameters
203 ): ?int {
204 $this->initRecords($filter_data);
205
206 return \count((array) $this->records);
207 }
208
213 public function getFilteredUserIds(?array $filter_data): array
214 {
215 $moderator_ids = ilForum::_getModerators($this->ref_id);
216
217 $admin_ids = $this->participants->getAdmins();
218 $member_ids = $this->participants->getMembers();
219 $tutor_ids = $this->participants->getTutors();
220
221 $filter = (string) ($filter_data['role'] ?? '');
222 switch ($filter) {
223 case 'member':
224 $user_ids = $member_ids;
225 break;
226
227 case 'tutor':
228 $user_ids = $tutor_ids;
229 break;
230
231 case 'admin':
232 $user_ids = $admin_ids;
233 break;
234
235 case 'moderators':
236 $user_ids = $moderator_ids;
237 break;
238
239 default:
240 $user_ids = array_merge($admin_ids, $member_ids, $tutor_ids, $moderator_ids);
241 break;
242 }
243
244 return array_unique($user_ids);
245 }
246
258 private function getRecords(Range $range, Order $order, ?array $filter_data): array
259 {
260 $this->initRecords($filter_data);
261 $records = $this->records;
262
263 if ($order) {
264 $records = $this->orderRecords($records, $order);
265 }
266
267 if ($range) {
268 $records = $this->limitRecords($records, $range);
269 }
270
271 return $records;
272 }
273
285 private function getUserNotificationTableData(array $user_ids): array
286 {
287 $icons = [
288 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small'),
289 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
290 ];
291
292 $counter = 0;
293 $users = [];
294 $moderator_ids = ilForum::_getModerators($this->ref_id);
295 foreach ($user_ids as $user_id) {
296 $forced_events = $this->forumNotificationObj->getForcedEventsObjectByUserId($user_id);
297 $member_const = 'IL_' . strtoupper($this->participants->getType()) . '_MEMBER';
298 $tutor_const = 'IL_' . strtoupper($this->participants->getType()) . '_TUTOR';
299 $admin_const = 'IL_' . strtoupper($this->participants->getType()) . '_ADMIN';
300 $member_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$member_const});
301 $tutor_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$tutor_const});
302 $admin_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$admin_const});
303
304 $types = implode(', ', array_map(function (int $role_id) use ($admin_id, $tutor_id, $member_id) {
305 return match ($role_id) {
306 $member_id => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
307 $tutor_id => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
308 $admin_id => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
309 default => ''
310 };
311 }, $this->participants->getAssignedRoles($user_id)));
312 if (\in_array($user_id, $moderator_ids, true)) {
313 $types .= ', ' . $this->lng->txt('frm_moderators');
314 }
315
316 $users[$counter]['user_id'] = $user_id;
317 $users[$counter]['login'] = ilObjUser::_lookupLogin($user_id);
319 $users[$counter]['firstname'] = $name['firstname'];
320 $users[$counter]['lastname'] = $name['lastname'];
321 $users[$counter]['user_toggle_noti'] = $icons[(int) $forced_events->getUserToggle()];
322 $users[$counter]['role'] = $types;
323
324 $counter++;
325 }
326
327 return $users;
328 }
329
333 public function getActions(
334 URLBuilder $url_builder,
335 URLBuilderToken $action_parameter_token,
336 URLBuilderToken $row_id_token
337 ): array {
338 return [
339 'enableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
340 $this->lng->txt('enable_hide_user_toggle'),
341 $url_builder->withParameter($action_parameter_token, 'enableHideUserToggleNoti'),
342 $row_id_token
343 ),
344 'disableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
345 $this->lng->txt('disable_hide_user_toggle'),
346 $url_builder->withParameter($action_parameter_token, 'disableHideUserToggleNoti'),
347 $row_id_token
348 ),
349 'notificationSettings' => $this->ui_factory->table()->action()->single(
350 $this->lng->txt('notification_settings'),
351 $url_builder->withParameter($action_parameter_token, 'notificationSettings'),
352 $row_id_token
353 )->withAsync(true),
354 ];
355 }
356
375 private function limitRecords(array $records, Range $range): array
376 {
377 return \array_slice($records, $range->getStart(), $range->getLength());
378 }
379
398 private function orderRecords(array $records, Order $order): array
399 {
400 [$order_field, $order_direction] = $order->join(
401 [],
402 fn($ret, $key, $value) => [$key, $value]
403 );
404 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
405 $left[$order_field] ?? '',
406 $right[$order_field] ?? ''
407 ));
408
409 if ($order_direction === Order::DESC) {
410 $records = array_reverse($records);
411 }
412
413 return $records;
414 }
415
419 private function getColumns(): array
420 {
421 return [
422 'login' => $this->ui_factory->table()->column()->text($this->lng->txt('login')),
423 'firstname' => $this->ui_factory->table()->column()->text($this->lng->txt('firstname')),
424 'lastname' => $this->ui_factory->table()->column()->text($this->lng->txt('lastname')),
425 'user_toggle_noti' => $this->ui_factory->table()->column()->statusIcon(
426 $this->lng->txt('allow_user_toggle_noti')
427 )
428 ->withIsSortable(false),
429 'role' => $this->ui_factory->table()->column()->text($this->lng->txt('role')),
430 ];
431 }
432}
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