From 036086ab8cd25bf491ccd563f4aa6fc714e9c287 Mon Sep 17 00:00:00 2001 From: Guido Vollbach Date: Mon, 6 Jan 2025 09:43:23 +0100 Subject: [PATCH] Badges: Bugfixes after testing (#8786) * Fixed multi action delete * Fixed permissions * Code style fixes * Fixed comments --- .../Badge/classes/class.ilBadgeGUIRequest.php | 19 +++++ .../class.ilBadgeImageTemplateTableGUI.php | 33 ++++---- .../classes/class.ilBadgeManagementGUI.php | 5 +- .../Badge/classes/class.ilBadgeTableGUI.php | 75 ++++++++++--------- .../classes/class.ilBadgeTypesTableGUI.php | 31 ++++---- .../class.ilObjBadgeAdministrationGUI.php | 25 +++++-- .../classes/class.ilObjectBadgeTableGUI.php | 69 ++++++++++------- 7 files changed, 157 insertions(+), 100 deletions(-) diff --git a/components/ILIAS/Badge/classes/class.ilBadgeGUIRequest.php b/components/ILIAS/Badge/classes/class.ilBadgeGUIRequest.php index f125c4693ae7..405350ce92ee 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeGUIRequest.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeGUIRequest.php @@ -22,6 +22,7 @@ class ilBadgeGUIRequest { private const ACTION_PARAMETER_TOKEN = 'tid_id'; + private const ACTION_PARAMETER_TOKEN_ID = 'id'; protected HTTP\Services $http; protected Refinery\Factory $refinery; @@ -186,6 +187,24 @@ public function getMultiActionBadgeIdsFromUrl(): array return $tmpl_ids; } + /** + * @return list + */ + public function getMultiActionBadgeIdsFromPost(): array + { + $tmpl_ids = []; + $query = $this->http->wrapper()->post(); + if ($query->has(self::ACTION_PARAMETER_TOKEN_ID)) { + $tmpl_ids = $query->retrieve( + self::ACTION_PARAMETER_TOKEN_ID, + $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()) + ); + } + + return $tmpl_ids; + } + + public function getBadgeId(): int { return $this->int("bid"); diff --git a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php index fe0b71d87939..38f79bb15fe6 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php @@ -13,7 +13,6 @@ * us at: * https://www.ilias.de * https://github.com/ILIAS-eLearning - * *********************************************************************/ declare(strict_types=1); @@ -35,7 +34,6 @@ use Generator; use ILIAS\UI\Component\Table\DataRetrieval; use ILIAS\UI\URLBuilderToken; -use ILIAS\DI\Container; use ILIAS\Filesystem\Stream\Streams; class ilBadgeImageTemplateTableGUI @@ -48,7 +46,7 @@ class ilBadgeImageTemplateTableGUI private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; - public function __construct() + public function __construct(protected bool $has_write = false) { global $DIC; $this->lng = $DIC->language(); @@ -191,19 +189,24 @@ private function getActions( URLBuilderToken $row_id_token ): array { $f = $this->factory; - return [ - 'badge_image_template_edit' => $f->table()->action()->single( - $this->lng->txt('edit'), - $url_builder->withParameter($action_parameter_token, 'badge_image_template_editImageTemplate'), - $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'), + 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'), $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 + ) + ]; + } else { + return []; + } + } public function renderTable(): void diff --git a/components/ILIAS/Badge/classes/class.ilBadgeManagementGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeManagementGUI.php index f4aa7243f5b6..5cd0d8b5ebfb 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeManagementGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeManagementGUI.php @@ -13,7 +13,6 @@ * us at: * https://www.ilias.de * https://github.com/ILIAS-eLearning - * *********************************************************************/ use ILIAS\Badge\ilBadgeImage; @@ -292,7 +291,7 @@ protected function listBadges(): void } } - $table = new ilBadgeTableGUI($this->parent_obj_id, $this->parent_obj_type); + $table = new ilBadgeTableGUI($this->parent_obj_id, $this->parent_obj_type, $this->hasWrite()); $table->renderTable(); } @@ -658,8 +657,6 @@ protected function deleteBadges(): void $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_select_one'), true); } - - $ilCtrl->redirect($this, 'listBadges'); } diff --git a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php index 28a6d1489a04..d2fab3095955 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php @@ -13,7 +13,6 @@ * us at: * https://www.ilias.de * https://github.com/ILIAS-eLearning - * *********************************************************************/ declare(strict_types=1); @@ -34,7 +33,6 @@ use Generator; use ILIAS\UI\Component\Table\DataRetrieval; use ILIAS\UI\URLBuilderToken; -use ILIAS\DI\Container; use ilBadge; use ilBadgeAuto; use ILIAS\Filesystem\Stream\Streams; @@ -52,7 +50,7 @@ class ilBadgeTableGUI private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; - public function __construct(int $parent_obj_id, string $parent_obj_type) + public function __construct(int $parent_obj_id, string $parent_obj_type, protected bool $has_write = false) { global $DIC; @@ -193,10 +191,10 @@ public function getRows( foreach ($records as $record) { $row_id = (string) $record['id']; yield $row_builder->buildDataRow($row_id, $record) - ->withDisabledAction( - 'award_revoke_badge', - !$record['manual'] || !$record['active'] - ); + ->withDisabledAction( + 'award_revoke_badge', + !$record['manual'] || !$record['active'] + ); } } @@ -269,37 +267,42 @@ private function getActions( ): array { $f = $this->factory; - return [ - 'badge_table_activate' => - $f->table()->action()->multi( - $this->lng->txt('activate'), - $url_builder->withParameter($action_parameter_token, 'badge_table_activate'), + if ($this->has_write) { + return [ + 'badge_table_activate' => + $f->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'badge_table_activate'), + $row_id_token + ), + 'badge_table_deactivate' => + $f->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'badge_table_deactivate'), + $row_id_token + ), + 'badge_table_edit' => $f->table()->action()->single( + $this->lng->txt('edit'), + $url_builder->withParameter($action_parameter_token, 'badge_table_edit'), $row_id_token ), - 'badge_table_deactivate' => - $f->table()->action()->multi( - $this->lng->txt('deactivate'), - $url_builder->withParameter($action_parameter_token, 'badge_table_deactivate'), - $row_id_token - ), - 'badge_table_edit' => $f->table()->action()->single( - $this->lng->txt('edit'), - $url_builder->withParameter($action_parameter_token, 'badge_table_edit'), - $row_id_token - ), - 'badge_table_delete' => - $f->table()->action()->standard( - $this->lng->txt('delete'), - $url_builder->withParameter($action_parameter_token, 'badge_table_delete'), - $row_id_token - ), - 'award_revoke_badge' => - $f->table()->action()->single( - $this->lng->txt('badge_award_revoke'), - $url_builder->withParameter($action_parameter_token, 'award_revoke_badge'), - $row_id_token - ) - ]; + 'badge_table_delete' => + $f->table()->action()->standard( + $this->lng->txt('delete'), + $url_builder->withParameter($action_parameter_token, 'badge_table_delete'), + $row_id_token + ), + 'award_revoke_badge' => + $f->table()->action()->single( + $this->lng->txt('badge_award_revoke'), + $url_builder->withParameter($action_parameter_token, 'award_revoke_badge'), + $row_id_token + ) + ]; + } else { + return []; + } + } public function renderTable(): void diff --git a/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php index af9ee10addf9..5c6dab84f14e 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php @@ -47,7 +47,7 @@ class ilBadgeTypesTableGUI private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; - public function __construct() + public function __construct(protected bool $a_has_write = false) { global $DIC; @@ -170,19 +170,24 @@ private function getActions( URLBuilderToken $row_id_token ): array { $f = $this->factory; - return [ - 'badge_type_activate' => $f->table()->action()->multi( - $this->lng->txt('activate'), - $url_builder->withParameter($action_parameter_token, 'badge_type_activate'), - $row_id_token - ), - 'badge_type_deactivate' => - $f->table()->action()->multi( - $this->lng->txt('deactivate'), - $url_builder->withParameter($action_parameter_token, 'badge_type_deactivate'), + if ($this->a_has_write) { + return [ + 'badge_type_activate' => $f->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'badge_type_activate'), $row_id_token - ) - ]; + ), + 'badge_type_deactivate' => + $f->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'badge_type_deactivate'), + $row_id_token + ) + ]; + } else { + return []; + } + } public function renderTable(): void diff --git a/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php b/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php index a9cf96aec60b..2794df7a7292 100755 --- a/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php +++ b/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php @@ -124,6 +124,9 @@ public function executeCommand(): void } elseif ($table_action === 'badge_image_template_delete') { $this->confirmDeleteImageTemplates(); $render_default = false; + } elseif ($table_action === 'obj_badge_delete') { + $this->confirmDeleteObjectBadges(); + $render_default = false; } if ($render_default) { @@ -257,7 +260,7 @@ protected function listTypes(): void $this->assertActive(); $this->tabs_gui->setTabActive('types'); - $tpl = new ilBadgeTypesTableGUI(); + $tpl = new ilBadgeTypesTableGUI($this->access->checkAccess("write", "", $this->object->getRefId())); $tpl->renderTable(); } @@ -341,7 +344,7 @@ protected function listImageTemplates(): void ); } - $template_table = new ilBadgeImageTemplateTableGUI(); + $template_table = new ilBadgeImageTemplateTableGUI($this->access->checkAccess("write", "", $this->object->getRefId())); $template_table->renderTable(); } @@ -651,7 +654,7 @@ protected function listObjectBadges(): void $this->assertActive(); $this->tabs_gui->setTabActive('obj_badges'); - $tbl = new ilObjectBadgeTableGUI($this); + $tbl = new ilObjectBadgeTableGUI($this, $this->access->checkAccess("write", "", $this->object->getRefId())); $tbl->renderTable(); } @@ -720,7 +723,7 @@ protected function confirmDeleteObjectBadges(): void $tpl = $this->tpl; $ilTabs = $this->tabs; - $badge_ids = $this->getObjectBadgesFromMultiAction(); + $badge_ids = $this->badge_request->getMultiActionBadgeIdsFromUrl(); $ilTabs->clearTargets(); $ilTabs->setBackTarget( @@ -734,11 +737,21 @@ protected function confirmDeleteObjectBadges(): void $confirmation_gui->setCancel($lng->txt('cancel'), 'listObjectBadges'); $confirmation_gui->setConfirm($lng->txt('delete'), 'deleteObjectBadges'); + if ($badge_ids === ['ALL_OBJECTS']) { + $badge_ids = []; + $filter = [ + 'type' => '', + 'title' => '', + 'object' => '' + ]; + foreach (ilBadge::getObjectInstances($filter) as $badge_item) { + $badge_ids[] = $badge_item['id']; + } + } foreach ($badge_ids as $badge_id) { $badge = new ilBadge($badge_id); $parent = $badge->getParentMeta(); - // :TODO: container presentation $container = '(' . $parent['type'] . '/' . $parent['id'] . ') ' . $parent['title']; @@ -763,7 +776,7 @@ protected function deleteObjectBadges(): void $ilCtrl = $this->ctrl; $lng = $this->lng; - $badge_ids = $this->getObjectBadgesFromMultiAction(); + $badge_ids = $this->badge_request->getMultiActionBadgeIdsFromPost(); foreach ($badge_ids as $badge_id) { $badge = new ilBadge($badge_id); diff --git a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php index a2d04f556953..e86e24ba5ea3 100755 --- a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php @@ -57,8 +57,10 @@ class ilObjectBadgeTableGUI private readonly ilObjBadgeAdministrationGUI $parent_obj; private readonly ilAccessHandler $access; - public function __construct(ilObjBadgeAdministrationGUI $parentObj) - { + public function __construct( + ilObjBadgeAdministrationGUI $parentObj, + protected bool $has_write = false + ) { global $DIC; $this->lng = $DIC->language(); @@ -287,31 +289,34 @@ private function getActions( URLBuilderToken $row_id_token ): array { $f = $this->factory; - $actions = [ - 'obj_badge_activate' => $f->table()->action()->multi( - $this->lng->txt('activate'), - $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'), - $row_id_token - ), - 'obj_badge_deactivate' => - $f->table()->action()->multi( - $this->lng->txt('deactivate'), - $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'), + $actions = []; + if ($this->has_write) { + $actions = [ + 'obj_badge_activate' => $f->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'), $row_id_token ), - 'obj_badge_delete' => - $f->table()->action()->multi( - $this->lng->txt('delete'), - $url_builder->withParameter($action_parameter_token, 'obj_badge_delete'), - $row_id_token - ), - 'obj_badge_show_users' => - $f->table()->action()->single( - $this->lng->txt('user'), - $url_builder->withParameter($action_parameter_token, 'obj_badge_show_users'), - $row_id_token - ) - ]; + 'obj_badge_deactivate' => + $f->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'), + $row_id_token + ), + 'obj_badge_delete' => + $f->table()->action()->multi( + $this->lng->txt('delete'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_delete'), + $row_id_token + ), + 'obj_badge_show_users' => + $f->table()->action()->single( + $this->lng->txt('user'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_show_users'), + $row_id_token + ) + ]; + } return $actions; } @@ -368,10 +373,22 @@ public function renderTable(): void if ($action === 'obj_badge_delete') { $items = []; if (\is_array($ids) && \count($ids) > 0) { + if ($ids === ['ALL_OBJECTS']) { + $filter = [ + 'type' => '', + 'title' => '', + 'object' => '' + ]; + $ids = []; + foreach (ilBadge::getObjectInstances($filter) as $badge_item) { + $ids[] = $badge_item['id']; + } + } + foreach ($ids as $id) { $badge = new ilBadge((int) $id); $items[] = $f->modal()->interruptiveItem()->keyValue( - $id, + (string) $id, (string) $badge->getId(), $badge->getTitle() );