ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': XML is: ' . $this->xml . (string) $root);
70 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Error parsing objective xml: ' . $this->parseXmlErrors());
71 return false;
72 }
73 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Handling element: ' . (string) $root->getName());
74 $this->parseSettings($root);
75 $this->parseObjectives($root);
76 }
77
81 public function parseObjectDependencies()
82 {
83 libxml_use_internal_errors(true);
84 $root = simplexml_load_string(trim($this->xml));
85 if (!$root instanceof SimpleXMLElement) {
86 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': XML is: ' . $this->xml . (string) $root);
87 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Error parsing objective xml: ' . $this->parseXmlErrors());
88 return false;
89 }
90
91 foreach ($root->Objective as $obj) {
92 $mapped_objective_id = $this->getMapping()->getMapping('Modules/Course', 'objectives', (string) $obj->attributes()->id);
93 if ($mapped_objective_id) {
94 $this->parseMaterials($obj, $mapped_objective_id);
95 $this->parseTests($obj, $mapped_objective_id);
96 }
97 }
98 }
99
104 protected function parseSettings(SimpleXMLElement $root)
105 {
106 include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
107 $settings = ilLOSettings::getInstanceByObjId($this->getCourse()->getId());
108 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Handling element: ' . (string) $root->Settings->getName());
109 foreach ($root->Settings as $set) {
110 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Handling element: ' . (string) $set->getName());
111 $settings->setInitialTestType((int) (string) $set->attributes()->initialTestType);
112 $settings->setInitialTestAsStart((bool) (string) $set->attributes()->initialTestStart);
113 $settings->setQualifyingTestType((int) (string) $set->attributes()->qualifyingTestType);
114 $settings->setQualifyingTestAsStart((bool) (string) $set->attributes()->qualifyingTestStart);
115 $settings->resetResults((bool) (string) $set->attributes()->resetResults);
116 $settings->setPassedObjectiveMode((int) (string) $set->attributes()->passedObjectivesMode);
117
118 // itest
119 $itest = (int) $this->getMappingInfoForItem((int) (string) $set->attributes()->iTest);
120 $settings->setInitialTest($itest);
121
122 // qtest
123 $qtest = (int) $this->getMappingInfoForItem((int) (string) $set->attributes()->qTest);
124 $settings->setQualifiedTest($qtest);
125
126 $settings->update();
127 }
128 }
129
134 protected function parseObjectives(SimpleXMLElement $root)
135 {
136 foreach ($root->Objective as $obj) {
137 include_once './Modules/Course/classes/class.ilCourseObjective.php';
138 $new_obj = new ilCourseObjective($this->getCourse());
139 $new_obj->setActive((bool) (string) $obj->attributes()->online);
140 $new_obj->setTitle((string) $obj->Title);
141 $new_obj->setDescription((string) $obj->Description);
142 $new_obj->setPosition((int) (string) $obj->attributes()->position);
143 $new_objective_id = $new_obj->add();
144
145 $this->getMapping()->addMapping('Modules/Course', 'objectives', (string) $obj->attributes()->id, $new_objective_id);
146 $this->getMapping()->addMapping('Services/COPage', 'pg', 'lobj:' . (string) $obj->attributes()->id, 'lobj:' . $new_objective_id);
147
148 // done after container import complete
149 //$this->parseMaterials($obj,$new_objective_id);
150 //$this->parseTests($obj, $new_objective_id);
151 }
152 }
153
158 protected function parseMaterials(SimpleXMLElement $obj, $a_objective_id)
159 {
160 foreach ($obj->Material as $mat) {
161 $mat_ref_id = (string) $mat->attributes()->refId;
162
163 $mapping_ref_id = $this->getMappingInfoForItem($mat_ref_id);
164 if ($mapping_ref_id) {
165 include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
166 $new_mat = new ilCourseObjectiveMaterials($a_objective_id);
167 $new_mat->setLMRefId($mapping_ref_id);
168
169 $mat_type = (string) $mat->attributes()->type;
170 $obj_id = 0;
171 switch ($mat_type) {
172 case 'st':
173 $mapped_chapter = $this->getMapping()->getMapping(
174 'Modules/LearningModule',
175 'lm_tree',
176 (int) (string) $mat->attributes()->objId
177 );
178 if ($mapped_chapter) {
179 $obj_id = $mapped_chapter;
180 }
181 break;
182
183 case 'pg':
184 $mapped_page = $this->getMapping()->getMapping(
185 'Modules/LearningModule',
186 'pg',
187 (int) (string) $mat->attributes()->objId
188 );
189 if ($mapped_page) {
190 $obj_id = $mapped_page;
191 }
192 break;
193
194 default:
195 $obj_id = ilObject::_lookupObjId($mapping_ref_id);
196 break;
197 }
198 if ($obj_id) {
199 $new_mat->setLMObjId($obj_id);
200 $new_mat->setType((string) $mat->attributes()->type);
201 $new_id = $new_mat->add();
202 $new_mat->writePosition($new_id, (int) (string) $mat->attributes()->position);
203 }
204 }
205 }
206 }
207
213 protected function parseTests(SimpleXMLElement $obj, $a_objective_id)
214 {
215 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Parsing ' . (string) $obj->getName());
216
217 foreach ($obj->Test as $tst) {
218 $type = (int) (string) $tst->attributes()->type;
219
220
221 if ($type == self::TYPE_TST_PO) {
222 $tst_ref_id = (string) $tst->attributes()->refId;
223 $mapping_ref_id = $this->getMappingInfoForItem($tst_ref_id);
224 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Found test ref id ' . (string) $tst_ref_id);
225 if (!$mapping_ref_id) {
226 continue;
227 }
228 include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignment.php';
229 $assignment = new ilLOTestAssignment();
230 $assignment->setContainerId($this->getCourse()->getId());
231 $assignment->setTestRefId($mapping_ref_id);
232 $assignment->setObjectiveId($a_objective_id);
233 $assignment->setAssignmentType((int) (string) $tst->attributes()->testType);
234 $assignment->save();
235 } elseif ($type == self::TYPE_TST_RND) {
236 $tst_obj_id = (int) (string) $tst->attributes()->objId;
237 $mapping_id = $this->getMappingInfoForItemObject($tst_obj_id);
238 if (!$mapping_id) {
239 continue;
240 }
241
242 $new_qpl_id = $this->getMappingForQpls((int) (string) $tst->attributes()->poolId);
243 if (!$new_qpl_id) {
244 continue;
245 }
246
248 $this->getCourse()->getId(),
249 $a_objective_id,
250 (int) (string) $tst->attributes()->testType,
251 (string) $new_qpl_id
252 );
253 $rnd->setTestId($mapping_id);
254 $rnd->setLimit((string) $tst->attributes()->limit);
255 $rnd->create();
256 } else {
257 $tst_ref_id = (string) $tst->attributes()->refId;
258 $mapping_ref_id = $this->getMappingInfoForItem($tst_ref_id);
259 if (!$mapping_ref_id) {
260 continue;
261 }
262 include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
263 $quest = new ilCourseObjectiveQuestion($a_objective_id);
264 $quest->setTestRefId($mapping_ref_id);
265 $quest->setTestObjId(ilObject::_lookupObjId($mapping_ref_id));
266 $quest->setTestStatus((string) $tst->attributes()->testType);
267 $quest->setTestSuggestedLimit((string) $tst->attributes()->limit);
268
269 foreach ($tst->Question as $qst) {
270 $qid = (string) $qst->attributes()->id;
271 $mapping_qid = $this->getMappingForQuestion($qid);
272 if ($mapping_qid) {
273 $quest->setQuestionId($mapping_qid);
274 $quest->add();
275 }
276 }
277 }
278 }
279 }
280
287 protected function getMappingInfoForItem($a_ref_id)
288 {
289 $new_ref_id = $this->getMapping()->getMapping('Services/Container', 'refs', $a_ref_id);
290 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Found new ref_id: ' . $new_ref_id . ' for ' . $a_ref_id);
291 return (int) $new_ref_id;
292 }
293
299 protected function getMappingInfoForItemObject($a_obj_id)
300 {
301 $new_obj_id = $this->getMapping()->getMapping('Services/Container', 'objs', $a_obj_id);
302 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Found new ref_id: ' . $new_obj_id . ' for ' . $a_obj_id);
303 return (int) $new_obj_id;
304 }
305
306 protected function getMappingForQuestion($qid)
307 {
308 $new_qid = $this->getMapping()->getMapping('Modules/Test', 'quest', $qid);
309 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Found new question_id: ' . $new_qid . ' for ' . $qid);
310 return $new_qid;
311 }
312
313 protected function getMappingForQpls($a_id)
314 {
315 $new_id = $this->getMapping()->getMapping('Modules/Test', 'rnd_src_pool_def', $a_id);
316 if ($new_id) {
317 return $new_id;
318 }
319 return 0;
320 }
321
322
323
324
325
326
327
328
334 protected function parseXmlErrors()
335 {
336 $errors = '';
337 foreach (libxml_get_errors() as $err) {
338 $errors .= $err->code . '<br/>';
339 }
340 return $errors;
341 }
342}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
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)
$type
$errors