Skip to content

Commit

Permalink
review dashboard #4 blockdto
Browse files Browse the repository at this point in the history
  • Loading branch information
fhelfer committed Jun 26, 2023
1 parent 1e03c45 commit 06b61b0
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 82 deletions.
150 changes: 150 additions & 0 deletions Services/Dashboard/Block/classes/class.ilBlockDataDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);
class ilBlockDataDTO
{
private string $type;
private int $ref_id;
private int $obj_id;
private string $title;
private string $description;
private ?ilDateTime $startDate;
private ?ilDateTime $endDate;
private array $additional_data;

public function __construct(
string $type,
int $ref_id,
int $obj_id,
string $title,
string $description,
?ilDateTime $startDate = null,
?ilDateTime $endDate = null,
array $additional_data = []
) {
$this->type = $type;
$this->ref_id = $ref_id;
$this->obj_id = $obj_id;
$this->title = $title;
$this->description = $description;
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->additional_data = $additional_data;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): void
{
$this->type = $type;
}

public function getRefId(): int
{
return $this->ref_id;
}

public function setRefId(int $ref_id): void
{
$this->ref_id = $ref_id;
}

public function getObjId(): int
{
return $this->obj_id;
}

public function setObjId(int $obj_id): void
{
$this->obj_id = $obj_id;
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): void
{
$this->title = $title;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

public function getStartDate(): ?ilDateTime
{
return $this->startDate;
}

public function setStartDate(?ilDateTime $startDate): void
{
$this->startDate = $startDate;
}

public function getEndDate(): ?ilDateTime
{
return $this->endDate;
}

public function setEndDate(?ilDateTime $endDate): void
{
$this->endDate = $endDate;
}

public function hasNotStarted(): bool
{
return $this->startDate && $this->startDate->get(IL_CAL_UNIX) > time();
}

public function hasEnded(): bool
{
return $this->endDate && $this->endDate->get(IL_CAL_UNIX) < time();
}

public function isRunning(): bool
{
return !$this->hasNotStarted() && !$this->hasEnded();
}

public function isDated(): bool
{
return $this->startDate || $this->endDate;
}

public function getAdditionalData(): array
{
return $this->additional_data;
}

public function setAdditionalData(array $additional_data): void
{
$this->additional_data = $additional_data;
}
}
110 changes: 71 additions & 39 deletions Services/Dashboard/Block/classes/class.ilDashboardBlockGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ abstract public function addCustomCommandsToActionMenu(ilObjectListGUI $itemList

abstract public function emptyHandling(): string;

protected function getCardForData(array $data): ?RepositoryObject
{
$itemListGui = $this->byType($data['type']);
ilObjectActivation::addListGUIActivationProperty($itemListGui, $data);

protected function getCardForData(ilBlockDataDTO $data): ?RepositoryObject
{
$itemListGui = $this->byType($data->getType());
$card = $itemListGui->getAsCard(
(int) $data['ref_id'],
(int) $data['obj_id'],
(string) $data['type'],
(string) $data['title'],
(string) $data['description']
$data->getRefId(),
$data->getObjId(),
$data->getDescription(),
$data->getTitle(),
$data->getDescription()
);

return $card;
Expand All @@ -96,7 +95,7 @@ protected function getListItemGroups(): array
foreach ($data as $title => $group) {
$items = [];
foreach ($group as $datum) {
$item = $this->getListItemForData($datum);
$item = $this->getListItemForDataDTO($datum);
if ($item !== null) {
$items[] = $item;
}
Expand All @@ -108,19 +107,16 @@ protected function getListItemGroups(): array
return $groupedCards;
}


protected function getListItemForData(array $data): ?Item
protected function getListItemForDataDTO(ilBlockDataDTO $data): ?Item
{
$itemListGui = $this->byType($data['type']);
$this->addCustomCommandsToActionMenu($itemListGui, $data['ref_id']);
ilObjectActivation::addListGUIActivationProperty($itemListGui, $data);

$itemListGui = $this->byType($data->getType());
$this->addCustomCommandsToActionMenu($itemListGui, $data->getRefId());
$list_item = $itemListGui->getAsListItem(
(int) $data['ref_id'],
(int) $data['obj_id'],
(string) $data['type'],
(string) $data['title'],
(string) $data['description']
$data->getRefId(),
$data->getObjId(),
$data->getDescription(),
$data->getTitle(),
$data->getDescription()
);

return $list_item;
Expand Down Expand Up @@ -203,14 +199,32 @@ public function getHTML(): string
return parent::getHTML();
}

/**
* @param array<string, ilBlockDataDTO[]> $a_data
*/
public function setData(array $a_data): void
{
$this->data = array_filter($a_data);
$this->data = array_filter(array_map(
static fn ($group) => array_filter($group, fn ($item) => $item instanceof ilBlockDataDTO),
$a_data
));
}

/**
* @return array<string, ilBlockDataDTO[]>
*/
public function getData(): array
{
return parent::getData();
}

/**
* @return array<string, ilBlockDataDTO[]>
*/
public function groupItemsByStartDate(): array
{
$data = $this->getData();
/** @var ilBlockDataDTO[] $items */
$items = array_merge(...array_values($data));

$groups = [
Expand All @@ -220,10 +234,10 @@ public function groupItemsByStartDate(): array
'not_dated' => []
];
foreach ($items as $item) {
if (isset($item['start'], $item['end']) && $item['start'] instanceof ilDateTime && $item['start']->get(IL_CAL_UNIX) > 0) {
if ($item['start']->get(IL_CAL_UNIX) > time()) {
if ($item->isDated()) {
if ($item->hasNotStarted()) {
$groups['upcoming'][] = $item;
} elseif ($item['end'] instanceof ilDateTime && $item['end']->get(IL_CAL_UNIX) > time()) {
} elseif ($item->isRunning()) {
$groups['ongoing'][] = $item;
} else {
$groups['ended'][] = $item;
Expand All @@ -233,17 +247,20 @@ public function groupItemsByStartDate(): array
}
}


$orderByDate = static function (array $left, array $right, bool $asc = true) {
if ($left['start']->get(IL_CAL_UNIX) < $right['start']->get(IL_CAL_UNIX)) {
$orderByDate = static function (ilBlockDataDTO $left, ilBlockDataDTO $right, bool $asc = true) {
if ($left->getStartDate() && $right->getStartDate() && $left->getStartDate()->get(
IL_CAL_UNIX
) < $right->getStartDate()->get(IL_CAL_UNIX)) {
return $asc ? -1 : 1;
}

if ($left['start']->get(IL_CAL_UNIX) > $right['start']->get(IL_CAL_UNIX)) {
if ($left->getStartDate() && $right->getStartDate() && $left->getStartDate()->get(
IL_CAL_UNIX
) > $right->getStartDate()->get(IL_CAL_UNIX)) {
return $asc ? 1 : -1;
}

return strcmp($left['title'], $right['title']);
return strcmp($left->getTitle(), $right->getTitle());
};

uasort($groups['upcoming'], static fn ($left, $right) => $orderByDate($left, $right));
Expand All @@ -259,19 +276,23 @@ public function groupItemsByStartDate(): array
return $groups;
}

/**
* @return array<string, ilBlockDataDTO[]>
*/
protected function groupItemsByType(): array
{
$object_types_by_container = $this->objDefinition->getGroupedRepositoryObjectTypes(
['cat', 'crs', 'grp', 'fold']
);
$grouped_items = [];
$data = $this->getData();
/** @var ilBlockDataDTO[] $data */
$data = array_merge(...array_values($data));
$provider = new ilPDSelectedItemsBlockMembershipsProvider($this->viewSettings->getActor());

foreach ($data as $item) {
if (isset($object_types_by_container[$item['type']])) {
$object_types_by_container[$item['type']]['items'][] = $item;
if (isset($object_types_by_container[$item->getType()])) {
$object_types_by_container[$item->getType()]['items'][] = $item;
}
}

Expand All @@ -295,19 +316,23 @@ protected function groupItemsByType(): array
return $grouped_items;
}

/**
* @return array<string, ilBlockDataDTO[]>
*/
protected function groupItemsByLocation(): array
{
$grouped_items = [];
$data = $this->getData();
/** @var ilBlockDataDTO[] $data */
$data = array_merge(...array_values($data));

$parent_ref_ids = array_values(array_unique(
array_map(fn (array $item): ?int => $this->tree->getParentId($item['ref_id']), $data)
array_map(fn (ilBlockDataDTO $item): ?int => $this->tree->getParentId($item->getRefId()), $data)
));
$this->object_cache->preloadReferenceCache($parent_ref_ids);

foreach ($data as $key => $item) {
$parent_ref = $this->tree->getParentId($item['ref_id']);
$parent_ref = $this->tree->getParentId($item->getRefId());
if ($this->isRootNode($parent_ref)) {
$title = $this->getRepositoryTitle();
} else {
Expand Down Expand Up @@ -438,6 +463,9 @@ protected function returnToContext(): void
$this->ctrl->redirectByClass('ildashboardgui', 'show');
}

/**
* @return array<string, ilBlockDataDTO[]>
*/
public function getItemGroups(): array
{
switch ($this->viewSettings->getEffectiveSortingMode()) {
Expand Down Expand Up @@ -473,7 +501,7 @@ public function removeFromDeskRoundtripObject(): void
case 'manage':
$modal = $this->ui->factory()->modal()->roundtrip(
$this->getRemoveMultipleActionText(),
$this->ui->factory()->legacy($this->manage($replace_signal))
$this->ui->factory()->legacy($this->manage($replace_signal ?? null))
);
$modal = $modal->withAdditionalOnLoadCode(function ($id) {
return "
Expand Down Expand Up @@ -520,10 +548,11 @@ public function manage(ReplaceSignal $replace_signal = null): string
$group->setLabel($key);
$items = [];
foreach ($item_group as $item) {
if ($this->rbacsystem->checkAccess('leave', $item['ref_id'])) {
if ($this->rbacsystem->checkAccess('leave', $item->getRefId())) {
$items[] = $item;
}
}
// TODO
$group->setItems($items);
$grouped_items[] = $group;
}
Expand Down Expand Up @@ -711,13 +740,16 @@ public function byType(string $a_type): ilObjectListGUI
return $item_list_gui;
}

/**
* @param ilBlockDataDTO[] $data
*/
private function sortByTitle(array $data, bool $asc = true): array
{
uasort(
$data,
static fn ($left, $right) => $asc ?
strcmp($left['title'], $right['title']) :
strcmp($right['title'], $left['title'])
static fn (ilBlockDataDTO $left, ilBlockDataDTO $right) => $asc ?
strcmp($left->getTitle(), $right->getTitle()) :
strcmp($right->getTitle(), $left->getTitle())
);
return $data;
}
Expand Down
Loading

0 comments on commit 06b61b0

Please sign in to comment.