ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilLOXmlParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=0);
25 {
26  public const TYPE_TST_PO = 1;
27  public const TYPE_TST_ALL = 2;
28  public const TYPE_TST_RND = 3;
29 
30  private string $xml = '';
33 
34  private ilLogger $logger;
35 
36  public function __construct(ilObjCourse $course, string $a_xml)
37  {
38  global $DIC;
39 
40  $this->logger = $DIC->logger()->crs();
41  $this->course = $course;
42  $this->xml = $a_xml;
43  }
44 
45  public function setMapping(ilImportMapping $mapping): void
46  {
47  $this->mapping = $mapping;
48  }
49 
50  public function getMapping(): ?ilImportMapping
51  {
52  return $this->mapping;
53  }
54 
55  protected function getCourse(): ilObjCourse
56  {
57  return $this->course;
58  }
59 
60  public function parse(): void
61  {
62  $use_internal_errors = libxml_use_internal_errors(true);
63  $root = simplexml_load_string(trim($this->xml));
64  libxml_use_internal_errors($use_internal_errors);
65  if (!$root instanceof SimpleXMLElement) {
66  $this->logger->debug('XML is: ' . $this->xml . $root);
67  $this->logger->debug('Error parsing objective xml: ' . $this->parseXmlErrors());
68  return;
69  }
70  $this->logger->debug('Handling element: ' . $root->getName());
71  $this->parseSettings($root);
72  $this->parseObjectives($root);
73  }
74 
78  public function parseObjectDependencies(): void
79  {
80  $use_internal_errors = libxml_use_internal_errors(true);
81  $root = simplexml_load_string(trim($this->xml));
82  libxml_use_internal_errors($use_internal_errors);
83  if (!$root instanceof SimpleXMLElement) {
84  $this->logger->debug('XML is: ' . $this->xml . $root);
85  $this->logger->debug('Error parsing objective xml: ' . $this->parseXmlErrors());
86  return;
87  }
88 
89  foreach ($root->Objective as $obj) {
90  $mapped_objective_id = $this->getMapping()->getMapping(
91  'components/ILIAS/Course',
92  'objectives',
93  (string) $obj->attributes()->id
94  );
95  if ($mapped_objective_id) {
96  $this->parseMaterials($obj, (int) $mapped_objective_id);
97  $this->parseTests($obj, (int) $mapped_objective_id);
98  }
99  }
100  }
101 
102  protected function parseSettings(SimpleXMLElement $root): void
103  {
104  $settings = ilLOSettings::getInstanceByObjId($this->getCourse()->getId());
105  $this->logger->debug(': Handling element: ' . $root->Settings->getName());
106  foreach ($root->Settings as $set) {
107  $this->logger->debug('Handling element: ' . $set->getName());
108  $settings->setInitialTestType((int) (string) $set->attributes()->initialTestType);
109  $settings->setInitialTestAsStart((bool) (string) $set->attributes()->initialTestStart);
110  $settings->setQualifyingTestType((int) (string) $set->attributes()->qualifyingTestType);
111  $settings->setQualifyingTestAsStart((bool) (string) $set->attributes()->qualifyingTestStart);
112  $settings->resetResults((bool) (string) $set->attributes()->resetResults);
113  $settings->setPassedObjectiveMode((int) (string) $set->attributes()->passedObjectivesMode);
114 
115  // itest
116  $itest = $this->getMappingInfoForItem((int) (string) $set->attributes()->iTest);
117  $settings->setInitialTest($itest);
118 
119  // qtest
120  $qtest = $this->getMappingInfoForItem((int) (string) $set->attributes()->qTest);
121  $settings->setQualifiedTest($qtest);
122 
123  $settings->update();
124  }
125  }
126 
130  protected function parseObjectives(SimpleXMLElement $root): void
131  {
132  foreach ($root->Objective as $obj) {
133  $new_obj = new ilCourseObjective($this->getCourse());
134  $new_obj->setActive((bool) (string) $obj->attributes()->online);
135  $new_obj->setTitle((string) $obj->Title);
136  $new_obj->setDescription((string) $obj->Description);
137  $new_obj->setPosition((int) (string) $obj->attributes()->position);
138  $new_objective_id = $new_obj->add();
139 
140  $this->getMapping()->addMapping(
141  'components/ILIAS/Course',
142  'objectives',
143  (string) $obj->attributes()->id,
144  (string) $new_objective_id
145  );
146  $this->getMapping()->addMapping(
147  'components/ILIAS/COPage',
148  'pg',
149  'lobj:' . $obj->attributes()->id,
150  'lobj:' . $new_objective_id
151  );
152  }
153  }
154 
155  protected function parseMaterials(SimpleXMLElement $obj, int $a_objective_id): void
156  {
157  foreach ($obj->Material as $mat) {
158  $mat_ref_id = (string) $mat->attributes()->refId;
159 
160  $mapping_ref_id = $this->getMappingInfoForItem((int) $mat_ref_id);
161  if ($mapping_ref_id) {
162  $new_mat = new ilCourseObjectiveMaterials($a_objective_id);
163  $new_mat->setLMRefId($mapping_ref_id);
164 
165  $mat_type = (string) $mat->attributes()->type;
166  $obj_id = 0;
167  switch ($mat_type) {
168  case 'st':
169  $mapped_chapter = $this->getMapping()->getMapping(
170  'components/ILIAS/LearningModule',
171  'lm_tree',
172  (string) $mat->attributes()->objId
173  );
174  if ($mapped_chapter) {
175  $obj_id = $mapped_chapter;
176  }
177  break;
178 
179  case 'pg':
180  $mapped_page = $this->getMapping()->getMapping(
181  'components/ILIAS/LearningModule',
182  'pg',
183  (string) $mat->attributes()->objId
184  );
185  if ($mapped_page) {
186  $obj_id = $mapped_page;
187  }
188  break;
189 
190  default:
191  $obj_id = ilObject::_lookupObjId($mapping_ref_id);
192  break;
193  }
194  if ($obj_id) {
195  $new_mat->setLMObjId($obj_id);
196  $new_mat->setType((string) $mat->attributes()->type);
197  $new_id = $new_mat->add();
198  $new_mat->writePosition($new_id, (int) (string) $mat->attributes()->position);
199  }
200  }
201  }
202  }
203 
204  protected function parseTests(SimpleXMLElement $obj, int $a_objective_id): void
205  {
206  $this->logger->debug(': Parsing ' . $obj->getName());
207 
208  foreach ($obj->Test as $tst) {
209  $type = (int) (string) $tst->attributes()->type;
210  if ($type == self::TYPE_TST_PO) {
211  $tst_ref_id = (string) $tst->attributes()->refId;
212  $mapping_ref_id = $this->getMappingInfoForItem((int) $tst_ref_id);
213  $this->logger->debug('Found test ref id ' . $tst_ref_id);
214  if (!$mapping_ref_id) {
215  continue;
216  }
217  $assignment = new ilLOTestAssignment();
218  $assignment->setContainerId($this->getCourse()->getId());
219  $assignment->setTestRefId($mapping_ref_id);
220  $assignment->setObjectiveId($a_objective_id);
221  $assignment->setAssignmentType((int) (string) $tst->attributes()->testType);
222  $assignment->save();
223  } elseif ($type == self::TYPE_TST_RND) {
224  $tst_obj_id = (int) (string) $tst->attributes()->objId;
225  $mapping_id = $this->getMappingInfoForItemObject($tst_obj_id);
226  if (!$mapping_id) {
227  continue;
228  }
229 
230  $new_qpl_id = $this->getMappingForQpls((int) (string) $tst->attributes()->poolId);
231  if (!$new_qpl_id) {
232  continue;
233  }
234 
235  $rnd = new ilLORandomTestQuestionPools(
236  $this->getCourse()->getId(),
237  $a_objective_id,
238  (int) (string) $tst->attributes()->testType,
239  $new_qpl_id
240  );
241  $rnd->setTestId($mapping_id);
242  $rnd->setLimit((int) $tst->attributes()->limit);
243  $rnd->create();
244  } else {
245  $tst_ref_id = (string) $tst->attributes()->refId;
246  $mapping_ref_id = $this->getMappingInfoForItem((int) $tst_ref_id);
247  if (!$mapping_ref_id) {
248  continue;
249  }
250  $quest = new ilCourseObjectiveQuestion($a_objective_id);
251  $quest->setTestRefId($mapping_ref_id);
252  $quest->setTestObjId(ilObject::_lookupObjId($mapping_ref_id));
253  $quest->setTestStatus((int) $tst->attributes()->testType);
254  $quest->setTestSuggestedLimit((int) $tst->attributes()->limit);
255 
256  foreach ($tst->Question as $qst) {
257  $qid = (string) $qst->attributes()->id;
258  $mapping_qid = $this->getMappingForQuestion((int) $qid);
259  if ($mapping_qid) {
260  $quest->setQuestionId($mapping_qid);
261  $quest->add();
262  }
263  }
264  }
265  }
266  }
267 
268  protected function getMappingInfoForItem(int $a_ref_id): int
269  {
270  $new_ref_id = $this->getMapping()->getMapping('components/ILIAS/Container', 'refs', (string) $a_ref_id);
271  $this->logger->debug(': Found new ref_id: ' . $new_ref_id . ' for ' . $a_ref_id);
272  return (int) $new_ref_id;
273  }
274 
275  protected function getMappingInfoForItemObject(int $a_obj_id): int
276  {
277  $new_obj_id = $this->getMapping()->getMapping('components/ILIAS/Container', 'objs', (string) $a_obj_id);
278  $this->logger->debug('Found new ref_id: ' . $new_obj_id . ' for ' . $a_obj_id);
279  return (int) $new_obj_id;
280  }
281 
282  protected function getMappingForQuestion(int $qid): int
283  {
284  $new_qid = $this->getMapping()->getMapping('components/ILIAS/Test', 'quest', (string) $qid);
285  $this->logger->debug('Found new question_id: ' . $new_qid . ' for ' . $qid);
286  return (int) $new_qid;
287  }
288 
289  protected function getMappingForQpls(int $a_id): int
290  {
291  $new_id = $this->getMapping()->getMapping('components/ILIAS/Test', 'rnd_src_pool_def', (string) $a_id);
292  if ($new_id) {
293  return (int) $new_id;
294  }
295  return 0;
296  }
297 
301  protected function parseXmlErrors(): string
302  {
303  $errors = '';
304  foreach (libxml_get_errors() as $err) {
305  $errors .= $err->code . '<br/>';
306  }
307  return $errors;
308  }
309 }
parseObjectives(SimpleXMLElement $root)
Parse objective.
parseSettings(SimpleXMLElement $root)
parseObjectDependencies()
Parse object dependencies (assigned strucure objects, page objects, fixed questions) ...
getMappingForQuestion(int $qid)
__construct(ilObjCourse $course, string $a_xml)
ilImportMapping $mapping
static _lookupObjId(int $ref_id)
parseTests(SimpleXMLElement $obj, int $a_objective_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
parseXmlErrors()
Parse xml errors from libxml_get_errors.
class ilCourseObjectiveMaterials
setMapping(ilImportMapping $mapping)
Class ilLOXmlWriter.
getMappingForQpls(int $a_id)
class ilcourseobjective
global $DIC
Definition: shib_login.php:22
getMappingInfoForItem(int $a_ref_id)
getMappingInfoForItemObject(int $a_obj_id)
static getInstanceByObjId(int $a_obj_id)
Settings for LO courses.
parseMaterials(SimpleXMLElement $obj, int $a_objective_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...