ILIAS  release_8 Revision v8.25
class.assImagemapQuestionImport.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 $now = getdate();
53 $questionimage = array();
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($rendertype))) {
62 case "ilqtirenderhotspot":
63 foreach ($rendertype->material as $mat) {
64 for ($i = 0; $i < $mat->getMaterialCount(); $i++) {
65 $m = $mat->getMaterial($i);
66 if (strcmp($m["type"], "matimage") == 0) {
67 $questionimage = array(
68 "imagetype" => $m["material"]->getImageType(),
69 "label" => $m["material"]->getLabel(),
70 "content" => $m["material"]->getContent()
71 );
72 }
73 }
74 }
75 foreach ($rendertype->response_labels as $response_label) {
76 $ident = $response_label->getIdent();
77 $answerhint = "";
78 foreach ($response_label->material as $mat) {
79 $answerhint .= $this->object->QTIMaterialToString($mat);
80 }
81 $answers[$ident] = array(
82 "answerhint" => $answerhint,
83 "areatype" => $response_label->getRarea(),
84 "coordinates" => $response_label->getContent(),
85 "points" => 0,
86 "answerorder" => $response_label->getIdent(),
87 "correctness" => "1",
88 "action" => "",
89 "points_unchecked" => 0
90 );
91 }
92 break;
93 }
94 break;
95 }
96 }
97 $responses = array();
98 $feedbacks = array();
99 $feedbacksgeneric = array();
100 foreach ($item->resprocessing as $resprocessing) {
101 foreach ($resprocessing->respcondition as $respcondition) {
102 $coordinates = "";
103 $correctness = 1;
104 $conditionvar = $respcondition->getConditionvar();
105 foreach ($conditionvar->order as $order) {
106 switch ($order["field"]) {
107 case "arr_not":
108 $correctness = 0;
109 break;
110 case "varinside":
111 $coordinates = $conditionvar->varinside[$order["index"]]->getContent();
112 break;
113 case "varequal":
114 $coordinates = $conditionvar->varequal[$order["index"]]->getContent();
115 break;
116 }
117 }
118 foreach ($respcondition->setvar as $setvar) {
119 foreach ($answers as $ident => $answer) {
120 if (strcmp($answer["coordinates"], $coordinates) == 0) {
121 if ($correctness) {
122 $answers[$ident]["action"] = $setvar->getAction();
123 $answers[$ident]["points"] = $setvar->getContent();
124 if (count($respcondition->displayfeedback)) {
125 foreach ($respcondition->displayfeedback as $feedbackpointer) {
126 if (strlen($feedbackpointer->getLinkrefid())) {
127 foreach ($item->itemfeedback as $ifb) {
128 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
129 // found a feedback for the identifier
130 if (count($ifb->material)) {
131 foreach ($ifb->material as $material) {
132 $feedbacksgeneric[1] = $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 $feedbacksgeneric[1] = $material;
140 }
141 }
142 }
143 }
144 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
145 // found a feedback for the identifier
146 if (count($ifb->material)) {
147 foreach ($ifb->material as $material) {
148 $feedbacksgeneric[0] = $material;
149 }
150 }
151 if ((count($ifb->flow_mat) > 0)) {
152 foreach ($ifb->flow_mat as $fmat) {
153 if (count($fmat->material)) {
154 foreach ($fmat->material as $material) {
155 $feedbacksgeneric[0] = $material;
156 }
157 }
158 }
159 }
160 }
161 if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
162 // found a feedback for the identifier
163 if (count($ifb->material)) {
164 foreach ($ifb->material as $material) {
165 $feedbacks[$ident] = $material;
166 }
167 }
168 if ((count($ifb->flow_mat) > 0)) {
169 foreach ($ifb->flow_mat as $fmat) {
170 if (count($fmat->material)) {
171 foreach ($fmat->material as $material) {
172 $feedbacks[$ident] = $material;
173 }
174 }
175 }
176 }
177 }
178 }
179 }
180 }
181 }
182 } else {
183 $answers[$ident]["action"] = $setvar->getAction();
184 $answers[$ident]["points_unchecked"] = $setvar->getContent();
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->object->QTIMaterialToString($item->getQuestiontext()));
199 $this->object->setObjId($questionpool_id);
200 $this->object->setIsMultipleChoice($item->getMetadataEntry("IS_MULTIPLE_CHOICE"));
201 $areas = array("2" => "rect", "1" => "circle", "3" => "poly");
202 $this->object->setImageFilename($questionimage["label"]);
203 foreach ($answers as $answer) {
204 $this->object->addAnswer($answer["answerhint"], $answer["points"], $answer["answerorder"], $answer["coordinates"], $areas[$answer["areatype"]], $answer["points_unchecked"]);
205 }
206 // additional content editing mode information
207 $this->object->setAdditionalContentEditingMode(
209 );
210 $this->object->saveToDb();
211 if (count($item->suggested_solutions)) {
212 foreach ($item->suggested_solutions as $suggested_solution) {
213 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
214 }
215 $this->object->saveToDb();
216 }
217 $image = base64_decode($questionimage["content"]);
218 $imagepath = $this->object->getImagePath();
219 if (!file_exists($imagepath)) {
220 ilFileUtils::makeDirParents($imagepath);
221 }
222 $imagepath .= $questionimage["label"];
223 $fh = fopen($imagepath, "wb");
224 if ($fh == true) {
225 $imagefile = fwrite($fh, $image);
226 fclose($fh);
227 }
228 // handle the import of media objects in XHTML code
229 foreach ($feedbacks as $ident => $material) {
230 $m = $this->object->QTIMaterialToString($material);
231 $feedbacks[$ident] = $m;
232 }
233 foreach ($feedbacksgeneric as $correctness => $material) {
234 $m = $this->object->QTIMaterialToString($material);
235 $feedbacksgeneric[$correctness] = $m;
236 }
237 $questiontext = $this->object->getQuestion();
238 if (is_array(ilSession::get("import_mob_xhtml"))) {
239 foreach (ilSession::get("import_mob_xhtml") as $mob) {
240 if ($tst_id > 0) {
241 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
242 } else {
243 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
244 }
245
246 global $DIC; /* @var ILIAS\DI\Container $DIC */
247 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
248
249 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
250 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
251 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
252 foreach ($feedbacks as $ident => $material) {
253 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
254 }
255 foreach ($feedbacksgeneric as $correctness => $material) {
256 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
257 }
258 }
259 }
260 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
261 foreach ($feedbacks as $ident => $material) {
262 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
263 $this->object->getId(),
264 0,
265 $ident,
267 );
268 }
269 foreach ($feedbacksgeneric as $correctness => $material) {
270 $this->object->feedbackOBJ->importGenericFeedback(
271 $this->object->getId(),
272 $correctness,
274 );
275 }
276 $this->object->saveToDb();
277 if ($tst_id > 0) {
278 $q_1_id = $this->object->getId();
279 $question_id = $this->object->duplicate(true, "", "", "", $tst_id);
280 $tst_object->questions[$question_counter++] = $question_id;
281 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
282 } else {
283 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
284 }
285 return $import_mapping;
286 }
287}
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.
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)
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
$i
Definition: metadata.php:41
int $created
Timestamp for when the object was created.
Definition: System.php:151
$response