diff --git a/components/ILIAS/Badge/classes/class.ilBadgeImage.php b/components/ILIAS/Badge/classes/class.ilBadgeImage.php index c8d97e2b4be3..f764e445336b 100644 --- a/components/ILIAS/Badge/classes/class.ilBadgeImage.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeImage.php @@ -52,14 +52,13 @@ public function __construct( public function getImageFromBadge(ilBadge $badge, int $size = self::IMAGE_SIZE_XS): string { - $image_rid = $badge->getImageRid(); - return $this->getImageFromResourceId($badge, (string) $image_rid, $size); + return $this->getImageFromResourceId($badge, (string) $badge->getImageRid(), $size); } public function getImageFromResourceId( ilBadge|array $badge, ?string $image_rid, - $size = self::IMAGE_SIZE_XS + int $size = self::IMAGE_SIZE_XS ): string { $image_src = ''; diff --git a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php index b1634c32facd..1ea09b8c1ed4 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php @@ -24,7 +24,6 @@ use ILIAS\Data\Range; use ILIAS\UI\Renderer; use Psr\Http\Message\ServerRequestInterface; -use ILIAS\HTTP\Services; use Psr\Http\Message\RequestInterface; use ILIAS\UI\Component\Table\DataRowBuilder; use ILIAS\UI\Component\Table\DataRetrieval; @@ -49,6 +48,8 @@ class ilBadgePersonalTableGUI private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; private readonly ILIAS\DI\Container $dic; + private readonly ilObjUser $user; + private readonly ilAccessHandler $access; public function __construct() { @@ -60,26 +61,26 @@ public function __construct() $this->renderer = $DIC->ui()->renderer(); $this->refinery = $DIC->refinery(); $this->request = $DIC->http()->request(); + $this->user = $DIC->user(); + $this->access = $DIC->access(); } - protected function buildDataRetrievalObject(Factory $f, Renderer $r): DataRetrieval - { - return new class ($f, $r) implements DataRetrieval { + protected function buildDataRetrievalObject( + Factory $f, + Renderer $r, + ilObjUser $user, + ilAccessHandler $access + ): DataRetrieval { + return new class ($f, $r, $user, $access) implements DataRetrieval { private readonly ilBadgeImage $badge_image_service; - private readonly Factory $factory; - private readonly Renderer $renderer; - private readonly ilObjUser $user; - private readonly ilAccess $access; public function __construct( - protected Factory $ui_factory, - protected Renderer $ui_renderer + private readonly Factory $ui_factory, + private readonly Renderer $ui_renderer, + private readonly ilObjUser $user, + private readonly ilAccess $access ) { global $DIC; - $this->factory = $this->ui_factory; - $this->renderer = $this->ui_renderer; - $this->user = $DIC->user(); - $this->access = $DIC->access(); $this->badge_image_service = new ilBadgeImage( $DIC->resourceStorage(), $DIC->upload(), @@ -96,7 +97,7 @@ public function getRows( ?array $additional_parameters ): Generator { $records = $this->getRecords($range, $order); - foreach ($records as $idx => $record) { + foreach ($records as $record) { $row_id = (string) $record['id']; yield $row_builder->buildDataRow($row_id, $record); } @@ -109,81 +110,137 @@ public function getTotalRowCount( return count($this->getRecords()); } + /** + * @return list + */ private function getRecords(Range $range = null, Order $order = null): array { - $data = []; + $rows = []; $a_user_id = $this->user->getId(); $badge_img_large = new Image(Image::STANDARD, '', ''); foreach (ilBadgeAssignment::getInstancesByUserId($a_user_id) as $ass) { - $image_html = ''; $badge = new ilBadge($ass->getBadgeId()); + + $parent = null; + if ($badge->getParentId()) { + $parent = $badge->getParentMeta(); + if ($parent['type'] === 'bdga') { + $parent = null; + } + } + $modal_container = new ModalBuilder($ass); - $image_rid = $this->badge_image_service->getImageFromBadge($badge); - if ($image_rid !== '') { - $badge_img = $this->factory->image()->responsive($image_rid, $badge->getTitle()); - $image_html = $this->renderer->render($badge_img); - $image_html_large = $this->badge_image_service->getImageFromResourceId( + $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() + ) + ); + + $image_src_large = $this->badge_image_service->getImageFromBadge( $badge, - $badge->getImageRid(), ilBadgeImage::IMAGE_SIZE_XL ); - if ($image_html_large !== '') { + if ($image_src_large !== '') { $badge_img_large = $this->ui_factory->image()->responsive( - $image_html_large, + $image_src_large, $badge->getTitle() ); } } - $parent = null; - if ($badge->getParentId()) { - $parent = $badge->getParentMeta(); - if ($parent['type'] === 'bdga') { - $parent = null; - } - } + $awarded_by = ''; + $awarded_by_sortable = ''; if ($parent !== null) { $ref_ids = ilObject::_getAllReferences($parent['id']); - reset($ref_ids); $ref_id = current($ref_ids); - if ($this->access->checkAccess('read', '', $ref_id)) { - $container_url = ilLink::_getLink($ref_id); - $awarded_by = $this->renderer->render(new Standard( + $awarded_by = $parent['title']; + $awarded_by_sortable = $parent['title']; + if ($ref_id && $this->access->checkAccess('read', '', $ref_id)) { + $awarded_by = $this->ui_renderer->render(new Standard( $parent['title'], - (string) new URI($container_url) + (string) new URI(ilLink::_getLink($ref_id)) )); + $awarded_by_sortable = $parent['title']; } } - $information = [ - 'awarded_by' => $awarded_by - ]; - - $modal = $modal_container->constructModal($badge_img_large, $badge->getTitle(), $information); + $modal = $modal_container->constructModal( + $badge_img_large, + $badge->getTitle(), + [ + 'awarded_by' => $awarded_by + ] + ); - $data[] = [ + $rows[] = [ 'id' => $badge->getId(), - 'image' => $this->ui_renderer->render($this->ui_factory->button()->shy( - $image_html, - $modal->getShowSignal() - )) . ' ' . $this->ui_renderer->render($modal), - 'title' => $this->ui_renderer->render($this->ui_factory->button()->shy( - $badge->getTitle(), - $modal->getShowSignal() - )), - 'user' => '', - 'badge_in_profile' => '', - 'badge_issued_on' => (new \DateTimeImmutable())->setTimestamp($ass->getTimestamp()), + 'image' => $images['rendered'] ? ($modal_container->renderShyButton( + $images['rendered'], + $modal + ) . ' ') : '', + // Just an boolean-like indicator for sorting + 'image_sortable' => $images['rendered'] ? 'A' . $badge->getId() : 'Z' . $badge->getId(), + 'title' => implode('', [ + $modal_container->renderShyButton($badge->getTitle(), $modal), + $modal_container->renderModal($modal) + ]), + 'title_sortable' => $badge->getTitle(), + 'badge_issued_on' => (new \DateTimeImmutable()) + ->setTimestamp($ass->getTimestamp()) + ->setTimezone(new DateTimeZone($this->user->getTimeZone())), 'awarded_by' => $awarded_by, - 'parent' => $parent, + 'awarded_by_sortable' => $awarded_by_sortable, 'active' => (bool) $ass->getPosition() ]; } - return $data; + 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', 'image', 'awarded_by'], true)) { + if (\in_array($order_field, ['title', 'image', 'awarded_by'], true)) { + $order_field .= '_sortable'; + } + + 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); + } + } + + return $rows; } }; } @@ -218,9 +275,13 @@ public function renderTable(): void $f = $this->factory; $r = $this->renderer; $request = $this->request; + $df = new \ILIAS\Data\Factory(); - $data_format = $df->dateFormat(); - $date_format = $data_format->withTime24($df->dateFormat()->germanShort()); + if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) { + $date_format = $df->dateFormat()->withTime12($this->user->getDateFormat()); + } else { + $date_format = $df->dateFormat()->withTime24($this->user->getDateFormat()); + } $columns = [ 'image' => $f->table()->column()->text($this->lng->txt('image')), @@ -238,19 +299,21 @@ public function renderTable(): void $url_builder = new URLBuilder($table_uri); $query_params_namespace = ['badge']; - list($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' ); - $data_retrieval = $this->buildDataRetrievalObject($f, $r); + $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->user, $this->access); $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token); $table = $f->table() ->data($this->lng->txt('badge_personal_badges'), $columns, $data_retrieval) + ->withId(self::class) + ->withOrder(new Order('title', Order::ASC)) ->withActions($actions) ->withRequest($request); diff --git a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php index 1a9ae26311fc..68f168a48a0f 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php @@ -88,8 +88,6 @@ private function buildDataRetrievalObject(Factory $f, Renderer $r, int $p, strin { return new class ($f, $r, $p, $type) implements DataRetrieval { private ilBadgeImage $badge_image_service; - private Factory $factory; - private Renderer $renderer; public function __construct( private readonly Factory $ui_factory, @@ -103,8 +101,6 @@ public function __construct( $DIC->upload(), $DIC->ui()->mainTemplate() ); - $this->factory = $this->ui_factory; - $this->renderer = $this->ui_renderer; } /** @@ -130,18 +126,17 @@ private function getBadges(): array 'rendered' => null, 'large' => null, ]; - $image_src = $this->badge_image_service->getImageFromResourceId($badge, $badge->getImageRid()); + $image_src = $this->badge_image_service->getImageFromBadge($badge); if ($image_src !== '') { - $images['rendered'] = $this->renderer->render( - $this->factory->image()->responsive( + $images['rendered'] = $this->ui_renderer->render( + $this->ui_factory->image()->responsive( $image_src, $badge->getTitle() ) ); - $image_src_large = $this->badge_image_service->getImageFromResourceId( + $image_src_large = $this->badge_image_service->getImageFromBadge( $badge, - $badge->getImageRid(), ilBadgeImage::IMAGE_SIZE_XL ); if ($image_src_large !== '') { diff --git a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php index 0d3b924e8803..5dd9dadbf23d 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php @@ -231,24 +231,36 @@ 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, static 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; } }; } @@ -263,7 +275,6 @@ private function getActions( ): array { $f = $this->factory; if ($this->award_badge) { - return [ 'badge_award_badge' => $f->table()->action()->multi( @@ -340,10 +351,13 @@ public function renderTable(): void } $table = $f->table() ->data($title, $columns, $data_retrieval) + ->withId(self::class) + ->withOrder(new Order('name', Order::ASC)) ->withActions($actions) ->withRequest($request); $out = [$table]; + $this->tpl->setContent($r->render($out)); } } diff --git a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php index 2324913760e8..5bc68133f5af 100755 --- a/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilObjectBadgeTableGUI.php @@ -43,7 +43,7 @@ use ilObjBadgeAdministrationGUI; use ILIAS\Filesystem\Stream\Streams; use ILIAS\UI\Component\Table\Action\Action; -use ilAccess; +use ilAccessHandler; class ilObjectBadgeTableGUI { @@ -55,7 +55,7 @@ class ilObjectBadgeTableGUI private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; private readonly ilObjBadgeAdministrationGUI $parent_obj; - private readonly ilAccess $access; + private readonly ilAccessHandler $access; public function __construct(ilObjBadgeAdministrationGUI $parentObj) { @@ -70,25 +70,22 @@ public function __construct(ilObjBadgeAdministrationGUI $parentObj) $this->http = $DIC->http(); $this->access = $DIC->access(); $this->parent_obj = $parentObj; - } private function buildDataRetrievalObject( Factory $f, Renderer $r, + ilAccessHandler $access, ilObjBadgeAdministrationGUI $p ): DataRetrieval { - return new class ($f, $r, $p) implements DataRetrieval { - private ilBadgeImage $badge_image_service; - private Factory $factory; - private Renderer $renderer; - private \ilCtrlInterface $ctrl; - private ilLanguage $lng; - private \ilAccessHandler $access; + 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; @@ -98,11 +95,7 @@ public function __construct( $DIC->upload(), $DIC->ui()->mainTemplate() ); - $this->factory = $this->ui_factory; - $this->renderer = $this->ui_renderer; - $this->ctrl = $DIC->ctrl(); $this->lng = $DIC->language(); - $this->access = $DIC->access(); } public function getRows( @@ -166,8 +159,8 @@ private function getRecords(Range $range = null, Order $order = null): array $badge_item['image_rid'] ); if ($image_src !== '') { - $images['rendered'] = $this->renderer->render( - $this->factory->image()->responsive( + $images['rendered'] = $this->ui_renderer->render( + $this->ui_factory->image()->responsive( $image_src, $badge_item['title'] ) @@ -204,7 +197,7 @@ private function getRecords(Range $range = null, Order $order = null): array $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( + $container_title_parts['title'] = $this->ui_renderer->render( new Standard( $container_title_parts['title'], (string) new URI(ilLink::_getLink($ref_id)) @@ -355,7 +348,7 @@ public function renderTable(): void 'id' ); - $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->parent_obj); + $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->access, $this->parent_obj); $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token);