Skip to content

Commit

Permalink
Test: Fix Import of Random With Taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
kergomard committed Feb 14, 2025
1 parent 5d29fc7 commit e939b9d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 60 deletions.
73 changes: 34 additions & 39 deletions components/ILIAS/Test/classes/class.ilTestImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function importXmlRepresentation(
$question_page_parser->setQuestionMapping($qti_parser->getImportMapping());
$question_page_parser->startParsing();

$a_mapping = $this->addTexonomyAndQuestionsMapping($qti_parser->getQuestionIdMapping(), $new_obj->getId(), $a_mapping);
$a_mapping = $this->addTaxonomyAndQuestionsMapping($qti_parser->getQuestionIdMapping(), $new_obj->getId(), $a_mapping);

if ($new_obj->isRandomTest()) {
$this->importRandomQuestionSetConfig($new_obj, $xmlfile, $a_mapping);
Expand Down Expand Up @@ -139,23 +139,23 @@ public function importXmlRepresentation(
);
}

public function addTexonomyAndQuestionsMapping(
public function addTaxonomyAndQuestionsMapping(
array $question_id_mapping,
int $new_obj_id,
ilImportMapping $mapping
): ilImportMapping {
foreach ($question_id_mapping as $old_question_id => $new_question_id) {
$mapping->addMapping(
'Services/Taxonomy',
'components/ILIAS/Taxonomy',
'tax_item',
"tst:quest:$old_question_id",
"tst:quest:{$old_question_id}",
(string) $new_question_id
);

$mapping->addMapping(
'Services/Taxonomy',
'components/ILIAS/Taxonomy',
'tax_item_obj_id',
"tst:quest:$old_question_id",
"tst:quest:{$old_question_id}",
(string) $new_obj_id
);

Expand Down Expand Up @@ -194,47 +194,43 @@ public function finalProcessing(ilImportMapping $a_mapping): void

protected function finalRandomTestTaxonomyProcessing(
ilImportMapping $mapping,
string $oldTstObjId,
string $newTstObjId,
string $old_tst_obj_id,
string $new_tst_obj_id,
ilObjTest $test_obj
): void {
$new_tax_ids = $mapping->getMapping(
'components/ILIAS/Taxonomy',
'tax_usage_of_obj',
$oldTstObjId
$old_tst_obj_id
);

if ($new_tax_ids !== null) {
$tax_ids = explode(":", $new_tax_ids);

foreach ($tax_ids as $tid) {
ilObjTaxonomy::saveUsage((int) $tid, (int) $newTstObjId);
foreach (explode(':', $new_tax_ids) as $tax_id) {
ilObjTaxonomy::saveUsage((int) $tax_id, (int) $new_tst_obj_id);
}
}

$srcPoolDefFactory = new ilTestRandomQuestionSetSourcePoolDefinitionFactory(
$this->db,
$test_obj
);

$srcPoolDefList = new ilTestRandomQuestionSetSourcePoolDefinitionList(
$src_pool_def_list = new ilTestRandomQuestionSetSourcePoolDefinitionList(
$this->db,
$test_obj,
$srcPoolDefFactory
new ilTestRandomQuestionSetSourcePoolDefinitionFactory(
$this->db,
$test_obj
)
);

$srcPoolDefList->loadDefinitions();
$src_pool_def_list->loadDefinitions();

foreach ($srcPoolDefList as $definition) {
// #21330
if (!is_array($definition->getMappedTaxonomyFilter()) || 0 === count($definition->getMappedTaxonomyFilter())) {
foreach ($src_pool_def_list as $definition) {
$mapped_taxonomy_filter = $definition->getMappedTaxonomyFilter();
if ($mapped_taxonomy_filter === []) {
continue;
}

$definition->setMappedTaxonomyFilter(
$this->getNewMappedTaxonomyFilter(
$mapping,
$definition->getMappedTaxonomyFilter()
$mapped_taxonomy_filter
)
);
$definition->saveToDb();
Expand All @@ -243,39 +239,38 @@ protected function finalRandomTestTaxonomyProcessing(

protected function getNewMappedTaxonomyFilter(
ilImportMapping $mapping,
array $mappedFilter
array $mapped_filter
): array {
$newMappedFilter = [];

foreach ($mappedFilter as $taxId => $taxNodes) {
$newTaxId = $mapping->getMapping(
$new_mapped_filter = [];
foreach ($mapped_filter as $tax_id => $tax_nodes) {
$new_tax_id = $mapping->getMapping(
'components/ILIAS/Taxonomy',
'tax',
(string) $taxId
(string) $tax_id
);

if (!$newTaxId) {
if ($new_tax_id === null) {
continue;
}

$newMappedFilter[$newTaxId] = [];
$new_mapped_filter[$new_tax_id] = [];

foreach ($taxNodes as $taxNodeId) {
$newTaxNodeId = $mapping->getMapping(
foreach ($tax_nodes as $tax_node_id) {
$new_tax_node_id = $mapping->getMapping(
'components/ILIAS/Taxonomy',
'tax_tree',
(string) $taxNodeId
(string) $tax_node_id
);

if (!$newTaxNodeId) {
if ($new_tax_node_id === null) {
continue;
}

$newMappedFilter[$newTaxId][] = $newTaxNodeId;
$new_mapped_filter[$new_tax_id][] = $new_tax_node_id;
}
}

return $newMappedFilter;
return $new_mapped_filter;
}

public function importRandomQuestionSetConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public function testPoolId(): void
public function testAddTaxonomyFilter(): void
{
$taxId = 20;
$taxNodes = 'test';
$taxNodes = [134, 345];
$this->testObj->addTaxonomyFilter($taxId, $taxNodes);
$this->assertEquals([$taxId => $taxNodes], $this->testObj->getTaxonomyFilters());
}

public function testTypeFilter(): void
{
$typeFilter = 5;
$typeFilter = [547, 1023];
$this->testObj->setTypeFilter($typeFilter);
$this->assertEquals($typeFilter, $this->testObj->getTypeFilter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ class ilTestQuestionPoolImporter extends ilXmlImporter
{
use TestQuestionsImportTrait;

/**
* @var ilObjQuestionPool
*/
private $pool_obj;
private ilObjUser $user;

private ilObjQuestionPool $pool_obj;
protected readonly RequestDataCollector $request_data_collector;

public function __construct()
Expand All @@ -50,11 +45,12 @@ public function __construct()
$this->request_data_collector = $local_dic['request_data_collector'];
}

/**
* Import XML
*/
public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
{
public function importXmlRepresentation(
string $a_entity,
string $a_id,
string $a_xml,
ilImportMapping $a_mapping
): void {
global $DIC;
// Container import => pool object already created
if (($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_id)) !== null) {
Expand Down Expand Up @@ -126,28 +122,28 @@ public function importXmlRepresentation(string $a_entity, string $a_id, string $
$questionPageParser->startParsing();

foreach ($qtiParser->getImportMapping() as $k => $v) {
$oldQuestionId = substr($k, strpos($k, 'qst_') + strlen('qst_'));
$newQuestionId = (string) $v['pool']; // yes, this is the new question id ^^
$old_question_id = substr($k, strpos($k, 'qst_') + strlen('qst_'));
$new_question_id = (string) $v['pool']; // yes, this is the new question id ^^

$a_mapping->addMapping(
'components/ILIAS/Taxonomy',
'tax_item',
'qpl:quest:$oldQuestionId',
$newQuestionId
"qpl:quest:{$old_question_id}",
$new_question_id
);

$a_mapping->addMapping(
'components/ILIAS/Taxonomy',
'tax_item_obj_id',
'qpl:quest:$oldQuestionId',
"qpl:quest:{$old_question_id}",
(string) $new_obj->getId()
);

$a_mapping->addMapping(
'components/ILIAS/TestQuestionPool',
'quest',
$oldQuestionId,
$newQuestionId
$old_question_id,
$new_question_id
);
}

Expand Down Expand Up @@ -175,7 +171,6 @@ public function finalProcessing(ilImportMapping $a_mapping): void
$maps = $a_mapping->getMappingsOfEntity('components/ILIAS/TestQuestionPool', 'qpl');
foreach ($maps as $old => $new) {
if ($old !== 'new_id' && (int) $old > 0) {
// get all new taxonomys of this object
$new_tax_ids = $a_mapping->getMapping('components/ILIAS/Taxonomy', 'tax_usage_of_obj', (string) $old);
if ($new_tax_ids !== null) {
$tax_ids = explode(':', $new_tax_ids);
Expand Down

0 comments on commit e939b9d

Please sign in to comment.