ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.assKprimChoiceImport.php
Go to the documentation of this file.
1 <?php
25 {
29  public $object;
30 
31  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
32  {
33  global $DIC;
34  $ilUser = $DIC['ilUser'];
35 
36  ilSession::clear('import_mob_xhtml');
37 
38  $shuffle = 0;
39  $answers = array();
40 
41  $presentation = $item->getPresentation();
42  foreach ($presentation->order as $entry) {
43  switch ($entry["type"]) {
44  case "response":
45  $response = $presentation->response[$entry["index"]];
46  $rendertype = $response->getRenderType();
47  switch (strtolower(get_class($response->getRenderType()))) {
48  case "ilqtirenderchoice":
49  $shuffle = $rendertype->getShuffle();
50  $answerorder = 0;
51  $foundimage = false;
52  foreach ($rendertype->response_labels as $response_label) {
53  $ident = $response_label->getIdent();
54  $answertext = "";
55  $answerimage = array();
56  foreach ($response_label->material as $mat) {
57  $embedded = false;
58  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
59  $foundmat = $mat->getMaterial($m);
60  if (strcmp($foundmat["type"], "mattext") == 0) {
61  }
62  if (strcmp($foundmat["type"], "matimage") == 0) {
63  if (strlen($foundmat["material"]->getEmbedded())) {
64  $embedded = true;
65  }
66  }
67  }
68  if ($embedded) {
69  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
70  $foundmat = $mat->getMaterial($m);
71  if (strcmp($foundmat["type"], "mattext") == 0) {
72  $answertext .= $foundmat["material"]->getContent();
73  }
74  if (strcmp($foundmat["type"], "matimage") == 0) {
75  $foundimage = true;
76  $answerimage = array(
77  "imagetype" => $foundmat["material"]->getImageType(),
78  "label" => $foundmat["material"]->getLabel(),
79  "content" => $foundmat["material"]->getContent()
80  );
81  }
82  }
83  } else {
84  $answertext = $this->QTIMaterialToString($mat);
85  }
86  }
87 
88  $answers[$ident] = array(
89  "answertext" => $answertext,
90  "imagefile" => $answerimage,
91  "answerorder" => $ident
92  );
93  }
94  break;
95  }
96  break;
97  }
98  }
99 
100  $feedbacks = array();
101  $feedbacksgeneric = array();
102 
103  foreach ($item->resprocessing as $resprocessing) {
104  foreach ($resprocessing->outcomes->decvar as $decvar) {
105  if ($decvar->getVarname() == 'SCORE') {
106  $this->object->setPoints($decvar->getMaxvalue());
107 
108  if ($decvar->getMinvalue() > 0) {
109  $this->object->setScorePartialSolutionEnabled(true);
110  } else {
111  $this->object->setScorePartialSolutionEnabled(false);
112  }
113  }
114  }
115 
116  foreach ($resprocessing->respcondition as $respcondition) {
117  if (!count($respcondition->setvar)) {
118  foreach ($respcondition->getConditionvar()->varequal as $varequal) {
119  $ident = $varequal->respident;
120  $answers[$ident]['correctness'] = (bool) $varequal->getContent();
121 
122  break;
123  }
124 
125  foreach ($respcondition->displayfeedback as $feedbackpointer) {
126  if (strlen($feedbackpointer->getLinkrefid())) {
127  foreach ($item->itemfeedback as $ifb) {
128  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
129  // found a feedback for the identifier
130  if (count($ifb->material)) {
131  foreach ($ifb->material as $material) {
132  $feedbacks[$ident] = $material;
133  }
134  }
135  if ((count($ifb->flow_mat) > 0)) {
136  foreach ($ifb->flow_mat as $fmat) {
137  if (count($fmat->material)) {
138  foreach ($fmat->material as $material) {
139  $feedbacks[$ident] = $material;
140  }
141  }
142  }
143  }
144  }
145  }
146  }
147  }
148  } else {
149  foreach ($respcondition->displayfeedback as $feedbackpointer) {
150  if (strlen($feedbackpointer->getLinkrefid())) {
151  foreach ($item->itemfeedback as $ifb) {
152  if ($ifb->getIdent() == "response_allcorrect") {
153  // found a feedback for the identifier
154  if (count($ifb->material)) {
155  foreach ($ifb->material as $material) {
156  $feedbacksgeneric[1] = $material;
157  }
158  }
159  if ((count($ifb->flow_mat) > 0)) {
160  foreach ($ifb->flow_mat as $fmat) {
161  if (count($fmat->material)) {
162  foreach ($fmat->material as $material) {
163  $feedbacksgeneric[1] = $material;
164  }
165  }
166  }
167  }
168  } elseif ($ifb->getIdent() == "response_onenotcorrect") {
169  // found a feedback for the identifier
170  if (count($ifb->material)) {
171  foreach ($ifb->material as $material) {
172  $feedbacksgeneric[0] = $material;
173  }
174  }
175  if ((count($ifb->flow_mat) > 0)) {
176  foreach ($ifb->flow_mat as $fmat) {
177  if (count($fmat->material)) {
178  foreach ($fmat->material as $material) {
179  $feedbacksgeneric[0] = $material;
180  }
181  }
182  }
183  }
184  }
185  }
186  }
187  }
188  }
189  }
190  }
191 
192  $this->addGeneralMetadata($item);
193  $this->object->setTitle($item->getTitle());
194  $this->object->setNrOfTries((int) $item->getMaxattempts());
195  $this->object->setComment($item->getComment());
196  $this->object->setAuthor($item->getAuthor());
197  $this->object->setOwner($ilUser->getId());
198  $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
199  $this->object->setObjId($questionpool_id);
200  $this->object->setShuffleAnswersEnabled($shuffle);
201  $this->object->setAnswerType($item->getMetadataEntry('answer_type'));
202  $this->object->setOptionLabel($item->getMetadataEntry('option_label_setting'));
203  $this->object->setCustomTrueOptionLabel($item->getMetadataEntry('custom_true_option_label'));
204  $this->object->setCustomFalseOptionLabel($item->getMetadataEntry('custom_false_option_label'));
205  $this->object->setThumbSize(
206  $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_size'))
207  );
208 
209  $this->object->saveToDb();
210 
211  foreach ($answers as $answerData) {
212  $answer = new ilAssKprimChoiceAnswer();
213  $answer->setImageFsDir($this->object->getImagePath());
214  $answer->setImageWebDir($this->object->getImagePathWeb());
215 
216  $answer->setPosition($answerData['answerorder']);
217  $answer->setAnswertext($answerData['answertext']);
218  $answer->setCorrectness($answerData['correctness']);
219 
220  if (isset($answerData['imagefile']['label'])) {
221  $answer->setImageFile($answerData['imagefile']['label']);
222  }
223 
224  $this->object->addAnswer($answer);
225  }
226  // additional content editing mode information
227  $this->object->setAdditionalContentEditingMode(
229  );
230 
231  $this->object->saveToDb();
232 
233  foreach ($answers as $answer) {
234  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
235  $image = base64_decode($answer["imagefile"]["content"]);
236  $imagepath = $this->object->getImagePath();
237  if (!file_exists($imagepath)) {
238  ilFileUtils::makeDirParents($imagepath);
239  }
240  $imagepath .= $answer["imagefile"]["label"];
241  if ($fh = fopen($imagepath, "wb")) {
242  $imagefile = fwrite($fh, $image);
243  fclose($fh);
244  $this->object->generateThumbForFile(
245  $answer["imagefile"]["label"],
246  $this->object->getImagePath(),
247  $this->object->getThumbSize()
248  );
249  }
250  }
251  }
252 
253  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
254  if (!is_null($feedbackSetting)) {
255  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
256  $this->object->setSpecificFeedbackSetting($feedbackSetting);
257  }
258 
259  // handle the import of media objects in XHTML code
260  foreach ($feedbacks as $ident => $material) {
261  $m = $this->QTIMaterialToString($material);
262  $feedbacks[$ident] = $m;
263  }
264  foreach ($feedbacksgeneric as $correctness => $material) {
265  $m = $this->QTIMaterialToString($material);
266  $feedbacksgeneric[$correctness] = $m;
267  }
268  $questiontext = $this->object->getQuestion();
269  $answers = $this->object->getAnswers();
270  if (is_array(ilSession::get("import_mob_xhtml"))) {
271  foreach (ilSession::get("import_mob_xhtml") as $mob) {
272  if ($tst_id > 0) {
273  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
274  } else {
275  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
276  }
277 
278  global $DIC; /* @var ILIAS\DI\Container $DIC */
279  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
280 
281  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
282  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
283  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
284  foreach ($answers as $key => $value) {
285  $answer_obj = &$answers[$key];
286  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
287  }
288  foreach ($feedbacks as $ident => $material) {
289  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
290  }
291  foreach ($feedbacksgeneric as $correctness => $material) {
292  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
293  }
294  }
295  }
296  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
297  foreach ($answers as $key => $value) {
298  $answer_obj = &$answers[$key];
299  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
300  }
301  foreach ($feedbacks as $ident => $material) {
302  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
303  $this->object->getId(),
304  0,
305  $ident,
307  );
308  }
309  foreach ($feedbacksgeneric as $correctness => $material) {
310  $this->object->feedbackOBJ->importGenericFeedback(
311  $this->object->getId(),
312  $correctness,
314  );
315  }
316  $this->object->saveToDb();
317  $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
318  if ($tst_id > 0) {
319  $q_1_id = $this->object->getId();
320  $question_id = $this->object->duplicate(true, "", "", -1, $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
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
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.
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)