Skip to content

Commit

Permalink
Remove legacy table from DC Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
iszmais committed Jun 25, 2024
1 parent f1a54b0 commit 0613e04
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 102 deletions.
168 changes: 168 additions & 0 deletions components/ILIAS/DataCollection/classes/DataRetrieval/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?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);

namespace ILIAS\DataCollection\DataRetrieval;

use Generator;
use ilDclFieldListGUI;
use ilDclTable;
use ilDclTableEditGUI;
use ilDclTableListGUI;
use ILIAS\Data\Factory;
use ILIAS\Data\Order;
use ILIAS\Data\Range;
use ILIAS\DI;
use ILIAS\DI\Container;
use ILIAS\UI\Component\Table\Action\Action;
use ILIAS\UI\Component\Table\DataRetrieval;
use ILIAS\UI\Component\Table\DataRowBuilder;
use ILIAS\UI\URLBuilder;
use ILIAS\UI\URLBuilderToken;
use ilObjDataCollection;
use ilObjDataCollectionGUI;

class Table implements DataRetrieval
{
private Container $dic;

public function __construct(protected readonly ilObjDataCollection $object)
{
global $DIC;
$this->dic = $DIC;
}

public function getRows(
DataRowBuilder $row_builder,
array $visible_column_ids,
Range $range,
Order $order,
?array $filter_data,
?array $additional_parameters
): Generator {
foreach ($this->object->getTables() as $table) {
$this->dic->ctrl()->setParameterByClass(ilDclFieldListGUI::class, 'table_id', $table->getId());
$row_builder = $row_builder->withSingleActions($this->getActions($table));
yield $row_builder->buildDataRow((string) $table->getId(), [
'title' => $this->dic->ui()->renderer()->render(
$this->dic->ui()->factory()->link()->standard(
$table->getTitle(),
$this->dic->ctrl()->getLinkTargetByClass(ilDclFieldListGUI::class, 'listFields')
)
),
'default' => $table->getOrder() === 10,
'visible' => $table->getIsVisible(),
'comments' => $table->getPublicCommentsEnabled()
]);
}
}

/**
* @return Action[]
*/
protected function getActions(ilDclTable $table): array
{
$this->dic->ctrl()->setParameterByClass(ilObjDataCollectionGUI::class, 'table_id', $table->getId());

$actions = [];
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'edit');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('settings'),
$builder,
$token
);

[$builder, $token] = $this->buildURL(ilDclFieldListGUI::class, 'listFields');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('dcl_list_fields'),
$builder,
$token
);

[$builder, $token] = $this->buildURL(ilDclFieldListGUI::class, 'show');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('dcl_tableviews'),
$builder,
$token
);

[$builder, $token] = $this->buildURL(ilDclFieldListGUI::class, 'confirmDelete');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('delete'),
$builder,
$token
);

if ($table->getIsVisible()) {
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'disableVisible');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('disable_visible'),
$builder,
$token
);
} else {
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'enableVisible');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('enable_visible'),
$builder,
$token
);
}

if ($table->getPublicCommentsEnabled()) {
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'disableComments');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('disable_comments'),
$builder,
$token
);
} else {
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'enableComments');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('enable_comments'),
$builder,
$token
);
}

if ($table->getOrder() !== 10) {
[$builder, $token] = $this->buildURL(ilDclTableEditGUI::class, 'setAsDefault');
$actions[] = $this->dic->ui()->factory()->table()->action()->single(
$this->dic->language()->txt('set_as_default'),
$builder,
$token
);
}

return $actions;
}

private function buildURL(string $class, string $cmd): array
{
global $DIC;
$link = ILIAS_HTTP_PATH . '/' . $DIC->ctrl()->getLinkTargetByClass($class, $cmd);
$url_builder = new URLBuilder((new Factory())->uri($link));
return $url_builder->acquireParameters(['i'], 'i');
}

public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
{
return count($this->object->getTables());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,52 @@ public function delete(): void
$this->ctrl->redirectByClass("ildcltablelistgui", "listtables");
}



public function enableVisible()
{
$this->table->setIsVisible(true);
$this->table->doUpdate();
$this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
}

public function disableVisible()
{
$this->table->setIsVisible(false);
$this->table->doUpdate();
$this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
}

public function enableComments()
{
$this->table->setPublicCommentsEnabled(true);
$this->table->doUpdate();
$this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
}

public function disableComments()
{
$this->table->setPublicCommentsEnabled(false);
$this->table->doUpdate();
$this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
}

public function setAsDefault()
{
$object = ilObjectFactory::getInstanceByObjId($this->obj_id);
$order = 20;
foreach ($object->getTables() as $table) {
if ($table->getId() === $this->table->getId()) {
$table->setOrder(10);
} else {
$table->setOrder($order);
$order += 10;
}
$table->doUpdate();
}
$this->ctrl->redirectByClass(ilDclTableListGUI::class, 'listTables');
}

protected function checkAccess(): bool
{
$ref_id = $this->parent_object->getDataCollectionObject()->getRefId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

declare(strict_types=1);

use ILIAS\Data\URI;
use ILIAS\DataCollection\DataRetrieval\Table;
use ILIAS\UI\URLBuilder;

/**
* @ilCtrl_Calls ilDclTableListGUI: ilDclFieldListGUI, ilDclFieldEditGUI, ilDclTableViewGUI, ilDclTableEditGUI
*/
Expand All @@ -35,10 +39,6 @@ class ilDclTableListGUI
protected ilObjDataCollectionGUI $parent_obj;
protected int $obj_id;

/**
* ilDclTableListGUI constructor.
* @param ilObjDataCollectionGUI $a_parent_obj
*/
public function __construct(ilObjDataCollectionGUI $a_parent_obj)
{
global $DIC;
Expand Down Expand Up @@ -77,9 +77,6 @@ public function getRefId(): int
return $this->parent_obj->getRefId();
}

/**
* execute command
*/
public function executeCommand(): void
{
global $DIC;
Expand All @@ -105,7 +102,7 @@ public function executeCommand(): void
if (count($role_titles) > 0) {
$this->tpl->setOnScreenMessage(
'info',
$DIC->language()->txt('dcl_rbac_roles_without_read_access_on_any_standard_view') . " " . implode(
$this->lng->txt('dcl_rbac_roles_without_read_access_on_any_standard_view') . " " . implode(
", ",
$role_titles
)
Expand Down Expand Up @@ -162,8 +159,23 @@ public function listTables(): void
);
$this->toolbar->addStickyItem($add_new);

$table_gui = new ilDclTableListTableGUI($this);
$this->tpl->setContent($table_gui->getHTML());
$checked = $this->ui_factory->symbol()->icon()->custom(ilUtil::getImagePath('standard/icon_checked.svg'), '');
$unchecked = $this->ui_factory->symbol()->icon()->custom(ilUtil::getImagePath('standard/icon_unchecked.svg'), '');

list($builder, $token) = (new URLBuilder(new URI('http://none')))->acquireParameters(['i'], 'i');

$table = $this->ui_factory->table()->data(
$this->lng->txt('dcl_tables'),
[
'title' => $this->ui_factory->table()->column()->text( $this->lng->txt('title'))->withIsSortable(false),
'default' => $this->ui_factory->table()->column()->boolean($this->lng->txt('default'), $checked, '')->withIsSortable(false),
'visible' => $this->ui_factory->table()->column()->boolean($this->lng->txt('visible'), $checked, $unchecked)->withIsSortable(false),
'comments' => $this->ui_factory->table()->column()->boolean($this->lng->txt('comments'), $checked, $unchecked)->withIsSortable(false),
],
new Table($this->getDataCollectionObject())
)->withActions([$this->ui_factory->table()->action()->single('', $builder, $token)]);

$this->tpl->setContent($this->renderer->render($table->withRequest($this->http->request())));
}

protected function setTabs(string $active): void
Expand All @@ -187,94 +199,6 @@ protected function setTabs(string $active): void
$this->tabs->activateTab($active);
}

protected function save(): void
{
if ($this->http->wrapper()->post()->has("comments")) {
$comments = $this->http->wrapper()->post()->retrieve(
'comments',
$this->refinery->kindlyTo()->dictOf($this->refinery->kindlyTo()->string())
);
}
if ($this->http->wrapper()->post()->has("visible")) {
$visible = $this->http->wrapper()->post()->retrieve(
'visible',
$this->refinery->kindlyTo()->dictOf($this->refinery->kindlyTo()->string())
);
}
$orders = $this->http->wrapper()->post()->retrieve(
'order',
$this->refinery->kindlyTo()->dictOf($this->refinery->kindlyTo()->string())
);
asort($orders);
$order = 10;
foreach (array_keys($orders) as $table_id) {
$table = ilDclCache::getTableCache($table_id);
$table->setOrder($order);
$table->setPublicCommentsEnabled(isset($comments[$table_id]));
$table->setIsVisible(isset($visible[$table_id]));
$table->doUpdate();
$order += 10;
}
$this->ctrl->redirect($this);
}

public function confirmDeleteTables(): void
{
//at least one table must exist
$has_dcl_table_ids = $this->http->wrapper()->post()->has('dcl_table_ids');
if (!$has_dcl_table_ids) {
$this->tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_delete_tables_no_selection'), true);
$this->ctrl->redirect($this, 'listTables');
}

$tables = $this->http->wrapper()->post()->retrieve(
'dcl_table_ids',
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
);
$this->checkTablesLeft(count($tables));

$this->tabs->clearSubTabs();
$conf = new ilConfirmationGUI();
$conf->setFormAction($this->ctrl->getFormAction($this));
$conf->setHeaderText($this->lng->txt('dcl_tables_confirm_delete'));

foreach ($tables as $table_id) {
$conf->addItem('dcl_table_ids[]', (string)$table_id, ilDclCache::getTableCache($table_id)->getTitle());
}
$conf->setConfirm($this->lng->txt('delete'), 'deleteTables');
$conf->setCancel($this->lng->txt('cancel'), 'listTables');
$this->tpl->setContent($conf->getHTML());
}

protected function deleteTables(): void
{
$has_dcl_table_ids = $this->http->wrapper()->post()->has('dcl_table_ids');
if ($has_dcl_table_ids) {
$tables = $this->http->wrapper()->post()->retrieve(
'dcl_table_ids',
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
);
foreach ($tables as $table_id) {
ilDclCache::getTableCache($table_id)->doDelete();
}
}
$this->tpl->setOnScreenMessage('success', $this->lng->txt('dcl_msg_tables_deleted'), true);
$this->ctrl->clearParameterByClass("ilobjdatacollectiongui", "table_id");
$this->ctrl->redirect($this, 'listTables');
}

/**
* redirects if there are no tableviews left after deletion of {$delete_count} tableviews
* @param $delete_count number of tableviews to delete
*/
public function checkTablesLeft(int $delete_count): void
{
if ($delete_count >= count($this->getDataCollectionObject()->getTables())) {
$this->tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_msg_tables_delete_all'), true);
$this->ctrl->redirect($this, 'listTables');
}
}

protected function checkAccess(): bool
{
$ref_id = $this->parent_obj->getRefId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public static function _hasReadAccess(int $ref): bool
public function getTables(): array
{
$query = "SELECT id FROM il_dcl_table WHERE obj_id = " . $this->db->quote($this->getId(), "integer") .
" ORDER BY -table_order DESC";
" ORDER BY title ASC";
$set = $this->db->query($query);
$tables = [];

Expand Down
Loading

0 comments on commit 0613e04

Please sign in to comment.