Skip to content

Commit

Permalink
IASS: custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaagen committed Feb 28, 2025
1 parent 30bca1a commit 64c32a5
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ public function updateMember(ilIndividualAssessmentMember $member): void

$this->db->update(self::MEMBERS_TABLE, $values, $where);
$this->custom_field_storage->updateUserValues(...$member->getGrading()->getCustomFields());

}

protected function getActualDateTime(): string
Expand All @@ -210,6 +209,17 @@ public function deleteMembers(ilObjIndividualAssessment $obj): void
$this->db->manipulate($sql);
}

public function deleteCustomFieldsForObj(ilObjIndividualAssessment $obj): void
{
$this->custom_field_storage->deleteAllUserValuesAndFields(
$this->irss,
$this->stakeholder,
$obj->getId()
);
}



protected function loadMemberQuery(): string
{
return "SELECT "
Expand Down Expand Up @@ -374,6 +384,13 @@ public function removeMembersRecord(ilObjIndividualAssessment $iass, array $reco
;

$this->db->manipulate($sql);
$this->custom_field_storage->deleteUserValues(
$this->irss,
$this->stakeholder,
$iass->getId(),
$record[ilIndividualAssessmentMembers::FIELD_USR_ID]
);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function delete(): bool
$this->deleteMetaData();
$this->settings_storage->deleteSettings($this);
$this->members_storage->deleteMembers($this);
$this->members_storage->deleteCustomFieldsForObj($this);
return parent::delete();
}

Expand Down Expand Up @@ -245,6 +246,7 @@ public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree =
$new_obj->settings_storage->updateSettings($new_settings);
$new_obj->settings_storage->updateInfoSettings($new_info_settings);

(new IAFPCollector())->cloneFields($this->getId(), $new_obj->getId());
return $new_obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function toFormInput(
bool $file_required = false,
bool $amend = false
): \ILIAS\UI\Component\Input\Container\Form\FormInput {

$name = $input
->text($lng->txt('name'), '')
->withDisabled(true)
Expand Down Expand Up @@ -220,25 +221,25 @@ public function toFormInput(
;


$custom = [];
$custom_fields = $this->custom_fields;
foreach ($custom_fields as $cf) {
$custom[$cf->getFieldId()] = $cf->toFormInput($input, $refinery, $file_handler);
}

$fields = [
'name' => $name,
'record' => $record,
'internal_note' => $internal_note,
'file' => $file,
'file_visible' => $file_visible,
'learning_progress' => $learning_progress,
'place' => $place,
'event_time' => $event_time,
'custom' => $input->group($custom),
'learning_progress' => $learning_progress,
'notify' => $notify
];

$custom = [];
$custom_fields = $this->custom_fields;
foreach ($custom_fields as $cf) {
$custom[$cf->getFieldId()] = $cf->toFormInput($input, $refinery);
}
$fields['custom'] = $input->group($custom);

if (!$amend) {
$finalized = $input
->checkbox($lng->txt('iass_finalize'), $lng->txt('iass_finalize_info'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,45 @@ public function copyFieldsToIASS(int $iafp_form_id, int $iass_obj_id): void
if ($config = $field->getConfig()) {
list($source_table, $target_table) = self::CONFIG_TABLES[get_class($config)];

$query = 'SELECT GROUP_CONCAT(COLUMN_NAME) as fields FROM INFORMATION_SCHEMA.COLUMNS'
. ' WHERE TABLE_NAME = "' . $source_table . '"';
$res = $this->db->query($query);
$fields = $this->db->fetchAssoc($res)['fields'];

$fields = $this->getFieldsFromTable($source_table);
$query = 'REPLACE INTO ' . $target_table . '(obj_id, ' . $fields . ')' . PHP_EOL
. 'SELECT ' . $this->db->quote($iass_obj_id, 'integer') . ',' . $fields . PHP_EOL
. 'FROM ' . $source_table;
. 'FROM ' . $source_table . PHP_EOL
. 'WHERE field_id = ' . $field->getFieldId();

$this->db->manipulate($query);
}
}
}

protected function getFieldsFromTable(string $table_name): string
{
$query = 'SELECT GROUP_CONCAT(COLUMN_NAME) as fields FROM INFORMATION_SCHEMA.COLUMNS' . PHP_EOL
. 'WHERE COLUMN_NAME <> "obj_id" AND TABLE_NAME = ' . $this->db->quote($table_name, 'text');
$res = $this->db->query($query);
return $this->db->fetchAssoc($res)['fields'];
}

/**
* clone fields (on copy of object)
*/
public function cloneFields(int $source_iass_obj_id, int $target_iass_obj_id): void
{
$tables = [
'iass_formfields',
'iass_cfg_singleselect',
'iass_cfg_tag',
];
foreach ($tables as $table) {
$fields = $this->getFieldsFromTable($table);
$query = 'INSERT INTO ' . $table . ' (obj_id,' . $fields . ')' . PHP_EOL
. 'SELECT '
. $this->db->quote($target_iass_obj_id, 'integer') . ',' . $fields . PHP_EOL
. 'FROM ' . $table . ' WHERE obj_id = ' . $this->db->quote($source_iass_obj_id, 'integer');
$this->db->manipulate($query);
}
}

protected function getFormPools(): array
{
$query = 'SELECT ref.obj_id, ref.ref_id, od.title FROM object_data od' . PHP_EOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ILIAS\Refinery\Factory as Refinery;
use ILIAS\UI\Implementation\Component\Input\Field\FormInput;
use ILIAS\IndividualAssessmentFormPool\FieldType;
use ILIAS\UI\Component\Input\Field\UploadHandler;

class IASSCustomField
{
Expand Down Expand Up @@ -74,7 +75,8 @@ public function getValue(): null|string|array

public function toFormInput(
FieldFactory $factory,
Refinery $refinery
Refinery $refinery,
UploadHandler $upload_handler
): FormInput {
$value = $this->value ?? $this->default;
switch ($this->type) {
Expand All @@ -85,9 +87,10 @@ public function toFormInput(
break;

case FieldType::FILE:
$upload_handler = new \ilUIDemoFileUploadHandlerGUI();
$field = $factory->file($upload_handler, $this->label, $this->description);
//->withValue((string) $value);
if ($value !== '') {
$field = $field->withValue([$value]);
}
break;

case FieldType::DATETIME:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use ILIAS\UI\Factory as UIFactory;
*/
use ILIAS\IndividualAssessmentFormPool\FieldType;
use ILIAS\ResourceStorage\Services as IRSS;

class IASSCustomFieldsStorageDB
{
Expand All @@ -48,8 +49,8 @@ public function getCustomFields(
. ',val.value, notes.value as note' . PHP_EOL
. ', GROUP_CONCAT(
COALESCE(
CONCAT(cfg_s.value,"=>", cfg_s.label),
CONCAT(cfg_t.value,"=>", cfg_t.label)
CONCAT(cfg_s.value),
CONCAT(cfg_t.value)
)
) AS options' . PHP_EOL

Expand All @@ -74,10 +75,7 @@ public function getCustomFields(
while ($row = $this->db->fetchAssoc($res)) {
$options = [];
if ($row['options'] !== null) {
foreach (explode(',', $row['options']) as $opt) {
list($k, $v) = explode('=>', $opt);
$options[$k] = $v;
}
$options = explode(',', $row['options']);
}
$ret[] = new IASSCustomField(
$obj_id,
Expand Down Expand Up @@ -130,4 +128,87 @@ public function updateUserValues(IASSCustomField ...$fields): void
}
}

public function deleteUserValues(
IRSS $irss,
ilIndividualAssessmentGradingStakeholder $stakeholder,
int $iass_obj_id,
int $member_usr_id,
): void {
$this->deleteFileResources(
$irss,
$stakeholder,
...$this->getResourceIdentifiers($iass_obj_id, $member_usr_id)
);

$tables = [
'iass_cust_values',
'iass_cust_notes'
];
foreach ($tables as $table) {
$query = 'DELETE FROM ' . $table . PHP_EOL
. 'WHERE obj_id = ' . $this->db->quote($iass_obj_id, 'integer') . PHP_EOL
. 'AND usr_id = ' . $this->db->quote($member_usr_id, 'integer');
$this->db->manipulate($query);
}
}

public function deleteAllUserValuesAndFields(
IRSS $irss,
ilIndividualAssessmentGradingStakeholder $stakeholder,
int $iass_obj_id
): void {
$this->deleteFileResources(
$irss,
$stakeholder,
...$this->getResourceIdentifiers($iass_obj_id)
);

$tables = [
'iass_cfg_singleselect',
'iass_cfg_tag',
'iass_cust_values',
'iass_cust_notes',
'iass_formfields',
];
foreach ($tables as $table) {
$query = 'DELETE FROM ' . $table . PHP_EOL
. 'WHERE obj_id = ' . $this->db->quote($iass_obj_id, 'integer');
$this->db->manipulate($query);
}
}

protected function deleteFileResources(
IRSS $irss,
ilIndividualAssessmentGradingStakeholder $stakeholder,
string ...$ids
): void {
foreach ($ids as $identifier) {
$resource_id = $irss->manage()->find($identifier);
if ($resource_id !== null) {
$irss->manage()->remove($resource_id, $stakeholder);
}
}
}

protected function getResourceIdentifiers(int $obj_id, int ...$usr_ids): array
{
$query = 'SELECT value FROM iass_cust_values ival' . PHP_EOL
. 'JOIN iass_formfields ifield' . PHP_EOL
. 'ON ival.obj_id = ifield.obj_id' . PHP_EOL
. 'AND ival.field_id = ifield.field_id' . PHP_EOL
. 'WHERE ival.obj_id = ' . $this->db->quote($obj_id, 'integer') . PHP_EOL
. 'AND ifield.type = ' . FieldType::FILE->value;

if ($usr_ids !== []) {
$query .= ' AND ' . $this->db->in('ival.usr_id', $usr_ids, false, 'integer');
}

$ret = [];
$res = $this->db->query($query);
while ($row = $this->db->fetchAssoc($res)) {
$ret[] = $row['value'];
}
return $ret;
}

}
3 changes: 1 addition & 2 deletions Modules/IndividualAssessmentFormPool/classes/Forms/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ public function toFormInput(

$inputs = [];
$inputs['id'] = $factory->hidden()
->withAdditionalTransformation($refinery->to()->int())
->withAdditionalTransformation($refinery->kindlyTo()->int())
->withValue($this->getFieldId());
$inputs['id'] = $factory->numeric($lng->txt('id'))->withValue($this->getFieldId());

$inputs['type'] = $factory->select($lng->txt('field_type'), $type_options, $lng->txt('field_type_byline'))
->withRequired(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function toFormInput(
Refinery $refinery
): FormInput {
return $factory->group([ //group is needed due to exisiting transforms on tag-input
$factory->tag($lng->txt('field_options'), [])
$factory->tag($lng->txt('field_options'), [], $lng->txt('field_options_byline'))
->withUserCreatedTagsAllowed(true)
->withRequired(true)
->withValue($this->getOptions())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function toFormInput(
Refinery $refinery
): FormInput {
return $factory->group([ //group is needed due to exisiting transforms on tag-input
$factory->tag($lng->txt('field_options'), [])
$factory->tag($lng->txt('field_options'), [], $lng->txt('field_options_byline'))
->withUserCreatedTagsAllowed(true)
->withRequired(true)
->withValue($this->getOptions())
Expand Down
5 changes: 2 additions & 3 deletions Modules/IndividualAssessmentFormPool/classes/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ public function toFormInput(

return $factory->section(
[
//'id' => $factory->hidden($lng->txt('id'))
'id' => $factory->numeric($lng->txt('id'))
'id' => $factory->hidden()
->withValue($this->getFormId())
->withAdditionalTransformation($refinery->to()->int()),
->withAdditionalTransformation($refinery->kindlyTo()->int()),
'name' => $factory->text($lng->txt('form_name'), $lng->txt('form_name_byline'))
->withRequired(true)
->withValue($this->getName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function storeForm(Form $form): int
foreach ($form->getFields() as $field) {
$position += 10;
$query = 'INSERT INTO iafp_fieldmap (form_id, field_id, position) VALUES (' . PHP_EOL
. $this->db->quote($form->getFormId(), 'integer') . ','
. $this->db->quote($id, 'integer') . ','
. $this->db->quote($field->getFieldId(), 'integer') . ','
. $this->db->quote($position, 'integer')
. ')';
Expand Down
Loading

0 comments on commit 64c32a5

Please sign in to comment.