ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HistoryTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
34use ilDateTime;
36use ilObjUser;
37use DateTimeImmutable;
39
40class HistoryTable implements Table
41{
43 private readonly Closure $format_date;
44
49 public function __construct(
50 private readonly HistoryRepository $repository,
51 private readonly ProvideDocument $document,
52 public readonly string $reset_command,
53 private readonly string $auto_complete_link,
54 private readonly UI $ui,
55 private readonly DocumentModal $modal,
56 private readonly Closure $create,
57 ?Closure $format_date = null
58 ) {
59 $this->format_date = $format_date ?? fn(DateTimeImmutable $date) => ilDatePresentation::formatDate(new ilDateTime($date->getTimestamp(), IL_CAL_UNIX));
60 }
61
62 public function columns(): array
63 {
64 return [
65 'created' => [$this->ui->txt('tbl_hist_head_acceptance_date'), 'created'],
66 'login' => [$this->ui->txt('tbl_hist_head_login'), 'login'],
67 'firstname' => [$this->ui->txt('tbl_hist_head_firstname'), 'firstname'],
68 'lastname' => [$this->ui->txt('tbl_hist_head_lastname'), 'lastname'],
69 'document' => [$this->ui->txt('tbl_hist_head_document'), 'document'],
70 'criteria' => [$this->ui->txt('tbl_hist_head_criteria')],
71 ];
72 }
73
74 public function config(TableConfig $config): void
75 {
76 $config->setTitle($this->ui->txt('acceptance_history'));
77 $config->setDefaultOrderField('created');
78 $config->setDefaultOrderDirection('DESC');
79 $config->setSelectableColumns('firstname', 'lastname', 'criteria');
80
81 $filter = $config->asFilter($this->reset_command);
82 $filter->addFilterItem($this->userName());
83 $filter->addFilterItem($this->time(), true);
84 }
85
86 public function rows(TableSelection $selection): array
87 {
88 $filter = $selection->filter();
89 $period = $filter['period'] ?? [];
90 unset($filter['period']);
91 $filter = array_filter([...$filter, ...$period]);
92
93 $selection->setMaxCount($this->repository->countAll($filter));
94
95 return array_map($this->row(...), $this->repository->all(
96 $filter,
97 [$selection->getOrderField() => $selection->getOrderDirection()],
98 $selection->getOffset(),
99 $selection->getLimit()
100 ));
101 }
102
103 public function name(): string
104 {
105 return self::class;
106 }
107
108 private function row(History $record): array
109 {
110 $user = ($this->create)(ilObjUser::class, $record->creation()->user());
111
112 return [
113 'created' => ($this->format_date)($record->creation()->time()),
114 'login' => $user?->getLogin() ?? $this->ui->txt('deleted'),
115 'firstname' => $user?->getFirstname() ?? '-',
116 'lastname' => $user?->getLastname() ?? '-',
117 'document' => $this->modal->create($record->documentContent()),
118 'criteria' => $this->showCriteria($record),
119 ];
120 }
121
122 private function userName(): ilTextInputGUI
123 {
124 $ul = ($this->create)(ilTextInputGUI::class, join('/', array_map($this->ui->txt(...), ['login', 'email', 'name'])), 'query');
125 $ul->setDataSource($this->auto_complete_link);
126 $ul->setSize(20);
127 $ul->setSubmitFormOnEnter(true);
128 return $ul;
129 }
130
131 private function time(): ilDateDurationInputGUI
132 {
133 class_exists(ilDateTime::class); // Trigger autoloader to ensure IL_CAL_UNIX is defined.
134 $duration = ($this->create)(ResettingDurationInputGUI::class, $this->ui->txt('period'), 'period');
135 $duration->setAllowOpenIntervals(true);
136 $duration->setShowTime(true);
137 $duration->setStartText($this->ui->txt('period_from'));
138 $duration->setEndText($this->ui->txt('period_until'));
139 $duration->setStart(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
140 $duration->setEnd(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
141
142 return $duration;
143 }
144
148 private function showCriteria(History $record)
149 {
150 $conv = $this->document->toCondition(...);
151 $components = array_map(fn($c) => $conv($c)->asComponent(), $record->criteriaContent());
152
153 return $components === [] ?
154 $this->ui->txt('tbl_hist_cell_not_criterion') :
156 }
157}
$duration
$components
The ilDateDurationInputGUI class doesn't set it's value back to an empty value when called with an em...
__construct(private readonly HistoryRepository $repository, private readonly ProvideDocument $document, public readonly string $reset_command, private readonly string $auto_complete_link, private readonly UI $ui, private readonly DocumentModal $modal, private readonly Closure $create, ?Closure $format_date=null)
rows(TableSelection $selection)
const IL_CAL_UNIX
input GUI for a time span (start and end date)
Class for date presentation.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
User class.
This class represents a text property in a property form.
setDataSource(string $href, ?string $a_delimiter=null)
$c
Definition: deliver.php:25
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
setDefaultOrderField(string $a_defaultorderfield)
setDefaultOrderDirection(string $a_defaultorderdirection)
asFilter(string $reset_command)
setSelectableColumns(string ... $names)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
modal(string $title="", string $cancel_label="")