ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.assOrderingQuestionImport.php
Go to the documentation of this file.
1 <?php
2 
29 {
33  public $object;
34 
48  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
49  {
50  global $DIC;
51  $ilUser = $DIC['ilUser'];
52 
53  // empty session variable for imported xhtml mobs
54  ilSession::clear('import_mob_xhtml');
55 
56  $presentation = $item->getPresentation();
57  $shuffle = 0;
58  $now = getdate();
59  $foundimage = false;
60  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
61  $answers = [];
62  $type = OQ_TERMS;
63 
64  foreach ($presentation->order as $entry) {
65  switch ($entry["type"]) {
66  case "response":
67  $response = $presentation->response[$entry["index"]];
68  $type = $response->getIdent();
69  if ($response->getIdent() == 'OQP') {
71  } elseif ($response->getIdent() == 'OQNP') {
73  } elseif ($response->getIdent() == 'OQNT') {
75  } elseif ($response->getIdent() == 'OQT') {
76  $type = OQ_TERMS;
77  }
78 
79  $rendertype = $response->getRenderType();
80  switch (strtolower(get_class($rendertype))) {
81  case "ilqtirenderchoice":
82  $shuffle = $rendertype->getShuffle();
83  $answerorder = 0;
84  foreach ($rendertype->response_labels as $response_label) {
85  $ident = $response_label->getIdent();
86  $answertext = "";
87  $answerimage = [];
88  $answerdepth = 0;
89  foreach ($response_label->material as $mat) {
90  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
91  $foundmat = $mat->getMaterial($m);
92 
93  if (strcmp($foundmat["material"]->getLabel(), "answerdepth") == 0) {
94  $answerdepth = $foundmat["material"]->getContent();
95  }
96  if (strcmp($foundmat["type"], "mattext") == 0
97  && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0) {
98  $answertext .= $foundmat["material"]->getContent();
99  }
100  if (strcmp($foundmat["type"], "matimage") == 0
101  && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0) {
102  $foundimage = true;
103  $answerimage = array(
104  "imagetype" => $foundmat["material"]->getImageType(),
105  "label" => $foundmat["material"]->getLabel(),
106  "content" => $foundmat["material"]->getContent()
107  );
108  }
109  }
110  }
111  $answers[$answerorder] = array(
112  'ident' => $ident,
113  "answertext" => $answertext,
114  "answerimage" => $answerimage,
115  "points" => 0,
116  "answerorder" => $answerorder,
117  "answerdepth" => $answerdepth,
118  "correctness" => "",
119  "action" => ""
120  );
121  $answerorder++;
122  }
123  break;
124  }
125  break;
126  }
127  }
128 
129  $feedbacksgeneric = array();
130  foreach ($item->resprocessing as $resprocessing) {
131  foreach ($resprocessing->respcondition as $respcondition) {
132  $ident = "";
133  $correctness = 1;
134  $conditionvar = $respcondition->getConditionvar();
135  foreach ($conditionvar->order as $order) {
136  switch ($order["field"]) {
137  case "arr_not":
138  $correctness = 0;
139  break;
140  case "varequal":
141  $ident = $conditionvar->varequal[$order["index"]]->getContent();
142  $orderindex = $conditionvar->varequal[$order["index"]]->getIndex();
143  break;
144  }
145  }
146  foreach ($respcondition->setvar as $setvar) {
147  if (strcmp($ident, "") != 0) {
148  $answers[$ident]["solutionorder"] = $orderindex;
149  $answers[$ident]["action"] = $setvar->getAction();
150  $answers[$ident]["points"] = $setvar->getContent();
151  }
152  }
153  if (count($respcondition->displayfeedback)) {
154  foreach ($respcondition->displayfeedback as $feedbackpointer) {
155  if (strlen($feedbackpointer->getLinkrefid())) {
156  foreach ($item->itemfeedback as $ifb) {
157  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
158  // found a feedback for the identifier
159  if (count($ifb->material)) {
160  foreach ($ifb->material as $material) {
161  $feedbacksgeneric[1] = $material;
162  }
163  }
164  if ((count($ifb->flow_mat) > 0)) {
165  foreach ($ifb->flow_mat as $fmat) {
166  if (count($fmat->material)) {
167  foreach ($fmat->material as $material) {
168  $feedbacksgeneric[1] = $material;
169  }
170  }
171  }
172  }
173  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
174  // found a feedback for the identifier
175  if (count($ifb->material)) {
176  foreach ($ifb->material as $material) {
177  $feedbacksgeneric[0] = $material;
178  }
179  }
180  if ((count($ifb->flow_mat) > 0)) {
181  foreach ($ifb->flow_mat as $fmat) {
182  if (count($fmat->material)) {
183  foreach ($fmat->material as $material) {
184  $feedbacksgeneric[0] = $material;
185  }
186  }
187  }
188  }
189  }
190  }
191  }
192  }
193  }
194  }
195  }
196 
197  $itemfeedbacks = $this->getFeedbackAnswerSpecific($item, 'link_');
198 
199  $this->addGeneralMetadata($item);
200  $this->object->setTitle($item->getTitle());
201  $this->object->setNrOfTries((int) $item->getMaxattempts());
202  $this->object->setComment($item->getComment());
203  $this->object->setAuthor($item->getAuthor());
204  $this->object->setOwner($ilUser->getId());
205  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
206  $this->object->setOrderingType($type);
207  $this->object->setObjId($questionpool_id);
208  $this->object->setThumbSize(
209  $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_geometry'))
210  );
211  $this->object->setElementHeight($item->getMetadataEntry('element_height')
212  ? (int) $item->getMetadataEntry('element_height')
213  : null);
214  $this->object->setShuffle($shuffle);
215  $this->object->setPoints(0);
216  $this->object->saveQuestionDataToDb();
217  $points = 0;
218  $solanswers = array();
219 
220  foreach ($answers as $answer) {
221  $solanswers[$answer["solutionorder"] ?? null] = $answer;
222  }
223  ksort($solanswers);
224  $position = 0;
225  $element_list = $this->object->getOrderingElementList();
226  foreach ($solanswers as $answer) {
227  $points += $answer["points"];
228 
229  $element = new ilAssOrderingElement();
230 
231  if ($element->isExportIdent($answer['ident'])) {
232  $element->setExportIdent($answer['ident']);
233  } else {
234  $element = $element->withPosition($position++);
235  if (isset($answer['answerdepth'])) {
236  $element = $element->withIndentation((int) $answer['answerdepth']);
237  }
238  }
239 
240  if ($this->object->isImageOrderingType()) {
241  $filename = $this->handleUploadedfile($answer);
242  if ($filename !== null) {
243  $element = $element->withContent($filename);
244  }
245  } else {
246  $element = $element->withContent($answer["answertext"]);
247  }
248 
249  $element_list->addElement($element);
250  }
251  $this->object->setOrderingElementList($element_list);
252  $points = ($item->getMetadataEntry("points") > 0) ? $item->getMetadataEntry("points") : $points;
253  $this->object->setPoints($points);
254  // additional content editing mode information
255  $this->object->setAdditionalContentEditingMode(
257  );
258  $this->object->saveToDb();
259  if (count($item->suggested_solutions)) {
260  foreach ($item->suggested_solutions as $suggested_solution) {
261  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
262  }
263  $this->object->saveToDb();
264  }
265 
266  foreach ($feedbacksgeneric as $correctness => $material) {
267  $m = $this->object->QTIMaterialToString($material);
268  $feedbacksgeneric[$correctness] = $m;
269  }
270  $questiontext = $this->object->getQuestion();
271 
272  // handle the import of media objects in XHTML code
273  if (is_array(ilSession::get("import_mob_xhtml"))) {
274  foreach (ilSession::get("import_mob_xhtml") as $mob) {
275  if ($tst_id > 0) {
276  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
277  } else {
278  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
279  }
280 
281  global $DIC; /* @var ILIAS\DI\Container $DIC */
282  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
283 
284  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
285  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
286  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
287  foreach ($this->object->getOrderingElementList() as $element) {
288  $element->setContent(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $element->getContent()));
289  }
290  foreach ($feedbacksgeneric as $correctness => $material) {
291  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
292  }
293  foreach ($itemfeedbacks as $ident => $material) {
294  $itemfeedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
295  }
296  }
297  }
298  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
299  foreach ($this->object->getOrderingElementList() as $element) {
300  $element->setContent(ilRTE::_replaceMediaObjectImageSrc($element->getContent(), 1));
301  }
302  foreach ($feedbacksgeneric as $correctness => $material) {
303  $this->object->feedbackOBJ->importGenericFeedback(
304  $this->object->getId(),
305  $correctness,
307  );
308  }
309  foreach ($itemfeedbacks as $ident => $material) {
310  $index = $this->fetchIndexFromFeedbackIdent($ident, 'link_');
311 
312  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
313  $this->object->getId(),
314  0,
315  $index,
317  );
318  }
319  $this->object->saveToDb();
320  if (isset($tst_id) && $tst_id !== $questionpool_id) {
321  $qplQid = $this->object->getId();
322  $tstQid = $this->object->duplicate(true, '', '', '', $tst_id);
323  $tst_object->questions[$question_counter++] = $tstQid;
324  $import_mapping[$item->getIdent()] = ["pool" => $qplQid, "test" => $tstQid];
325  return $import_mapping;
326  }
327 
328  if ($tst_id > 0) {
329  $tst_object->questions[$question_counter++] = $this->object->getId();
330  $import_mapping[$item->getIdent()] = ["pool" => 0, "test" => $this->object->getId()];
331  return $import_mapping;
332  }
333 
334  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
335  return $import_mapping;
336  }
337 
338  protected function handleUploadedFile(array $answer): ?string
339  {
340  $image = base64_decode($answer["answerimage"]["content"] ?? '');
341  $image_file_name = $answer['answerimage']['label'] ?? '';
342  $tmp_path = ilFileUtils::ilTempnam();
343 
344  $file_handle = fopen($tmp_path, "wb");
345  if ($file_handle === false) {
346  return null;
347  }
348  fwrite($file_handle, $image);
349  fclose($file_handle);
350 
351  $filename_path_parts = explode(".", $image_file_name);
352  $suffix = strtolower(array_pop($filename_path_parts));
353  if (!in_array($suffix, assOrderingQuestion::VALID_UPLOAD_SUFFIXES)) {
354  return null;
355  }
356 
357  $this->ensureImagePathExists();
358  $target_filename = $this->object->buildHashedImageFilename($image_file_name, true);
359  $target_filepath = $this->object->getImagePath() . $target_filename;
360  if (rename($tmp_path, $target_filepath)) {
361  $thumb_path = $this->object->getImagePath() . $this->object->getThumbPrefix() . $target_filename;
362  ilShellUtil::convertImage($target_filepath, $thumb_path, "JPEG", $this->object->getThumbSize());
363  return $target_filename;
364  }
365 
366  return null;
367  }
368 
369  protected function ensureImagePathExists()
370  {
371  if (!file_exists($this->object->getImagePath())) {
372  ilFileUtils::makeDirParents($this->object->getImagePath());
373  }
374  }
375 }
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static get(string $a_var)
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
deduceThumbSizeFromImportValue(?int $size)
addGeneralMetadata(ilQTIItem $item)
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
Creates a question from a QTI file.
const IL_INST_ID
Definition: constants.php:40
$type
const OQ_NESTED_TERMS
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
$index
Definition: metadata.php:145
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const OQ_PICTURES
Ordering question constants.
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
convert image
$filename
Definition: buildRTE.php:78
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
const OQ_TERMS
const OQ_NESTED_PICTURES
$ilUser
Definition: imgupload.php:34
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$response
static clear(string $a_var)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
int $created
Timestamp for when the object was created.
Definition: System.php:151