ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
50 public function __construct(
51 private readonly HistoryRepository $repository,
52 private readonly ProvideDocument $document,
53 public readonly string $reset_command,
54 private readonly string $auto_complete_link,
55 private readonly UI $ui,
56 private readonly DocumentModal $modal,
57 private readonly Closure $create,
58 ?Closure $format_date = null
59 ) {
60 $this->format_date = $format_date ?? fn(DateTimeImmutable $date) => ilDatePresentation::formatDate(new ilDateTime($date->getTimestamp(), IL_CAL_UNIX));
61 }
62
63 public function columns(): array
64 {
65 return [
66 'created' => [$this->ui->txt('tbl_hist_head_acceptance_date'), 'created'],
67 'login' => [$this->ui->txt('tbl_hist_head_login'), 'login'],
68 'firstname' => [$this->ui->txt('tbl_hist_head_firstname'), 'firstname'],
69 'lastname' => [$this->ui->txt('tbl_hist_head_lastname'), 'lastname'],
70 'document' => [$this->ui->txt('tbl_hist_head_document'), 'document'],
71 'criteria' => [$this->ui->txt('tbl_hist_head_criteria')],
72 ];
73 }
74
75 public function config(TableConfig $config): void
76 {
77 $config->setTitle($this->ui->txt('acceptance_history'));
78 $config->setDefaultOrderField('created');
79 $config->setDefaultOrderDirection('DESC');
80 $config->setSelectableColumns('firstname', 'lastname', 'criteria');
81
82 $filter = $config->asFilter($this->reset_command);
83 $filter->addFilterItem($this->userName());
84 $filter->addFilterItem($this->time(), true);
85 }
86
87 public function rows(TableSelection $selection): array
88 {
89 $filter = $selection->filter();
90 $period = $filter['period'] ?? [];
91 unset($filter['period']);
92 $filter = array_filter([...$filter, ...$period]);
93
94 $selection->setMaxCount($this->repository->countAll($filter));
95
96 return array_map($this->row(...), $this->repository->all(
97 $filter,
98 [$selection->getOrderField() => $selection->getOrderDirection()],
99 $selection->getOffset(),
100 $selection->getLimit()
101 ));
102 }
103
104 public function name(): string
105 {
106 return self::class;
107 }
108
109 private function row(History $record): array
110 {
111 $user = ($this->create)(ilObjUser::class, $record->creation()->user());
112
113 return [
114 'created' => ($this->format_date)($record->creation()->time()),
115 'login' => $user?->getLogin() ?? $this->ui->txt('deleted'),
116 'firstname' => $user?->getFirstname() ?? '-',
117 'lastname' => $user?->getLastname() ?? '-',
118 'document' => [...$this->modal->create($record->documentContent()), ...$this->modal->popComponents()],
119 'criteria' => $this->showCriteria($record),
120 ];
121 }
122
123 private function userName(): ilTextInputGUI
124 {
125 $ul = ($this->create)(ilTextInputGUI::class, join('/', array_map($this->ui->txt(...), ['login', 'email', 'name'])), 'query');
126 $ul->setDataSource($this->auto_complete_link);
127 $ul->setSize(20);
128 $ul->setSubmitFormOnEnter(true);
129 return $ul;
130 }
131
132 private function time(): ilDateDurationInputGUI
133 {
134 class_exists(ilDateTime::class); // Trigger autoloader to ensure IL_CAL_UNIX is defined.
135 $duration = ($this->create)(ResettingDurationInputGUI::class, $this->ui->txt('period'), 'period');
136 $duration->setAllowOpenIntervals(true);
137 $duration->setShowTime(true);
138 $duration->setStartText($this->ui->txt('period_from'));
139 $duration->setEndText($this->ui->txt('period_until'));
140 $duration->setStart(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
141 $duration->setEnd(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
142
143 return $duration;
144 }
145
149 private function showCriteria(History $record)
150 {
151 $conv = $this->document->toCondition(...);
152 $components = array_map(fn($c) => $conv($c)->asComponent(), $record->criteriaContent());
153
154 return $components === [] ?
155 $this->ui->txt('tbl_hist_cell_not_criterion') :
157 }
158}
$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)
@template C
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="")