Skip to content

Commit

Permalink
MetaData: wired up vocab table, added lang vars
Browse files Browse the repository at this point in the history
  • Loading branch information
schmitz-ilias committed Sep 26, 2024
1 parent 86ed6c4 commit c49090e
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 152 deletions.
5 changes: 5 additions & 0 deletions Services/MetaData/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
Reused constants should be collected into bespoke classes as
appropriate, instead of being scattered across the component.

### Get URI from Data Factory

Get URI from the data factory, instead of instantiating it
directly.

### Clean up Remains of Migrations for Ilias 10

With ILIAS 10, the migrations `ilMDLOMConformanceMigration` and
Expand Down
119 changes: 119 additions & 0 deletions Services/MetaData/classes/Settings/Vocabularies/DataRetrieval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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\MetaData\Settings\Vocabularies;

use ILIAS\UI\Component\Table\DataRetrieval as BaseDataRetrieval;
use ILIAS\UI\Component\Table\DataRowBuilder;
use ILIAS\Data\Range;
use ILIAS\Data\Order;
use ILIAS\MetaData\Vocabularies\Manager\Manager as VocabManager;
use ILIAS\MetaData\Vocabularies\VocabularyInterface;

class DataRetrieval implements BaseDataRetrieval
{
protected const MAX_PREVIEW_VALUES = 5;

protected VocabManager $vocab_manager;
protected Presentation $presentation;

/**
* @var VocabularyInterface[]
*/
protected array $vocabs;

public function __construct(
VocabManager $vocab_manager,
Presentation $presentation
) {
$this->vocab_manager = $vocab_manager;
$this->presentation = $presentation;
}

public function getRows(
DataRowBuilder $row_builder,
array $visible_column_ids,
Range $range,
Order $order,
?array $filter_data,
?array $additional_parameters
): \Generator {
$infos = $this->vocab_manager->infos();
foreach ($this->getVocabs($range) as $vocab) {
$record = [];

$record['element'] = $this->presentation->makeSlotPresentable($vocab->slot());
$record['type'] = $this->presentation->makeTypePresentable($vocab->type());
$record['source'] = $vocab->source();
$record['preview'] = implode(
', ',
$this->presentation->makeValuesPresentable($vocab, self::MAX_PREVIEW_VALUES)
);
$record['active'] = $vocab->isActive();
if ($infos->isCustomInputApplicable($vocab)) {
$record['custom_input'] = $vocab->allowsCustomInputs();
}

yield $row_builder->buildDataRow(
$vocab->id(),
$record
)->withDisabledAction(
'delete',
!$infos->canBeDeleted($vocab)
)->withDisabledAction(
'activate',
$vocab->isActive()
)->withDisabledAction(
'deactivate',
!$vocab->isActive() ||
!$infos->isDeactivatable($vocab)
)->withDisabledAction(
'allow_custom_input',
!$infos->isCustomInputApplicable($vocab) ||
$vocab->allowsCustomInputs()
)->withDisabledAction(
'disallow_custom_input',
!$infos->isCustomInputApplicable($vocab) ||
!$infos->canDisallowCustomInput($vocab) ||
!$vocab->allowsCustomInputs()
);
};
}

public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
{
return count($this->getVocabs());
}

protected function getVocabs(Range $range = null): array
{
if (isset($this->vocabs)) {
$vocabs = $this->vocabs;
} else {
$vocabs = iterator_to_array($this->vocab_manager->getAllVocabularies(), false);
}

if ($range) {
$vocabs = array_slice($vocabs, $range->getStart(), $range->getLength());
}

return $this->vocabs = $vocabs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

declare(strict_types=1);

use ILIAS\UI\Component\Table\DataRetrieval;
use ILIAS\UI\Component\Table\DataRowBuilder;
use ILIAS\Data\Range;
use ILIAS\Data\Order;
use ILIAS\MetaData\Vocabularies\Manager\Manager as VocabManager;
namespace ILIAS\MetaData\Settings\Vocabularies;

use ILIAS\MetaData\Presentation\ElementsInterface as ElementsPresentation;
use ILIAS\MetaData\Presentation\UtilitiesInterface as PresentationUtilities;
use ILIAS\MetaData\Vocabularies\Dispatch\Presentation\PresentationInterface as VocabPresentation;
Expand All @@ -36,85 +33,54 @@
use ILIAS\MetaData\Elements\Structure\StructureElementInterface;
use ILIAS\MetaData\Elements\Data\Type as DataType;
use ILIAS\MetaData\Vocabularies\Slots\Conditions\ConditionInterface;
use ILIAS\MetaData\Paths\FactoryInterface as PathFactory;

class ilMDVocabulariesDataRetrieval implements DataRetrieval
class Presentation
{
protected const MAX_PREVIEW_VALUES = 5;

protected VocabManager $vocab_manager;
protected ElementsPresentation $elements_presentation;
protected PresentationUtilities $presentation_utils;
protected VocabPresentation $vocab_presentation;
protected SlotHandler $slot_handler;
protected Structure $structure;
protected NavigatorFactory $navigator_factory;
protected PathFactory $path_factory;

/**
* @var VocabularyInterface[]
*/
protected array $vocabs;

public function __construct(
VocabManager $vocab_manager,
ElementsPresentation $elements_presentation,
PresentationUtilities $presentation_utils,
VocabPresentation $vocab_presentation,
SlotHandler $slot_handler,
Structure $structure,
NavigatorFactory $navigator_factory
NavigatorFactory $navigator_factory,
PathFactory $path_factory
) {
$this->vocab_manager = $vocab_manager;
$this->elements_presentation = $elements_presentation;
$this->presentation_utils = $presentation_utils;
$this->vocab_presentation = $vocab_presentation;
$this->slot_handler = $slot_handler;
$this->structure = $structure;
$this->navigator_factory = $navigator_factory;
$this->path_factory = $path_factory;
}

public function getRows(
DataRowBuilder $row_builder,
array $visible_column_ids,
Range $range,
Order $order,
?array $filter_data,
?array $additional_parameters
): Generator {
$infos = $this->vocab_manager->infos();
foreach ($this->getVocabs($range) as $vocab) {
$record = [];

$record['element'] = $this->translateSlot($vocab->slot());
$record['type'] = $this->translateType($vocab->type());
$record['source'] = $vocab->source();
$record['preview'] = $this->buildValuesPreview($vocab);
$record['active'] = $vocab->isActive();
if ($infos->isCustomInputApplicable($vocab)) {
$record['custom_input'] = $vocab->allowsCustomInputs();
}

yield $row_builder->buildDataRow(
$vocab->id(),
$record
)->withDisabledAction(
'delete',
!$infos->canBeDeleted($vocab)
)->withDisabledAction(
'toggle_active',
$vocab->isActive() && !$infos->isDeactivatable($vocab)
)->withDisabledAction(
'toggle_custom_input',
!$infos->canDisallowCustomInput($vocab)
);
};
public function txt(string $key): string
{
return $this->presentation_utils->txt($key);
}

public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
public function txtFill(string $key, string ...$values): string
{
return count($this->getVocabs());
return $this->presentation_utils->txtFill($key, ...$values);
}

protected function translateType(VocabType $type): string
public function makeTypePresentable(VocabType $type): string
{
return match ($type) {
VocabType::STANDARD => $this->presentation_utils->txt('md_vocab_type_standard'),
Expand All @@ -125,7 +91,7 @@ protected function translateType(VocabType $type): string
};
}

protected function translateSlot(SlotIdentifier $slot): string
public function makeSlotPresentable(SlotIdentifier $slot): string
{
//skip the name of the element if it does not add any information
$skip_data = [DataType::VOCAB_VALUE, DataType::STRING];
Expand Down Expand Up @@ -162,44 +128,17 @@ protected function translateSlot(SlotIdentifier $slot): string
'md_vocab_element_with_condition',
$element_name,
$condition_element_name,
$this->translateConditionValue($condition)
$this->translateConditionValue($condition, $element)
);
}

protected function translateConditionValue(ConditionInterface $condition): string
{
$slot = $this->slot_handler->identiferFromPathAndCondition($condition->path(), null, null);
return (string) $this->vocab_presentation->presentableLabels(
$this->presentation_utils,
$slot,
false,
$condition->value()
)->current()?->label();
}

protected function findFirstCommonParent(
StructureElementInterface $a,
StructureElementInterface $b
): StructureElementInterface {
while ($a !== $b) {
$a = $a->getSuperElement();
$b = $b->getSuperElement();
}
return $a;
}

protected function getStructureElementFromPath(
PathInterface $path,
StructureElementInterface $start
): StructureElementInterface {
return $this->navigator_factory->structureNavigator(
$path,
$start
)->elementAtFinalStep();
}

protected function buildValuesPreview(VocabularyInterface $vocabulary): string
{
/**
* @return string[]
*/
public function makeValuesPresentable(
VocabularyInterface $vocabulary,
int $limit = null
): array {
$presentable_values = [];
$i = 0;

Expand All @@ -208,10 +147,11 @@ protected function buildValuesPreview(VocabularyInterface $vocabulary): string
$vocabulary
);
foreach ($labelled_values as $labelled_value) {
if ($i >= self::MAX_PREVIEW_VALUES) {
if ($limit !== null && $i >= $limit) {
$presentable_values[] = '...';
break;
}
$i++;

if (
$vocabulary->type() === VocabType::STANDARD ||
Expand All @@ -228,20 +168,43 @@ protected function buildValuesPreview(VocabularyInterface $vocabulary): string
$presentable_values[] = $presentable_value;
}

return implode(', ', $presentable_values);
return $presentable_values;
}

protected function getVocabs(Range $range = null): array
{
if (isset($this->vocabs)) {
return $this->vocabs;
}
protected function translateConditionValue(
ConditionInterface $condition,
StructureElementInterface $element
): string {
$path_from_root = $this->path_factory->toElement(
$this->getStructureElementFromPath($condition->path(), $element),
);
$slot = $this->slot_handler->identiferFromPathAndCondition($path_from_root, null, null);
return (string) $this->vocab_presentation->presentableLabels(
$this->presentation_utils,
$slot,
false,
$condition->value()
)->current()?->label();
}

$vocabs = iterator_to_array($this->vocab_manager->getAllVocabularies(), false);
if ($range) {
$vocabs = array_slice($vocabs, $range->getStart(), $range->getLength());
protected function findFirstCommonParent(
StructureElementInterface $a,
StructureElementInterface $b
): StructureElementInterface {
while ($a !== $b) {
$a = $a->getSuperElement();
$b = $b->getSuperElement();
}
return $a;
}

return $this->vocabs = $vocabs;
protected function getStructureElementFromPath(
PathInterface $path,
StructureElementInterface $start
): StructureElementInterface {
return $this->navigator_factory->structureNavigator(
$path,
$start
)->elementAtFinalStep();
}
}
Loading

0 comments on commit c49090e

Please sign in to comment.