ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilLOXmlParser.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
13{
14 const TYPE_TST_PO = 1;
15 const TYPE_TST_ALL = 2;
16 const TYPE_TST_RND = 3;
17
18
19 private $xml = '';
20 private $course = null;
21 private $mapping = null;
22
28 public function __construct(ilObjCourse $course, $a_xml)
29 {
30 $this->course = $course;
31 $this->xml = $a_xml;
32 }
33
39 {
40 $this->mapping = $mapping;
41 }
42
47 public function getMapping()
48 {
49 return $this->mapping;
50 }
51
56 protected function getCourse()
57 {
58 return $this->course;
59 }
60
64 public function parse()
65 {
66 libxml_use_internal_errors(true);
67 $root = simplexml_load_string(trim($this->xml));
68 if(!$root instanceof SimpleXMLElement)
69 {
70 $GLOBALS['ilLog']->write(__METHOD__.': XML is: '. $this->xml. (string) $root);
71 $GLOBALS['ilLog']->write(__METHOD__.': Error parsing objective xml: '.$this->parseXmlErrors());
72 return false;
73 }
74 $GLOBALS['ilLog']->write(__METHOD__.': Handling element: '. (string) $root->getName());
75 $this->parseSettings($root);
76 $this->parseObjectives($root);
77 }
78
82 public function parseObjectDependencies()
83 {
84 libxml_use_internal_errors(true);
85 $root = simplexml_load_string(trim($this->xml));
86 if(!$root instanceof SimpleXMLElement)
87 {
88 $GLOBALS['ilLog']->write(__METHOD__.': XML is: '. $this->xml. (string) $root);
89 $GLOBALS['ilLog']->write(__METHOD__.': Error parsing objective xml: '.$this->parseXmlErrors());
90 return false;
91 }
92
93 foreach($root->Objective as $obj)
94 {
95 $mapped_objective_id = $this->getMapping()->getMapping('Modules/Course', 'objectives', (string) $obj->attributes()->id);
96 if($mapped_objective_id)
97 {
98 $this->parseMaterials($obj, $mapped_objective_id);
99 $this->parseTests($obj, $mapped_objective_id);
100 }
101 }
102 }
103
108 protected function parseSettings(SimpleXMLElement $root)
109 {
110 include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
111 $settings = ilLOSettings::getInstanceByObjId($this->getCourse()->getId());
112 $GLOBALS['ilLog']->write(__METHOD__.': Handling element: '. (string) $root->Settings->getName());
113 foreach($root->Settings as $set)
114 {
115 $GLOBALS['ilLog']->write(__METHOD__.': Handling element: '. (string) $set->getName());
116 $settings->setInitialTestType((int) (string) $set->attributes()->initialTestType);
117 $settings->setInitialTestAsStart((bool) (string) $set->attributes()->initialTestStart);
118 $settings->setQualifyingTestType((int) (string) $set->attributes()->qualifyingTestType);
119 $settings->setQualifyingTestAsStart((bool) (string) $set->attributes()->qualifyingTestStart);
120 $settings->resetResults((bool) (string) $set->attributes()->resetResults);
121 $settings->setPassedObjectiveMode((int) (string) $set->attributes()->passedObjectivesMode);
122
123 // itest
124 $itest = (int) $this->getMappingInfoForItem((int) (string) $set->attributes()->iTest);
125 $settings->setInitialTest($itest);
126
127 // qtest
128 $qtest = (int) $this->getMappingInfoForItem((int) (string) $set->attributes()->qTest);
129 $settings->setQualifiedTest($qtest);
130
131 $settings->update();
132 }
133 }
134
139 protected function parseObjectives(SimpleXMLElement $root)
140 {
141 foreach($root->Objective as $obj)
142 {
143 include_once './Modules/Course/classes/class.ilCourseObjective.php';
144 $new_obj = new ilCourseObjective($this->getCourse());
145 $new_obj->setActive((bool) (string) $obj->attributes()->online);
146 $new_obj->setTitle((string) $obj->Title);
147 $new_obj->setDescription((string) $obj->Description);
148 $new_obj->setPosition((int) (string) $obj->attributes()->position);
149 $new_objective_id = $new_obj->add();
150
151 $this->getMapping()->addMapping('Modules/Course', 'objectives', (string) $obj->attributes()->id, $new_objective_id);
152 $this->getMapping()->addMapping('Services/COPage', 'pg', 'lobj:'.(string) $obj->attributes()->id, 'lobj:'.$new_objective_id);
153
154 // done after container import complete
155 //$this->parseMaterials($obj,$new_objective_id);
156 //$this->parseTests($obj, $new_objective_id);
157
158 }
159 }
160
165 protected function parseMaterials(SimpleXMLElement $obj, $a_objective_id)
166 {
167 foreach($obj->Material as $mat)
168 {
169 $mat_ref_id = (string) $mat->attributes()->refId;
170
171 $mapping_ref_id = $this->getMappingInfoForItem($mat_ref_id);
172 if($mapping_ref_id)
173 {
174 include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
175 $new_mat = new ilCourseObjectiveMaterials($a_objective_id);
176 $new_mat->setLMRefId($mapping_ref_id);
177
178 $mat_type = (string) $mat->attributes()->type;
179 $obj_id = 0;
180 switch($mat_type)
181 {
182 case 'st':
183 $mapped_chapter = $this->getMapping()->getMapping(
184 'Modules/LearningModule',
185 'lm_tree',
186 (int) (string) $mat->attributes()->objId
187 );
188 if($mapped_chapter)
189 {
190 $obj_id = $mapped_chapter;
191 }
192 break;
193
194 case 'pg':
195 $mapped_page = $this->getMapping()->getMapping(
196 'Modules/LearningModule',
197 'pg',
198 (int) (string) $mat->attributes()->objId
199 );
200 if($mapped_page)
201 {
202 $obj_id = $mapped_page;
203 }
204 break;
205
206 default:
207 $obj_id = ilObject::_lookupObjId($mapping_ref_id);
208 break;
209 }
210 if($obj_id)
211 {
212 $new_mat->setLMObjId($obj_id);
213 $new_mat->setType((string) $mat->attributes()->type);
214 $new_mat->add();
215 $new_id = $new_mat->add();
216 $new_mat->writePosition($new_id, (int) (string) $mat->attributes());
217 }
218 }
219 }
220 }
221
227 protected function parseTests(SimpleXMLElement $obj, $a_objective_id)
228 {
229 $GLOBALS['ilLog']->write(__METHOD__.': Parsing ' . (string) $obj->getName());
230
231 foreach($obj->Test as $tst)
232 {
233 $type = (int) (string) $tst->attributes()->type;
234
235
236 if($type == self::TYPE_TST_PO)
237 {
238 $tst_ref_id = (string) $tst->attributes()->refId;
239 $mapping_ref_id = $this->getMappingInfoForItem($tst_ref_id);
240 $GLOBALS['ilLog']->write(__METHOD__.': Found test ref id ' . (string) $tst_ref_id);
241 if(!$mapping_ref_id)
242 {
243 continue;
244 }
245 include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignment.php';
246 $assignment = new ilLOTestAssignment();
247 $assignment->setContainerId($this->getCourse()->getId());
248 $assignment->setTestRefId($mapping_ref_id);
249 $assignment->setObjectiveId($a_objective_id);
250 $assignment->setAssignmentType((int) (string) $tst->attributes()->testType);
251 $assignment->save();
252 }
253 elseif($type == self::TYPE_TST_RND)
254 {
255 $tst_obj_id = (int) (string) $tst->attributes()->objId;
256 $mapping_id = $this->getMappingInfoForItemObject($tst_obj_id);
257 if(!$mapping_id)
258 {
259 continue;
260 }
261
262 $new_qpl_id = $this->getMappingForQpls((int) (string) $tst->attributes()->poolId);
263 if(!$new_qpl_id)
264 {
265 continue;
266 }
267
268 include_once './Modules/Course/classes/Objectives/class.ilLORandomTestQuestionPools.php';
270 $this->getCourse()->getId(),
271 $a_objective_id,
272 (int) (string) $tst->attributes()->testType
273 );
274 $rnd->setTestId($mapping_id);
275 $rnd->setLimit((string) $tst->attributes()->limit);
276 $rnd->setQplSequence($new_qpl_id);
277 $rnd->create();
278 }
279 else
280 {
281 $tst_ref_id = (string) $tst->attributes()->refId;
282 $mapping_ref_id = $this->getMappingInfoForItem($tst_ref_id);
283 if(!$mapping_ref_id)
284 {
285 continue;
286 }
287 include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
288 $quest = new ilCourseObjectiveQuestion($a_objective_id);
289 $quest->setTestRefId($mapping_ref_id);
290 $quest->setTestObjId(ilObject::_lookupObjId($mapping_ref_id));
291 $quest->setTestStatus((string) $tst->attributes()->testType);
292 $quest->setTestSuggestedLimit((string) $tst->attributes()->limit);
293
294 foreach($tst->Question as $qst)
295 {
296 $qid = (string) $qst->attributes()->id;
297 $mapping_qid = $this->getMappingForQuestion($qid);
298 if($mapping_qid)
299 {
300 $quest->setQuestionId($mapping_qid);
301 $quest->add();
302 }
303 }
304 }
305 }
306 }
307
314 protected function getMappingInfoForItem($a_ref_id)
315 {
316 $new_ref_id = $this->getMapping()->getMapping('Services/Container', 'refs', $a_ref_id);
317 $GLOBALS['ilLog']->write(__METHOD__.': Found new ref_id: ' .$new_ref_id.' for '. $a_ref_id);
318 return (int) $new_ref_id;
319 }
320
326 protected function getMappingInfoForItemObject($a_obj_id)
327 {
328 $new_obj_id = $this->getMapping()->getMapping('Services/Container', 'objs', $a_obj_id);
329 $GLOBALS['ilLog']->write(__METHOD__.': Found new ref_id: ' .$new_obj_id.' for '. $a_obj_id);
330 return (int) $new_obj_id;
331 }
332
333 protected function getMappingForQuestion($qid)
334 {
335 $new_qid = $this->getMapping()->getMapping('Modules/Test', 'quest', $qid);
336 $GLOBALS['ilLog']->write(__METHOD__.': Found new question_id: ' .$new_qid.' for '. $qid);
337 return $new_qid;
338 }
339
340 protected function getMappingForQpls($a_id)
341 {
342 $new_id = $this->getMapping()->getMapping('Modules/Test', 'rnd_src_pool_def', $a_id);
343 if($new_id)
344 {
345 return $new_id;
346 }
347 return 0;
348 }
349
350
351
352
353
354
355
356
362 protected function parseXmlErrors()
363 {
364 $errors = '';
365 foreach(libxml_get_errors() as $err)
366 {
367 $errors .= $err->code.'<br/>';
368 }
369 return $errors;
370 }
371}
372?>
class ilCourseObjectiveMaterials
class ilcourseobjectiveQuestion
class ilcourseobjective
static getInstanceByObjId($a_obj_id)
get singleton instance
Settings for LO courses.
Class ilLOXmlWriter.
parseObjectDependencies()
Parse object dependencies (assigned strucure objects, page objects, fixed questions)
getMappingInfoForItem($a_ref_id)
Get mapping info.
parseXmlErrors()
Parse xml errors from libxml_get_errors.
parseObjectives(SimpleXMLElement $root)
Parse objective.
setMapping(ilImportMapping $mapping)
Set import mapping.
parseSettings(SimpleXMLElement $root)
__construct(ilObjCourse $course, $a_xml)
Constructor.
getMappingInfoForItemObject($a_obj_id)
Get obj_id mapping.
getCourse()
Get course.
parseTests(SimpleXMLElement $obj, $a_objective_id)
Parse tests of objective.
parseMaterials(SimpleXMLElement $obj, $a_objective_id)
Parse assigned materials.
getMapping()
Get import mapping.
Class ilObjCourse.
static _lookupObjId($a_id)
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
$errors