ILIAS  release_8 Revision v8.24
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 = 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 $answerorder = 0;
65 $foundimage = false;
66 foreach ($rendertype->response_labels as $response_label) {
67 $ident = $response_label->getIdent();
68 $answertext = "";
69 $answerimage = array();
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 (strlen($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 = array(
91 "imagetype" => $foundmat["material"]->getImageType(),
92 "label" => $foundmat["material"]->getLabel(),
93 "content" => $foundmat["material"]->getContent()
94 );
95 }
96 }
97 } else {
98 $answertext = $this->object->QTIMaterialToString($mat);
99 }
100 }
101 $answers[$ident] = array(
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 = array();
116 $feedbacks = array();
117 $feedbacksgeneric = array();
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->object->QTIMaterialToString($item->getQuestiontext()));
211 $this->object->setObjId($questionpool_id);
212 $this->object->setShuffle($shuffle);
213 $this->object->setThumbSize(
214 $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_size'))
215 );
216 foreach ($answers as $answer) {
217 if ($item->getMetadataEntry('singleline') || (is_array($answer["imagefile"]) && count($answer["imagefile"]) > 0)) {
218 $this->object->setIsSingleline(true);
219 }
220 if (isset($answer["imagefile"]["label"])) {
221 $this->object->addAnswer(
222 $answer["answertext"],
223 $answer["points"],
224 $answer["answerorder"],
225 $answer["imagefile"]["label"]
226 );
227 } else {
228 $this->object->addAnswer($answer["answertext"], $answer["points"], $answer["answerorder"]);
229 }
230 }
231 // additional content editing mode information
232 $this->object->setAdditionalContentEditingMode(
234 );
235 $this->object->saveToDb();
236 foreach ($answers as $answer) {
237 if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
238 $image = base64_decode($answer["imagefile"]["content"]);
239 $imagepath = $this->object->getImagePath();
240 if (!file_exists($imagepath)) {
241 ilFileUtils::makeDirParents($imagepath);
242 }
243 $imagepath .= $answer["imagefile"]["label"];
244 $fh = fopen($imagepath, "wb");
245 if ($fh == false) {
246 } else {
247 $imagefile = fwrite($fh, $image);
248 fclose($fh);
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 }
257
258 foreach ($feedbacks as $ident => $material) {
259 $m = $this->object->QTIMaterialToString($material);
260 $feedbacks[$ident] = $m;
261 }
262 foreach ($feedbacksgeneric as $correctness => $material) {
263 $m = $this->object->QTIMaterialToString($material);
264 $feedbacksgeneric[$correctness] = $m;
265 }
266 // handle the import of media objects in XHTML code
267 $questiontext = $this->object->getQuestion();
268 $answers = &$this->object->getAnswers();
269 if (is_array(ilSession::get("import_mob_xhtml"))) {
270 foreach (ilSession::get("import_mob_xhtml") as $mob) {
271 if ($tst_id > 0) {
272 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
273 } else {
274 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
275 }
276
277 global $DIC; /* @var ILIAS\DI\Container $DIC */
278 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
279
280 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
281 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
282 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
283 foreach ($answers as $key => $value) {
284 $answer_obj = $answers[$key];
285 $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
286 }
287 foreach ($feedbacks as $ident => $material) {
288 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
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 }
294 }
295 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
296 foreach ($answers as $key => $value) {
297 $answer_obj = &$answers[$key];
298 $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
299 }
300 foreach ($feedbacks as $ident => $material) {
301 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
302 $this->object->getId(),
303 0,
304 $ident,
306 );
307 }
308 foreach ($feedbacksgeneric as $correctness => $material) {
309 $this->object->feedbackOBJ->importGenericFeedback(
310 $this->object->getId(),
311 $correctness,
313 );
314 }
315 $this->object->saveToDb();
316 if (count($item->suggested_solutions)) {
317 foreach ($item->suggested_solutions as $suggested_solution) {
318 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
319 }
320 $this->object->saveToDb();
321 }
322 if ($tst_id > 0) {
323 $q_1_id = $this->object->getId();
324 $question_id = $this->object->duplicate(true, "", "", "", $tst_id);
325 $tst_object->questions[$question_counter++] = $question_id;
326 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
327 } else {
328 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
329 }
330 return $import_mapping;
331 }
332}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir)
addGeneralMetadata(ilQTIItem $item)
deduceThumbSizeFromImportValue(?int $size)
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
Creates a question from a QTI file.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
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 _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.
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)
static clear(string $a_var)
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
string $key
Consumer key/client ID value.
Definition: System.php:193
int $created
Timestamp for when the object was created.
Definition: System.php:151
$response