ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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  return $this->ui_service->filter()->standard(
174  'certificates_overview_filter',
175  $this->ctrl->getLinkTargetByClass(
176  ilObjCertificateSettingsGUI::class,
178  ),
179  [
180  'certificate_id' => $this->ui_factory->input()->field()->text($this->lng->txt('certificate_id')),
181  'issue_date' => $this->ui_factory->input()->field()
182  ->duration($this->lng->txt('certificate_issue_date'))
183  ->withFormat($this->user->getDateTimeFormat())
184  ->withUseTime(true),
185  'object' => $this->ui_factory->input()->field()->text($this->lng->txt('obj')),
186  'obj_id' => $this->ui_factory->input()->field()->text($this->lng->txt('object_id')),
187  'owner' => $this->ui_factory->input()->field()->text($this->lng->txt('owner')),
188  ],
189  [true, true, true, true, true],
190  true,
191  true
192  );
193  }
194 
195  private function buildTable(): Data
196  {
197  $ui_table = $this->ui_factory->table();
198 
199  return $ui_table->data(
200  $this,
201  $this->lng->txt('certificates'),
202  [
203  'certificate_id' => $ui_table->column()->text($this->lng->txt('certificate_id')),
204  'issue_date' => $ui_table->column()->date($this->lng->txt('certificate_issue_date'), $this->user->getDateTimeFormat()),
205  'object' => $ui_table->column()->text($this->lng->txt('obj')),
206  'obj_id' => $ui_table->column()->text($this->lng->txt('object_id')),
207  'owner' => $ui_table->column()->text($this->lng->txt('owner'))
208  ],
209  )
210  ->withOrder(new Order('issue_date', Order::DESC))
211  ->withId('certificateOverviewTable')
212  ->withRequest($this->request)
213  ->withActions($this->buildTableActions());
214  }
215 
219  private function buildTableActions(): array
220  {
221  $uri_download = $this->data_factory->uri(
222  ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
223  ilObjCertificateSettingsGUI::class,
225  )
226  );
227 
233  [
234  $url_builder_download,
235  $action_parameter_token_download,
236  $row_id_token_download
237  ] =
238  (new URLBuilder($uri_download))->acquireParameters(
239  ['cert_overview'],
240  'action',
241  'id'
242  );
243 
244  return [
245  'download' => $this->ui_factory->table()->action()->single(
246  $this->lng->txt('download'),
247  $url_builder_download->withParameter($action_parameter_token_download, 'download'),
248  $row_id_token_download
249  )
250  ];
251  }
252 
257  private function buildTableRows(array $certificates): array
258  {
259  $table_rows = [];
260 
261  $ref_id_cache = [];
262  $owner_cache = [];
263  $object_title_cache = [];
264 
265  foreach ($certificates as $certificate) {
266  if (!isset($ref_id_cache[$certificate->getObjId()])) {
267  $ref_id_cache[$certificate->getObjId()] = ilObject::_getAllReferences($certificate->getObjId());
268  }
269  $ref_ids = $ref_id_cache[$certificate->getObjId()];
270 
271  if (!isset($object_title_cache[$certificate->getObjId()])) {
272  $object_title = ilObject::_lookupTitle($certificate->getObjId());
273  foreach ($ref_ids as $refId) {
274  if ($this->access->checkAccess('read', '', $refId)) {
275  $object_title = $this->ui_renderer->render(
276  $this->ui_factory->link()->standard($object_title, ilLink::_getLink($refId))
277  );
278  break;
279  }
280  }
281 
282  $object_title_cache[$certificate->getObjId()] = $object_title;
283  }
284 
285 
286 
287  if (!isset($owner_cache[$certificate->getUserId()])) {
288  $owner_cache[$certificate->getUserId()] = ilObjUser::_lookupLogin($certificate->getUserId());
289  }
290 
291  $table_rows[] = [
292  'id' => $certificate->getId(),
293  'certificate_id' => $certificate->getCertificateId()->asString(),
294  'issue_date' => $certificate->getAcquiredTimestamp(),
295  'object' => $object_title_cache[$certificate->getObjId()],
296  'obj_id' => (string) $certificate->getObjId(),
297  'owner' => $owner_cache[$certificate->getUserId()],
298  ];
299  }
300 
301  return $table_rows;
302  }
303 
304  public function render(): string
305  {
306  return $this->ui_renderer->render([$this->filter, $this->table]);
307  }
308 }
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:26
__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)