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 Oct 1, 2024
1 parent ae363ec commit 4237abc
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
*
*********************************************************************/

/**
* Class ilDclBaseFieldModel
* @author Michael Herren <[email protected]>
* @author Martin Studer <[email protected]>
* @author Marcel Raimann <[email protected]>
* @author Fabian Schmid <[email protected]>
* @author Oskar Truffer <[email protected]>
* @ingroup ModulesDataCollection
*/
class ilDclBaseFieldModel
{
protected string $id = "";
Expand Down Expand Up @@ -716,11 +707,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 @@ -16,11 +16,6 @@
*
*********************************************************************/

/**
* Class ilDclBaseFieldRepresentation
* @author Michael Herren <[email protected]>
* @version 1.0.0
*/
abstract class ilDclBaseFieldRepresentation
{
protected ilDclBaseFieldModel $field;
Expand Down Expand Up @@ -142,7 +137,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 +155,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
26 changes: 12 additions & 14 deletions Modules/DataCollection/classes/Fields/Base/class.ilDclDatatype.php
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,19 +14,8 @@
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
********************************************************************
*/
*********************************************************************/

/**
* Class ilDclDatatype
* @author Martin Studer <[email protected]>
* @author Marcel Raimann <[email protected]>
* @author Fabian Schmid <[email protected]>
* @author Oskar Truffer <[email protected]>
* @author Stefan Wanzenried <[email protected]>
* @version $Id:
* @ingroup ModulesDataCollection
*/
class ilDclDatatype
{
public const INPUTFORMAT_NONE = 0;
Expand All @@ -40,6 +30,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 +113,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 +128,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;
}
}
15 changes: 3 additions & 12 deletions Modules/DataCollection/classes/Fields/class.ilDclFieldEditGUI.php
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,18 +14,8 @@
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
********************************************************************
*/
*********************************************************************/

/**
* Class ilDclFieldEditGUI
* @author Martin Studer <[email protected]>
* @author Marcel Raimann <[email protected]>
* @author Fabian Schmid <[email protected]>
* @author Oskar Truffer <[email protected]>
* @version $Id:
* @ingroup ModulesDataCollection
*/
class ilDclFieldEditGUI
{
protected int $obj_id;
Expand Down Expand Up @@ -261,7 +252,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
63 changes: 14 additions & 49 deletions Modules/DataCollection/classes/Fields/class.ilDclFieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*
*********************************************************************/

/**
* Class ilDclFieldFactory
* This Class handles the creation of all field-classes
* @author Michael Herren <[email protected]>
* @version 1.0.0
*/
class ilDclFieldFactory
{
public static string $field_base_path_patter = "./Modules/DataCollection/classes/Fields/%s/";
Expand All @@ -39,10 +33,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 +58,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 +214,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 +249,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
Loading

0 comments on commit 4237abc

Please sign in to comment.