ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.assMultipleChoiceImport.php
Go to the documentation of this file.
1 <?php
2 
29 {
43  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
44  {
45  global $DIC;
46  $ilUser = $DIC['ilUser'];
47  // empty session variable for imported xhtml mobs
48  ilSession::clear('import_mob_xhtml');
49 
50  $presentation = $item->getPresentation();
51  $shuffle = 0;
52  $selectionLimit = null;
53  $now = getdate();
54  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
55  $answers = array();
56  foreach ($presentation->order as $entry) {
57  switch ($entry["type"]) {
58  case "response":
59  $response = $presentation->response[$entry["index"]];
60  $rendertype = $response->getRenderType();
61  switch (strtolower(get_class($response->getRenderType()))) {
62  case "ilqtirenderchoice":
63  $shuffle = $rendertype->getShuffle();
64  if ($rendertype->getMaxnumber()) {
65  $selectionLimit = $rendertype->getMaxnumber();
66  }
67  $answerorder = 0;
68  $foundimage = false;
69  foreach ($rendertype->response_labels as $response_label) {
70  $ident = $response_label->getIdent();
71  $answertext = "";
72  $answerimage = array();
73  foreach ($response_label->material as $mat) {
74  $embedded = false;
75  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
76  $foundmat = $mat->getMaterial($m);
77  if (strcmp($foundmat["type"], "mattext") == 0) {
78  }
79  if (strcmp($foundmat["type"], "matimage") == 0) {
80  if (strlen($foundmat["material"]->getEmbedded())) {
81  $embedded = true;
82  }
83  }
84  }
85  if ($embedded) {
86  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
87  $foundmat = $mat->getMaterial($m);
88  if (strcmp($foundmat["type"], "mattext") == 0) {
89  $answertext .= $foundmat["material"]->getContent();
90  }
91  if (strcmp($foundmat["type"], "matimage") == 0) {
92  $foundimage = true;
93  $answerimage = array(
94  "imagetype" => $foundmat["material"]->getImageType(),
95  "label" => $foundmat["material"]->getLabel(),
96  "content" => $foundmat["material"]->getContent()
97  );
98  }
99  }
100  } else {
101  $answertext = $this->object->QTIMaterialToString($mat);
102  }
103  }
104  $answers[$ident] = array(
105  "answertext" => $answertext,
106  "imagefile" => $answerimage,
107  "points" => 0,
108  "answerorder" => $answerorder++,
109  "points_unchecked" => 0,
110  "action" => ""
111  );
112  }
113  break;
114  }
115  break;
116  }
117  }
118  $responses = array();
119  $feedbacks = array();
120  $feedbacksgeneric = array();
121  foreach ($item->resprocessing as $resprocessing) {
122  foreach ($resprocessing->respcondition as $respcondition) {
123  $ident = "";
124  $correctness = 1;
125  $conditionvar = $respcondition->getConditionvar();
126  foreach ($conditionvar->order as $order) {
127  switch ($order["field"]) {
128  case "arr_not":
129  $correctness = 0;
130  break;
131  case "varequal":
132  $ident = $conditionvar->varequal[$order["index"]]->getContent();
133  break;
134  }
135  }
136  foreach ($respcondition->setvar as $setvar) {
137  if (strcmp($ident, "") != 0) {
138  if ($correctness) {
139  $answers[$ident]["action"] = $setvar->getAction();
140  $answers[$ident]["points"] = $setvar->getContent();
141  if (count($respcondition->displayfeedback)) {
142  foreach ($respcondition->displayfeedback as $feedbackpointer) {
143  if (strlen($feedbackpointer->getLinkrefid())) {
144  foreach ($item->itemfeedback as $ifb) {
145  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
146  // found a feedback for the identifier
147  if (count($ifb->material)) {
148  foreach ($ifb->material as $material) {
149  $feedbacksgeneric[1] = $material;
150  }
151  }
152  if ((count($ifb->flow_mat) > 0)) {
153  foreach ($ifb->flow_mat as $fmat) {
154  if (count($fmat->material)) {
155  foreach ($fmat->material as $material) {
156  $feedbacksgeneric[1] = $material;
157  }
158  }
159  }
160  }
161  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
162  // found a feedback for the identifier
163  if (count($ifb->material)) {
164  foreach ($ifb->material as $material) {
165  $feedbacksgeneric[0] = $material;
166  }
167  }
168  if ((count($ifb->flow_mat) > 0)) {
169  foreach ($ifb->flow_mat as $fmat) {
170  if (count($fmat->material)) {
171  foreach ($fmat->material as $material) {
172  $feedbacksgeneric[0] = $material;
173  }
174  }
175  }
176  }
177  }
178  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
179  // found a feedback for the identifier
180  if (count($ifb->material)) {
181  foreach ($ifb->material as $material) {
182  $feedbacks[$ident] = $material;
183  }
184  }
185  if ((count($ifb->flow_mat) > 0)) {
186  foreach ($ifb->flow_mat as $fmat) {
187  if (count($fmat->material)) {
188  foreach ($fmat->material as $material) {
189  $feedbacks[$ident] = $material;
190  }
191  }
192  }
193  }
194  }
195  }
196  }
197  }
198  }
199  } else {
200  $answers[$ident]["action"] = $setvar->getAction();
201  $answers[$ident]["points_unchecked"] = $setvar->getContent();
202  }
203  }
204  }
205  }
206  }
207  $this->addGeneralMetadata($item);
208  $this->object->setTitle($item->getTitle());
209  $this->object->setNrOfTries((int) $item->getMaxattempts());
210  $this->object->setComment($item->getComment());
211  $this->object->setAuthor($item->getAuthor());
212  $this->object->setOwner($ilUser->getId());
213  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
214  $this->object->setObjId($questionpool_id);
215  $this->object->setShuffle($shuffle);
216  $this->object->setSelectionLimit($selectionLimit);
217  $this->object->setThumbSize(
218  $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_size'))
219  );
220 
221  foreach ($answers as $answer) {
222  if ($item->getMetadataEntry('singleline') || (is_array($answer["imagefile"]) && count($answer["imagefile"]) > 0)) {
223  $this->object->setIsSingleline(true);
224  }
225  $this->object->addAnswer($answer["answertext"], $answer["points"], $answer["points_unchecked"], $answer["answerorder"], $answer["imagefile"]["label"] ?? '');
226  }
227  // additional content editing mode information
228  $this->object->setAdditionalContentEditingMode(
230  );
231  $this->object->saveToDb();
232  foreach ($answers as $answer) {
233  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
234  $image = base64_decode($answer["imagefile"]["content"]);
235  $imagepath = $this->object->getImagePath();
236  if (!file_exists($imagepath)) {
237  ilFileUtils::makeDirParents($imagepath);
238  }
239  $imagepath .= $answer["imagefile"]["label"];
240  $fh = fopen($imagepath, "wb");
241  if ($fh == false) {
242  } else {
243  $imagefile = fwrite($fh, $image);
244  fclose($fh);
245  }
246  }
247  }
248 
249  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
250  if (!is_null($feedbackSetting)) {
251  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
252  }
253 
254  // handle the import of media objects in XHTML code
255  foreach ($feedbacks as $ident => $material) {
256  $m = $this->object->QTIMaterialToString($material);
257  $feedbacks[$ident] = $m;
258  }
259  foreach ($feedbacksgeneric as $correctness => $material) {
260  $m = $this->object->QTIMaterialToString($material);
261  $feedbacksgeneric[$correctness] = $m;
262  }
263  $questiontext = $this->object->getQuestion();
264  $answers = &$this->object->getAnswers();
265  if (is_array(ilSession::get("import_mob_xhtml"))) {
266  foreach (ilSession::get("import_mob_xhtml") as $mob) {
267  if ($tst_id > 0) {
268  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
269  } else {
270  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
271  }
272 
273  global $DIC; /* @var ILIAS\DI\Container $DIC */
274  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
275 
276  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
277  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
278  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
279  foreach ($answers as $key => $value) {
280  $answer_obj = &$answers[$key];
281  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
282  }
283  foreach ($feedbacks as $ident => $material) {
284  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
285  }
286  foreach ($feedbacksgeneric as $correctness => $material) {
287  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
288  }
289  }
290  }
291  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
292  foreach ($answers as $key => $value) {
293  $answer_obj = &$answers[$key];
294  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
295  }
296  foreach ($feedbacks as $ident => $material) {
297  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
298  $this->object->getId(),
299  0,
300  $ident,
302  );
303  }
304  foreach ($feedbacksgeneric as $correctness => $material) {
305  $this->object->feedbackOBJ->importGenericFeedback(
306  $this->object->getId(),
307  $correctness,
309  );
310  }
311  $this->object->saveToDb();
312  if (count($item->suggested_solutions)) {
313  foreach ($item->suggested_solutions as $suggested_solution) {
314  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
315  }
316  $this->object->saveToDb();
317  }
318  if ($tst_id > 0) {
319  $q_1_id = $this->object->getId();
320  $question_id = $this->object->duplicate(true, "", "", "", $tst_id);
321  $tst_object->questions[$question_counter++] = $question_id;
322  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
323  } else {
324  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
325  }
326  return $import_mapping;
327  }
328 }
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)
deduceThumbSizeFromImportValue(?int $size)
addGeneralMetadata(ilQTIItem $item)
const IL_INST_ID
Definition: constants.php:40
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.
global $DIC
Definition: feed.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
Creates a question from a QTI file.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$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)
int $created
Timestamp for when the object was created.
Definition: System.php:151