ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Table.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\UI\Factory as UIFactory;
24use ILIAS\UI\Component\Table\Data as DataTable;
31use ILIAS\Data\Factory as DataFactory;
32use Psr\Http\Message\RequestInterface;
33
34class Table implements DataRetrieval
35{
36 private const FILTER_ID = 'perm_table_filter';
37 private const FILTER_FIELD_ACTION = 'action';
38 private const FILTER_FIELD_PERIOD = 'period';
39
40 private const COLUMN_DATE = 'created';
41 private const COLUMN_NAME = 'name';
42 private const COLUMN_LOGIN = 'login';
43 private const COLUMN_ACTION = 'action';
44 private const COLUMN_CHANGES = 'changes';
45
49 private ?array $filter_data;
50
51 private array $action_map = [];
52 private array $operations = [];
53
54 public function __construct(
55 private readonly \ilRbacLog $rbac_log,
56 private readonly UIFactory $ui_factory,
57 private readonly DataFactory $data_factory,
58 private readonly \ilLanguage $lng,
59 private readonly \ilCtrl $ctrl,
60 private readonly \ilUIService $ui_service,
61 private readonly \ilObjectDefinition $object_definition,
62 private readonly RequestInterface $request,
63 \ilRbacReview $rbac_review,
64 private readonly \ilObjUser $current_user,
65 private readonly \ilObjectGUI $gui_object
66 ) {
67 $this->action_map = [
68 \ilRbacLog::EDIT_PERMISSIONS => $this->lng->txt('rbac_log_edit_permissions'),
69 \ilRbacLog::MOVE_OBJECT => $this->lng->txt('rbac_log_move_object'),
70 \ilRbacLog::LINK_OBJECT => $this->lng->txt('rbac_log_link_object'),
71 \ilRbacLog::COPY_OBJECT => $this->lng->txt('rbac_log_copy_object'),
72 \ilRbacLog::CREATE_OBJECT => $this->lng->txt('rbac_log_create_object'),
73 \ilRbacLog::EDIT_TEMPLATE => $this->lng->txt('rbac_log_edit_template'),
74 \ilRbacLog::EDIT_TEMPLATE_EXISTING => $this->lng->txt('rbac_log_edit_template_existing'),
75 \ilRbacLog::CHANGE_OWNER => $this->lng->txt('rbac_log_change_owner')
76 ];
77
78 foreach ($rbac_review->getOperations() as $op) {
79 $this->operations[$op['ops_id']] = $op['operation'];
80 }
81 }
82
83 public function getTableAndFilter(): array
84 {
85 return [
86 $this->getFilter(),
87 $this->getTable()
88 ];
89 }
90
91 private function getTable(): DataTable
92 {
93 $cf = $this->ui_factory->table()->column();
94
95 return $this->ui_factory->table()->data(
96 $this,
97 $this->lng->txt('rbac_log'),
98 [
99 self::COLUMN_DATE => $cf->date(
100 $this->lng->txt('date'),
101 $this->buildUserDateTimeFormat()
102 ),
103 self::COLUMN_NAME => $cf->text($this->lng->txt('name')),
104 self::COLUMN_LOGIN => $cf->text($this->lng->txt('login')),
105 self::COLUMN_ACTION => $cf->text($this->lng->txt('action')),
106 self::COLUMN_CHANGES => $cf->text($this->lng->txt('rbac_changes'))
107 ->withIsSortable(false)
108 ],
109 )->withRequest($this->request);
110 }
111
112 private function getFilter(): Filter
113 {
114 $ff = $this->ui_factory->input()->field();
115
116 $inputs = [
117 self::FILTER_FIELD_ACTION => $ff->multiSelect(
118 $this->lng->txt('action'),
119 $this->action_map
120 ),
121 self::FILTER_FIELD_PERIOD => $ff->duration($this->lng->txt('date'))
122 ];
123
124 $active = array_fill(0, count($inputs), true);
125
126 $filter = $this->ui_service->filter()->standard(
127 self::FILTER_ID,
128 $this->ctrl->getFormActionByClass([get_class($this->gui_object), \ilPermissionGUI::class], 'log'),
129 $inputs,
130 $active,
131 true,
132 true
133 );
134 $this->filter_data = $this->applyFilterValuesTrafos($this->ui_service->filter()->getData($filter));
135 return $filter;
136 }
137
138 public function getRows(
139 DataRowBuilder $row_builder,
140 array $visible_column_ids,
142 Order $order,
143 mixed $additional_viewcontrol_data,
144 mixed $filter_data,
145 mixed $additional_parameters
146 ): \Generator {
147 $log_data = $this->rbac_log->getLogItems(
148 $this->getRefId(),
149 $range,
150 $order,
151 $this->filter_data
152 );
153
154 foreach ($log_data as $entry) {
155 $user_data = \ilObjUser::_lookupName($entry['user_id']);
156 yield $row_builder->buildDataRow(
157 (string) $entry['log_id'],
158 [
159 self::COLUMN_DATE => (new \DateTimeImmutable('@' . $entry['created']))
160 ->setTimezone(new \DateTimeZone($this->current_user->getTimeZone())),
161 self::COLUMN_NAME => "{$user_data['lastname']}, {$user_data['firstname']}",
162 self::COLUMN_LOGIN => $user_data['login'],
163 self::COLUMN_ACTION => $this->action_map[$entry['action']] ?? '',
164 self::COLUMN_CHANGES => $this->buildChangeColumn($entry['action'], $entry['data'] ?? [])
165 ]
166 );
167 }
168 }
169
170 public function getTotalRowCount(
171 mixed $additional_viewcontrol_data,
172 mixed $filter_data,
173 mixed $additional_parameters
174 ): ?int {
175 return $this->rbac_log->getLogItemsCount($this->getRefId(), $filter_data ?? []);
176 }
177
178 private function getRefId(): int
179 {
180 // special case: role folder should display root folder entries
181 if ($this->gui_object->getRefId() === ROLE_FOLDER_ID) {
182 return ROOT_FOLDER_ID;
183 }
184 return $this->gui_object->getRefId();
185 }
186
188 {
189 $user_format = $this->current_user->getDateFormat();
190 if ($this->current_user->getTimeFormat() == \ilCalendarSettings::TIME_FORMAT_24) {
191 return $this->data_factory->dateFormat()->withTime24($user_format);
192 }
193 return $this->data_factory->dateFormat()->withTime12($user_format);
194 }
195
196 private function applyFilterValuesTrafos(array $filter_values): array
197 {
198 $transformed_values = [
199 'action' => $filter_values['action']
200 ];
201 if (isset($filter_values['period'][0])) {
202 $transformed_values['from'] = (new \DateTimeImmutable(
203 $filter_values['period'][0],
204 new \DateTimeZone($this->current_user->getTimeZone())
205 ))->getTimestamp();
206 }
207 if (isset($filter_values['period'][1])) {
208 $transformed_values['to'] = (new \DateTimeImmutable(
209 $filter_values['period'][1] . '23:59:59',
210 new \DateTimeZone($this->current_user->getTimeZone())
211 ))->getTimestamp();
212 }
213 return $transformed_values;
214 }
215
216 private function buildChangeColumn(int $action, array $data): string
217 {
218 if ($action === \ilRbacLog::CHANGE_OWNER) {
219
220 $user_name = isset($data[0]) && is_numeric($data[0])
222 : '';
223 return "{$this->lng->txt('rbac_log_changed_owner')}: {$user_name}";
224 }
225
226 if ($action === \ilRbacLog::EDIT_TEMPLATE) {
227 return $this->parseChangesTemplate($data);
228 }
229
230 return $this->parseChangesFaPa($data);
231 }
232
233 private function parseChangesFaPa(array $raw): string
234 {
235 $result = [];
236
237 if (isset($raw['src']) && is_int($raw['src'])) {
238 $obj_id = \ilObject::_lookupObjectId($raw['src']);
239 if ($obj_id) {
240 $result[] = "{$this->lng->txt('rbac_log_source_object')}: "
241 . '<a href="' . \ilLink::_getLink($raw['src']) . '">'
242 . \ilObject::_lookupTitle($obj_id) . '</a>';
243 }
244
245 // added only
246 foreach ($raw['ops'] as $role_id => $ops) {
247 foreach ($ops as $op) {
248 $result[] = sprintf(
249 $this->lng->txt('rbac_log_operation_add'),
251 ) . ': ' . $this->getOPCaption($this->gui_object->getObject()->getType(), $op);
252 }
253 }
254 } elseif (isset($raw['ops'])) {
255 foreach ($raw['ops'] as $role_id => $actions) {
256 foreach ($actions as $action => $ops) {
257 foreach ((array) $ops as $op) {
258 $result[] = sprintf(
259 $this->lng->txt('rbac_log_operation_' . $action),
261 ) . ': ' . $this->getOPCaption($this->gui_object->getObject()->getType(), $op);
262 }
263 }
264 }
265 }
266
267 if (isset($raw['inht'])) {
268 foreach ($raw['inht'] as $action => $role_ids) {
269 foreach ((array) $role_ids as $role_id) {
270 $result[] = sprintf(
271 $this->lng->txt('rbac_log_inheritance_' . $action),
273 );
274 }
275 }
276 }
277
278 return implode('<br>', $result);
279 }
280
281 private function parseChangesTemplate(array $raw): string
282 {
283 $result = [];
284 foreach ($raw as $type => $actions) {
285 foreach ($actions as $action => $ops) {
286 foreach ($ops as $op) {
287 $result[] = sprintf(
288 $this->lng->txt('rbac_log_operation_' . $action),
289 $this->lng->txt('obj_' . $type)
290 ) . ': ' . $this->getOPCaption($type, $op);
291 }
292 }
293 }
294 return implode('<br>', $result);
295 }
296
297 private function getOPCaption(string $type, array|int|string $op): string
298 {
299 if (is_array($op)) {
300 return array_reduce(
301 $op,
302 fn(string $c, array|int|string $v) => $c === ''
303 ? $this->getOPCaption($type, $v)
304 : $c . ',' . $this->getOPCaption($type, $v),
305 ''
306 );
307 }
308
309 if (!isset($this->operations[$op])) {
310 return '';
311 }
312
313 $op_id = $this->operations[$op];
314 if (substr($op_id, 0, 7) !== 'create_') {
315 return $this->getNonCreateTranslation($type, $op_id);
316 }
317
318 return $this->getCreateTranslation($type, $op_id);
319 }
320
321 private function getNonCreateTranslation(string $type, string $op_id): string
322 {
323 $perm = $this->getTranslationFromPlugin($type, $op_id);
324 if ($this->isTranslated($perm, $op_id)) {
325 return $perm;
326 }
327
328 if ($this->lng->exists($type . '_' . $op_id . '_short')) {
329 return $this->lng->txt($type . '_' . $op_id . '_short');
330 }
331
332 return $this->lng->txt($op_id);
333 }
334
335 private function getCreateTranslation(string $type, string $op_id): string
336 {
337 $obj_type = substr($op_id, 7, strlen($op_id));
338 $perm = $this->getTranslationFromPlugin($obj_type, $op_id);
339
340 if ($this->isTranslated($perm, $op_id)) {
341 return $perm;
342 }
343
344 return $this->lng->txt('rbac_' . $op_id);
345 }
346
347 private function getTranslationFromPlugin(string $type, string $op_id): ?string
348 {
349 if ($this->object_definition->isPlugin($type)) {
350 return \ilObjectPlugin::lookupTxtById($type, $op_id);
351 }
352 return null;
353 }
354
355 private function isTranslated(?string $perm, string $op_id): bool
356 {
357 return $perm !== null && strpos($perm, $op_id) === false;
358 }
359}
getNonCreateTranslation(string $type, string $op_id)
Definition: Table.php:321
buildChangeColumn(int $action, array $data)
Definition: Table.php:216
getCreateTranslation(string $type, string $op_id)
Definition: Table.php:335
parseChangesFaPa(array $raw)
Definition: Table.php:233
applyFilterValuesTrafos(array $filter_values)
Definition: Table.php:196
parseChangesTemplate(array $raw)
Definition: Table.php:281
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
Definition: Table.php:170
getOPCaption(string $type, array|int|string $op)
Definition: Table.php:297
isTranslated(?string $perm, string $op_id)
Definition: Table.php:355
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
Definition: Table.php:138
__construct(private readonly \ilRbacLog $rbac_log, private readonly UIFactory $ui_factory, private readonly DataFactory $data_factory, private readonly \ilLanguage $lng, private readonly \ilCtrl $ctrl, private readonly \ilUIService $ui_service, private readonly \ilObjectDefinition $object_definition, private readonly RequestInterface $request, \ilRbacReview $rbac_review, private readonly \ilObjUser $current_user, private readonly \ilObjectGUI $gui_object)
Definition: Table.php:54
getTranslationFromPlugin(string $type, string $op_id)
Definition: Table.php:347
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
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Class ilCtrl provides processing control methods.
language handling
static _getTranslation(string $a_role_title)
User class.
static _lookupFullname(int $a_user_id)
static _lookupName(int $a_user_id)
parses the objects.xml it handles the xml-description of all ilias objects
Class ilObjectGUI Basic methods of all Output classes.
static _lookupObjectId(int $ref_id)
static _lookupTitle(int $obj_id)
class ilRbacLog Log changes in Rbac-related settings
const EDIT_PERMISSIONS
const COPY_OBJECT
const MOVE_OBJECT
const EDIT_TEMPLATE_EXISTING
const CHANGE_OWNER
const LINK_OBJECT
const EDIT_TEMPLATE
const CREATE_OBJECT
class ilRbacReview Contains Review functions of core Rbac.
getOperations()
get all possible operations
Filter service.
const ROLE_FOLDER_ID
Definition: constants.php:34
const ROOT_FOLDER_ID
Definition: constants.php:32
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes commonalities between all filters.
Definition: Filter.php:34
buildDataRow(string $id, array $record)
This describes a Data Table.
Definition: Data.php:33
global $lng
Definition: privfeed.php:31