diff --git a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php index c4ebf0da4885..ae59145f51e2 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeImageTemplateTableGUI.php @@ -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; @@ -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 + */ + 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 - */ - 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 + */ + 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 - */ - 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 + */ + 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')) + ]; } /** @@ -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(); @@ -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() @@ -260,7 +243,7 @@ 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() @@ -268,7 +251,7 @@ public function renderTable(): void } } 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() @@ -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'), '#' @@ -296,6 +279,6 @@ public function renderTable(): void } } - $this->tpl->setContent($r->render($out)); + $this->tpl->setContent($this->renderer->render($out)); } } diff --git a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php index 2a40e91f60db..6e3458af766b 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php @@ -33,12 +33,12 @@ use ILIAS\Badge\ilBadgeImage; use ILIAS\Badge\PresentationHeader; use ILIAS\Badge\Tile; +use ILIAS\UI\Component\Table\Column\Column; class ilBadgePersonalTableGUI implements DataRetrieval { private readonly Factory $factory; private readonly Renderer $renderer; - private readonly \ILIAS\Refinery\Factory $refinery; private readonly ServerRequestInterface|RequestInterface $request; private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; @@ -55,7 +55,6 @@ public function __construct() $this->tpl = $DIC->ui()->mainTemplate(); $this->factory = $DIC->ui()->factory(); $this->renderer = $DIC->ui()->renderer(); - $this->refinery = $DIC->refinery(); $this->request = $DIC->http()->request(); $this->user = $DIC->user(); $this->access = $DIC->access(); @@ -130,10 +129,12 @@ private function getRecords(Range $range = null, Order $order = null): array } $awarded_by = implode(' ', [ - $this->renderer->render($this->factory->symbol()->icon()->standard( - $parent['type'], - $parent['title'] - )), + $this->renderer->render( + $this->factory->symbol()->icon()->standard( + $parent['type'], + $parent['title'] + ) + ), $awarded_by ]); } @@ -194,9 +195,12 @@ private function getRecords(Range $range = null, Order $order = null): array return $rows; } + /** + * @return array + */ private function getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format): array { - $columns = [ + return [ 'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false), 'title' => $this->factory->table()->column()->text($this->lng->txt('title')), 'awarded_by' => $this->factory->table()->column()->text($this->lng->txt('awarded_by')), @@ -213,8 +217,6 @@ private function getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format): arr $this->lng->txt('badge_sort_excluded_from_profile_first') ) ]; - - return $columns; } /** @@ -253,23 +255,23 @@ public function renderTable(): void $url_builder = new URLBuilder($table_uri); $query_params_namespace = ['badge']; - [$url_builder, $action_parameter_token, $row_id_token] = - $url_builder->acquireParameters( - $query_params_namespace, - 'table_action', - 'id' - ); + [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters( + $query_params_namespace, + 'table_action', + 'id', + ); - $table = $this->factory->table() - ->data( - $this->lng->txt('badge_personal_badges'), - $this->getColumns($date_format), - $this - ) - ->withId(self::class) - ->withOrder(new Order('title', Order::ASC)) - ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token)) - ->withRequest($this->request); + $table = $this->factory + ->table() + ->data( + $this->lng->txt('badge_personal_badges'), + $this->getColumns($date_format), + $this + ) + ->withId(self::class) + ->withOrder(new Order('title', Order::ASC)) + ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token)) + ->withRequest($this->request); $pres = new PresentationHeader($this->dic, ilBadgeProfileGUI::class); $pres->show($this->lng->txt('table_view')); diff --git a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php index d74db152bd5e..049add1e2bfc 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php @@ -38,7 +38,7 @@ use ILIAS\Filesystem\Stream\Streams; use ILIAS\UI\Component\Table\Column\Column; -class ilBadgeTableGUI +class ilBadgeTableGUI implements DataRetrieval { private readonly Factory $factory; private readonly Renderer $renderer; @@ -49,6 +49,7 @@ class ilBadgeTableGUI private readonly string $parent_type; private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; + private ilBadgeImage $badge_image_service; public function __construct(int $parent_obj_id, string $parent_obj_type, protected bool $has_write = false) { @@ -64,193 +65,188 @@ public function __construct(int $parent_obj_id, string $parent_obj_type, protect $this->parent_id = $parent_obj_id; $this->parent_type = $parent_obj_type; + $this->badge_image_service = new ilBadgeImage( + $DIC->resourceStorage(), + $DIC->upload(), + $DIC->ui()->mainTemplate() + ); } /** - * @return array + * @return list */ - private function buildColumns(): array + private function getBadges(): array { - $column = $this->factory->table()->column(); - $lng = $this->lng; + $rows = []; + $modal_container = new ModalBuilder(); - return [ - 'image' => $column->text($lng->txt('image'))->withIsSortable(false), - 'title' => $column->text($lng->txt('title')), - 'type' => $column->text($lng->txt('type')), - 'active' => $column->boolean($lng->txt('active'), $lng->txt('yes'), $lng->txt('no')), - ]; - } + foreach (ilBadge::getInstancesByParentId($this->parent_id) as $badge) { + $images = [ + 'rendered' => null, + 'large' => null, + ]; + $image_src = $this->badge_image_service->getImageFromBadge($badge); + if ($image_src !== '') { + $images['rendered'] = $this->renderer->render( + $this->factory->image()->responsive( + $image_src, + $badge->getTitle() + ) + ); - private function buildDataRetrievalObject(Factory $f, Renderer $r, int $p, string $type): DataRetrieval - { - return new class ($f, $r, $p, $type) implements DataRetrieval { - private ilBadgeImage $badge_image_service; - - public function __construct( - private readonly Factory $ui_factory, - private readonly Renderer $ui_renderer, - private readonly int $parent_id, - private readonly string $parent_type - ) { - global $DIC; - $this->badge_image_service = new ilBadgeImage( - $DIC->resourceStorage(), - $DIC->upload(), - $DIC->ui()->mainTemplate() + $image_src_large = $this->badge_image_service->getImageFromBadge( + $badge, + ilBadgeImage::IMAGE_SIZE_XL ); + if ($image_src_large !== '') { + $images['large'] = $this->factory->image()->responsive( + $image_src_large, + $badge->getTitle() + ); + } } - /** - * @return list - */ - private function getBadges(): array - { - $rows = []; - $modal_container = new ModalBuilder(); - - foreach (ilBadge::getInstancesByParentId($this->parent_id) as $badge) { - $images = [ - 'rendered' => null, - 'large' => null, - ]; - $image_src = $this->badge_image_service->getImageFromBadge($badge); - if ($image_src !== '') { - $images['rendered'] = $this->ui_renderer->render( - $this->ui_factory->image()->responsive( - $image_src, - $badge->getTitle() - ) - ); + $modal = $modal_container->constructModal( + $images['large'], + $badge->getTitle(), + [ + 'description' => $badge->getDescription(), + 'badge_criteria' => $badge->getCriteria(), + ] + ); - $image_src_large = $this->badge_image_service->getImageFromBadge( - $badge, - ilBadgeImage::IMAGE_SIZE_XL - ); - if ($image_src_large !== '') { - $images['large'] = $this->ui_factory->image()->responsive( - $image_src_large, - $badge->getTitle() - ); - } - } + $rows[] = [ + 'id' => $badge->getId(), + 'badge' => $badge, + 'active' => $badge->isActive(), + 'type' => $this->parent_type !== 'bdga' + ? ilBadge::getExtendedTypeCaption($badge->getTypeInstance()) + : $badge->getTypeInstance()->getCaption(), + 'manual' => !$badge->getTypeInstance() instanceof ilBadgeAuto, + 'image' => $images['rendered'] ? ($modal_container->renderShyButton( + $images['rendered'], + $modal + ) . ' ') : '', + 'title' => implode('', [ + $modal_container->renderShyButton($badge->getTitle(), $modal), + $modal_container->renderModal($modal) + ]), + 'title_sortable' => $badge->getTitle() + ]; + } - $modal = $modal_container->constructModal( - $images['large'], - $badge->getTitle(), - [ - 'description' => $badge->getDescription(), - 'badge_criteria' => $badge->getCriteria(), - ] - ); + return $rows; + } - $rows[] = [ - 'id' => $badge->getId(), - 'badge' => $badge, - 'active' => $badge->isActive(), - 'type' => $this->parent_type !== 'bdga' - ? ilBadge::getExtendedTypeCaption($badge->getTypeInstance()) - : $badge->getTypeInstance()->getCaption(), - 'manual' => !$badge->getTypeInstance() instanceof ilBadgeAuto, - 'image' => $images['rendered'] ? ($modal_container->renderShyButton( - $images['rendered'], - $modal - ) . ' ') : '', - 'title' => implode('', [ - $modal_container->renderShyButton($badge->getTitle(), $modal), - $modal_container->renderModal($modal) - ]), - 'title_sortable' => $badge->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) + ->withDisabledAction( + 'award_revoke_badge', + !$record['manual'] || !$record['active'] + ); + } + } - 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) - ->withDisabledAction( - 'award_revoke_badge', - !$record['manual'] || !$record['active'] - ); - } - } + /** + * @return list + */ + private function getRecords(Range $range = null, Order $order = null): array + { + $rows = $this->getBadges(); - 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 - */ - private function getRecords(Range $range = null, Order $order = null): array - { - $rows = $this->getBadges(); - - 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 (\in_array($order_field, ['title', 'type'], true)) { - if ($order_field === 'title') { - $order_field .= '_sortable'; - } - - return \ilStr::strCmp( - $left[$order_field], - $right[$order_field] - ); - } - - return $left[$order_field] <=> $right[$order_field]; + usort( + $rows, + static function (array $left, array $right) use ($order_field): int { + if (\in_array($order_field, ['title', 'type'], true)) { + if ($order_field === 'title') { + $order_field .= '_sortable'; } - ); - if ($order_direction === Order::DESC) { - $rows = array_reverse($rows); + + return \ilStr::strCmp( + $left[$order_field], + $right[$order_field] + ); } - } - if ($range) { - $rows = \array_slice($rows, $range->getStart(), $range->getLength()); + if ($order_field === 'active') { + return $right[$order_field] <=> $left[$order_field]; + } + + 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 + */ + private 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')), + 'type' => $this->factory->table()->column()->text($this->lng->txt('type')), + 'active' => $this->factory->table()->column()->boolean( + $this->lng->txt('active'), + $this->lng->txt('yes'), + $this->lng->txt('no') + )->withOrderingLabels( + $this->lng->txt('badge_sort_active_badges_first'), + $this->lng->txt('badge_sort_active_badges_last') + ) + ]; } /** @@ -261,98 +257,84 @@ private function getActions( URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token, ): array { - $f = $this->factory; - - 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'), + return $this->has_write ? [ + 'badge_table_activate' => + $this->factory->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'badge_table_activate'), $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 - ) - ]; - } - - return []; + 'badge_table_deactivate' => + $this->factory->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'badge_table_deactivate'), + $row_id_token + ), + 'badge_table_edit' => $this->factory->table()->action()->single( + $this->lng->txt('edit'), + $url_builder->withParameter($action_parameter_token, 'badge_table_edit'), + $row_id_token + ), + 'badge_table_delete' => + $this->factory->table()->action()->standard( + $this->lng->txt('delete'), + $url_builder->withParameter($action_parameter_token, 'badge_table_delete'), + $row_id_token + ), + 'award_revoke_badge' => + $this->factory->table()->action()->single( + $this->lng->txt('badge_award_revoke'), + $url_builder->withParameter($action_parameter_token, 'award_revoke_badge'), + $row_id_token + ) + ] : []; } public function renderTable(): void { - $f = $this->factory; - $r = $this->renderer; $df = new \ILIAS\Data\Factory(); - $refinery = $this->refinery; - $request = $this->request; - - $columns = $this->buildColumns(); - $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' - ); - - $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->parent_id, $this->parent_type); - $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token); - $table = $f->table() - ->data($this->lng->txt('obj_bdga'), $columns, $data_retrieval) - ->withId(self::class . '_' . $this->parent_id) - ->withOrder(new Order('title', Order::ASC)) - ->withActions($actions) - ->withRequest($request); + [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters( + $query_params_namespace, + 'table_action', + 'id', + ); + + $table = $this->factory + ->table() + ->data($this->lng->txt('obj_bdga'), $this->getColumns(), $this) + ->withId(self::class . '_' . $this->parent_id) + ->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(); if ($query->has($action_parameter_token->getName())) { - $action = $query->retrieve($action_parameter_token->getName(), $refinery->to()->string()); - $ids = $query->retrieve($row_id_token->getName(), $refinery->custom()->transformation(fn($v) => $v)); + $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->to()->string()); + $ids = $query->retrieve($row_id_token->getName(), $this->refinery->custom()->transformation(fn($v) => $v)); if ($action === 'delete') { $items = []; foreach ($ids as $id) { - $items[] = $f->modal()->interruptiveItem()->keyValue($id, $row_id_token->getName(), $id); + $items[] = $this->factory->modal()->interruptiveItem()->keyValue( + $id, + $row_id_token->getName(), + $id + ); } $this->http->saveResponse( $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'), '#' @@ -365,6 +347,6 @@ public function renderTable(): void } } - $this->tpl->setContent($r->render($out)); + $this->tpl->setContent($this->renderer->render($out)); } } diff --git a/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php index 63d0305577e1..16834cbbec90 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeTypesTableGUI.php @@ -36,6 +36,7 @@ use ILIAS\UI\URLBuilderToken; use ilBadgeHandler; use ilBadgeAuto; +use ILIAS\UI\Component\Table\Column\Column; class ilBadgeTypesTableGUI implements DataRetrieval { @@ -153,9 +154,12 @@ private function getRecords(Range $range = null, Order $order = null): array return $rows; } + /** + * @return array + */ private function getColumns(): array { - $columns = [ + return [ 'name' => $this->factory->table()->column()->text($this->lng->txt('name')), 'comp' => $this->factory->table()->column()->text($this->lng->txt('cmps_component')), 'manual' => $this->factory->table()->column()->boolean( @@ -181,10 +185,8 @@ private function getColumns(): array )->withOrderingLabels( $this->lng->txt('badge_sort_active_badges_first'), $this->lng->txt('badge_sort_active_badges_last') - ), + ) ]; - - return $columns; } /** @@ -195,23 +197,19 @@ private function getActions( URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token ): array { - if ($this->a_has_write) { - return [ - 'badge_type_activate' => $this->factory->table()->action()->multi( - $this->lng->txt('activate'), - $url_builder->withParameter($action_parameter_token, 'badge_type_activate'), + return $this->a_has_write ? [ + 'badge_type_activate' => $this->factory->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'badge_type_activate'), + $row_id_token + ), + 'badge_type_deactivate' => + $this->factory->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'badge_type_deactivate'), $row_id_token - ), - 'badge_type_deactivate' => - $this->factory->table()->action()->multi( - $this->lng->txt('deactivate'), - $url_builder->withParameter($action_parameter_token, 'badge_type_deactivate'), - $row_id_token - ) - ]; - } - - return []; + ) + ] : []; } public function renderTable(): void @@ -222,19 +220,19 @@ public function renderTable(): void $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', - ); - - $table = $this->factory->table() - ->data($this->lng->txt('badge_types'), $this->getColumns(), $this) - ->withId(self::class) - ->withOrder(new Order('name', Order::ASC)) - ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token)) - ->withRequest($this->request); + [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters( + $query_params_namespace, + 'table_action', + 'id', + ); + + $table = $this->factory + ->table() + ->data($this->lng->txt('badge_types'), $this->getColumns(), $this) + ->withId(self::class) + ->withOrder(new Order('name', Order::ASC)) + ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token)) + ->withRequest($this->request); $out = [$table]; diff --git a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php index a6911cbcb689..d2bc04459c05 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php @@ -44,8 +44,10 @@ use ilCalendarSettings; use ilObjUser; use ILIAS\Data\DateFormat\DateFormat; +use ILIAS\UI\Component\Table\Column\Column; +use ILIAS\UI\Component\Table\Action\Action; -class ilBadgeUserTableGUI +class ilBadgeUserTableGUI implements DataRetrieval { private readonly Factory $factory; private readonly Renderer $renderer; @@ -55,6 +57,7 @@ class ilBadgeUserTableGUI private readonly ilTree $tree; private readonly ilObjUser $user; private DateFormat $date_format; + private bool $is_container_context = false; public function __construct( private readonly ?int $parent_ref_id = null, @@ -71,299 +74,275 @@ public function __construct( $this->request = $DIC->http()->request(); $this->tree = $DIC->repositoryTree(); $this->user = $DIC->user(); + + if ($this->parent_ref_id) { + $parent_type = ilObject::_lookupType($this->parent_ref_id, true); + if (\in_array($parent_type, ['grp', 'crs'], true)) { + $this->is_container_context = $this->parent_obj_id === null && $this->award_badge === null; + } + } } - private function buildDataRetrievalObject( - bool $is_container_context, - Factory $f, - Renderer $r, - ilLanguage $lng, - ilTree $tree, - ilObjUser $user, - DateFormat $date_format, - ?int $parent_ref_id, - ?int $parent_obj_id, - ?int $restrict_badge_id, - ?ilBadge $award_badge - ): DataRetrieval { - return new class ( - $is_container_context, - $f, - $r, - $lng, - $tree, - $user, - $date_format, - $parent_ref_id, - $parent_obj_id, - $restrict_badge_id, - $award_badge - ) implements - DataRetrieval { - public function __construct( - private readonly bool $is_container_context, - private readonly Factory $ui_factory, - private readonly Renderer $ui_renderer, - private readonly ilLanguage $lng, - private readonly ilTree $tree, - private readonly ilObjUser $user, - private readonly DateFormat $date_format, - private readonly ?int $parent_ref_id, - private readonly ?int $parent_obj_id, - private readonly ?int $restrict_badge_id, - private readonly ?ilBadge $award_badge - ) { + /** + * @return list + */ + private function getBadgeImageTemplates(): array + { + /** @var array> $assignments */ + $assignments = []; + $user_ids = []; + $rows = []; + $badges = []; + + $parent_obj_id = $this->parent_obj_id; + if (!$parent_obj_id && $this->parent_ref_id) { + $parent_obj_id = ilObject::_lookupObjId($this->parent_ref_id); + } + + if ($this->parent_ref_id) { + $user_ids = ilBadgeHandler::getInstance()->getUserIds($this->parent_ref_id, $parent_obj_id); + } + + $obj_ids = [$parent_obj_id]; + if ($this->is_container_context) { + foreach ($this->tree->getSubTree($this->tree->getNodeData($this->parent_ref_id)) as $node) { + $obj_ids[] = (int) $node['obj_id']; } + $obj_ids = array_unique($obj_ids); + } - /** - * @return list - */ - private function getBadgeImageTemplates(): array - { - /** @var array> $assignments */ - $assignments = []; - $user_ids = []; - $rows = []; - $badges = []; - - $parent_obj_id = $this->parent_obj_id; - if (!$parent_obj_id && $this->parent_ref_id) { - $parent_obj_id = ilObject::_lookupObjId($this->parent_ref_id); - } + foreach ($obj_ids as $obj_id) { + foreach (ilBadge::getInstancesByParentId($obj_id) as $badge) { + $badges[$badge->getId()] = $badge; + } - if ($this->parent_ref_id) { - $user_ids = ilBadgeHandler::getInstance()->getUserIds($this->parent_ref_id, $parent_obj_id); + foreach (ilBadgeAssignment::getInstancesByParentId($obj_id) as $ass) { + if ($this->restrict_badge_id && $this->restrict_badge_id !== $ass->getBadgeId()) { + continue; } - $obj_ids = [$parent_obj_id]; - if ($this->is_container_context) { - foreach ($this->tree->getSubTree($this->tree->getNodeData($this->parent_ref_id)) as $node) { - $obj_ids[] = (int) $node['obj_id']; - } - $obj_ids = array_unique($obj_ids); + if ($this->award_badge instanceof ilBadge && + $ass->getBadgeId() !== $this->award_badge->getId()) { + continue; } - foreach ($obj_ids as $obj_id) { - foreach (ilBadge::getInstancesByParentId($obj_id) as $badge) { - $badges[$badge->getId()] = $badge; - } + $assignments[$ass->getUserId()][] = $ass; + } + } - foreach (ilBadgeAssignment::getInstancesByParentId($obj_id) as $ass) { - if ($this->restrict_badge_id && $this->restrict_badge_id !== $ass->getBadgeId()) { - continue; - } + if (!$user_ids) { + $user_ids = array_keys($assignments); + } - if ($this->award_badge instanceof ilBadge && - $ass->getBadgeId() !== $this->award_badge->getId()) { - continue; - } + $tmp['set'] = []; + if (\count($user_ids) > 0) { + $uquery = new ilUserQuery(); + $uquery->setLimit(9999); + $uquery->setUserFilter($user_ids); + $tmp = $uquery->query(); + } - $assignments[$ass->getUserId()][] = $ass; + foreach ($tmp['set'] as $user) { + if (\array_key_exists($user['usr_id'], $assignments)) { + foreach ($assignments[$user['usr_id']] as $user_ass) { + $idx = $user_ass->getBadgeId() . '_' . $user_ass->getUserId(); + + $badge = $badges[$user_ass->getBadgeId()]; + + $parent = null; + $paren_sortable = null; + if ($this->is_container_context) { + $parent_metadata = $badge->getParentMeta(); + + $parent = implode(' ', [ + $this->renderer->render( + $this->factory->symbol()->icon()->custom( + ilObject::_getIcon($parent_metadata['id'], 'big', $parent_metadata['type']), + $this->lng->txt('obj_' . $parent_metadata['type']) + ) + ), + $parent_metadata['title'] + ]); + $paren_sortable = $parent_metadata['title']; } - } - if (!$user_ids) { - $user_ids = array_keys($assignments); + $rows[] = [ + 'id' => $idx, + 'name' => $user['lastname'] . ', ' . $user['firstname'], + 'login' => $user['login'], + 'type' => ilBadge::getExtendedTypeCaption($badge->getTypeInstance()), + 'title' => $badge->getTitle(), + 'issued' => (new DateTimeImmutable()) + ->setTimestamp($user_ass->getTimestamp()) + ->setTimezone(new \DateTimeZone($this->user->getTimeZone())) + ->format($this->date_format->toString()), + 'issued_sortable' => (new DateTimeImmutable()) + ->setTimestamp($user_ass->getTimestamp()) + ->setTimezone(new \DateTimeZone($this->user->getTimeZone())), + 'parent' => $parent, + 'parent_sortable' => $paren_sortable, + ]; } + } elseif ($this->award_badge) { + $idx = $this->award_badge->getId() . '_' . $user['usr_id']; + + $rows[] = [ + 'id' => $idx, + 'name' => $user['lastname'] . ', ' . $user['firstname'], + 'login' => $user['login'], + 'type' => '', + 'title' => '', + 'issued' => null, + 'issued_sortable' => null, + 'parent' => null, + 'parent_sortable' => null, + ]; + } + } - $tmp['set'] = []; - if (\count($user_ids) > 0) { - $uquery = new ilUserQuery(); - $uquery->setLimit(9999); - $uquery->setUserFilter($user_ids); - $tmp = $uquery->query(); - } + return $rows; + } - foreach ($tmp['set'] as $user) { - if (\array_key_exists($user['usr_id'], $assignments)) { - foreach ($assignments[$user['usr_id']] as $user_ass) { - $idx = $user_ass->getBadgeId() . '_' . $user_ass->getUserId(); - - $badge = $badges[$user_ass->getBadgeId()]; - - $parent = null; - $paren_sortable = null; - if ($this->is_container_context) { - $parent_metadata = $badge->getParentMeta(); - - $parent = implode(' ', [ - $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom( - ilObject::_getIcon($parent_metadata['id'], 'big', $parent_metadata['type']), - $this->lng->txt('obj_' . $parent_metadata['type']) - )), - $parent_metadata['title'] - ]); - $paren_sortable = $parent_metadata['title']; - } - - $rows[] = [ - 'id' => $idx, - 'name' => $user['lastname'] . ', ' . $user['firstname'], - 'login' => $user['login'], - 'type' => ilBadge::getExtendedTypeCaption($badge->getTypeInstance()), - 'title' => $badge->getTitle(), - 'issued' => (new DateTimeImmutable()) - ->setTimestamp($user_ass->getTimestamp()) - ->setTimezone(new \DateTimeZone($this->user->getTimeZone())) - ->format($this->date_format->toString()), - 'issued_sortable' => (new DateTimeImmutable()) - ->setTimestamp($user_ass->getTimestamp()) - ->setTimezone(new \DateTimeZone($this->user->getTimeZone())), - 'parent' => $parent, - 'parent_sortable' => $paren_sortable, - ]; + 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) { + yield $row_builder->buildDataRow($record['id'], $record)->withDisabledAction( + 'badge_award_badge', + $record['issued'] === null + )->withDisabledAction( + 'badge_revoke_badge', + $record['issued'] !== null + ); + } + } + + public function getTotalRowCount( + ?array $filter_data, + ?array $additional_parameters + ): ?int { + return \count($this->getRecords()); + } + + /** + * @return list + */ + 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 (\in_array($order_field, ['name', 'login', 'type', 'title', 'parent'], true)) { + if ($order_field === 'parent') { + $order_field .= '_sortable'; } - } elseif ($this->award_badge) { - $idx = $this->award_badge->getId() . '_' . $user['usr_id']; - - $rows[] = [ - 'id' => $idx, - 'name' => $user['lastname'] . ', ' . $user['firstname'], - 'login' => $user['login'], - 'type' => '', - 'title' => '', - 'issued' => null, - 'issued_sortable' => null, - 'parent' => null, - 'parent_sortable' => null, - ]; + + return \ilStr::strCmp( + $left[$order_field] ?? '', + $right[$order_field] ?? '' + ); } - } - return $rows; - } + if ($order_field === 'issued') { + $order_field .= '_sortable'; + return $left[$order_field] <=> $right[$order_field]; + } - 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) { - yield $row_builder->buildDataRow($record['id'], $record)->withDisabledAction( - 'badge_award_badge', - $record['issued'] === null - )->withDisabledAction( - 'badge_revoke_badge', - $record['issued'] !== null - ); + return $left[$order_field] <=> $right[$order_field]; } + ); + if ($order_direction === ORDER::DESC) { + $rows = array_reverse($rows); } + } - public function getTotalRowCount( - ?array $filter_data, - ?array $additional_parameters - ): ?int { - return \count($this->getRecords()); - } + if ($range) { + $rows = \array_slice($rows, $range->getStart(), $range->getLength()); + } - /** - * @return list - */ - 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 (\in_array($order_field, ['name', 'login', 'type', 'title', 'parent'], true)) { - if ($order_field === 'parent') { - $order_field .= '_sortable'; - } - - return \ilStr::strCmp( - $left[$order_field] ?? '', - $right[$order_field ] ?? '' - ); - } - - if ($order_field === 'issued') { - $order_field .= '_sortable'; - return $left[$order_field] <=> $right[$order_field]; - } - - return $left[$order_field] <=> $right[$order_field]; - } - ); - if ($order_direction === ORDER::DESC) { - $rows = array_reverse($rows); - } - } + return $rows; + } - if ($range) { - $rows = \array_slice($rows, $range->getStart(), $range->getLength()); - } + /** + * @return array + */ + public function getColumns(): array + { + $columns = [ + 'name' => $this->factory->table()->column()->text($this->lng->txt('name')), + 'login' => $this->factory->table()->column()->text($this->lng->txt('login')), + 'type' => $this->factory->table()->column()->text($this->lng->txt('type')), + 'title' => $this->factory->table()->column()->text($this->lng->txt('title')), + // Cannot be a date column, because when awarding/revoking badges for uses, the list items may contain NULL values for `issued` + 'issued' => $this->factory->table()->column()->text($this->lng->txt('badge_issued_on')) + ]; - return $rows; - } - }; + if ($this->is_container_context) { + $columns['parent'] = $this->factory->table()->column()->text($this->lng->txt('object')); + } + + return $columns; } /** - * @return array + * @return array */ private function getActions( URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token, ): array { - if ($this->award_badge instanceof ilBadge) { - $f = $this->factory; - - return [ - 'badge_award_badge' => - $f->table()->action()->multi( - $this->lng->txt('badge_award_badge'), - $url_builder->withParameter($action_parameter_token, 'assignBadge'), - $row_id_token - ), - 'badge_revoke_badge' => - $f->table()->action()->multi( - $this->lng->txt('badge_remove_badge'), - $url_builder->withParameter($action_parameter_token, 'revokeBadge'), - $row_id_token - ), - ]; - } - - return []; + return ($this->award_badge instanceof ilBadge) ? [ + 'badge_award_badge' => + $this->factory->table()->action()->multi( + $this->lng->txt('badge_award_badge'), + $url_builder->withParameter($action_parameter_token, 'assignBadge'), + $row_id_token + ), + 'badge_revoke_badge' => + $this->factory->table()->action()->multi( + $this->lng->txt('badge_remove_badge'), + $url_builder->withParameter($action_parameter_token, 'revokeBadge'), + $row_id_token + ) + ] : []; } public function renderTable(): void { - $f = $this->factory; - $r = $this->renderer; - $request = $this->request; - $df = new \ILIAS\Data\Factory(); if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) { $this->date_format = $df->dateFormat()->withTime12($this->user->getDateFormat()); @@ -371,24 +350,15 @@ public function renderTable(): void $this->date_format = $df->dateFormat()->withTime24($this->user->getDateFormat()); } - $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', - ); - - $is_container_context = false; - if ($this->parent_ref_id) { - $parent_type = ilObject::_lookupType($this->parent_ref_id, true); - if (\in_array($parent_type, ['grp', 'crs'], true)) { - $is_container_context = $this->parent_obj_id === null && $this->award_badge === null; - } - } + [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters( + $query_params_namespace, + 'table_action', + 'id', + ); if ($this->award_badge instanceof ilBadge) { $title = $this->lng->txt('badge_award_badge') . ': ' . $this->award_badge->getTitle(); @@ -413,44 +383,16 @@ public function renderTable(): void $title = $parent . $this->lng->txt('users'); } - $data_retrieval = $this->buildDataRetrievalObject( - $is_container_context, - $f, - $r, - $this->lng, - $this->tree, - $this->user, - $this->date_format, - $this->parent_ref_id, - $this->parent_obj_id, - $this->restrict_badge_id, - $this->award_badge - ); - - $columns = [ - 'name' => $f->table()->column()->text($this->lng->txt('name')), - 'login' => $f->table()->column()->text($this->lng->txt('login')), - 'type' => $f->table()->column()->text($this->lng->txt('type')), - 'title' => $f->table()->column()->text($this->lng->txt('title')), - // Cannot be a date column, because when awarding/revoking badges for uses, the list items may contain NULL values for `issued` - 'issued' => $f->table()->column()->text($this->lng->txt('badge_issued_on')) - ]; - - if ($is_container_context) { - $columns['parent'] = $f->table()->column()->text($this->lng->txt('object')); - } - - $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token); - - $table = $f->table() - ->data($title, $columns, $data_retrieval) - ->withId(self::class . '_' . $this->parent_ref_id) - ->withOrder(new Order('name', Order::ASC)) - ->withActions($actions) - ->withRequest($request); + $table = $this->factory + ->table() + ->data($title, $this->getColumns(), $this) + ->withId(self::class . '_' . $this->parent_ref_id) + ->withOrder(new Order('name', Order::ASC)) + ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token)) + ->withRequest($this->request); $out = [$table]; - $this->tpl->setContent($r->render($out)); + $this->tpl->setContent($this->renderer->render($out)); } } diff --git a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php index a61f3f4cf147..9cd69a8fe7a0 100755 --- a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php @@ -44,8 +44,9 @@ use ILIAS\Filesystem\Stream\Streams; use ILIAS\UI\Component\Table\Action\Action; use ilAccessHandler; +use ILIAS\UI\Component\Table\Column\Column; -class ilObjectBadgeTableGUI +class ilObjectBadgeTableGUI implements DataRetrieval { private readonly Factory $factory; private readonly Renderer $renderer; @@ -56,6 +57,7 @@ class ilObjectBadgeTableGUI private readonly ilGlobalTemplateInterface $tpl; private readonly ilObjBadgeAdministrationGUI $parent_obj; private readonly ilAccessHandler $access; + private readonly ilBadgeImage $badge_image_service; public function __construct( ilObjBadgeAdministrationGUI $parentObj, @@ -72,210 +74,209 @@ public function __construct( $this->http = $DIC->http(); $this->access = $DIC->access(); $this->parent_obj = $parentObj; + $this->badge_image_service = new ilBadgeImage( + $DIC->resourceStorage(), + $DIC->upload(), + $DIC->ui()->mainTemplate() + ); } - private function buildDataRetrievalObject( - Factory $f, - Renderer $r, - ilAccessHandler $access, - ilObjBadgeAdministrationGUI $p - ): DataRetrieval { - return new class ($f, $r, $access, $p) implements DataRetrieval { - private readonly ilBadgeImage $badge_image_service; - private readonly ilLanguage $lng; - - public function __construct( - private readonly Factory $ui_factory, - private readonly Renderer $ui_renderer, - private readonly ilAccessHandler $access, - private readonly ilObjBadgeAdministrationGUI $parent - ) { - global $DIC; - - $this->badge_image_service = new ilBadgeImage( - $DIC->resourceStorage(), - $DIC->upload(), - $DIC->ui()->mainTemplate() + 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) { + yield $row_builder->buildDataRow((string) $record['id'], $record); + } + } + + public function getTotalRowCount( + ?array $filter_data, + ?array $additional_parameters + ): ?int { + return \count($this->getRecords()); + } + + /** + * @return list + */ + private function getRecords(Range $range = null, Order $order = null): array + { + $container_deleted_title_part = '' . $this->lng->txt('deleted') . ''; + $modal_container = new ModalBuilder(); + + // A filter is not implemented, yet + $filter = [ + 'type' => '', + 'title' => '', + 'object' => '' + ]; + + $types = ilBadgeHandler::getInstance()->getAvailableTypes(false); + $rows = []; + foreach (ilBadge::getObjectInstances($filter) as $badge_item) { + $type_caption = ilBadge::getExtendedTypeCaption($types[$badge_item['type_id']]); + + $badge = new ilBadge(0); + $badge->setId($badge_item['id']); + $badge->setImageRid($badge_item['image_rid']); + $badge->setImage($badge_item['image']); + + $images = [ + 'rendered' => null, + 'large' => null, + ]; + $image_src = $this->badge_image_service->getImageFromResourceId($badge); + if ($image_src !== '') { + $images['rendered'] = $this->renderer->render( + $this->factory->image()->responsive( + $image_src, + $badge_item['title'] + ) ); - $this->lng = $DIC->language(); - } - 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) { - yield $row_builder->buildDataRow((string) $record['id'], $record); + $image_src_large = $this->badge_image_service->getImageFromResourceId( + $badge, + ilBadgeImage::IMAGE_SIZE_XL + ); + if ($image_src_large !== '') { + $images['large'] = $this->factory->image()->responsive( + $image_src_large, + $badge_item['title'] + ); } } - public function getTotalRowCount( - ?array $filter_data, - ?array $additional_parameters - ): ?int { - return \count($this->getRecords()); + $sortable_container_title_parts = [ + 'title' => $badge_item['parent_title'] ?? '' + ]; + $container_title_parts = [ + 'icon' => $this->renderer->render( + $this->factory->symbol()->icon()->custom( + ilObject::_getIcon($badge_item['parent_id'], 'big', $badge_item['parent_type'] ?? ''), + $this->lng->txt('obj_' . ($badge_item['parent_type'] ?? '')) + ) + ), + 'title' => $sortable_container_title_parts['title'], + ]; + + if ($badge_item['deleted']) { + $container_title_parts['suffix'] = $container_deleted_title_part; + $sortable_container_title_parts['suffix'] = $container_deleted_title_part; + } else { + $ref_ids = ilObject::_getAllReferences($badge_item['parent_id']); + $ref_id = array_shift($ref_ids); + if ($ref_id && $this->access->checkAccess('read', '', $ref_id)) { + $container_title_parts['title'] = $this->renderer->render( + new Standard( + $container_title_parts['title'], + (string) new URI(ilLink::_getLink($ref_id)) + ) + ); + } else { + $container_title_parts['suffix'] = $container_deleted_title_part; + $sortable_container_title_parts['suffix'] = $container_deleted_title_part; + } } - /** - * @return list - */ - private function getRecords(Range $range = null, Order $order = null): array - { - $container_deleted_title_part = '' . $this->lng->txt('deleted') . ''; - $modal_container = new ModalBuilder(); - - // A filter is not implemented, yet - $filter = [ - 'type' => '', - 'title' => '', - 'object' => '' - ]; - - $types = ilBadgeHandler::getInstance()->getAvailableTypes(false); - $rows = []; - foreach (ilBadge::getObjectInstances($filter) as $badge_item) { - $type_caption = ilBadge::getExtendedTypeCaption($types[$badge_item['type_id']]); - - $badge = new ilBadge(0); - $badge->setId($badge_item['id']); - $badge->setImageRid($badge_item['image_rid']); - $badge->setImage($badge_item['image']); - - $images = [ - 'rendered' => null, - 'large' => null, - ]; - $image_src = $this->badge_image_service->getImageFromResourceId($badge); - if ($image_src !== '') { - $images['rendered'] = $this->ui_renderer->render( - $this->ui_factory->image()->responsive( - $image_src, - $badge_item['title'] - ) - ); + $modal = $modal_container->constructModal( + $images['large'], + $badge_item['title'], + [ + 'active' => $badge_item['active'] ? $this->lng->txt('yes') : $this->lng->txt('no'), + 'type' => $type_caption, + 'container' => implode(' ', \array_slice($container_title_parts, 1, null, true)), + ] + ); - $image_src_large = $this->badge_image_service->getImageFromResourceId( - $badge, - ilBadgeImage::IMAGE_SIZE_XL - ); - if ($image_src_large !== '') { - $images['large'] = $this->ui_factory->image()->responsive( - $image_src_large, - $badge_item['title'] - ); - } - } + $rows[] = [ + 'id' => $badge_item['id'], + 'active' => (bool) $badge_item['active'], + 'type' => $type_caption, + 'image' => $images['rendered'] ? ($modal_container->renderShyButton( + $images['rendered'], + $modal + ) . ' ') : '', + 'title' => implode('', [ + $modal_container->renderShyButton($badge_item['title'], $modal), + $modal_container->renderModal($modal) + ]), + 'title_sortable' => $badge_item['title'], + 'container' => implode(' ', $container_title_parts), + 'container_sortable' => implode(' ', $sortable_container_title_parts), + ]; + } - $sortable_container_title_parts = [ - 'title' => $badge_item['parent_title'] ?? '' - ]; - $container_title_parts = [ - 'icon' => $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom( - ilObject::_getIcon($badge_item['parent_id'], 'big', $badge_item['parent_type'] ?? ''), - $this->lng->txt('obj_' . ($badge_item['parent_type'] ?? '')) - )), - 'title' => $sortable_container_title_parts['title'], - ]; - - if ($badge_item['deleted']) { - $container_title_parts['suffix'] = $container_deleted_title_part; - $sortable_container_title_parts['suffix'] = $container_deleted_title_part; - } else { - $ref_ids = ilObject::_getAllReferences($badge_item['parent_id']); - $ref_id = array_shift($ref_ids); - if ($ref_id && $this->access->checkAccess('read', '', $ref_id)) { - $container_title_parts['title'] = $this->ui_renderer->render( - new Standard( - $container_title_parts['title'], - (string) new URI(ilLink::_getLink($ref_id)) - ) - ); - } else { - $container_title_parts['suffix'] = $container_deleted_title_part; - $sortable_container_title_parts['suffix'] = $container_deleted_title_part; - } + 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 (\in_array($order_field, ['container', 'title', 'type'], true)) { + if (\in_array($order_field, ['container', 'title'], true)) { + $order_field .= '_sortable'; } - $modal = $modal_container->constructModal( - $images['large'], - $badge_item['title'], - [ - 'active' => $badge_item['active'] ? $this->lng->txt('yes') : $this->lng->txt('no'), - 'type' => $type_caption, - 'container' => implode(' ', \array_slice($container_title_parts, 1, null, true)), - ] + return \ilStr::strCmp( + $left[$order_field], + $right[$order_field] ); - - $rows[] = [ - 'id' => $badge_item['id'], - 'active' => (bool) $badge_item['active'], - 'type' => $type_caption, - 'image' => $images['rendered'] ? ($modal_container->renderShyButton( - $images['rendered'], - $modal - ) . ' ') : '', - 'title' => implode('', [ - $modal_container->renderShyButton($badge_item['title'], $modal), - $modal_container->renderModal($modal) - ]), - 'title_sortable' => $badge_item['title'], - 'container' => implode(' ', $container_title_parts), - 'container_sortable' => implode(' ', $sortable_container_title_parts), - ]; } - 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 (\in_array($order_field, ['container', 'title', 'type'], true)) { - if (\in_array($order_field, ['container', 'title'], true)) { - $order_field .= '_sortable'; - } - - return \ilStr::strCmp( - $left[$order_field], - $right[$order_field] - ); - } - - return $left[$order_field] <=> $right[$order_field]; - } - ); - if ($order_field === 'active') { - if ($order_direction === Order::ASC) { - $rows = array_reverse($rows); - } - } elseif ($order_direction === Order::DESC) { - $rows = array_reverse($rows); - } + if ($order_field === 'active') { + return $right[$order_field] <=> $left[$order_field]; } - 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 + */ + 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')), + 'type' => $this->factory->table()->column()->text($this->lng->txt('type')), + 'container' => $this->factory->table()->column()->text($this->lng->txt('container')), + 'active' => $this->factory->table()->column()->boolean( + $this->lng->txt('active'), + $this->lng->txt('yes'), + $this->lng->txt('no') + )->withOrderingLabels( + $this->lng->txt('badge_sort_active_badges_first'), + $this->lng->txt('badge_sort_active_badges_last') + ) + ]; } /** @@ -286,87 +287,61 @@ private function getActions( URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token ): array { - $f = $this->factory; - $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'), + return $this->has_write ? [ + 'obj_badge_activate' => $this->factory->table()->action()->multi( + $this->lng->txt('activate'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'), + $row_id_token + ), + 'obj_badge_deactivate' => + $this->factory->table()->action()->multi( + $this->lng->txt('deactivate'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'), $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; + 'obj_badge_delete' => + $this->factory->table()->action()->multi( + $this->lng->txt('delete'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_delete'), + $row_id_token + ), + 'obj_badge_show_users' => + $this->factory->table()->action()->single( + $this->lng->txt('user'), + $url_builder->withParameter($action_parameter_token, 'obj_badge_show_users'), + $row_id_token + ) + ] : []; } public function renderTable(): void { - $f = $this->factory; - $r = $this->renderer; - $refinery = $this->refinery; - $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')), - 'type' => $f->table()->column()->text($this->lng->txt('type')), - 'container' => $f->table()->column()->text($this->lng->txt('container')), - 'active' => $f->table()->column()->boolean( - $this->lng->txt('active'), - $this->lng->txt('yes'), - $this->lng->txt('no') - ), - ]; - - $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' - ); - - $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->access, $this->parent_obj); - - $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token); + [$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_object_badges'), $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_object_badges'), $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(); if ($query->has($action_parameter_token->getName())) { - $action = $query->retrieve($action_parameter_token->getName(), $refinery->kindlyTo()->string()); - $ids = $query->retrieve($row_id_token->getName(), $refinery->custom()->transformation(fn($v) => $v)); + $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->kindlyTo()->string()); + $ids = $query->retrieve($row_id_token->getName(), $this->refinery->custom()->transformation(fn($v) => $v)); if ($action === 'obj_badge_delete') { $items = []; @@ -385,7 +360,7 @@ public function renderTable(): void foreach ($ids as $id) { $badge = new ilBadge((int) $id); - $items[] = $f->modal()->interruptiveItem()->keyValue( + $items[] = $this->factory->modal()->interruptiveItem()->keyValue( (string) $id, (string) $badge->getId(), $badge->getTitle() @@ -396,8 +371,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'), '#' @@ -411,6 +386,6 @@ public function renderTable(): void } } - $this->tpl->setContent($r->render($out)); + $this->tpl->setContent($this->renderer->render($out)); } }