ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
RegistrationCodesTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Registration;
22 
23 use ilObject;
24 use Generator;
25 use ilObjUser;
26 use ilLanguage;
27 use DateTimeZone;
28 use ilRbacReview;
43 
44 class RegistrationCodesTable implements DataRetrieval
45 {
46  public function __construct(
47  private readonly ServerRequestInterface $http_request,
48  private readonly ilLanguage $lng,
49  private readonly UIFactory $ui_factory,
50  private readonly DataFactory $data_factory,
51  private readonly ilRbacReview $rbac_review,
52  private readonly string $action,
53  private readonly ilObjUser $actor,
54  private readonly RegistrationCodeRepository $code_repository,
55  private readonly bool $has_permission_to_delete = false,
56  ) {
57  }
58 
62  public function getRows(
63  DataRowBuilder $row_builder,
64  array $visible_column_ids,
65  Range $range,
66  Order $order,
67  ?array $filter_data,
68  ?array $additional_parameters,
69  ): Generator {
70  $records = $this->getRecords($range, $order, $filter_data);
71  foreach ($records as $record) {
72  yield $row_builder->buildDataRow((string) $record['code_id'], $record);
73  }
74  }
75 
76  public function getTableComponent(RegistrationFilterComponent $filter): DataTable
77  {
78  $query_params_namespace = ['registration', 'codes'];
79  $table_uri = $this->data_factory->uri(ILIAS_HTTP_PATH . '/' . $this->action);
80  $url_builder = new URLBuilder($table_uri);
82  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
83  $query_params_namespace,
84  'table_action',
85  'code_ids',
86  );
87 
88  return $this->ui_factory->table()
89  ->data(
90  $this,
91  '',
92  $this->getColumns()
93  )
94  ->withId(
95  'registration_code'
96  )
97  ->withFilter($filter->getFilterData()->getData())
98  ->withOrder(new Order('generated', Order::DESC))
99  ->withRequest($this->http_request)
100  ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token));
101  }
102 
106  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
107  {
108  return $this->code_repository->getTotalCodeCount(
109  (new CodeFilter())->withData($filter_data)
110  );
111  }
112 
125  private function getRecords(Range $range, Order $order, ?array $filter_data): array
126  {
127  [$order_field, $order_direction] = $order->join(
128  [],
129  fn(array $ret, string $key, string $value): array => [$key, $value]
130  );
131  $filter = (new CodeFilter())->withData($filter_data);
132 
133  $codes_data = $this->code_repository->getCodesData(
134  $order_field,
135  $order_direction,
136  $range->getStart(),
137  $range->getLength(),
138  $filter
139  );
140 
141  if (\count($codes_data) === 0 && $range->getStart() > 0) {
142  $codes_data = $this->code_repository->getCodesData(
143  $order_field,
144  $order_direction,
145  0,
146  $range->getLength(),
147  $filter
148  );
149  }
150 
151  if ((int) $this->actor->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
152  $date_format = $this->data_factory->dateFormat()->withTime12($this->actor->getDateFormat());
153  } else {
154  $date_format = $this->data_factory->dateFormat()->withTime24($this->actor->getDateFormat());
155  }
156 
157  $role_map = [];
158  foreach ($this->rbac_review->getGlobalRoles() as $role_id) {
159  if (!\in_array($role_id, [SYSTEM_ROLE_ID, ANONYMOUS_ROLE_ID], true)) {
160  $role_map[$role_id] = ilObject::_lookupTitle($role_id);
161  }
162  }
163 
164  $result = [];
165  foreach ($codes_data as $k => $code) {
166  $result[$k]['code'] = $code['code'];
167  $result[$k]['code_id'] = (int) $code['code_id'];
168 
169  $result[$k]['generated'] = (new DateTimeImmutable('@' . $code['generated']))->setTimezone(
170  new DateTimeZone($this->actor->getTimeZone())
171  );
172  if ($code['used']) {
173  $result[$k]['used'] = $date_format->applyTo(
174  (new DateTimeImmutable('@' . $code['used']))->setTimezone(
175  new DateTimeZone($this->actor->getTimeZone())
176  )
177  );
178  } else {
179  $result[$k]['used'] = null;
180  }
181 
182  if ($code['role']) {
183  $result[$k]['role'] = $role_map[$code['role']] ?? $this->lng->txt('deleted');
184  } else {
185  $result[$k]['role'] = '';
186  }
187 
188  if (\is_string($code['role_local'])) {
189  $local = [];
190  foreach (explode(';', $code['role_local']) as $role_id) {
191  $role = ilObject::_lookupTitle((int) $role_id);
192  if ($role) {
193  $local[] = $role;
194  }
195  }
196  if (\count($local)) {
197  sort($local);
198  $result[$k]['role_local'] = implode('<br />', $local);
199  }
200  } else {
201  $result[$k]['role_local'] = '';
202  }
203 
204  if ($code['alimit']) {
205  switch ($code['alimit']) {
206  case 'unlimited':
207  $result[$k]['alimit'] = $this->lng->txt('reg_access_limitation_none');
208  break;
209 
210  case 'absolute':
211  $result[$k]['alimit'] = $this->lng->txt('reg_access_limitation_mode_absolute_target') .
212  ': ' .
213  $date_format->applyTo(
214  (new DateTimeImmutable('@' . $code['alimitdt']))->setTimezone(
215  new DateTimeZone($this->actor->getTimeZone())
216  )
217  );
218  break;
219 
220  case 'relative':
221  $limit_caption = [];
222  $limit = unserialize($code['alimitdt'], ['allowed_classes' => false]);
223  if ((int) $limit['d']) {
224  $limit_caption[] = (int) $limit['d'] . ' ' . $this->lng->txt('days');
225  }
226  if ((int) $limit['m']) {
227  $limit_caption[] = (int) $limit['m'] . ' ' . $this->lng->txt('months');
228  }
229  if ((int) $limit['y']) {
230  $limit_caption[] = (int) $limit['y'] . ' ' . $this->lng->txt('years');
231  }
232  if (\count($limit_caption)) {
233  $result[$k]['alimit'] = $this->lng->txt('reg_access_limitation_mode_relative_target') .
234  ': ' . implode(', ', $limit_caption);
235  }
236  break;
237  }
238  }
239  }
240 
241  return $result;
242  }
243 
247  public function getActions(
248  URLBuilder $url_builder,
249  URLBuilderToken $action_parameter_token,
250  URLBuilderToken $row_id_token
251  ): array {
252  $actions = [
253  $this->ui_factory->table()->action()->multi(
254  $this->lng->txt('registration_codes_export'),
255  $url_builder->withParameter($action_parameter_token, 'exportCodes'),
256  $row_id_token
257  ),
258  ];
259  if ($this->has_permission_to_delete) {
260  $actions[] = $this->ui_factory->table()->action()->multi(
261  $this->lng->txt('delete'),
262  $url_builder->withParameter($action_parameter_token, 'deleteConfirmation'),
263  $row_id_token
264  );
265  }
266 
267  return $actions;
268  }
269 
273  private function getColumns(): array
274  {
275  if ((int) $this->actor->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
276  $date_format = $this->data_factory->dateFormat()->withTime12($this->actor->getDateFormat());
277  } else {
278  $date_format = $this->data_factory->dateFormat()->withTime24($this->actor->getDateFormat());
279  }
280 
281  return [
282  'code' => $this->ui_factory->table()->column()
283  ->text($this->lng->txt('registration_code')),
284  'role' => $this->ui_factory->table()->column()
285  ->text($this->lng->txt('registration_codes_roles')),
286  'role_local' => $this->ui_factory->table()->column()
287  ->text($this->lng->txt('registration_codes_roles_local'))
288  ->withIsSortable(false),
289  'alimit' => $this->ui_factory->table()->column()
290  ->text($this->lng->txt('reg_access_limitations'))
291  ->withIsSortable(false),
292  'generated' => $this->ui_factory->table()->column()
293  ->date($this->lng->txt('registration_generated'), $date_format),
294  'used' => $this->ui_factory->table()->column()
295  ->text($this->lng->txt('registration_used')),
296  ];
297  }
298 }
join($init, callable $fn)
Definition: Order.php:75
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
const SYSTEM_ROLE_ID
Definition: constants.php:29
sort()
description: > Example for rendering a Sort Glyph.
Definition: sort.php:41
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
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
getRecords(Range $range, Order $order, ?array $filter_data)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters,)
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
__construct(private readonly ServerRequestInterface $http_request, private readonly ilLanguage $lng, private readonly UIFactory $ui_factory, private readonly DataFactory $data_factory, private readonly ilRbacReview $rbac_review, private readonly string $action, private readonly ilObjUser $actor, private readonly RegistrationCodeRepository $code_repository, private readonly bool $has_permission_to_delete=false,)