Skip to content

Commit

Permalink
Badges: Fix missing table id and sorting issues
Browse files Browse the repository at this point in the history
(cherry picked from commit 0bf89f1)
  • Loading branch information
mjansenDatabay committed Dec 18, 2024
1 parent 81a40a7 commit 74b5f27
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
9 changes: 6 additions & 3 deletions components/ILIAS/Badge/classes/class.ilBadgeImageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,19 @@ public function setImageRid(?string $image_rid = null): void
$this->image_rid = $image_rid;
}

public function getImageFromResourceId(?string $image_rid, int $badge_id = null, $size = ilBadgeImage::IMAGE_SIZE_XS): string
{
public function getImageFromResourceId(
?string $image_rid,
int $badge_id = null,
int $size = ilBadgeImage::IMAGE_SIZE_XS
): string {
$image_src = '';

if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$flavour = $this->resource_storage->flavours()->get($identification, new \ilBadgePictureDefinition());
$urls = $this->resource_storage->consume()->flavourUrls($flavour)->getURLsAsArray(false);
if (sizeof($urls) === ilBadgeImage::IMAGE_URL_COUNT && isset($urls[$size])) {
if (count($urls) === ilBadgeImage::IMAGE_URL_COUNT && isset($urls[$size])) {
$image_src = $urls[$size];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,48 +70,53 @@ public function __construct(
}

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

foreach (ilBadgeImageTemplate::getInstances() as $template) {
$image_html = '';
if ($template->getId() !== null) {
$badge_template_image = $template->getImageFromResourceId($template->getImageRid());
$badge_template_image = $template->getImageFromResourceId($template->getImageRid());

$image = '';
$title = $template->getTitle();

if ($badge_template_image !== '') {
$image_component = $this->ui_factory->image()->responsive(
$badge_template_image,
$template->getTitle()
);
$image_html = $DIC->ui()->renderer()->render($image_component);

$badge_template_image_large = $template->getImageFromResourceId(
$template->getImageRid(),
null,
0
ilBadgeImage::IMAGE_SIZE_XL
);
$large_image_component = $this->ui_factory->image()->responsive(
$badge_template_image_large,
$template->getTitle()
);
if ($badge_template_image !== '') {
$badge_img = $DIC->ui()->factory()->image()->responsive(
$badge_template_image,
$template->getTitle()
);
$image_html = $DIC->ui()->renderer()->render($badge_img);
}

if ($badge_template_image_large !== '') {
$badge_img_large = $DIC->ui()->factory()->image()->responsive(
$badge_template_image_large,
$template->getTitle()
);

$modal = $modal_container->constructModal($badge_img_large, $template->getTitle());
$data[] = [
'id' => $template->getId(),
'image_rid' => $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());

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

$rows[] = [
'id' => $template->getId(),
'image' => $image,
'title' => $title,
];
}

return $data;
return $rows;
}

public function getRows(
Expand All @@ -137,30 +142,42 @@ public function getTotalRowCount(
}

/**
* @return list<array{id: int, image_rid: string, title: string}>
* @return list<array{id: int, image: string, title: string}>
*/
private function getRecords(Range $range = null, Order $order = null): array
{
global $DIC;

$data = $this->getBadgeImageTemplates($DIC);
$rows = $this->getBadgeImageTemplates($DIC);

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

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

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

return $data;
return $rows;
}
};
}
Expand Down Expand Up @@ -197,7 +214,7 @@ public function renderTable(): void
$df = new \ILIAS\Data\Factory();

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

Expand All @@ -218,6 +235,8 @@ public function renderTable(): void

$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);

Expand Down Expand Up @@ -278,6 +297,7 @@ public function renderTable(): void
}
}
}

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

0 comments on commit 74b5f27

Please sign in to comment.