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