ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
DataRetrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use ILIAS\UI\Factory as UIFactory;
28
30{
31 public const string ACTION_DELETE = 'delete';
32 public const string ACTION_ROLLBACK = 'rollback';
33 public const string ACTION_PUBLISH = 'publish';
34 public const string ACTION_UNPUBLISH = 'unpublish';
35
36 public function __construct(
37 private readonly \ilObjFile $file,
38 private readonly int $current_version,
39 private readonly bool $current_version_is_draft,
40 private readonly int $amount_of_versions,
41 private readonly \ilCtrlInterface $ctrl,
42 private readonly \ilFileVersionsGUI $parent_gui,
43 private readonly \ilLanguage $lng,
44 private readonly UIFactory $ui_factory
45 ) {
46 }
47
48 public function getRows(
49 I\DataRowBuilder $row_builder,
50 array $visible_column_ids,
52 Order $order,
53 mixed $additional_viewcontrol_data,
54 mixed $filter_data,
55 mixed $additional_parameters
56 ): \Generator {
57 $records = $this->getRecords($order);
58 foreach ($records as $record) {
59 $row_id = (string) $record['hist_entry_id'];
60 $version_number = (int) $record['version'];
61 $is_current = $version_number === $this->current_version;
62
63 yield $row_builder->buildDataRow($row_id, $this->mapRecord($record))
64 ->withDisabledAction(
65 self::ACTION_DELETE,
66 $this->current_version_is_draft
67 )
68 ->withDisabledAction(
69 self::ACTION_ROLLBACK,
70 $this->current_version_is_draft || $is_current
71 )
72 ->withDisabledAction(
73 self::ACTION_PUBLISH,
74 !($this->current_version_is_draft && $is_current)
75 )
76 ->withDisabledAction(
77 self::ACTION_UNPUBLISH,
78 $this->current_version_is_draft || !$is_current || $this->amount_of_versions <= 1
79 );
80 }
81 }
82
83 public function getTotalRowCount(
84 mixed $additional_viewcontrol_data,
85 mixed $filter_data,
86 mixed $additional_parameters
87 ): ?int {
88 return count($this->file->getVersions());
89 }
90
94 private function getRecords(Order $order): array
95 {
96 $records = [];
97 foreach ($this->file->getVersions() as $version) {
98 $records[] = $version->getArrayCopy();
99 }
100
101 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value): array => [$key, $value]);
102 if ($order_field === 'version') {
103 usort($records, static fn(array $a, array $b): int => (int) $a['version'] <=> (int) $b['version']);
104 } else {
105 usort($records, static fn(array $a, array $b): int => ($a[$order_field] ?? null) <=> ($b[$order_field] ?? null));
106 }
107 if ($order_direction === 'DESC') {
108 $records = array_reverse($records);
109 }
110 return $records;
111 }
112
117 private function mapRecord(array $record): array
118 {
119 $hist_id = (int) $record['hist_entry_id'];
120
121 $name = \ilObjUser::_lookupName((int) $record['user_id']);
122 $username = trim(($name['title'] ?? '') . ' ' . ($name['firstname'] ?? '') . ' ' . ($name['lastname'] ?? ''));
123
124 $action_label = $this->lng->txt('file_version_' . $record['action']);
125 if ($record['action'] === 'rollback') {
126 $rollback_name = \ilObjUser::_lookupName((int) $record['rollback_user_id']);
127 $rollback_username = trim(
128 ($rollback_name['title'] ?? '') . ' ' .
129 ($rollback_name['firstname'] ?? '') . ' ' .
130 ($rollback_name['lastname'] ?? '')
131 );
132 $action_label = sprintf($action_label, $record['rollback_version'], $rollback_username);
133 }
134
135 $this->ctrl->setParameter($this->parent_gui, \ilFileVersionsGUI::HIST_ID, $hist_id);
136 $download_url = $this->ctrl->getLinkTarget($this->parent_gui, \ilFileVersionsGUI::CMD_DOWNLOAD_VERSION);
137 $this->ctrl->setParameter($this->parent_gui, \ilFileVersionsGUI::HIST_ID, '');
138
139 $filename_link = $this->ui_factory->link()->standard((string) $record['filename'], $download_url);
140
141 $size = new DataSize((int) ($record['size'] ?? 0), DataSize::KB);
142
143 return [
144 'version' => (int) $record['version'],
145 'filename' => $filename_link,
146 'date' => new \DateTimeImmutable((string) $record['date']),
147 'uploaded_by' => $username,
148 'versionname' => (string) ($record['title'] ?? ''),
149 'filesize' => (string) $size,
150 'status' => $action_label,
151 ];
152 }
153}
$version
Definition: plugin.php:24
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
__construct(private readonly \ilObjFile $file, private readonly int $current_version, private readonly bool $current_version_is_draft, private readonly int $amount_of_versions, private readonly \ilCtrlInterface $ctrl, private readonly \ilFileVersionsGUI $parent_gui, private readonly \ilLanguage $lng, private readonly UIFactory $ui_factory)
getRows(I\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
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...
language handling
Class ilObjFile.
static _lookupName(int $a_user_id)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $lng
Definition: privfeed.php:26