ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
HistoryTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Closure;
33 use ilLanguage;
34 use ilTextInputGUI;
36 use ilDateTime;
38 use ilObjUser;
40 
44 class HistoryTable implements Table
45 {
47  private readonly Closure $format_date;
48 
53  public function __construct(
54  private readonly HistoryRepository $repository,
55  private readonly ProvideDocument $document,
56  public readonly string $reset_command,
57  private readonly string $auto_complete_link,
58  private readonly UI $ui,
59  private readonly DocumentModal $modal,
60  private readonly Closure $create,
61  ?Closure $format_date = null
62  ) {
63  $this->format_date = $format_date ?? fn(DateTimeImmutable $date) => ilDatePresentation::formatDate(new ilDateTime($date->getTimestamp(), IL_CAL_UNIX));
64  }
65 
66  public function columns(): array
67  {
68  return [
69  'created' => [$this->ui->txt('tbl_hist_head_acceptance_date'), 'created'],
70  'login' => [$this->ui->txt('tbl_hist_head_login'), 'login'],
71  'firstname' => [$this->ui->txt('tbl_hist_head_firstname'), 'firstname'],
72  'lastname' => [$this->ui->txt('tbl_hist_head_lastname'), 'lastname'],
73  'document' => [$this->ui->txt('tbl_hist_head_document'), 'document'],
74  'criteria' => [$this->ui->txt('tbl_hist_head_criteria')],
75  ];
76  }
77 
78  public function config(TableConfig $config): void
79  {
80  $config->setTitle($this->ui->txt('acceptance_history'));
81  $config->setDefaultOrderField('created');
82  $config->setDefaultOrderDirection('DESC');
83  $config->setSelectableColumns('firstname', 'lastname', 'criteria');
84 
85  $filter = $config->asFilter($this->reset_command);
86  $filter->addFilterItem($this->userName());
87  $filter->addFilterItem($this->time(), true);
88  }
89 
90  public function rows(TableSelection $selection): array
91  {
92  $filter = $selection->filter();
93  $period = $filter['period'] ?? [];
94  unset($filter['period']);
95  $filter = array_filter([...$filter, ...$period]);
96 
97  $selection->setMaxCount($this->repository->countAll($filter));
98 
99  return array_map($this->row(...), $this->repository->all(
100  $filter,
101  [$selection->getOrderField() => $selection->getOrderDirection()],
102  $selection->getOffset(),
103  $selection->getLimit()
104  ));
105  }
106 
107  public function name(): string
108  {
109  return self::class;
110  }
111 
112  private function row(History $record): array
113  {
114  $user = ($this->create)(ilObjUser::class, $record->creation()->user());
115 
116  return [
117  'created' => ($this->format_date)($record->creation()->time()),
118  'login' => $user?->getLogin() ?? $this->ui->txt('deleted'),
119  'firstname' => $user?->getFirstname() ?? '-',
120  'lastname' => $user?->getLastname() ?? '-',
121  'document' => $this->modal->create($record->documentContent()),
122  'criteria' => $this->showCriteria($record),
123  ];
124  }
125 
126  private function userName(): ilTextInputGUI
127  {
128  $ul = ($this->create)(ilTextInputGUI::class, join('/', array_map($this->ui->txt(...), ['login', 'email', 'name'])), 'query');
129  $ul->setDataSource($this->auto_complete_link);
130  $ul->setSize(20);
131  $ul->setSubmitFormOnEnter(true);
132  return $ul;
133  }
134 
135  private function time(): ilDateDurationInputGUI
136  {
137  class_exists(ilDateTime::class); // Trigger autoloader to ensure IL_CAL_UNIX is defined.
138  $duration = ($this->create)(ResettingDurationInputGUI::class, $this->ui->txt('period'), 'period');
139  $duration->setAllowOpenIntervals(true);
140  $duration->setShowTime(true);
141  $duration->setStartText($this->ui->txt('period_from'));
142  $duration->setEndText($this->ui->txt('period_until'));
143  $duration->setStart(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
144  $duration->setEnd(($this->create)(ilDateTime::class, null, IL_CAL_UNIX));
145 
146  return $duration;
147  }
148 
152  private function showCriteria(History $record)
153  {
154  $conv = $this->document->toCondition(...);
155  $components = array_map(fn($c) => $conv($c)->asComponent(), $record->criteriaContent());
156 
157  return $components === [] ?
158  $this->ui->txt('tbl_hist_cell_not_criterion') :
159  $components;
160  }
161 }
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
const IL_CAL_UNIX
$duration
setDefaultOrderDirection(string $a_defaultorderdirection)
asFilter(string $reset_command)
modal(string $title="", string $cancel_label="")
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
__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)
setDefaultOrderField(string $a_defaultorderfield)
rows(TableSelection $selection)