Skip to content

Commit

Permalink
Badge: Fix sorting of badged tables + streamlining
Browse files Browse the repository at this point in the history
(cherry picked from commit a00c13c)
  • Loading branch information
mjansenDatabay committed Jan 16, 2025
1 parent e4f9a0b commit 6ddf85c
Show file tree
Hide file tree
Showing 6 changed files with 879 additions and 997 deletions.
281 changes: 132 additions & 149 deletions components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
use ILIAS\UI\Component\Table\DataRetrieval;
use ILIAS\UI\URLBuilderToken;
use ILIAS\Filesystem\Stream\Streams;
use ILIAS\UI\Component\Table\Column\Column;

class ilBadgeImageTemplateTableGUI
class ilBadgeImageTemplateTableGUI implements DataRetrieval
{
private readonly Factory $factory;
private readonly Renderer $renderer;
Expand All @@ -58,122 +59,121 @@ public function __construct(protected bool $has_write = false)
$this->http = $DIC->http();
}

private function buildDataRetrievalObject(Factory $f, Renderer $r): DataRetrieval
/**
* @return list<array{id: int, image: string, title: string, title_sortable: string}>
*/
private function getBadgeImageTemplates(): array
{
return new class ($f, $r) implements DataRetrieval {
public function __construct(
private readonly Factory $ui_factory,
private readonly Renderer $ui_renderer
) {
}

/**
* @return list<array{id: int, image: string, title: string, title_sortable: string}>
*/
private function getBadgeImageTemplates(): array
{
$modal_container = new ModalBuilder();
$rows = [];
$modal_container = new ModalBuilder();
$rows = [];

foreach (ilBadgeImageTemplate::getInstances() as $template) {
$image = '';
$title = $template->getTitle();

$image_src = $template->getImageFromResourceId();
if ($image_src !== '') {
$image_component = $this->factory->image()->responsive(
$image_src,
$template->getTitle()
);
$image_html = $this->renderer->render($image_component);

foreach (ilBadgeImageTemplate::getInstances() as $template) {
$image = '';
$title = $template->getTitle();
$image_src_large = $template->getImageFromResourceId(
ilBadgeImage::IMAGE_SIZE_XL
);
$large_image_component = $this->factory->image()->responsive(
$image_src_large,
$template->getTitle()
);

$image_src = $template->getImageFromResourceId();
if ($image_src !== '') {
$image_component = $this->ui_factory->image()->responsive(
$image_src,
$template->getTitle()
);
$image_html = $this->ui_renderer->render($image_component);
$modal = $modal_container->constructModal($large_image_component, $template->getTitle());

$image_src_large = $template->getImageFromResourceId(
ilBadgeImage::IMAGE_SIZE_XL
);
$large_image_component = $this->ui_factory->image()->responsive(
$image_src_large,
$template->getTitle()
);
$image = implode('', [
$modal_container->renderShyButton($image_html, $modal),
$modal_container->renderModal($modal)
]);
$title = $modal_container->renderShyButton($template->getTitle(), $modal);
}

$modal = $modal_container->constructModal($large_image_component, $template->getTitle());
$rows[] = [
'id' => $template->getId(),
'image' => $image,
'title' => $title,
'title_sortable' => $template->getTitle()
];
}

$image = implode('', [
$modal_container->renderShyButton($image_html, $modal),
$modal_container->renderModal($modal)
]);
$title = $modal_container->renderShyButton($template->getTitle(), $modal);
}
return $rows;
}

$rows[] = [
'id' => $template->getId(),
'image' => $image,
'title' => $title,
'title_sortable' => $template->getTitle()
];
}
public function getRows(
DataRowBuilder $row_builder,
array $visible_column_ids,
Range $range,
Order $order,
?array $filter_data,
?array $additional_parameters
): Generator {
$records = $this->getRecords($range, $order);
foreach ($records as $record) {
$row_id = (string) $record['id'];
yield $row_builder->buildDataRow($row_id, $record);
}
}

return $rows;
}
public function getTotalRowCount(
?array $filter_data,
?array $additional_parameters
): ?int {
return \count($this->getRecords());
}

public function getRows(
DataRowBuilder $row_builder,
array $visible_column_ids,
Range $range,
Order $order,
?array $filter_data,
?array $additional_parameters
): Generator {
$records = $this->getRecords($range, $order);
foreach ($records as $record) {
$row_id = (string) $record['id'];
yield $row_builder->buildDataRow($row_id, $record);
}
}
/**
* @return list<array{id: int, image: string, title: string, title_sortable: string}>
*/
private function getRecords(Range $range = null, Order $order = null): array
{
$rows = $this->getBadgeImageTemplates();

public function getTotalRowCount(
?array $filter_data,
?array $additional_parameters
): ?int {
return \count($this->getRecords());
}
if ($order) {
[$order_field, $order_direction] = $order->join(
[],
fn($ret, $key, $value) => [$key, $value]
);

/**
* @return list<array{id: int, image: string, title: string, title_sortable: string}>
*/
private function getRecords(Range $range = null, Order $order = null): array
{
$rows = $this->getBadgeImageTemplates();

if ($order) {
[$order_field, $order_direction] = $order->join(
[],
fn($ret, $key, $value) => [$key, $value]
usort($rows, static function (array $left, array $right) use ($order_field): int {
if ($order_field === 'title') {
return \ilStr::strCmp(
$left[$order_field . '_sortable'],
$right[$order_field . '_sortable']
);
usort(
$rows,
static function (array $left, array $right) use ($order_field): int {
if ($order_field === 'title') {
return \ilStr::strCmp(
$left[$order_field . '_sortable'],
$right[$order_field . '_sortable']
);
}

return $left[$order_field] <=> $right[$order_field];
}
);
if ($order_direction === Order::DESC) {
$rows = array_reverse($rows);
}
}

if ($range) {
$rows = \array_slice($rows, $range->getStart(), $range->getLength());
}
return $left[$order_field] <=> $right[$order_field];
});

return $rows;
if ($order_direction === Order::DESC) {
$rows = array_reverse($rows);
}
};
}

if ($range) {
$rows = \array_slice($rows, $range->getStart(), $range->getLength());
}

return $rows;
}

/**
* @return array<string, Column>
*/
public function getColumns(): array
{
return [
'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
'title' => $this->factory->table()->column()->text($this->lng->txt('title'))
];
}

/**
Expand All @@ -184,59 +184,42 @@ private function getActions(
URLBuilderToken $action_parameter_token,
URLBuilderToken $row_id_token
): array {
$f = $this->factory;
if ($this->has_write) {
return [
'badge_image_template_edit' => $f->table()->action()->single(
$this->lng->txt('edit'),
$url_builder->withParameter($action_parameter_token, 'badge_image_template_editImageTemplate'),
return $this->has_write ? [
'badge_image_template_edit' => $this->factory->table()->action()->single(
$this->lng->txt('edit'),
$url_builder->withParameter($action_parameter_token, 'badge_image_template_editImageTemplate'),
$row_id_token
),
'badge_image_template_delete' =>
$this->factory->table()->action()->standard(
$this->lng->txt('delete'),
$url_builder->withParameter($action_parameter_token, 'badge_image_template_delete'),
$row_id_token
),
'badge_image_template_delete' =>
$f->table()->action()->standard(
$this->lng->txt('delete'),
$url_builder->withParameter($action_parameter_token, 'badge_image_template_delete'),
$row_id_token
)
];
}

return [];
)
] : [];
}

public function renderTable(): void
{
$f = $this->factory;
$r = $this->renderer;
$request = $this->request;
$df = new \ILIAS\Data\Factory();

$columns = [
'image' => $f->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
'title' => $f->table()->column()->text($this->lng->txt('title')),
];

$table_uri = $df->uri($request->getUri()->__toString());
$table_uri = $df->uri($this->request->getUri()->__toString());
$url_builder = new URLBuilder($table_uri);
$query_params_namespace = ['tid'];

[$url_builder, $action_parameter_token, $row_id_token] =
$url_builder->acquireParameters(
$query_params_namespace,
'table_action',
'id'
);

$actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token);

$data_retrieval = $this->buildDataRetrievalObject($f, $r);
[$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
$query_params_namespace,
'table_action',
'id',
);

$table = $f->table()
->data($this->lng->txt('badge_image_templates'), $columns, $data_retrieval)
->withId(self::class)
->withOrder(new Order('title', Order::ASC))
->withActions($actions)
->withRequest($request);
$table = $this->factory
->table()
->data($this->lng->txt('badge_image_templates'), $this->getColumns(), $this)
->withId(self::class)
->withOrder(new Order('title', Order::ASC))
->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
->withRequest($this->request);

$out = [$table];
$query = $this->http->wrapper()->query();
Expand All @@ -250,7 +233,7 @@ public function renderTable(): void
if ($query_values === ['ALL_OBJECTS']) {
foreach (ilBadgeImageTemplate::getInstances() as $template) {
if ($template->getId() !== null) {
$items[] = $f->modal()->interruptiveItem()->keyValue(
$items[] = $this->factory->modal()->interruptiveItem()->keyValue(
(string) $template->getId(),
(string) $template->getId(),
$template->getTitle()
Expand All @@ -260,15 +243,15 @@ public function renderTable(): void
} elseif (\is_array($query_values)) {
foreach ($query_values as $id) {
$badge = new ilBadgeImageTemplate((int) $id);
$items[] = $f->modal()->interruptiveItem()->keyValue(
$items[] = $this->factory->modal()->interruptiveItem()->keyValue(
(string) $id,
(string) $badge->getId(),
$badge->getTitle()
);
}
} else {
$badge = new ilBadgeImageTemplate($query_values);
$items[] = $f->modal()->interruptiveItem()->keyValue(
$items[] = $this->factory->modal()->interruptiveItem()->keyValue(
(string) $badge->getId(),
(string) $badge->getId(),
$badge->getTitle()
Expand All @@ -281,8 +264,8 @@ public function renderTable(): void
$this->http
->response()
->withBody(
Streams::ofString($r->renderAsync([
$f->modal()->interruptive(
Streams::ofString($this->renderer->renderAsync([
$this->factory->modal()->interruptive(
$this->lng->txt('badge_deletion'),
$this->lng->txt('badge_deletion_confirmation'),
'#'
Expand All @@ -296,6 +279,6 @@ public function renderTable(): void
}
}

$this->tpl->setContent($r->render($out));
$this->tpl->setContent($this->renderer->render($out));
}
}
Loading

0 comments on commit 6ddf85c

Please sign in to comment.