ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilTestRandomQuestionSetPoolDeriver.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
30 
31  public function __construct(
32  private readonly ilDBInterface $db,
33  private readonly ilComponentRepository $component_repository,
34  private readonly ilObjTest $test_obj,
35  private readonly ilTestRandomQuestionSetSourcePoolDefinitionList $source_pool_definition_list,
36  private int $owner_id,
37  private int $target_container_ref
38  ) {
39  $this->pool_factory = new ilQuestionPoolFactory();
40  }
41 
42  public function derive(
43  ilTestRandomQuestionSetNonAvailablePool $non_available_pool
45  $pool = $this->createNewPool($non_available_pool);
46  $questions = $this->getQuestionsForPool($non_available_pool);
47  $pool_qid_by_test_qid_map = $this->copyQuestionsToPool($pool, $questions);
48 
49  $this->updateTestQuestionStage($pool_qid_by_test_qid_map);
51  $this->duplicateTaxonomies($pool_qid_by_test_qid_map, $pool),
52  $pool->getId(),
53  $non_available_pool->getId()
54  );
55 
56  return $pool;
57  }
58 
59  private function getQuestionsForPool(
60  ilTestRandomQuestionSetNonAvailablePool $non_available_pool
61  ): array {
63  $this->db,
64  $this->component_repository
65  );
66  $question_list->setTestObjId($this->test_obj->getId());
67  $question_list->setTestId($this->test_obj->getTestId());
68  $question_list->setPoolId($non_available_pool->getId());
69  $question_list->loadQuestions();
70 
71  $questions = [];
72  foreach ($question_list->getQuestions() as $question_id) {
73  $questions[] = assQuestion::instantiateQuestion($question_id);
74  }
75 
76  return $questions;
77  }
78 
79  private function createNewPool(
80  ilTestRandomQuestionSetNonAvailablePool $non_available_pool
82  $pool = $this->pool_factory->createNewInstance($this->target_container_ref);
83 
84  if ($non_available_pool->getTitle() !== '') {
85  $pool->setTitle($non_available_pool->getTitle());
86  $pool->update();
87  }
88 
89  return $pool;
90  }
91 
92  private function copyQuestionsToPool(
93  ilObjQuestionPool $pool,
94  array $questions
95  ): array {
96  $pool_qid_by_test_qid_map = [];
97  foreach ($questions as $question_obj) {
98  $pool_qid_by_test_qid_map[$question_obj->getId()] = $question_obj
99  ->duplicate(false, '', '', $this->owner_id, $pool->getId());
100  }
101 
102  return $pool_qid_by_test_qid_map;
103  }
104 
105  private function updateTestQuestionStage(
106  array $pool_qid_by_test_qid_map
107  ): void {
108  foreach ($pool_qid_by_test_qid_map as $test_qid => $pool_qid) {
109  assQuestion::resetOriginalId($pool_qid);
110  assQuestion::saveOriginalId($test_qid, $pool_qid);
111  }
112  }
113 
115  array $taxonomy_ids,
116  array $related_question_ids
117  ): array {
118  $filtered_tax_ids = [];
119  foreach ($taxonomy_ids as $taxonomy_id) {
120  $tax_node_assignment = new ilTaxNodeAssignment(
121  $this->test_obj->getType(),
122  $this->test_obj->getId(),
123  'quest',
124  $taxonomy_id
125  );
126 
127  foreach ($related_question_ids as $question_id) {
128  if ($tax_node_assignment->getAssignmentsOfItem($question_id) !== []) {
129  $filtered_tax_ids[] = $taxonomy_id;
130  break;
131  }
132  }
133  }
134 
135  return $filtered_tax_ids;
136  }
137 
138  private function duplicateTaxonomies(
139  array $pool_qid_by_test_qid_map,
140  ilObjQuestionPool $pool
142  $tax_duplicator = new ilQuestionPoolTaxonomiesDuplicator();
143  $tax_duplicator->setSourceObjId($this->test_obj->getId());
144  $tax_duplicator->setSourceObjType($this->test_obj->getType());
145  $tax_duplicator->setTargetObjId($pool->getId());
146  $tax_duplicator->setTargetObjType($pool->getType());
147  $tax_duplicator->setQuestionIdMapping($pool_qid_by_test_qid_map);
148 
149  $tax_duplicator->duplicate($this->filterForQuestionRelatedTaxonomies(
150  $tax_duplicator->getAllTaxonomiesForSourceObject(),
151  array_keys($pool_qid_by_test_qid_map)
152  ));
153 
154  return $tax_duplicator->getDuplicatedTaxonomiesKeysMap();
155  }
156 
159  array $mapped_taxonomy_filter
160  ): array {
161  $original_taxonomy_filter = [];
162  foreach ($mapped_taxonomy_filter as $test_taxonomy_id => $test_tax_nodes) {
163  $pool_taxonomy_id = $tax_keys_map->getMappedTaxonomyId($test_taxonomy_id);
164  if ($pool_taxonomy_id === null) {
165  continue;
166  }
167  $original_taxonomy_filter[$pool_taxonomy_id] = [];
168 
169  foreach ($test_tax_nodes as $test_tax_node) {
170  $mapped_tax_node_id = $tax_keys_map->getMappedTaxNodeId((int) $test_tax_node);
171  if ($mapped_tax_node_id !== null) {
172  $original_taxonomy_filter[$pool_taxonomy_id][] = $mapped_tax_node_id;
173  }
174  }
175  }
176 
177  return $original_taxonomy_filter;
178  }
179 
182  int $derived_pool_id,
183  int $non_available_pool_id
184  ): void {
185  foreach ($this->source_pool_definition_list as $definition) {
186  if ($definition->getPoolId() !== $non_available_pool_id) {
187  continue;
188  }
189 
190  $definition->setPoolId($derived_pool_id);
191  $definition->setOriginalTaxonomyFilter(
193  $tax_keys_map,
194  $definition->getMappedTaxonomyFilter()
195  )
196  );
197 
198  $definition->saveToDb();
199  }
200  }
201 }
derive(ilTestRandomQuestionSetNonAvailablePool $non_available_pool)
Readable part of repository interface to ilComponentDataDB.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
duplicateTaxonomies(array $pool_qid_by_test_qid_map, ilObjQuestionPool $pool)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static resetOriginalId(int $questionId)
copyQuestionsToPool(ilObjQuestionPool $pool, array $questions)
static instantiateQuestion(int $question_id)
__construct(private readonly ilDBInterface $db, private readonly ilComponentRepository $component_repository, private readonly ilObjTest $test_obj, private readonly ilTestRandomQuestionSetSourcePoolDefinitionList $source_pool_definition_list, private int $owner_id, private int $target_container_ref)
getQuestionsForPool(ilTestRandomQuestionSetNonAvailablePool $non_available_pool)
filterForQuestionRelatedTaxonomies(array $taxonomy_ids, array $related_question_ids)
static saveOriginalId(int $questionId, int $originalId)
updateRelatedSourcePoolDefinitions(ilQuestionPoolDuplicatedTaxonomiesKeysMap $tax_keys_map, int $derived_pool_id, int $non_available_pool_id)
buildOriginalTaxonomyFilterForDerivedPool(ilQuestionPoolDuplicatedTaxonomiesKeysMap $tax_keys_map, array $mapped_taxonomy_filter)
createNewPool(ilTestRandomQuestionSetNonAvailablePool $non_available_pool)