From dab0180568423ce159fed51866facd370cdc09db Mon Sep 17 00:00:00 2001 From: mjansen Date: Wed, 18 Dec 2024 16:22:16 +0100 Subject: [PATCH] Badges: Fix missing language variable (cherry picked from commit fd4cf23b60ceba0d5b2418b5614e5d93607cfc23) --- .../classes/class.ilBadgePersonalTableGUI.php | 17 +- .../classes/class.ilBadgeUserTableGUI.php | 163 +++++++++--------- .../class.ilObjBadgeAdministrationGUI.php | 11 +- 3 files changed, 97 insertions(+), 94 deletions(-) diff --git a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php index 1ea09b8c1ed4..0d46e1e29af6 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php @@ -118,14 +118,15 @@ public function getTotalRowCount( * image_sortable: string, * awarded_by: string, * awarded_by_sortable: string, + * badge_issued_on: DateTimeImmutable, * title: string, - * title_sortable: string}> + * title_sortable: string + * }> */ private function getRecords(Range $range = null, Order $order = null): array { $rows = []; $a_user_id = $this->user->getId(); - $badge_img_large = new Image(Image::STANDARD, '', ''); foreach (ilBadgeAssignment::getInstancesByUserId($a_user_id) as $ass) { $badge = new ilBadge($ass->getBadgeId()); @@ -159,7 +160,7 @@ private function getRecords(Range $range = null, Order $order = null): array ilBadgeImage::IMAGE_SIZE_XL ); if ($image_src_large !== '') { - $badge_img_large = $this->ui_factory->image()->responsive( + $images['large'] = $this->ui_factory->image()->responsive( $image_src_large, $badge->getTitle() ); @@ -184,7 +185,7 @@ private function getRecords(Range $range = null, Order $order = null): array } $modal = $modal_container->constructModal( - $badge_img_large, + $images['large'], $badge->getTitle(), [ 'awarded_by' => $awarded_by @@ -204,7 +205,7 @@ private function getRecords(Range $range = null, Order $order = null): array $modal_container->renderModal($modal) ]), 'title_sortable' => $badge->getTitle(), - 'badge_issued_on' => (new \DateTimeImmutable()) + 'badge_issued_on' => (new DateTimeImmutable()) ->setTimestamp($ass->getTimestamp()) ->setTimezone(new DateTimeZone($this->user->getTimeZone())), 'awarded_by' => $awarded_by, @@ -221,12 +222,12 @@ private function getRecords(Range $range = null, Order $order = null): array 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)) { + 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( + return ilStr::strCmp( $left[$order_field], $right[$order_field] ); diff --git a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php index 5dd9dadbf23d..94a5a2135ba2 100755 --- a/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php +++ b/components/ILIAS/Badge/classes/class.ilBadgeUserTableGUI.php @@ -32,7 +32,6 @@ use ILIAS\UI\Component\Table\DataRowBuilder; use Generator; use ILIAS\UI\Component\Table\DataRetrieval; -use ILIAS\DI\Container; use ilBadgeHandler; use ilObject; use ilBadge; @@ -41,6 +40,9 @@ use DateTimeImmutable; use ILIAS\UI\URLBuilderToken; use ilObjectDataDeletionLog; +use ilTree; +use ilCalendarSettings; +use ilObjUser; class ilBadgeUserTableGUI { @@ -50,11 +52,13 @@ class ilBadgeUserTableGUI private readonly int $parent_ref_id; private readonly ilLanguage $lng; private readonly ilGlobalTemplateInterface $tpl; + private readonly ilTree $tree; + private readonly ilObjUser $user; public function __construct( int $parent_ref_id, private readonly ?ilBadge $award_badge = null, - private readonly int $restrict_badge_id = 0 + private readonly ?int $restrict_badge_id = null ) { global $DIC; @@ -63,75 +67,80 @@ public function __construct( $this->factory = $DIC->ui()->factory(); $this->renderer = $DIC->ui()->renderer(); $this->request = $DIC->http()->request(); + $this->tree = $DIC->repositoryTree(); + $this->user = $DIC->user(); $this->parent_ref_id = $parent_ref_id; } private function buildDataRetrievalObject( Factory $f, Renderer $r, + ilTree $tree, + ilObjUser $user, int $parent_ref_id, + ?int $restrict_badge_id = null, ?ilBadge $award_badge = null ): DataRetrieval { - return new class ($f, $r, $parent_ref_id, $award_badge) implements DataRetrieval { + return new class ($f, $r, $tree, $user, $parent_ref_id, $restrict_badge_id, $award_badge) implements DataRetrieval { public function __construct( - private Factory $ui_factory, - private Renderer $ui_renderer, - private int $parent_ref_id, - private ?ilBadge $award_badge = null + private readonly Factory $ui_factory, + private readonly Renderer $ui_renderer, + private readonly ilTree $tree, + private readonly ilObjUser $user, + private readonly int $parent_ref_id, + private readonly ?int $restrict_badge_id = null, + private readonly ?ilBadge $award_badge = null ) { } /** * @return list + * issued: ?DateTimeImmutable + * }> */ - private function getBadgeImageTemplates(Container $DIC): array + private function getBadgeImageTemplates(): array { - $assignments = null; - $user_ids = null; - $parent_ref_id = $this->parent_ref_id; - $data = []; + /** @var array> $assignments */ + $assignments = []; + $user_ids = []; + $rows = []; $badges = []; - $tree = $DIC->repositoryTree(); - $restrict_badge_id = 0; - $a_parent_obj_id = ilObject::_lookupObjId($parent_ref_id); - - if ($parent_ref_id) { - $user_ids = ilBadgeHandler::getInstance()->getUserIds($parent_ref_id, $a_parent_obj_id); + $a_parent_obj_id = ilObject::_lookupObjId($this->parent_ref_id); + if ($this->parent_ref_id) { + $user_ids = ilBadgeHandler::getInstance()->getUserIds($this->parent_ref_id, $a_parent_obj_id); } $obj_ids = [$a_parent_obj_id]; - - foreach ($tree->getSubTree($tree->getNodeData($parent_ref_id)) as $node) { + 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); foreach ($obj_ids as $obj_id) { foreach (ilBadge::getInstancesByParentId($obj_id) as $badge) { $badges[$badge->getId()] = $badge; } + foreach (ilBadgeAssignment::getInstancesByParentId($obj_id) as $ass) { - if ($restrict_badge_id && - $restrict_badge_id !== $ass->getBadgeId()) { + if ($this->restrict_badge_id && $this->restrict_badge_id !== $ass->getBadgeId()) { continue; } - if ($this->award_badge && - $ass->getBadgeId() !== $this->award_badge->getId()) { + + if ($this->award_badge && $ass->getBadgeId() !== $this->award_badge->getId()) { continue; } $assignments[$ass->getUserId()][] = $ass; } } - if (!$user_ids && $assignments !== null) { + + if (!$user_ids) { $user_ids = array_keys($assignments); } @@ -142,51 +151,40 @@ private function getBadgeImageTemplates(Container $DIC): array $uquery->setUserFilter($user_ids); $tmp = $uquery->query(); } + foreach ($tmp['set'] as $user) { - if (\is_array($assignments) && \array_key_exists($user['usr_id'], $assignments)) { + if (\array_key_exists($user['usr_id'], $assignments)) { foreach ($assignments[$user['usr_id']] as $user_ass) { $idx = $user_ass->getBadgeId() . '-' . $user['usr_id']; + $badge = $badges[$user_ass->getBadgeId()]; - $parent = $badge->getParentMeta(); - $timestamp = $user_ass->getTimestamp(); - $immutable = new DateTimeImmutable(); - $user_id = $user['usr_id']; - $name = $user['lastname'] . ', ' . $user['firstname']; - $login = $user['login']; - $type = ilBadge::getExtendedTypeCaption($badge->getTypeInstance()); - $title = $badge->getTitle(); - $issued = $immutable->setTimestamp($timestamp); - $parent_id = $parent['id']; - $data[$idx] = [ - 'id' => $user_id, - 'user_id' => $user_id, - 'name' => $name, - 'login' => $login, - 'type' => $type, - 'title' => $title, - 'issued' => $issued, - 'parent_id' => $parent_id, - 'parent_meta' => $parent + + $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())) ]; } } elseif ($this->award_badge) { $idx = '0-' . $user['usr_id']; - $user_id = $user['usr_id']; - $name = $user['lastname'] . ', ' . $user['firstname']; - $login = $user['login']; - $data[$idx] = [ - 'id' => $user_id, - 'user_id' => $user_id, - 'name' => $name, - 'login' => $login, + + $rows[] = [ + 'id' => $idx, + 'name' => $user['lastname'] . ', ' . $user['firstname'], + 'login' => $user['login'], 'type' => '', 'title' => '', - 'parent_id' => '' + 'issued' => null, ]; } } - return $data; + return $rows; } public function getRows( @@ -199,13 +197,7 @@ public function getRows( ): Generator { $records = $this->getRecords($range, $order); foreach ($records as $record) { - - $row_id = (string) $record['id']; - if ($this->award_badge !== null) { - $row_id = $record['id'] . '_' . $this->award_badge->getId(); - } - - yield $row_builder->buildDataRow($row_id, $record); + yield $row_builder->buildDataRow($record['id'], $record); } } @@ -218,20 +210,17 @@ public function getTotalRowCount( /** * @return list + * issued: ?DateTimeImmutable + * }> */ private function getRecords(Range $range = null, Order $order = null): array { - global $DIC; - - $rows = $this->getBadgeImageTemplates($DIC); + $rows = $this->getBadgeImageTemplates(); if ($order) { [$order_field, $order_direction] = $order->join( @@ -241,7 +230,7 @@ private function getRecords(Range $range = null, Order $order = null): array usort( $rows, static function (array $left, array $right) use ($order_field): int { - if ($order_field === 'title') { + if (\in_array($order_field, ['name', 'login', 'type', 'title'], true)) { return \ilStr::strCmp( $left[$order_field], $right[$order_field ] @@ -273,8 +262,9 @@ private function getActions( URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token, ): array { - $f = $this->factory; if ($this->award_badge) { + $f = $this->factory; + return [ 'badge_award_badge' => $f->table()->action()->multi( @@ -299,7 +289,13 @@ 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) { + $date_format = $df->dateFormat()->withTime12($this->user->getDateFormat()); + } else { + $date_format = $df->dateFormat()->withTime24($this->user->getDateFormat()); + } $columns = [ 'name' => $f->table()->column()->text($this->lng->txt('name')), @@ -308,7 +304,7 @@ public function renderTable(): void 'title' => $f->table()->column()->text($this->lng->txt('title')), 'issued' => $f->table()->column()->date( $this->lng->txt('badge_issued_on'), - $df->dateFormat()->germanShort() + $date_format ) ]; @@ -323,7 +319,15 @@ public function renderTable(): void 'id', ); - $data_retrieval = $this->buildDataRetrievalObject($f, $r, $this->parent_ref_id, $this->award_badge); + $data_retrieval = $this->buildDataRetrievalObject( + $f, + $r, + $this->tree, + $this->user, + $this->parent_ref_id, + $this->restrict_badge_id, + $this->award_badge + ); $actions = $this->getActions($url_builder, $action_parameter_token, $row_id_token); if ($this->award_badge) { @@ -349,9 +353,10 @@ public function renderTable(): void } $title = $parent . $this->lng->txt('users'); } + $table = $f->table() ->data($title, $columns, $data_retrieval) - ->withId(self::class) + ->withId(self::class . '_' . $this->parent_ref_id) ->withOrder(new Order('name', Order::ASC)) ->withActions($actions) ->withRequest($request); diff --git a/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php b/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php index de259969ea1d..daa496be5159 100755 --- a/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php +++ b/components/ILIAS/Badge/classes/class.ilObjBadgeAdministrationGUI.php @@ -632,9 +632,6 @@ protected function resetObjectFilter(): void protected function listObjectBadgeUsers(): void { - $ilCtrl = $this->ctrl; - $lng = $this->lng; - $parent_obj_id = $this->badge_request->getParentId(); $parent_ref_ids = ilObject::_getAllReferences($parent_obj_id); $parent_ref_id = array_pop($parent_ref_ids); @@ -643,18 +640,18 @@ protected function listObjectBadgeUsers(): void $parent_ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int()); } if (!$parent_ref_id) { - $ilCtrl->redirect($this, 'listObjectBadges'); + $this->ctrl->redirect($this, 'listObjectBadges'); } $this->assertActive(); $this->tabs_gui->clearTargets(); $this->tabs_gui->setBackTarget( - $lng->txt('back'), - $ilCtrl->getLinkTarget($this, 'listObjectBadges') + $this->lng->txt('back'), + $this->ctrl->getLinkTarget($this, 'listObjectBadges') ); - $ilCtrl->saveParameter($this, 'pid'); + $this->ctrl->saveParameter($this, 'pid'); $tbl = new ilBadgeUserTableGUI($parent_ref_id, null, $this->badge_request->getBadgeId()); $tbl->renderTable();