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