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