Skip to content

Commit

Permalink
refactor dc plugin fields
Browse files Browse the repository at this point in the history
  • Loading branch information
iszmais committed Sep 30, 2024
1 parent 54c8fe0 commit cb4471b
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,17 @@ public function checkFieldCreationInput(ilPropertyFormGUI $form): bool
return true;
}

/**
* @deprecated
*/
public function getStorageLocationOverride(): ?int
{
return $this->storage_location_override;
}

/**
* @deprecated override ilDclFieldTypePlugin::getStorageLocation() instead
*/
public function setStorageLocationOverride(?int $storage_location_override): void
{
$this->storage_location_override = $storage_location_override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function addFieldCreationForm(
): void {
$opt = $this->buildFieldCreationInput($dcl, $mode);
if ($opt !== null) {
if ($mode != 'create' && $this->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
if ($mode != 'create' && ilDclFieldTypePlugin::isPluginDatatype($this->getField()->getDatatype()->getTitle())) {
$new_plugin_title = $opt->getTitle();
$plugin_name = ilDclFieldFactory::getPluginNameFromFieldModel($this->getField());
if ($plugin_name !== "DclBase") {
Expand All @@ -160,11 +160,15 @@ public function addFieldCreationForm(
*/
protected function buildFieldCreationInput(ilObjDataCollection $dcl, string $mode = 'create'): ?ilRadioOption
{
$opt = new ilRadioOption(
$this->lng->txt('dcl_' . $this->getField()->getDatatype()->getTitle()),
$this->getField()->getDatatypeId()
);
$opt->setInfo($this->lng->txt('dcl_' . $this->getField()->getDatatype()->getTitle() . '_desc'));
$title = $this->lng->txt('dcl_' . $this->getField()->getDatatype()->getTitle());
$info = $this->lng->txt('dcl_' . $this->getField()->getDatatype()->getTitle() . '_desc');
if (ilDclFieldTypePlugin::isPluginDatatype($this->field->getDatatype()->getTitle())) {
$plugin = $this->component_factory->getPlugin(ilDclFieldTypePlugin::getPluginId($this->field->getDatatype()->getTitle()));
$title = (!str_ends_with($plugin->txt('field_type_name'), 'field_type_name-')) ? $plugin->txt('field_type_name') : $plugin->getPluginName();
$info = (!str_ends_with($plugin->txt('field_type_info'), 'field_type_info-')) ? $plugin->txt('field_type_info') : '';
}
$opt = new ilRadioOption($title, $this->getField()->getDatatypeId());
$opt->setInfo($info);

return $opt;
}
Expand Down
12 changes: 10 additions & 2 deletions Modules/DataCollection/classes/Fields/Base/class.ilDclDatatype.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ilDclDatatype
public const INPUTFORMAT_MOB = 9;
public const INPUTFORMAT_REFERENCELIST = 10;
public const INPUTFORMAT_FORMULA = 11;
/** @deprecated */
public const INPUTFORMAT_PLUGIN = 12;
public const INPUTFORMAT_TEXT_SELECTION = 14;
public const INPUTFORMAT_DATE_SELECTION = 15;
Expand Down Expand Up @@ -122,7 +123,7 @@ public function doRead(): void
/**
* Get all possible Datatypes
*/
public static function getAllDatatype(): array
public static function getAllDatatype(bool $force = false): array
{
global $DIC;
$ilDB = $DIC['ilDB'];
Expand All @@ -137,7 +138,14 @@ public static function getAllDatatype(): array
$instance = new ilDclDatatype();
$instance->loadDatatype($rec);

self::$datatype_cache[$rec['id']] = $instance;
if (
$force ||
!ilDclFieldTypePlugin::isPluginDatatype($instance->getTitle()) ||
$DIC['component.repository']->hasActivatedPlugin(ilDclFieldTypePlugin::getPluginId($instance->getTitle()))
) {
self::$datatype_cache[$rec['id']] = $instance;
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -13,56 +14,65 @@
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
********************************************************************
*/
*********************************************************************/

declare(strict_types=1);

/**
* Class ilDclFieldTypePlugin
* Definition of the PluginHook
* @author Michael Herren
* @extends ilPlugin
*/
abstract class ilDclFieldTypePlugin extends ilPlugin
{
public const DB_TYPES = ['text', 'text', 'integer', 'date'];
public const COMPONENT_NAME = "DataCollection";
public const SLOT_NAME = "FieldTypeHook";
public const SLOT_ID = "dclfth";
public const PLUGIN_SLOT_PREFIX = 'plugin_fth_';

/**
* @var ilDclFieldTypePlugin[] singleton-instance
*/
protected static array $instances = array();

/**
* Singleton for abstract class
*/
public static function getInstance(): ilDclFieldTypePlugin
public function install(): void
{
$class = get_called_class();
if (!isset(self::$instances[$class])) {
self::$instances[$class] = new $class();
$field_type_name = ilDclFieldTypePlugin::PLUGIN_SLOT_PREFIX . $this->getId();
$datatypes = ilDclDatatype::getAllDatatype(true);
foreach ($datatypes as $datatype) {
if ($datatype->getTitle() === $field_type_name) {
parent::install();
return;
}
}

return self::$instances[$class];
$field_model_class = 'il' . $this->getPluginName() . 'FieldModel';
$type = (new $field_model_class())->getStorageLocationOverride() ?? $this->getStorageLocation();
$this->db->manipulateF(
'INSERT INTO il_dcl_datatype (id, title, ildb_type, storage_location, sort) SELECT GREATEST(MAX(id), 1000) + 1, %s, %s, %s, GREATEST(MAX(sort), 10000) + 10 FROM il_dcl_datatype;',
[
ilDBConstants::T_TEXT,
ilDBConstants::T_TEXT,
ilDBConstants::T_INTEGER,
],
[
$field_type_name,
$this::DB_TYPES[$type],
$type
]
);
}

public function getPluginTablePrefix(): string
public function getStorageLocation(): int
{
return $this->getLanguageHandler()->getPrefix();
return 1;
}

public function getPluginTableName(): string
public static function getDataType(string $plugin_id): string
{
return $this->getPluginTablePrefix() . "_props";
return self::PLUGIN_SLOT_PREFIX . $plugin_id;
}

public function getPluginConfigTableName(): string
public static function getPluginId(string $datatype): string
{
return $this->getPluginTablePrefix() . "_config";
if (self::isPluginDatatype($datatype)) {
return substr($datatype, strlen(self::PLUGIN_SLOT_PREFIX));
}
throw new ilPluginException('Invalid datatype prefix for FieldTypHook-plugin');
}

public function getPluginClassPrefix(): string
public static function isPluginDatatype(string $datatype): bool
{
return 'il';
return substr($datatype, 0, strlen(self::PLUGIN_SLOT_PREFIX)) === self::PLUGIN_SLOT_PREFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function initForm(string $a_mode = "create"): void
$model = new ilDclBaseFieldModel();
$model->setDatatypeId($datatype->getId());

if ($a_mode == 'edit' && $datatype->getId() == $this->field_obj->getDatatypeId()) {
if ($a_mode == 'edit' && $datatype->getId() === $this->field_obj->getDatatypeId()) {
$model = $this->field_obj;
}

Expand Down
57 changes: 14 additions & 43 deletions Modules/DataCollection/classes/Fields/class.ilDclFieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ class ilDclFieldFactory
* @throws ilDclException
* @throws Exception
*/
public static function getRecordFieldInstance(
object $field, //object|ilDclBaseFieldModel
object $record //object|ilDclBaseRecordModel
): ?ilDclBaseRecordFieldModel {
public static function getRecordFieldInstance(object $field, object $record): ilDclBaseRecordFieldModel
{
if (!empty(self::$record_field_cache[$field->getId()][$record->getId()])) {
return self::$record_field_cache[$field->getId()][$record->getId()];
}
Expand All @@ -66,7 +64,6 @@ public static function getRecordFieldInstance(
}

throw new RuntimeException("file not found " . $path);
return null;
}

protected static array $field_class_cache = array();
Expand Down Expand Up @@ -223,34 +220,23 @@ public static function getFieldModelInstanceByClass(
public static function getFieldTypeByInstance(ilDclBaseFieldModel $field): string
{
global $DIC;
$component_factory = $DIC["component.factory"];
$component_repository = $DIC["component.repository"];
$datatype = $field->getDatatype();

if (!empty(self::$field_type_cache[$datatype->getId()])) {
if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
if (!empty(self::$field_type_cache[$datatype->getId()][$field->getId()])) {
return self::$field_type_cache[$datatype->getId()][$field->getId()];
}
} else {
return self::$field_type_cache[$datatype->getId()];
}
return self::$field_type_cache[$datatype->getId()];
}

if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
if ($field->hasProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME)) {
$pd = $component_repository->getPluginByName($field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME));
$plugin_data = $component_factory->getPlugin($pd->getId());
$fieldtype = $plugin_data->getPluginClassPrefix() . ucfirst($plugin_data->getPluginName());
if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
$plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
$fieldtype = 'il' . $DIC["component.repository"]->getPluginById($plugin_id)->getName();
} else {
$fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
$fieldtype = '';
}
self::$field_type_cache[$datatype->getId()][$field->getId()] = $fieldtype;
} else {
$fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
self::$field_type_cache[$datatype->getId()] = $fieldtype;
}

self::$field_type_cache[$datatype->getId()] = $fieldtype;
return $fieldtype;
}

Expand All @@ -269,33 +255,18 @@ public static function getClassByInstance(ilDclBaseFieldModel $field, string $cl
public static function getClassPathByInstance(ilDclBaseFieldModel $field, string $class_pattern): string
{
global $DIC;
$component_factory = $DIC["component.factory"];
$component_repository = $DIC["component.repository"];
$datatype = $field->getDatatype();

if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
return self::$class_path_cache[$field->getId()][$class_pattern];
}

if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
if ($field->hasProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME)) {
if (!$component_repository->getPluginSlotById(ilDclFieldTypePlugin::SLOT_ID)->hasPluginName($field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME))) {
throw new ilDclException(
"Something went wrong by initializing the FieldHook-Plugin '"
. $field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME) . "' on Component '"
. ilDclFieldTypePlugin::COMPONENT_NAME . "' with slot '" . ilDclFieldTypePlugin::SLOT_ID . "' on field: "
. $field->getTitle()
);
}
$pd = $component_repository->getPluginByName($field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME));
$plugin_data = $component_factory->getPlugin($pd->getId());

$class_path = $plugin_data->getDirectory() . "/classes/";
if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
$plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
$class_path = $DIC["component.repository"]->getPluginById($plugin_id)->getPath() . '/classes/';
} else {
$class_path = sprintf(
self::$field_base_path_patter,
ucfirst(self::parseDatatypeTitle($datatype->getTitle()))
);
return '';
}
} else {
$class_path = sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __construct(ilDclFieldListGUI $a_parent_obj, string $a_parent_cm

$this->parent_obj = $a_parent_obj;
$this->table = ilDclCache::getTableCache($table_id);
$this->table->showInvalidFields(true);

$this->setId('dcl_field_list');
$this->addColumn('', '', '1', true);
Expand Down Expand Up @@ -282,7 +283,9 @@ public function fillRowFromObject(ilDclBaseFieldModel $a_set): void
$this->parent_obj->getDataCollectionObject()->getRefId(),
$this->table->getId()
)) {
$alist->addItem($lng->txt('edit'), 'edit', $ilCtrl->getLinkTargetByClass('ildclfieldeditgui', 'edit'));
if (in_array($a_set->getDatatypeId(), array_keys(ilDclDatatype::getAllDatatype()))) {
$alist->addItem($lng->txt('edit'), 'edit', $ilCtrl->getLinkTargetByClass('ildclfieldeditgui', 'edit'));
}
$alist->addItem(
$lng->txt('delete'),
'delete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,51 @@
*********************************************************************/

use ILIAS\Setup;
use ILIAS\Setup\Config;
use ILIAS\Setup\Objective;
use ILIAS\Setup\Metrics\Storage;
use ILIAS\Setup\Objective\NullObjective;
use ILIAS\Refinery\Transformation;

class ilDataCollectionAgent extends Setup\Agent\NullAgent
class ilDataCollectionAgent implements Setup\Agent
{
public function getUpdateObjective(Setup\Config $config = null): Setup\Objective
public function getUpdateObjective(Config $config = null): Objective
{
return new \ilDatabaseUpdateStepsExecutedObjective(new ilDataCollectionDBUpdateSteps());
return new ilDataCollectionObjective(new ilDataCollectionDBUpdateSteps());
}

public function hasConfig(): bool
{
return false;
}

public function getArrayToConfigTransformation(): Transformation
{
throw new LogicException(self::class . " has no config.");
}

public function getInstallObjective(Config $config = null): Objective
{
return new NullObjective();
}

public function getBuildArtifactObjective(): Objective
{
return new NullObjective();
}

public function getStatusObjective(Storage $storage): Objective
{
return new NullObjective();
}

public function getMigrations(): array
{
return [];
}

public function getNamedObjectives(?Config $config = null): array
{
return [];
}
}
Loading

0 comments on commit cb4471b

Please sign in to comment.