ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TestLogViewer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Test\Logging;
22
27use ILIAS\UI\Factory as UIFactory;
28use ILIAS\UI\Renderer as UIRenderer;
29use ILIAS\Refinery\Factory as Refinery;
30use ILIAS\Data\Factory as DataFactory;
34use Psr\Http\Message\ServerRequestInterface;
35
37{
38 private DataFactory $data_factory;
39
40 public function __construct(
41 private readonly TestLoggingRepository $logging_repository,
42 private readonly TestLogger $logger,
43 private readonly TitleColumnsBuilder $title_builder,
44 private readonly GeneralQuestionPropertiesRepository $question_repository,
45 private readonly ServerRequestInterface $request,
46 private readonly RequestWrapper $request_wrapper,
47 private readonly \ilUIService $ui_service,
48 private readonly UIFactory $ui_factory,
49 private readonly UIRenderer $ui_renderer,
50 private readonly Refinery $refinery,
51 private readonly \ilLanguage $lng,
52 private \ilGlobalTemplateInterface $tpl,
53 private readonly StreamDelivery $stream_delivery,
54 private readonly \ilObjUser $current_user
55 ) {
56 $this->data_factory = new DataFactory();
57 }
58
59 public function getLogTable(
60 URLBuilder $url_builder,
61 URLBuilderToken $action_parameter_token,
62 URLBuilderToken $row_id_token,
63 ?int $ref_id = null
64 ): array {
65 $log_table = new LogTable(
66 $this->logging_repository,
67 $this->logger,
68 $this,
69 $this->title_builder,
70 $this->question_repository,
71 $this->ui_service,
72 $this->ui_factory,
73 $this->ui_renderer,
74 $this->lng,
75 $this->tpl,
76 $url_builder,
77 $action_parameter_token,
78 $row_id_token,
79 $this->stream_delivery,
80 $this->current_user,
81 $ref_id,
82 );
83
84 return [
85 $log_table->getFilter(),
86 $log_table->getTable()->withRequest($this->request)
87 ];
88 }
89
90 public function executeLogTableAction(
91 URLBuilder $url_builder,
92 URLBuilderToken $action_parameter_token,
93 URLBuilderToken $row_id_token,
94 ?int $ref_id = null
95 ): void {
96 $log_table = new LogTable(
97 $this->logging_repository,
98 $this->logger,
99 $this,
100 $this->title_builder,
101 $this->question_repository,
102 $this->ui_service,
103 $this->ui_factory,
104 $this->ui_renderer,
105 $this->lng,
106 $this->tpl,
107 $url_builder,
108 $action_parameter_token,
109 $row_id_token,
110 $this->stream_delivery,
111 $this->current_user,
112 $ref_id,
113 );
114
115 $action = $this->request_wrapper->retrieve(
116 $action_parameter_token->getName(),
117 $this->refinery->kindlyTo()->string()
118 );
119
120 if ($action === '') {
121 return;
122 }
123
124 $affected_items = [];
125 if ($this->request_wrapper->has($row_id_token->getName())) {
126 $affected_items = $this->request_wrapper->retrieve(
127 $row_id_token->getName(),
128 $this->refinery->byTrying(
129 [
130 $this->refinery->container()->mapValues(
131 $this->refinery->kindlyTo()->string()
132 ),
133 $this->refinery->always([])
134 ]
135 )
136 );
137 }
138
139 $log_table->executeAction($action, $affected_items);
140 }
141
142 public function getLogExportForRefjId(int $ref_id): \ilExcel
143 {
144 return $this->buildExcelWorkbookForLogs(
145 $this->logging_repository->getLogs(
146 $this->logger->getInteractionTypes(),
147 [$ref_id]
148 )
149 );
150 }
151
152 public function buildExcelWorkbookForLogs(\Generator $logs): \ilExcel
153 {
154 $workbook = new \ilExcel();
155 $workbook->addSheet($this->lng->txt('history'));
156
157 $column = 0;
158 foreach ($this->getColumHeadingsForExport() as $header_cell) {
159 $workbook->setCell(1, $column++, $header_cell);
160 }
161 $workbook->setBold('A' . 1 . ':' . $workbook->getColumnCoord($column - 1) . 1);
162 $workbook->setColors('A' . 1 . ':' . $workbook->getColumnCoord($column - 1) . 1, 'C0C0C0');
163
164 return $this->addRowsFromLogs($logs, $workbook);
165 }
166
167 private function addRowsFromLogs(
168 \Generator $logs,
169 \ilExcel $workbook
170 ): \ilExcel {
171 $row = 1;
172 foreach ($logs as $log) {
173 $row++;
174 $column = 0;
175 foreach ($log->getLogEntryAsExportRow(
176 $this->lng,
177 $this->title_builder,
178 $this->logger->getAdditionalInformationGenerator(),
179 [
180 'timezone' => new \DateTimeZone($this->current_user->getTimeZone()),
181 'date_format' => $this->buildUserDateTimeFormat()->toString()
182 ]
183 ) as $cell_content) {
184 $workbook->setCell(
185 $row,
186 $column++,
187 $cell_content
188 );
189 }
190 }
191 return $workbook;
192 }
193
194 private function getColumHeadingsForExport(): array
195 {
196 return [
197 $this->lng->txt('date_time'),
198 $this->lng->txt('test'),
199 $this->lng->txt('author'),
200 $this->lng->txt('tst_participant'),
201 $this->lng->txt('client_ip'),
202 $this->lng->txt('question'),
203 $this->lng->txt('log_entry_type'),
204 $this->lng->txt('interaction_type'),
205 $this->lng->txt('additional_info')
206 ];
207 }
208
210 {
211 $user_format = $this->current_user->getDateFormat();
212 if ($this->current_user->getTimeFormat() == \ilCalendarSettings::TIME_FORMAT_24) {
213 return $this->data_factory->dateFormat()->amend(
214 $this->data_factory->dateFormat()->withTime24($user_format)
215 )->colon()->seconds()->get();
216 }
217 return $this->data_factory->dateFormat()->amend(
218 $user_format
219 )->space()->hours12()->colon()->minutes()->colon()->seconds()->meridiem()->get();
220 }
221
222 /* The following functions will be removed with ILIAS 11 */
223
224 public function getLegacyLogExportForObjId(?int $obj_id = null): string
225 {
226 $log_output = $this->logging_repository->getLegacyLogsForObjId($obj_id);
227
228 $users = [];
229 $csv = [];
230 $separator = ';';
231 $header_row = [
232 $this->lng->txt('date_time'),
233 $this->lng->txt('user'),
234 $this->lng->txt('log_text'),
235 $this->lng->txt('question')
236 ];
237
238 $csv[] = $this->processCSVRow($header_row);
239 foreach ($log_output as $log) {
240 if (!array_key_exists($log['user_fi'], $users)) {
241 $users[$log['user_fi']] = \ilObjUser::_lookupName((int) $log['user_fi']);
242 }
243
244 $content_row = [];
245 $date = new \ilDateTime((int) $log['tstamp'], IL_CAL_UNIX);
246 $content_row[] = $date->get(IL_CAL_FKT_DATE, 'Y-m-d H:i');
247 $content_row[] = trim($users[$log['user_fi']]['title'] . ' '
248 . $users[$log['user_fi']]['firstname'] . ' ' . $users[$log['user_fi']]['lastname']);
249 $content_row[] = trim($log['logtext']);
250 $content_row[] = $this->buildQuestionTitleForLegacyLog($log);
251 $csv[] = $this->processCSVRow($content_row);
252 }
253 $csvoutput = '';
254 foreach ($csv as $row) {
255 $csvoutput .= implode($separator, $row) . "\n";
256 }
257 return $csvoutput;
258 }
259
260 public function buildQuestionTitleForLegacyLog(array $log): string
261 {
262 if (!$log['question_fi'] && !$log['original_fi']) {
263 return '';
264 }
265 $title = '';
266 if ($log['question_fi']) {
267 $title = $this->question_repository->getForQuestionId((int) $log['question_fi'])?->getTitle();
268 }
269
270 if ($title === null && $log['original_fi']) {
271 $title = $this->question_repository->getForQuestionId((int) $log['original_fi'])?->getTitle();
272 }
273
274 if ($title === null) {
275 return '';
276 }
277
278 return $this->lng->txt('question') . ': ' . $title;
279 }
280
281 private function processCSVRow(
282 mixed $row,
283 bool $quote_all = false,
284 string $separator = ";"
285 ): array {
286 $resultarray = [];
287 foreach ($row as $rowindex => $entry) {
288 $surround = false;
289 if ($quote_all) {
290 $surround = true;
291 }
292 if (is_string($entry) && strpos($entry, "\"") !== false) {
293 $entry = str_replace("\"", "\"\"", $entry);
294 $surround = true;
295 }
296 if (is_string($entry) && strpos($entry, $separator) !== false) {
297 $surround = true;
298 }
299
300 if (is_string($entry)) {
301 // replace all CR LF with LF (for Excel for Windows compatibility
302 $entry = str_replace(chr(13) . chr(10), chr(10), $entry);
303 }
304
305 if ($surround) {
306 $entry = "\"" . $entry . "\"";
307 }
308
309 $resultarray[$rowindex] = $entry;
310 }
311 return $resultarray;
312 }
313}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:27
Builds data types.
Definition: Factory.php:36
executeAction(string $action, array $affected_items)
Definition: LogTable.php:263
getLegacyLogExportForObjId(?int $obj_id=null)
buildExcelWorkbookForLogs(\Generator $logs)
getLogTable(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token, ?int $ref_id=null)
__construct(private readonly TestLoggingRepository $logging_repository, private readonly TestLogger $logger, private readonly TitleColumnsBuilder $title_builder, private readonly GeneralQuestionPropertiesRepository $question_repository, private readonly ServerRequestInterface $request, private readonly RequestWrapper $request_wrapper, private readonly \ilUIService $ui_service, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly Refinery $refinery, private readonly \ilLanguage $lng, private \ilGlobalTemplateInterface $tpl, private readonly StreamDelivery $stream_delivery, private readonly \ilObjUser $current_user)
processCSVRow(mixed $row, bool $quote_all=false, string $separator=";")
executeLogTableAction(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token, ?int $ref_id=null)
addRowsFromLogs(\Generator $logs, \ilExcel $workbook)
getName()
Get the full name of the token including its namespace.
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
language handling
User class.
static _lookupName(int $a_user_id)
Filter service.
Interface RequestWrapper.
An entity that renders components to a string output.
Definition: Renderer.php:31
$ref_id
Definition: ltiauth.php:66
$log
Definition: ltiresult.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31