ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjTestXMLParser.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected ?ilObjTest $testOBJ = null;
28 
29  protected ?ilImportMapping $importMapping = null;
30 
31  protected String $cdata = '';
32 
33  public function getTestOBJ(): ?\ilObjTest
34  {
35  return $this->testOBJ;
36  }
37 
38  public function setTestOBJ(\ilObjTest $testOBJ): void
39  {
40  $this->testOBJ = $testOBJ;
41  }
42 
43  public function getImportMapping(): ?\ilImportMapping
44  {
45  return $this->importMapping;
46  }
47 
48  public function setImportMapping(\ilImportMapping $importMapping): void
49  {
50  $this->importMapping = $importMapping;
51  }
52 
53  public function setHandlers($a_xml_parser): void
54  {
55  xml_set_object($a_xml_parser, $this);
56  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
57  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
58  }
59 
60  public function handlerBeginTag($xmlParser, $tagName, $tagAttributes): void
61  {
62  switch ($tagName) {
63  case 'RandomQuestionSetConfig':
64  $this->inRandomQuestionSetConfig = true;
65  break;
66 
67  case 'RandomQuestionSetSettings':
68  if ($this->inRandomQuestionSetConfig) {
69  $this->inRandomQuestionSetSettings = true;
70  $this->cdata = '';
71  $this->attr = $tagAttributes;
72  }
73  break;
74 
75  case 'RandomQuestionStage':
76  if ($this->inRandomQuestionSetConfig) {
77  $this->inRandomQuestionStage = true;
78  }
79  break;
80 
81  case 'RandomQuestionStagingPool':
82  if ($this->inRandomQuestionStage) {
83  $this->cdata = '';
84  $this->attr = $tagAttributes;
85  }
86  break;
87 
88  case 'RandomQuestionSelectionDefinitions':
89  if ($this->inRandomQuestionSetConfig) {
90  $this->inRandomQuestionSelectionDefinitions = true;
91  }
92  break;
93 
94  case 'RandomQuestionSelectionDefinition':
95  if ($this->inRandomQuestionSelectionDefinitions) {
96  $this->sourcePoolDefinition = $this->getRandomQuestionSourcePoolDefinitionInstance();
97  $this->attr = $tagAttributes;
98  }
99  break;
100 
101  case 'RandomQuestionSourcePoolTitle':
102  case 'RandomQuestionSourcePoolPath':
103  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
104  $this->cdata = '';
105  }
106  break;
107  }
108  }
109 
110  public function handlerEndTag($xmlParser, $tagName): void
111  {
112  switch ($tagName) {
113  case 'RandomQuestionSetConfig':
114  $this->inRandomQuestionSetConfig = false;
115  break;
116 
117  case 'RandomQuestionSetSettings':
118  if ($this->inRandomQuestionSetConfig) {
119  $this->importRandomQuestionSetSettings($this->attr);
120  $this->attr = null;
121  }
122  break;
123 
124  case 'RandomQuestionStage':
125  if ($this->inRandomQuestionSetConfig) {
126  $this->inRandomQuestionStage = false;
127  }
128  break;
129 
130  case 'RandomQuestionStagingPool':
131  if ($this->inRandomQuestionSetConfig && $this->inRandomQuestionStage) {
132  $this->importRandomQuestionStagingPool($this->attr, $this->cdata);
133  $this->attr = null;
134  $this->cdata = '';
135  }
136  break;
137 
138  case 'RandomQuestionSelectionDefinitions':
139  if ($this->inRandomQuestionSetConfig) {
140  $this->inRandomQuestionSelectionDefinitions = false;
141  }
142  break;
143 
144  case 'RandomQuestionSelectionDefinition':
145  if ($this->inRandomQuestionSetConfig && $this->inRandomQuestionSelectionDefinitions) {
146  $this->importRandomQuestionSourcePoolDefinition($this->sourcePoolDefinition, $this->attr);
147  $this->sourcePoolDefinition->saveToDb();
148 
149  $this->getImportMapping()->addMapping(
150  'Modules/Test',
151  'rnd_src_pool_def',
152  $this->attr['id'],
153  $this->sourcePoolDefinition->getId()
154  );
155 
156  $this->sourcePoolDefinition = null;
157  $this->attr = null;
158  }
159  break;
160 
161  case 'RandomQuestionSourcePoolTitle':
162  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
163  $this->sourcePoolDefinition->setPoolTitle($this->cdata);
164  $this->cdata = '';
165  }
166  break;
167 
168  case 'RandomQuestionSourcePoolPath':
169  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
170  $this->sourcePoolDefinition->setPoolPath($this->cdata);
171  $this->cdata = '';
172  }
173  break;
174  }
175  }
176 
177  public function handlerCharacterData($xmlParser, $charData): void
178  {
179  if ($charData != "\n") {
180  // Replace multiple tabs with one space
181  $charData = preg_replace("/\t+/", " ", $charData);
182 
183  $this->cdata .= $charData;
184  }
185  }
186 
187  protected function importRandomQuestionSetSettings($attr): void
188  {
189  global $DIC;
190  $tree = $DIC['tree'];
191  $ilDB = $DIC['ilDB'];
192  $component_repository = $DIC['component.repository'];
193  $questionSetConfig = new ilTestRandomQuestionSetConfig($tree, $ilDB, $component_repository, $this->testOBJ);
194 
195  if (!$questionSetConfig->isValidQuestionAmountConfigurationMode($attr['amountMode'])) {
196  throw new ilTestException(
197  'invalid random test question set config amount mode given: "' . $attr['amountMode'] . '"'
198  );
199  }
200 
201  $questionSetConfig->setQuestionAmountConfigurationMode($attr['amountMode']);
202  $questionSetConfig->setQuestionAmountPerTest((int) $attr['questAmount']);
203  $questionSetConfig->setPoolsWithHomogeneousScoredQuestionsRequired((bool) $attr['homogeneous']);
204  $questionSetConfig->setLastQuestionSyncTimestamp((int) $attr['synctimestamp']);
205 
206  $questionSetConfig->saveToDb();
207  }
208 
209  protected function importRandomQuestionStagingPool($attr, $cdata): void
210  {
211  global $DIC;
212  $ilDB = $DIC['ilDB'];
213 
214  $oldPoolId = $attr['poolId'];
215  $newPoolId = $ilDB->nextId('object_data'); // yes !!
216 
217  $this->getImportMapping()->addMapping(
218  'Modules/Test',
219  'pool',
220  $oldPoolId,
221  $newPoolId
222  );
223 
224  $oldQuestionIds = explode(',', $cdata);
225 
226  foreach ($oldQuestionIds as $oldQuestionId) {
227  $newQuestionId = $this->getImportMapping()->getMapping(
228  'Modules/Test',
229  'quest',
230  $oldQuestionId
231  );
232 
233  $stagingQuestion = new ilTestRandomQuestionSetStagingPoolQuestion($ilDB);
234  $stagingQuestion->setTestId($this->testOBJ->getTestId());
235  $stagingQuestion->setPoolId($newPoolId);
236  $stagingQuestion->setQuestionId($newQuestionId);
237 
238  $stagingQuestion->saveQuestionStaging();
239  }
240  }
241 
243  {
244  global $DIC;
245  $ilDB = $DIC['ilDB'];
246 
247  return new ilTestRandomQuestionSetSourcePoolDefinition($ilDB, $this->testOBJ);
248  }
249 
250  protected function importRandomQuestionSourcePoolDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition, $attr): void
251  {
252  $source_pool_id = (int) $attr['poolId'];
253  $effective_pool_id = (int) $this->getImportMapping()->getMapping(
254  'Modules/Test',
255  'pool',
256  $source_pool_id
257  );
258  $sourcePoolDefinition->setPoolId($effective_pool_id);
259 
260  $derive_from_obj_id = true;
261  // The ref_id might not be given in old export files, so we have to check for existence
262  if (isset($attr['ref_id']) && is_numeric($attr['ref_id'])) {
263  if ($source_pool_id === $effective_pool_id) {
264  $derive_from_obj_id = false;
265  $sourcePoolDefinition->setPoolRefId((int) $attr['ref_id']);
266  }
267  }
268 
269  if ($derive_from_obj_id) {
270  $ref_ids = ilObject::_getAllReferences($effective_pool_id);
271  $ref_id = current($ref_ids);
272  $sourcePoolDefinition->setPoolRefId($ref_id ? $ref_id : null);
273  }
274 
275  $sourcePoolDefinition->setPoolQuestionCount((int) $attr['poolQuestCount']);
276  $sourcePoolDefinition->setQuestionAmount((int) $attr['questAmount']);
277  $sourcePoolDefinition->setSequencePosition((int) $attr['position']);
278 
279  if (isset($attr['typeFilter']) && strlen($attr['typeFilter']) > 0) {
280  $sourcePoolDefinition->setTypeFilterFromTypeTags(explode(',', $attr['typeFilter']));
281  }
282 
283  // #21330
284  if (isset($attr['tax']) && isset($attr['taxNode'])) {
285  $mappedTaxFilter = array(
286  (int) $attr['tax'] => array(
287  (int) $attr['taxNode']
288  )
289  );
290  $sourcePoolDefinition->setMappedTaxonomyFilter($mappedTaxFilter);
291  } elseif (isset($attr['taxFilter']) && strlen($attr['taxFilter']) > 0) {
292  $mappedTaxFilter = unserialize($attr['taxFilter']);
293  $sourcePoolDefinition->setMappedTaxonomyFilter($mappedTaxFilter);
294  }
295  }
296 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getAllReferences(int $id)
get all reference ids for object ID
setTestOBJ(\ilObjTest $testOBJ)
setImportMapping(\ilImportMapping $importMapping)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
setMappedTaxonomyFilter($filter=array())
set the original taxonomy filter condition
handlerBeginTag($xmlParser, $tagName, $tagAttributes)
importRandomQuestionStagingPool($attr, $cdata)
importRandomQuestionSourcePoolDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition, $attr)
setTypeFilterFromTypeTags(array $tags)
Set the type filter from a list of type tags.
handlerEndTag($xmlParser, $tagName)
handlerCharacterData($xmlParser, $charData)