ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
CertificateOverviewTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use DateInterval;
25 use Generator;
26 use ilAccessHandler;
28 use ilCtrl;
29 use ilCtrlInterface;
39 use ilLanguage;
40 use ilLink;
42 use ilObject;
43 use ilObjUser;
44 use ilUIService;
50 use Throwable;
51 
53 {
55  private readonly ilUIService $ui_service;
56  private readonly Factory $ui_factory;
57  private readonly ilLanguage $lng;
58  private readonly ServerRequestInterface $request;
59  private readonly \ILIAS\Data\Factory $data_factory;
60  private readonly ilCtrl|ilCtrlInterface $ctrl;
61  private readonly \ILIAS\UI\Component\Input\Container\Filter\Standard $filter;
62  private readonly Data $table;
63  private readonly Renderer $ui_renderer;
64  private readonly ilAccessHandler $access;
65  private readonly ilObjUser $user;
66  private readonly \DateTimeZone $user_timezone;
67 
68  public function __construct(
69  ?Factory $ui_factory = null,
71  ?ilUIService $ui_service = null,
72  ?ilLanguage $lng = null,
74  ?\ILIAS\Data\Factory $data_factory = null,
75  ?ilCtrl $ctrl = null,
76  ?Renderer $ui_renderer = null,
77  ?ilAccessHandler $access = null,
78  ?ilObjUser $user = null
79  ) {
80  global $DIC;
81  $this->ui_factory = $ui_factory ?: $DIC->ui()->factory();
82  $this->repo = $repo ?: new ilUserCertificateRepository();
83  $this->ui_service = $ui_service ?: $DIC->uiService();
84  $this->lng = $lng ?: $DIC->language();
85  $this->request = $request ?: $DIC->http()->request();
86  $this->data_factory = $data_factory ?: new \ILIAS\Data\Factory();
87  $this->ctrl = $ctrl ?: $DIC->ctrl();
88  $this->ui_renderer = $ui_renderer ?: $DIC->ui()->renderer();
89  $this->access = $access ?: $DIC->access();
90  $this->user = $user ?: $DIC->user();
91  $this->user_timezone = new \DateTimeZone($this->user->getTimeZone());
92 
93  $this->filter = $this->buildFilter();
94  $this->table = $this->buildTable();
95  }
96 
97  public function getRows(
98  DataRowBuilder $row_builder,
99  array $visible_column_ids,
100  Range $range,
101  Order $order,
102  ?array $filter_data,
103  ?array $additional_parameters
104  ): Generator {
108  [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
109 
110  $ui_filter_data = $this->mapUiFilterData($this->ui_service->filter()->getData($this->filter));
111 
112  $table_rows = $this->buildTableRows($this->repo->fetchCertificatesForOverview(
113  $this->user->getLanguage(),
114  $ui_filter_data,
115  $range,
116  $order_field,
117  $order_direction
118  ));
119 
120  foreach ($table_rows as $row) {
121  $row['issue_date'] = (new DateTimeImmutable())
122  ->setTimestamp($row['issue_date'])
123  ->setTimezone($this->user_timezone);
124  yield $row_builder->buildDataRow((string) $row['id'], $row);
125  }
126  }
127 
132  private function mapUiFilterData(array $filter_data): array
133  {
134  if (isset($filter_data['issue_date']) && $filter_data['issue_date'] !== '') {
135  try {
136  $from = new DateTimeImmutable($filter_data['issue_date'][0], $this->user_timezone);
137  } catch (Throwable) {
138  $from = null;
139  }
140 
141  try {
142  $to = new DateTimeImmutable($filter_data['issue_date'][1], $this->user_timezone);
143  $seconds_to_add = 59 - (int) $to->format('s');
144  $to = $to->modify("+$seconds_to_add seconds");
145  } catch (Throwable) {
146  $to = null;
147  }
148 
149  $filter_data['issue_date'] = [
150  'from' => $from,
151  'to' => $to
152  ];
153  } else {
154  $filter_data['issue_date'] = [
155  'from' => null,
156  'to' => null
157  ];
158  }
159 
160  return $filter_data;
161  }
162 
163  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
164  {
165  $ui_filter_data = $this->mapUiFilterData($this->ui_service->filter()->getData($this->filter));
166 
167  return $this->repo->fetchCertificatesForOverviewCount($ui_filter_data);
168  }
169 
170 
171  private function buildFilter(): \ILIAS\UI\Component\Input\Container\Filter\Standard
172  {
173  if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
174  $date_format = $this->data_factory->dateFormat()->withTime12($this->user->getDateFormat());
175  } else {
176  $date_format = $this->data_factory->dateFormat()->withTime24($this->user->getDateFormat());
177  }
178 
179  return $this->ui_service->filter()->standard(
180  'certificates_overview_filter',
181  $this->ctrl->getLinkTargetByClass(
182  ilObjCertificateSettingsGUI::class,
184  ),
185  [
186  'certificate_id' => $this->ui_factory->input()->field()->text($this->lng->txt('certificate_id')),
187  'issue_date' => $this->ui_factory->input()->field()
188  ->duration($this->lng->txt('certificate_issue_date'))
189  ->withFormat($date_format)
190  ->withUseTime(true),
191  'object' => $this->ui_factory->input()->field()->text($this->lng->txt('obj')),
192  'obj_id' => $this->ui_factory->input()->field()->text($this->lng->txt('object_id')),
193  'owner' => $this->ui_factory->input()->field()->text($this->lng->txt('owner')),
194  ],
195  [true, true, true, true, true],
196  true,
197  true
198  );
199  }
200 
201  private function buildTable(): Data
202  {
203  $ui_table = $this->ui_factory->table();
204 
205  if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
206  $date_format = $this->data_factory->dateFormat()->withTime12($this->user->getDateFormat());
207  } else {
208  $date_format = $this->data_factory->dateFormat()->withTime24($this->user->getDateFormat());
209  }
210  return $ui_table->data(
211  $this,
212  $this->lng->txt('certificates'),
213  [
214  'certificate_id' => $ui_table->column()->text($this->lng->txt('certificate_id')),
215  'issue_date' => $ui_table->column()->date($this->lng->txt('certificate_issue_date'), $date_format),
216  'object' => $ui_table->column()->text($this->lng->txt('obj')),
217  'obj_id' => $ui_table->column()->text($this->lng->txt('object_id')),
218  'owner' => $ui_table->column()->text($this->lng->txt('owner'))
219  ],
220  )
221  ->withOrder(new Order('issue_date', Order::DESC))
222  ->withId('certificateOverviewTable')
223  ->withRequest($this->request)
224  ->withActions($this->buildTableActions());
225  }
226 
230  private function buildTableActions(): array
231  {
232  $uri_download = $this->data_factory->uri(
233  ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
234  ilObjCertificateSettingsGUI::class,
236  )
237  );
238 
244  [
245  $url_builder_download,
246  $action_parameter_token_download,
247  $row_id_token_download
248  ] =
249  (new URLBuilder($uri_download))->acquireParameters(
250  ['cert_overview'],
251  'action',
252  'id'
253  );
254 
255  return [
256  'download' => $this->ui_factory->table()->action()->single(
257  $this->lng->txt('download'),
258  $url_builder_download->withParameter($action_parameter_token_download, 'download'),
259  $row_id_token_download
260  )
261  ];
262  }
263 
268  private function buildTableRows(array $certificates): array
269  {
270  $table_rows = [];
271 
272  $ref_id_cache = [];
273  $owner_cache = [];
274  $object_title_cache = [];
275 
276  foreach ($certificates as $certificate) {
277  if (!isset($ref_id_cache[$certificate->getObjId()])) {
278  $ref_id_cache[$certificate->getObjId()] = ilObject::_getAllReferences($certificate->getObjId());
279  }
280  $ref_ids = $ref_id_cache[$certificate->getObjId()];
281 
282  if (!isset($object_title_cache[$certificate->getObjId()])) {
283  $object_title = ilObject::_lookupTitle($certificate->getObjId());
284  foreach ($ref_ids as $refId) {
285  if ($this->access->checkAccess('read', '', $refId)) {
286  $object_title = $this->ui_renderer->render(
287  $this->ui_factory->link()->standard($object_title, ilLink::_getLink($refId))
288  );
289  break;
290  }
291  }
292 
293  $object_title_cache[$certificate->getObjId()] = $object_title;
294  }
295 
296 
297 
298  if (!isset($owner_cache[$certificate->getUserId()])) {
299  $owner_cache[$certificate->getUserId()] = ilObjUser::_lookupLogin($certificate->getUserId());
300  }
301 
302  $table_rows[] = [
303  'id' => $certificate->getId(),
304  'certificate_id' => $certificate->getCertificateId()->asString(),
305  'issue_date' => $certificate->getAcquiredTimestamp(),
306  'object' => $object_title_cache[$certificate->getObjId()],
307  'obj_id' => (string) $certificate->getObjId(),
308  'owner' => $owner_cache[$certificate->getUserId()],
309  ];
310  }
311 
312  return $table_rows;
313  }
314 
315  public function render(): string
316  {
317  return $this->ui_renderer->render([$this->filter, $this->table]);
318  }
319 }
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
join($init, callable $fn)
Definition: Order.php:75
Interface Observer Contains several chained tasks and infos about them.
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$refId
Definition: xapitoken.php:58
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
buildDataRow(string $id, array $record)
readonly ILIAS UI Component Input Container Filter Standard $filter
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _lookupTitle(int $obj_id)
This is how the factory for UI elements looks.
Definition: Factory.php:37
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
global $DIC
Definition: shib_login.php:22
__construct(?Factory $ui_factory=null, ?ilUserCertificateRepository $repo=null, ?ilUIService $ui_service=null, ?ilLanguage $lng=null, ServerRequestInterface|RequestInterface|null $request=null, ?\ILIAS\Data\Factory $data_factory=null, ?ilCtrl $ctrl=null, ?Renderer $ui_renderer=null, ?ilAccessHandler $access=null, ?ilObjUser $user=null)
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
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
static _lookupLogin(int $a_user_id)