From 74b5f27b024e3274aaccd04605d46ba443e8f95f Mon Sep 17 00:00:00 2001 From: mjansen Date: Wed, 18 Dec 2024 13:18:24 +0100 Subject: [PATCH] Badges: Fix missing table id and sorting issues (cherry picked from commit 0bf89f18f8c47980261c29ed167a7f3a5039939e) --- .../classes/class.ilBadgeImageTemplate.php | 9 +- .../class.ilBadgeImageTemplateTableGUI.php | 92 +++++++++++-------- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplate.php b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplate.php index 3b7751c133ad..04caa5df51b2 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplate.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplate.php @@ -373,8 +373,11 @@ 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) { @@ -382,7 +385,7 @@ public function getImageFromResourceId(?string $image_rid, int $badge_id = null, 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]; } } diff --git a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php index 732f4da9803c..71e622a7f6d8 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php @@ -70,48 +70,53 @@ public function __construct( } /** - * @return list + * @return list */ 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( @@ -137,30 +142,42 @@ public function getTotalRowCount( } /** - * @return list + * @return list */ 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; } }; } @@ -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')), ]; @@ -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); @@ -278,6 +297,7 @@ public function renderTable(): void } } } + $this->tpl->setContent($r->render($out)); } }