Skip to content

Commit eca7365

Browse files
authored
Merge pull request ILIAS-eLearning#11177 from surlabs/ilias11_LTI_table_sorting
[LTI] fix: Implement table sorting in UI tables
2 parents 77d05ff + 31eaf0e commit eca7365

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

components/ILIAS/LTIConsumer/classes/class.ilLTIConsumerGradeSynchronizationTableGUI.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function getRows(
6666
mixed $filter_data,
6767
mixed $additional_parameters
6868
): Generator {
69-
foreach ($this->records as $record) {
69+
$records = $this->applyOrdering($this->records, $order);
70+
foreach ($records as $record) {
7071
$record['lti_timestamp'] = new DateTimeImmutable($record['lti_timestamp']);
7172
$record['score_given'] = $record['score_given'] . ' / ' . $record['score_maximum'];
7273
$record['activity_progress'] = $this->lng->txt('grade_activity_progress_' . strtolower($record['activity_progress']));
@@ -90,6 +91,34 @@ public function setRecords(array $records): void
9091
$this->records = $records;
9192
}
9293

94+
protected function applyOrdering(array $records, Order $order): array
95+
{
96+
[$order_field, $order_direction] = $order->join(
97+
[],
98+
fn($ret, $key, $value) => [$key, $value]
99+
);
100+
101+
usort($records, static function (array $left, array $right) use ($order_field): int {
102+
$left_val = $left[$order_field] ?? '';
103+
$right_val = $right[$order_field] ?? '';
104+
105+
if ($left_val instanceof DateTimeImmutable) {
106+
$left_val = $left_val->getTimestamp();
107+
}
108+
if ($right_val instanceof DateTimeImmutable) {
109+
$right_val = $right_val->getTimestamp();
110+
}
111+
112+
return $left_val <=> $right_val;
113+
});
114+
115+
if ($order_direction === Order::DESC) {
116+
$records = array_reverse($records);
117+
}
118+
119+
return $records;
120+
}
121+
93122
public function getHTML(): string
94123
{
95124
$table = $this->ui_factory->table()

components/ILIAS/LTIConsumer/classes/class.ilLTIConsumerProviderTableGUI.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public function getRows(
104104
mixed $filter_data,
105105
mixed $additional_parameters
106106
): Generator {
107-
foreach ($this->records as $record) {
107+
$records = $this->applyOrdering($this->records, $order);
108+
foreach ($records as $record) {
108109
$record["icon"] = $record["icon"] ?? "lti";
109110
$record["icon"] = $this->ui_factory->symbol()->icon()->standard($record["icon"], $record["icon"], IconAlias::SMALL);
110111

@@ -143,6 +144,26 @@ public function setData(array $data): void
143144
$this->records = $data;
144145
}
145146

147+
protected function applyOrdering(array $records, Order $order): array
148+
{
149+
[$order_field, $order_direction] = $order->join(
150+
[],
151+
fn($ret, $key, $value) => [$key, $value]
152+
);
153+
154+
usort($records, static function (array $left, array $right) use ($order_field): int {
155+
$left_val = $left[$order_field] ?? '';
156+
$right_val = $right[$order_field] ?? '';
157+
return ilStr::strCmp($left_val, $right_val);
158+
});
159+
160+
if ($order_direction === Order::DESC) {
161+
$records = array_reverse($records);
162+
}
163+
164+
return $records;
165+
}
166+
146167
/**
147168
* @throws ilCtrlException
148169
*/

components/ILIAS/LTIConsumer/classes/class.ilLTIConsumerProviderUsageTableGUI.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function getRows(
6767
/** @var Services $static_url */
6868
$static_url = $DIC["static_url"];
6969

70-
foreach ($this->records as $record) {
70+
$records = $this->applyOrdering($this->records, $order);
71+
foreach ($records as $record) {
7172
$record['icon'] = $record['icon'] ?? "lti";
7273
$record['icon'] = $this->ui_factory->symbol()->icon()->standard($record['icon'], $record['icon'], Icon::SMALL);
7374

@@ -98,6 +99,26 @@ public function setData(array $data): void
9899
$this->records = $data;
99100
}
100101

102+
protected function applyOrdering(array $records, Order $order): array
103+
{
104+
[$order_field, $order_direction] = $order->join(
105+
[],
106+
fn($ret, $key, $value) => [$key, $value]
107+
);
108+
109+
usort($records, static function (array $left, array $right) use ($order_field): int {
110+
$left_val = $left[$order_field] ?? '';
111+
$right_val = $right[$order_field] ?? '';
112+
return ilStr::strCmp($left_val, $right_val);
113+
});
114+
115+
if ($order_direction === Order::DESC) {
116+
$records = array_reverse($records);
117+
}
118+
119+
return $records;
120+
}
121+
101122
public function getHTML(): string
102123
{
103124
$table = $this->ui_factory->table()

components/ILIAS/LTIProvider/classes/Consumer/class.ilObjectConsumerTableGUI.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public function getRows(
176176
mixed $filter_data,
177177
mixed $additional_parameters
178178
): Generator {
179-
foreach ($this->records as $record) {
179+
$records = $this->applyOrdering($this->records, $order);
180+
foreach ($records as $record) {
180181
$record["active"] = $this->ui_factory->symbol()->icon()->custom(
181182
$record["active"] ?
182183
'assets/images/standard/icon_ok.svg' :
@@ -235,4 +236,24 @@ public function setEditable(bool $a_editable): void
235236
{
236237
$this->editable = $a_editable;
237238
}
239+
240+
protected function applyOrdering(array $records, Order $order): array
241+
{
242+
[$order_field, $order_direction] = $order->join(
243+
[],
244+
fn($ret, $key, $value) => [$key, $value]
245+
);
246+
247+
usort($records, static function (array $left, array $right) use ($order_field): int {
248+
$left_val = $left[$order_field] ?? '';
249+
$right_val = $right[$order_field] ?? '';
250+
return ilStr::strCmp($left_val, $right_val);
251+
});
252+
253+
if ($order_direction === Order::DESC) {
254+
$records = array_reverse($records);
255+
}
256+
257+
return $records;
258+
}
238259
}

components/ILIAS/LTIProvider/classes/InternalProvider/class.ilLTIProviderReleasedObjectsTableGUI.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function getRows(
118118
/** @var Services $static_url */
119119
$static_url = $DIC["static_url"];
120120

121-
foreach ($this->records as $record) {
121+
$records = $this->applyOrdering($this->records, $order);
122+
foreach ($records as $record) {
122123
$link = (string) $static_url->builder()->build(
123124
$record['type'],
124125
new ReferenceId($record['ref_id'])
@@ -158,4 +159,24 @@ public function getId(): string
158159
{
159160
return $this->id;
160161
}
162+
163+
protected function applyOrdering(array $records, Order $order): array
164+
{
165+
[$order_field, $order_direction] = $order->join(
166+
[],
167+
fn($ret, $key, $value) => [$key, $value]
168+
);
169+
170+
usort($records, static function (array $left, array $right) use ($order_field): int {
171+
$left_val = $left[$order_field] ?? '';
172+
$right_val = $right[$order_field] ?? '';
173+
return ilStr::strCmp($left_val, $right_val);
174+
});
175+
176+
if ($order_direction === Order::DESC) {
177+
$records = array_reverse($records);
178+
}
179+
180+
return $records;
181+
}
161182
}

0 commit comments

Comments
 (0)