ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ForumNotificationTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilStr;
24 use ilForum;
25 use Generator;
26 use ilObjUser;
27 use ilLanguage;
28 use ilUIService;
29 use ilParticipants;
46 use ilUtil;
47 
49 {
58  private ?array $records = null;
61 
62  public function __construct(
63  private readonly ServerRequestInterface $http_request,
64  private readonly ilLanguage $lng,
65  private readonly UIFactory $ui_factory,
66  private readonly DataFactory $data_factory,
67  private readonly int $ref_id,
68  private readonly ilParticipants $participants,
69  private readonly ilForumNotification $forumNotificationObj,
70  private readonly ilUIService $ui_service,
71  private readonly string $action
72  ) {
73  }
74 
78  public function getRows(
79  \ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
80  array $visible_column_ids,
81  Range $range,
82  Order $order,
83  ?array $filter_data,
84  ?array $additional_parameters,
85  ): Generator {
86  $records = $this->getRecords($range, $order, $filter_data);
87  foreach ($records as $record) {
88  yield $row_builder->buildDataRow((string) $record['user_id'], $record);
89  }
90  }
91 
95  public function initRecords(?array $filter_data): void
96  {
97  if ($this->records === null) {
98  $this->records = $this->getUserNotificationTableData($this->getFilteredUserIds($filter_data));
99  }
100  }
101 
108  public function getComponents(): array
109  {
110  return [$this->getFilterComponent(), $this->getTableComponent()];
111  }
112 
113  public function getTableComponent(): DataTable
114  {
115  if (!isset($this->table_component)) {
116  $query_params_namespace = ['frm', 'notifications', 'table'];
117  $table_uri = $this->data_factory->uri(ilUtil::_getHttpPath() . '/' . $this->action);
118  $url_builder = new URLBuilder($table_uri);
119  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
120  $query_params_namespace,
121  'action',
122  'usr_ids'
123  );
124 
125  $this->table_component = $this->ui_factory->table()
126  ->data(
127  $this,
128  $this->lng->txt(''),
129  $this->getColumns()
130  )
131  ->withFilter(
132  $this->ui_service->filter()->getData(
133  $this->getFilterComponent()
134  )
135  )
136  ->withActions(
137  $this->getActions(
138  $url_builder,
139  $action_parameter_token,
140  $row_id_token
141  )
142  )
143  ->withId('forum_notification_table')
144  ->withRequest($this->http_request);
145  }
146 
147  return $this->table_component;
148  }
149 
151  {
152  if (!isset($this->filter_component)) {
153  $filter_inputs = [];
154  $is_input_initially_rendered = [];
155  $field_factory = $this->ui_factory->input()->field();
156 
157  foreach ($this->getFilterFields($field_factory) as $filter_id => $filter) {
158  [$filter_inputs[$filter_id], $is_input_initially_rendered[$filter_id]] = $filter;
159  }
160 
161  $this->filter_component = $this->ui_service->filter()->standard(
162  'forum_notification_filter',
163  $this->action,
164  $filter_inputs,
165  $is_input_initially_rendered,
166  true,
167  true
168  );
169  }
170 
172  }
173 
177  public function getFilterFields(Factory $field_factory): array
178  {
179  $options = [
180  'member' => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
181  'tutor' => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
182  'admin' => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
183  'moderators' => $this->lng->txt('frm_moderators'),
184  ];
185 
186  return [
187  'role' => [
188  $field_factory->select($this->lng->txt('roles'), $options),
189  true
190  ]
191  ];
192  }
193 
197  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
198  {
199  $this->initRecords($filter_data);
200 
201  return \count((array) $this->records);
202  }
203 
208  public function getFilteredUserIds(?array $filter_data): array
209  {
210  $moderator_ids = ilForum::_getModerators($this->ref_id);
211 
212  $admin_ids = $this->participants->getAdmins();
213  $member_ids = $this->participants->getMembers();
214  $tutor_ids = $this->participants->getTutors();
215 
216  $filter = (string) ($filter_data['role'] ?? '');
217  switch ($filter) {
218  case 'member':
219  $user_ids = $member_ids;
220  break;
221 
222  case 'tutor':
223  $user_ids = $tutor_ids;
224  break;
225 
226  case 'admin':
227  $user_ids = $admin_ids;
228  break;
229 
230  case 'moderators':
231  $user_ids = $moderator_ids;
232  break;
233 
234  default:
235  $user_ids = array_merge($admin_ids, $member_ids, $tutor_ids, $moderator_ids);
236  break;
237  }
238 
239  return array_unique($user_ids);
240  }
241 
253  private function getRecords(Range $range, Order $order, ?array $filter_data): array
254  {
255  $this->initRecords($filter_data);
256  $records = $this->records;
257 
258  if ($order) {
259  $records = $this->orderRecords($records, $order);
260  }
261 
262  if ($range) {
263  $records = $this->limitRecords($records, $range);
264  }
265 
266  return $records;
267  }
268 
280  private function getUserNotificationTableData(array $user_ids): array
281  {
282  $icons = [
283  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small'),
284  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
285  ];
286 
287  $counter = 0;
288  $users = [];
289  $moderator_ids = ilForum::_getModerators($this->ref_id);
290  foreach ($user_ids as $user_id) {
291  $forced_events = $this->forumNotificationObj->getForcedEventsObjectByUserId($user_id);
292  $member_const = 'IL_' . strtoupper($this->participants->getType()) . '_MEMBER';
293  $tutor_const = 'IL_' . strtoupper($this->participants->getType()) . '_TUTOR';
294  $admin_const = 'IL_' . strtoupper($this->participants->getType()) . '_ADMIN';
295  $member_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$member_const});
296  $tutor_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$tutor_const});
297  $admin_id = $this->participants->getAutoGeneratedRoleId(ilParticipants::{$admin_const});
298 
299  $types = implode(', ', array_map(function (int $role_id) use ($admin_id, $tutor_id, $member_id) {
300  return match ($role_id) {
301  $member_id => $this->lng->txt('il_' . $this->participants->getType() . '_member'),
302  $tutor_id => $this->lng->txt('il_' . $this->participants->getType() . '_tutor'),
303  $admin_id => $this->lng->txt('il_' . $this->participants->getType() . '_admin'),
304  default => ''
305  };
306  }, $this->participants->getAssignedRoles($user_id)));
307  if (\in_array($user_id, $moderator_ids, true)) {
308  $types .= ', ' . $this->lng->txt('frm_moderators');
309  }
310 
311  $users[$counter]['user_id'] = $user_id;
312  $users[$counter]['login'] = ilObjUser::_lookupLogin($user_id);
313  $name = ilObjUser::_lookupName($user_id);
314  $users[$counter]['firstname'] = $name['firstname'];
315  $users[$counter]['lastname'] = $name['lastname'];
316  $users[$counter]['user_toggle_noti'] = $icons[(int) $forced_events->getUserToggle()];
317  $users[$counter]['role'] = $types;
318 
319  $counter++;
320  }
321 
322  return $users;
323  }
324 
328  public function getActions(
329  URLBuilder $url_builder,
330  URLBuilderToken $action_parameter_token,
331  URLBuilderToken $row_id_token
332  ): array {
333  return [
334  'enableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
335  $this->lng->txt('enable_hide_user_toggle'),
336  $url_builder->withParameter($action_parameter_token, 'enableHideUserToggleNoti'),
337  $row_id_token
338  ),
339  'disableHideUserToggleNoti' => $this->ui_factory->table()->action()->multi(
340  $this->lng->txt('disable_hide_user_toggle'),
341  $url_builder->withParameter($action_parameter_token, 'disableHideUserToggleNoti'),
342  $row_id_token
343  ),
344  'notificationSettings' => $this->ui_factory->table()->action()->single(
345  $this->lng->txt('notification_settings'),
346  $url_builder->withParameter($action_parameter_token, 'notificationSettings'),
347  $row_id_token
348  )->withAsync(true),
349  ];
350  }
351 
370  private function limitRecords(array $records, Range $range): array
371  {
372  return \array_slice($records, $range->getStart(), $range->getLength());
373  }
374 
393  private function orderRecords(array $records, Order $order): array
394  {
395  [$order_field, $order_direction] = $order->join(
396  [],
397  fn($ret, $key, $value) => [$key, $value]
398  );
399  usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
400  $left[$order_field] ?? '',
401  $right[$order_field] ?? ''
402  ));
403 
404  if ($order_direction === Order::DESC) {
405  $records = array_reverse($records);
406  }
407 
408  return $records;
409  }
410 
414  private function getColumns(): array
415  {
416  return [
417  'login' => $this->ui_factory->table()->column()->text($this->lng->txt('login')),
418  'firstname' => $this->ui_factory->table()->column()->text($this->lng->txt('firstname')),
419  'lastname' => $this->ui_factory->table()->column()->text($this->lng->txt('lastname')),
420  'user_toggle_noti' => $this->ui_factory->table()->column()->statusIcon(
421  $this->lng->txt('allow_user_toggle_noti')
422  )
423  ->withIsSortable(false),
424  'role' => $this->ui_factory->table()->column()->text($this->lng->txt('role')),
425  ];
426  }
427 }
This is what a factory for input fields looks like.
Definition: Factory.php:30
join($init, callable $fn)
Definition: Order.php:75
select(string $label, array $options, ?string $byline=null)
description: purpose: > A select is used to allow users to pick among a number of options...
Interface Observer Contains several chained tasks and infos about them.
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
static _lookupName(int $a_user_id)
lookup user name
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__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)
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
static _getHttpPath()
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
global $lng
Definition: privfeed.php:31
static _getModerators(int $a_ref_id)
getRecords(Range $range, Order $order, ?array $filter_data)
URLBuilder.
Definition: URLBuilder.php:40
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
getRows(\ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters,)
static _lookupLogin(int $a_user_id)