ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
67  case 'RandomQuestionSetConfig':
68  $this->inRandomQuestionSetConfig = true;
69  break;
70 
71  case 'RandomQuestionSetSettings':
72  if($this->inRandomQuestionSetConfig)
73  {
74  $this->inRandomQuestionSetSettings = true;
75  $this->cdata = '';
76  $this->attr = $tagAttributes;
77  }
78  break;
79 
80  case 'RandomQuestionStage':
81  if($this->inRandomQuestionSetConfig)
82  {
83  $this->inRandomQuestionStage = true;
84  }
85  break;
86 
87  case 'RandomQuestionStagingPool':
88  if($this->inRandomQuestionStage)
89  {
90  $this->cdata = '';
91  $this->attr = $tagAttributes;
92  }
93  break;
94 
95  case 'RandomQuestionSelectionDefinitions':
96  if($this->inRandomQuestionSetConfig)
97  {
98  $this->inRandomQuestionSelectionDefinitions = true;
99  }
100  break;
101 
102  case 'RandomQuestionSelectionDefinition':
103  if($this->inRandomQuestionSelectionDefinitions)
104  {
105  $this->sourcePoolDefinition = $this->getRandomQuestionSourcePoolDefinitionInstance();
106  $this->attr = $tagAttributes;
107  }
108  break;
109 
110  case 'RandomQuestionSourcePoolTitle':
111  case 'RandomQuestionSourcePoolPath':
112  if($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition)
113  {
114  $this->cdata = '';
115  }
116  break;
117  }
118  }
119 
120  public function handlerEndTag($xmlParser, $tagName)
121  {
122  switch($tagName)
123  {
124  case 'RandomQuestionSetConfig':
125  $this->inRandomQuestionSetConfig = false;
126  break;
127 
128  case 'RandomQuestionSetSettings':
129  if($this->inRandomQuestionSetConfig)
130  {
131  $this->importRandomQuestionSetSettings($this->attr);
132  $this->attr = null;
133  }
134  break;
135 
136  case 'RandomQuestionStage':
137  if($this->inRandomQuestionSetConfig)
138  {
139  $this->inRandomQuestionStage = false;
140  }
141  break;
142 
143  case 'RandomQuestionStagingPool':
144  if($this->inRandomQuestionSetConfig && $this->inRandomQuestionStage)
145  {
146  $this->importRandomQuestionStagingPool($this->attr, $this->cdata);
147  $this->attr = null;
148  $this->cdata = '';
149  }
150  break;
151 
152  case 'RandomQuestionSelectionDefinitions':
153  if($this->inRandomQuestionSetConfig)
154  {
155  $this->inRandomQuestionSelectionDefinitions = false;
156  }
157  break;
158 
159  case 'RandomQuestionSelectionDefinition':
160  if($this->inRandomQuestionSetConfig && $this->inRandomQuestionSelectionDefinitions)
161  {
162  $this->importRandomQuestionSourcePoolDefinition($this->sourcePoolDefinition, $this->attr);
163  $this->sourcePoolDefinition->saveToDb();
164 
165  $this->getImportMapping()->addMapping(
166  'Modules/Test', 'rnd_src_pool_def', $this->attr['id'], $this->sourcePoolDefinition->getId()
167  );
168 
169  $this->sourcePoolDefinition = null;
170  $this->attr = null;
171  }
172  break;
173 
174  case 'RandomQuestionSourcePoolTitle':
175  if($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition)
176  {
177  $this->sourcePoolDefinition->setPoolTitle($this->cdata);
178  $this->cdata = '';
179  }
180  break;
181 
182  case 'RandomQuestionSourcePoolPath':
183  if($this->sourcePoolDefinition instanceof ilTestRandomQuestionSetSourcePoolDefinition)
184  {
185  $this->sourcePoolDefinition->setPoolPath($this->cdata);
186  $this->cdata = '';
187  }
188  break;
189  }
190  }
191 
192  public function handlerCharacterData($xmlParser, $charData)
193  {
194  if( $charData != "\n" )
195  {
196  // Replace multiple tabs with one space
197  $charData = preg_replace("/\t+/"," ",$charData);
198 
199  $this->cdata .= $charData;
200  }
201  }
202 
203  protected function importRandomQuestionSetSettings($attr)
204  {
205  global $tree, $ilDB, $ilPluginAdmin;
206 
207  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetConfig.php';
208  $questionSetConfig = new ilTestRandomQuestionSetConfig($tree, $ilDB, $ilPluginAdmin, $this->testOBJ);
209 
210  if( !$questionSetConfig->isValidQuestionAmountConfigurationMode($attr['amountMode']) )
211  {
212  require_once 'Modules/Test/exceptions/class.ilTestException.php';
213  throw new ilTestException(
214  'invalid random test question set config amount mode given: "'.$attr['amountMode'].'"'
215  );
216  }
217 
218  $questionSetConfig->setQuestionAmountConfigurationMode($attr['amountMode']);
219  $questionSetConfig->setQuestionAmountPerTest((int)$attr['questAmount']);
220  $questionSetConfig->setPoolsWithHomogeneousScoredQuestionsRequired((bool)$attr['homogeneous']);
221 
222  $questionSetConfig->saveToDb();
223  }
224 
225  protected function importRandomQuestionStagingPool($attr, $cdata)
226  {
227  global $ilDB;
228 
229  $oldPoolId = $attr['poolId'];
230  $newPoolId = $ilDB->nextId('object_data'); // yes !!
231 
232  $this->getImportMapping()->addMapping(
233  'Modules/Test', 'pool', $oldPoolId, $newPoolId
234  );
235 
236  $oldQuestionIds = explode(',', $cdata);
237 
238  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetStagingPoolQuestion.php';
239 
240  foreach($oldQuestionIds as $oldQuestionId)
241  {
242  $newQuestionId = $this->getImportMapping()->getMapping(
243  'Modules/Test', 'quest', $oldQuestionId
244  );
245 
246  $stagingQuestion = new ilTestRandomQuestionSetStagingPoolQuestion($ilDB);
247  $stagingQuestion->setTestId($this->testOBJ->getTestId());
248  $stagingQuestion->setPoolId($newPoolId);
249  $stagingQuestion->setQuestionId($newQuestionId);
250 
251  $stagingQuestion->saveQuestionStaging();
252  }
253  }
254 
256  {
257  global $ilDB;
258 
259  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinition.php';
260 
261  return new ilTestRandomQuestionSetSourcePoolDefinition($ilDB, $this->testOBJ);
262  }
263 
265  {
266  $sourcePoolDefinition->setPoolId($this->getImportMapping()->getMapping(
267  'Modules/Test', 'pool', (int)$attr['poolId']
268  ));
269 
270  $sourcePoolDefinition->setPoolQuestionCount((int)$attr['poolQuestCount']);
271  $sourcePoolDefinition->setQuestionAmount((int)$attr['questAmount']);
272  $sourcePoolDefinition->setSequencePosition((int)$attr['position']);
273 
274  if( isset($attr['tax']) && isset($attr['taxNode']) )
275  {
276  $sourcePoolDefinition->setMappedFilterTaxId((int)$attr['tax']);
277  $sourcePoolDefinition->setMappedFilterTaxNodeId((int)$attr['taxNode']);
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 ...
handlerBeginTag($xmlParser, $tagName, $tagAttributes)
importRandomQuestionStagingPool($attr, $cdata)
global $ilDB
importRandomQuestionSourcePoolDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition, $attr)
handlerEndTag($xmlParser, $tagName)
handlerCharacterData($xmlParser, $charData)