Skip to content

Commit

Permalink
Badges: Fix missing table id and sorting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansenDatabay committed Dec 18, 2024
1 parent 62a51da commit 3ef144d
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 97 deletions.
5 changes: 2 additions & 3 deletions components/ILIAS/Badge/classes/class.ilBadgeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
183 changes: 123 additions & 60 deletions components/ILIAS/Badge/classes/class.ilBadgePersonalTableGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand All @@ -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(),
Expand All @@ -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);
}
Expand All @@ -109,81 +110,137 @@ public function getTotalRowCount(
return count($this->getRecords());
}

/**
* @return list<array{
* id: int,
* active: bool,
* image: string,
* image_sortable: string,
* awarded_by: string,
* awarded_by_sortable: string,
* title: string,
* title_sortable: string}>
*/
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;
}
};
}
Expand Down Expand Up @@ -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')),
Expand All @@ -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);

Expand Down
13 changes: 4 additions & 9 deletions components/ILIAS/Badge/classes/class.ilBadgeTableGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -103,8 +101,6 @@ public function __construct(
$DIC->upload(),
$DIC->ui()->mainTemplate()
);
$this->factory = $this->ui_factory;
$this->renderer = $this->ui_renderer;
}

/**
Expand All @@ -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 !== '') {
Expand Down
Loading

0 comments on commit 3ef144d

Please sign in to comment.