From 1e6edbee05320363a1c1b98ffee48b36e11a244b Mon Sep 17 00:00:00 2001 From: Guido Vollbach Date: Thu, 7 Dec 2023 09:49:22 +0100 Subject: [PATCH] Refactoring/restructure --- Services/Badge/classes/Tile.php | 7 ++- .../Badge/classes/class.ilBadgeBackpack.php | 1 + Services/Badge/classes/class.ilBadgeImage.php | 38 ++++++++++++ .../classes/class.ilBadgeManagementGUI.php | 9 +++ .../Badge/classes/class.ilBadgeTableGUI.php | 59 +++++++++---------- .../Init/classes/class.ilInitialisation.php | 2 +- 6 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 Services/Badge/classes/class.ilBadgeImage.php diff --git a/Services/Badge/classes/Tile.php b/Services/Badge/classes/Tile.php index 4f4e2572e0ca..80b7ca3f1a00 100644 --- a/Services/Badge/classes/Tile.php +++ b/Services/Badge/classes/Tile.php @@ -40,6 +40,8 @@ class Tile /** @var Closure(int): string */ private readonly Closure $format_date; + private ilBadgeImage|null $badge_image = null; + /** * @param Closure(string): string $sign_file * @param Closure(int): string $format_date @@ -61,6 +63,8 @@ class_exists(ilDateTime::class); // Ensure ilDateTime is loaded as IL_CAL_UNIX i ); } $this->format_date = $format_date; + global $DIC; + $this->badge_image = new ilBadgeImage($DIC); } /** @@ -130,10 +134,11 @@ private function modal(Card $card): Component private function image(Component $modal, ilBadge $badge): Component { + $image_src = $this->badge_image->getImageFromBadge($badge); return $this->container->ui() ->factory() ->image() - ->responsive(($this->sign_file)($badge->getImagePath()), $badge->getImage()) + ->responsive(($this->sign_file)($image_src), $image_src) ->withAction($modal->getShowSignal()); } diff --git a/Services/Badge/classes/class.ilBadgeBackpack.php b/Services/Badge/classes/class.ilBadgeBackpack.php index 3ffa5cc3dcc8..ac2c9c160e0f 100644 --- a/Services/Badge/classes/class.ilBadgeBackpack.php +++ b/Services/Badge/classes/class.ilBadgeBackpack.php @@ -99,6 +99,7 @@ public function getBadges(string $a_group_id): ?array "title" => $badge->name, "description" => $badge->description, "image_url" => $badge->image, + "image_rid" => $badge->image_rid, "criteria_url" => $badge->criteria, "issuer_name" => $badge->issuer->name, "issuer_url" => $badge->issuer->origin, diff --git a/Services/Badge/classes/class.ilBadgeImage.php b/Services/Badge/classes/class.ilBadgeImage.php new file mode 100644 index 000000000000..b230e9ba16af --- /dev/null +++ b/Services/Badge/classes/class.ilBadgeImage.php @@ -0,0 +1,38 @@ +dic = $DIC; + } + + /** + * @param int $badge_id + * @param string $image_rid + * @return string + */ + public function getImageFromResourceId(int $badge_id, string $image_rid) : string + { + if ($image_rid !== null) { + $identification = $this->dic['resource_storage']->manage()->find($image_rid); + $image_src = $this->dic['resource_storage']->consume()->src($identification)->getSrc(); + } else { + $badge = new ilBadge($badge_id); + $image_src = $this->dic['resource_storage']->consume()->src($badge->getImage()); + } + return $image_src; + } + + public function getImageFromBadge(ilBadge $badge) : string + { + $image_rid = $badge->getImageRid(); + return $this->getImageFromResourceId($badge->getId(), $image_rid); + } +} \ No newline at end of file diff --git a/Services/Badge/classes/class.ilBadgeManagementGUI.php b/Services/Badge/classes/class.ilBadgeManagementGUI.php index 573bb3470faa..c08402a4198c 100644 --- a/Services/Badge/classes/class.ilBadgeManagementGUI.php +++ b/Services/Badge/classes/class.ilBadgeManagementGUI.php @@ -16,6 +16,8 @@ * *********************************************************************/ +use ILIAS\Badge\ilBadgeImage; + /** * Class ilBadgeManagementGUI * @@ -37,6 +39,8 @@ class ilBadgeManagementGUI private int $parent_obj_id; private string $parent_obj_type; + private ?ilBadgeImage $badge_image = null; + public function __construct( private readonly int $parent_ref_id, int $a_parent_obj_id = null, @@ -70,6 +74,7 @@ public function __construct( ); $this->session_repo = new ilBadgeManagementSessionRepository(); + $this->badge_image = new ilBadgeImage($DIC); } public function executeCommand(): void @@ -449,6 +454,10 @@ protected function setBadgeFormValues( $a_form->getItemByPostVar('crit')->setValue($a_badge->getCriteria()); $a_form->getItemByPostVar('img')->setValue($a_badge->getImage()); $a_form->getItemByPostVar('img')->setImage($a_badge->getImagePath()); + + $image_src = $this->badge_image->getImageFromBadge($a_badge); + $a_form->getItemByPostVar('img')->setImage($image_src); + $a_form->getItemByPostVar('valid')->setValue($a_badge->getValid()); $custom = $a_type->getConfigGUIInstance(); diff --git a/Services/Badge/classes/class.ilBadgeTableGUI.php b/Services/Badge/classes/class.ilBadgeTableGUI.php index 417efb6e011c..3238a41aec75 100644 --- a/Services/Badge/classes/class.ilBadgeTableGUI.php +++ b/Services/Badge/classes/class.ilBadgeTableGUI.php @@ -18,6 +18,7 @@ use ILIAS\DI\UIServices; use ILIAS\Badge\Tile; +use ILIAS\Badge\ilBadgeImage; /** * TableGUI class for badge listing @@ -29,6 +30,7 @@ class ilBadgeTableGUI extends ilTable2GUI protected array $filter = []; private readonly Tile $tile; private readonly UIServices $ui; + private ilBadgeImage|null $badge_image = null; public function __construct( object $a_parent_obj, @@ -44,6 +46,7 @@ public function __construct( $this->tile = new Tile($DIC); $ilCtrl = $DIC->ctrl(); $lng = $DIC->language(); + $this->badge_image = new ilBadgeImage($DIC); $this->setId("bdgbdg"); $this->parent_type = ilObject::_lookupType($a_parent_obj_id); @@ -137,49 +140,45 @@ protected function fillRow(array $a_set): void $ilCtrl = $this->ctrl; if ($this->has_write) { - $this->tpl->setVariable("VAL_ID", $a_set["id"]); + $this->tpl->setVariable('VAL_ID', $a_set['id']); } - global $DIC; - $rid_string = $a_set['image_rid']; - - if (null !== $rid_string) { - $identification = $DIC['resource_storage']->manage()->find($rid_string); - $image_src = $DIC['resource_storage']->consume()->src($identification); - $image = $DIC->ui()->factory()->image()->responsive($image_src->getSrc(), $a_set['title']); - $image = $this->ui->renderer()->render($image); + $image_src = $this->badge_image->getImageFromResourceId($a_set['id'], $a_set['image_rid']); + if (null !== $a_set['image_rid']) { + global $DIC; + $image_responsive = $DIC->ui()->factory()->image()->responsive($image_src, $a_set['title']); + $image = $this->ui->renderer()->render($image_responsive); } else { - $image = $this->ui->renderer()->render($a_set["renderer"]()); + $image = $this->ui->renderer()->render($a_set['renderer']()); } - - - $this->tpl->setVariable("PREVIEW", $image); - $this->tpl->setVariable("TXT_TYPE", $a_set["type"]); - $this->tpl->setVariable("TXT_ACTIVE", $a_set["active"] - ? $lng->txt("yes") - : $lng->txt("no")); + + $this->tpl->setVariable('PREVIEW', $image); + $this->tpl->setVariable('TXT_TYPE', $a_set['type']); + $this->tpl->setVariable('TXT_ACTIVE', $a_set['active'] + ? $lng->txt('yes') + : $lng->txt('no')); if ($this->has_write) { $buttons = []; - if ($a_set["manual"] && $a_set["active"]) { - $ilCtrl->setParameter($this->getParentObject(), "bid", $a_set["id"]); - $ilCtrl->setParameter($this->getParentObject(), "tgt", "bdgl"); - $url = $ilCtrl->getLinkTarget($this->getParentObject(), "awardBadgeUserSelection"); - $ilCtrl->setParameter($this->getParentObject(), "bid", ""); - $ilCtrl->setParameter($this->getParentObject(), "tgt", ""); + if ($a_set['manual'] && $a_set['active']) { + $ilCtrl->setParameter($this->getParentObject(), 'bid', $a_set['id']); + $ilCtrl->setParameter($this->getParentObject(), 'tgt', 'bdgl'); + $url = $ilCtrl->getLinkTarget($this->getParentObject(), 'awardBadgeUserSelection'); + $ilCtrl->setParameter($this->getParentObject(), 'bid', ''); + $ilCtrl->setParameter($this->getParentObject(), 'tgt', ''); - $buttons[] = $this->ui->factory()->button()->shy($lng->txt("badge_award_badge"), $url); + $buttons[] = $this->ui->factory()->button()->shy($lng->txt('badge_award_badge'), $url); } - $ilCtrl->setParameter($this->getParentObject(), "bid", $a_set["id"]); - $url = $ilCtrl->getLinkTarget($this->getParentObject(), "editBadge"); - $ilCtrl->setParameter($this->getParentObject(), "bid", ""); + $ilCtrl->setParameter($this->getParentObject(), 'bid', $a_set['id']); + $url = $ilCtrl->getLinkTarget($this->getParentObject(), 'editBadge'); + $ilCtrl->setParameter($this->getParentObject(), 'bid', ''); - $buttons[] = $this->ui->factory()->button()->shy($lng->txt("edit"), $url); - $actions = $this->ui->factory()->dropdown()->standard($buttons)->withLabel($lng->txt("actions")); + $buttons[] = $this->ui->factory()->button()->shy($lng->txt('edit'), $url); + $actions = $this->ui->factory()->dropdown()->standard($buttons)->withLabel($lng->txt('actions')); - $this->tpl->setVariable("ACTIONS", $this->ui->renderer()->render($actions)); + $this->tpl->setVariable('ACTIONS', $this->ui->renderer()->render($actions)); } } } diff --git a/Services/Init/classes/class.ilInitialisation.php b/Services/Init/classes/class.ilInitialisation.php index cd207f3a1de2..69c871256476 100644 --- a/Services/Init/classes/class.ilInitialisation.php +++ b/Services/Init/classes/class.ilInitialisation.php @@ -1487,7 +1487,7 @@ protected static function initRefinery(\ILIAS\DI\Container $container): void /** * @param Container $container */ - protected static function replaceSuperGlobals(\ILIAS\DI\Container $container): void + protected static function replaceSuperGlobals(\ILIAS\DI\Container $container): void { /** @var ilIniFile $client_ini */ $client_ini = $container['ilClientIniFile'];