ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilObjTestXMLParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Xml/classes/class.ilSaxParser.php';
5 
13 {
17  protected $testOBJ;
18 
22  protected $importMapping;
23 
27  public function getTestOBJ()
28  {
29  return $this->testOBJ;
30  }
31 
35  public function setTestOBJ($testOBJ)
36  {
37  $this->testOBJ = $testOBJ;
38  }
39 
43  public function getImportMapping()
44  {
45  return $this->importMapping;
46  }
47 
52  {
53  $this->importMapping = $importMapping;
54  }
55 
56  public function setHandlers($xmlParser)
57  {
58  xml_set_object($xmlParser, $this);
59  xml_set_element_handler($xmlParser, 'handlerBeginTag', 'handlerEndTag');
60  xml_set_character_data_handler($xmlParser, 'handlerCharacterData');
61  }
62 
63  public function handlerBeginTag($xmlParser, $tagName, $tagAttributes)
64  {
65  switch ($tagName) {
66  case 'RandomQuestionSetConfig':
67  $this->inRandomQuestionSetConfig = true;
68  break;
69 
70  case 'RandomQuestionSetSettings':
71  if ($this->inRandomQuestionSetConfig) {
72  $this->inRandomQuestionSetSettings = true;
73  $this->cdata = '';
74  $this->attr = $tagAttributes;
75  }
76  break;
77 
78  case 'RandomQuestionStage':
79  if ($this->inRandomQuestionSetConfig) {
80  $this->inRandomQuestionStage = true;
81  }
82  break;
83 
84  case 'RandomQuestionStagingPool':
85  if ($this->inRandomQuestionStage) {
86  $this->cdata = '';
87  $this->attr = $tagAttributes;
88  }
89  break;
90 
91  case 'RandomQuestionSelectionDefinitions':
92  if ($this->inRandomQuestionSetConfig) {
93  $this->inRandomQuestionSelectionDefinitions = true;
94  }
95  break;
96 
97  case 'RandomQuestionSelectionDefinition':
98  if ($this->inRandomQuestionSelectionDefinitions) {
99  $this->sourcePoolDefinition = $this->getRandomQuestionSourcePoolDefinitionInstance();
100  $this->attr = $tagAttributes;
101  }
102  break;
103 
104  case 'RandomQuestionSourcePoolTitle':
105  case 'RandomQuestionSourcePoolPath':
106  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
107  $this->cdata = '';
108  }
109  break;
110  }
111  }
112 
113  public function handlerEndTag($xmlParser, $tagName)
114  {
115  switch ($tagName) {
116  case 'RandomQuestionSetConfig':
117  $this->inRandomQuestionSetConfig = false;
118  break;
119 
120  case 'RandomQuestionSetSettings':
121  if ($this->inRandomQuestionSetConfig) {
122  $this->importRandomQuestionSetSettings($this->attr);
123  $this->attr = null;
124  }
125  break;
126 
127  case 'RandomQuestionStage':
128  if ($this->inRandomQuestionSetConfig) {
129  $this->inRandomQuestionStage = false;
130  }
131  break;
132 
133  case 'RandomQuestionStagingPool':
134  if ($this->inRandomQuestionSetConfig && $this->inRandomQuestionStage) {
135  $this->importRandomQuestionStagingPool($this->attr, $this->cdata);
136  $this->attr = null;
137  $this->cdata = '';
138  }
139  break;
140 
141  case 'RandomQuestionSelectionDefinitions':
142  if ($this->inRandomQuestionSetConfig) {
143  $this->inRandomQuestionSelectionDefinitions = false;
144  }
145  break;
146 
147  case 'RandomQuestionSelectionDefinition':
148  if ($this->inRandomQuestionSetConfig && $this->inRandomQuestionSelectionDefinitions) {
149  $this->importRandomQuestionSourcePoolDefinition($this->sourcePoolDefinition, $this->attr);
150  $this->sourcePoolDefinition->saveToDb();
151 
152  $this->getImportMapping()->addMapping(
153  'Modules/Test',
154  'rnd_src_pool_def',
155  $this->attr['id'],
156  $this->sourcePoolDefinition->getId()
157  );
158 
159  $this->sourcePoolDefinition = null;
160  $this->attr = null;
161  }
162  break;
163 
164  case 'RandomQuestionSourcePoolTitle':
165  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
166  $this->sourcePoolDefinition->setPoolTitle($this->cdata);
167  $this->cdata = '';
168  }
169  break;
170 
171  case 'RandomQuestionSourcePoolPath':
172  if ($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition) {
173  $this->sourcePoolDefinition->setPoolPath($this->cdata);
174  $this->cdata = '';
175  }
176  break;
177  }
178  }
179 
180  public function handlerCharacterData($xmlParser, $charData)
181  {
182  if ($charData != "\n") {
183  // Replace multiple tabs with one space
184  $charData = preg_replace("/\t+/", " ", $charData);
185 
186  $this->cdata .= $charData;
187  }
188  }
189 
190  protected function importRandomQuestionSetSettings($attr)
191  {
192  global $tree, $ilDB, $ilPluginAdmin;
193 
194  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetConfig.php';
195  $questionSetConfig = new ilTestRandomQuestionSetConfig($tree, $ilDB, $ilPluginAdmin, $this->testOBJ);
196 
197  if (!$questionSetConfig->isValidQuestionAmountConfigurationMode($attr['amountMode'])) {
198  require_once 'Modules/Test/exceptions/class.ilTestException.php';
199  throw new ilTestException(
200  'invalid random test question set config amount mode given: "' . $attr['amountMode'] . '"'
201  );
202  }
203 
204  $questionSetConfig->setQuestionAmountConfigurationMode($attr['amountMode']);
205  $questionSetConfig->setQuestionAmountPerTest((int) $attr['questAmount']);
206  $questionSetConfig->setPoolsWithHomogeneousScoredQuestionsRequired((bool) $attr['homogeneous']);
207  $questionSetConfig->setLastQuestionSyncTimestamp((int) $attr['synctimestamp']);
208 
209  $questionSetConfig->saveToDb();
210  }
211 
212  protected function importRandomQuestionStagingPool($attr, $cdata)
213  {
214  global $ilDB;
215 
216  $oldPoolId = $attr['poolId'];
217  $newPoolId = $ilDB->nextId('object_data'); // yes !!
218 
219  $this->getImportMapping()->addMapping(
220  'Modules/Test',
221  'pool',
222  $oldPoolId,
223  $newPoolId
224  );
225 
226  $oldQuestionIds = explode(',', $cdata);
227 
228  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetStagingPoolQuestion.php';
229 
230  foreach ($oldQuestionIds as $oldQuestionId) {
231  $newQuestionId = $this->getImportMapping()->getMapping(
232  'Modules/Test',
233  'quest',
234  $oldQuestionId
235  );
236 
237  $stagingQuestion = new ilTestRandomQuestionSetStagingPoolQuestion($ilDB);
238  $stagingQuestion->setTestId($this->testOBJ->getTestId());
239  $stagingQuestion->setPoolId($newPoolId);
240  $stagingQuestion->setQuestionId($newQuestionId);
241 
242  $stagingQuestion->saveQuestionStaging();
243  }
244  }
245 
247  {
248  global $ilDB;
249 
250  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinition.php';
251 
252  return new ilTestRandomQuestionSetSourcePoolDefinition($ilDB, $this->testOBJ);
253  }
254 
256  {
257  $sourcePoolDefinition->setPoolId($this->getImportMapping()->getMapping(
258  'Modules/Test',
259  'pool',
260  (int) $attr['poolId']
261  ));
262 
263  $sourcePoolDefinition->setPoolQuestionCount((int) $attr['poolQuestCount']);
264  $sourcePoolDefinition->setQuestionAmount((int) $attr['questAmount']);
265  $sourcePoolDefinition->setSequencePosition((int) $attr['position']);
266 
267  // #21330
268  if (isset($attr['tax']) && isset($attr['taxNode'])) {
269  $mappedTaxFilter = array(
270  (int) $attr['tax'] => array(
271  (int) $attr['taxNode']
272  )
273  );
274  $sourcePoolDefinition->setMappedTaxonomyFilter($mappedTaxFilter);
275  } elseif (isset($attr['taxFilter']) && strlen($attr['taxFilter']) > 0) {
276  $mappedTaxFilter = unserialize($attr['taxFilter']);
277  $sourcePoolDefinition->setMappedTaxonomyFilter($mappedTaxFilter);
278  }
279  }
280 }
setImportMapping($importMapping)
Base Exception for all Exceptions relating to Modules/Test.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
setMappedTaxonomyFilter($filter=array())
set the original taxonomy filter condition
handlerBeginTag($xmlParser, $tagName, $tagAttributes)
Create styles array
The data for the language used.
importRandomQuestionStagingPool($attr, $cdata)
global $ilDB
importRandomQuestionSourcePoolDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition, $attr)
handlerEndTag($xmlParser, $tagName)
handlerCharacterData($xmlParser, $charData)