ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.assImagemapQuestionImport.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 $questionimage = [];
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($rendertype))) {
52 case "ilqtirenderhotspot":
53 foreach ($rendertype->material as $mat) {
54 for ($i = 0; $i < $mat->getMaterialCount(); $i++) {
55 $m = $mat->getMaterial($i);
56 if (strcmp($m["type"], "matimage") == 0) {
57 $questionimage = [
58 "imagetype" => $m["material"]->getImageType(),
59 "label" => $m["material"]->getLabel(),
60 "content" => $m["material"]->getContent()
61 ];
62 }
63 }
64 }
65 foreach ($rendertype->response_labels as $response_label) {
66 $ident = $response_label->getIdent();
67 $answerhint = "";
68 foreach ($response_label->material as $mat) {
69 $answerhint .= $this->QTIMaterialToString($mat);
70 }
71 $answers[$ident] = [
72 "answerhint" => $answerhint,
73 "areatype" => $response_label->getRarea(),
74 "coordinates" => $response_label->getContent(),
75 "points" => 0,
76 "answerorder" => $response_label->getIdent(),
77 "correctness" => "1",
78 "action" => "",
79 "points_unchecked" => 0
80 ];
81 }
82 break;
83 }
84 break;
85 }
86 }
87 $responses = [];
88 $feedbacks = [];
89 $feedbacksgeneric = [];
90 foreach ($item->resprocessing as $resprocessing) {
91 foreach ($resprocessing->respcondition as $respcondition) {
92 $coordinates = "";
93 $correctness = 1;
94 $conditionvar = $respcondition->getConditionvar();
95 foreach ($conditionvar->order as $order) {
96 switch ($order["field"]) {
97 case "arr_not":
98 $correctness = 0;
99 break;
100 case "varinside":
101 $coordinates = $conditionvar->varinside[$order["index"]]->getContent();
102 break;
103 case "varequal":
104 $coordinates = $conditionvar->varequal[$order["index"]]->getContent();
105 break;
106 }
107 }
108 foreach ($respcondition->setvar as $setvar) {
109 foreach ($answers as $ident => $answer) {
110 if (strcmp($answer["coordinates"], $coordinates) == 0) {
111 if ($correctness) {
112 $answers[$ident]["action"] = $setvar->getAction();
113 $answers[$ident]["points"] = $setvar->getContent();
114 if (count($respcondition->displayfeedback)) {
115 foreach ($respcondition->displayfeedback as $feedbackpointer) {
116 if (strlen($feedbackpointer->getLinkrefid())) {
117 foreach ($item->itemfeedback as $ifb) {
118 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
119 // found a feedback for the identifier
120 if (count($ifb->material)) {
121 foreach ($ifb->material as $material) {
122 $feedbacksgeneric[1] = $material;
123 }
124 }
125 if ((count($ifb->flow_mat) > 0)) {
126 foreach ($ifb->flow_mat as $fmat) {
127 if (count($fmat->material)) {
128 foreach ($fmat->material as $material) {
129 $feedbacksgeneric[1] = $material;
130 }
131 }
132 }
133 }
134 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
135 // found a feedback for the identifier
136 if (count($ifb->material)) {
137 foreach ($ifb->material as $material) {
138 $feedbacksgeneric[0] = $material;
139 }
140 }
141 if ((count($ifb->flow_mat) > 0)) {
142 foreach ($ifb->flow_mat as $fmat) {
143 if (count($fmat->material)) {
144 foreach ($fmat->material as $material) {
145 $feedbacksgeneric[0] = $material;
146 }
147 }
148 }
149 }
150 }
151 if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
152 // found a feedback for the identifier
153 if (count($ifb->material)) {
154 foreach ($ifb->material as $material) {
155 $feedbacks[$ident] = $material;
156 }
157 }
158 if ((count($ifb->flow_mat) > 0)) {
159 foreach ($ifb->flow_mat as $fmat) {
160 if (count($fmat->material)) {
161 foreach ($fmat->material as $material) {
162 $feedbacks[$ident] = $material;
163 }
164 }
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 } else {
173 $answers[$ident]["action"] = $setvar->getAction();
174 $answers[$ident]["points_unchecked"] = $setvar->getContent();
175 }
176 }
177 }
178 }
179 }
180 }
181
182 $this->addGeneralMetadata($item);
183 $this->object->setTitle($item->getTitle());
184 $this->object->setNrOfTries((int) $item->getMaxattempts());
185 $this->object->setComment($item->getComment());
186 $this->object->setAuthor($item->getAuthor());
187 $this->object->setOwner($user_id);
188 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
189 $this->object->setObjId($questionpool_id);
190 $this->object->setIsMultipleChoice($item->getMetadataEntry("IS_MULTIPLE_CHOICE"));
191 $areas = ["2" => "rect", "1" => "circle", "3" => "poly"];
192 $this->object->setImageFilename($questionimage["label"]);
193 foreach ($answers as $answer) {
194 $this->object->addAnswer($answer["answerhint"], $answer["points"], $answer["answerorder"], $answer["coordinates"], $areas[$answer["areatype"]], $answer["points_unchecked"]);
195 }
196 // additional content editing mode information
197 $this->object->setAdditionalContentEditingMode(
199 );
200 $this->object->saveToDb();
201 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
202 $image = base64_decode($questionimage["content"]);
203 $imagepath = $this->object->getImagePath();
204 if (!file_exists($imagepath)) {
205 ilFileUtils::makeDirParents($imagepath);
206 }
207 $imagepath .= $questionimage["label"];
208 $fh = fopen($imagepath, "wb");
209 if ($fh == true) {
210 $imagefile = fwrite($fh, $image);
211 fclose($fh);
212 }
213 // handle the import of media objects in XHTML code
214 foreach ($feedbacks as $ident => $material) {
215 $m = $this->QTIMaterialToString($material);
216 $feedbacks[$ident] = $m;
217 }
218 foreach ($feedbacksgeneric as $correctness => $material) {
219 $m = $this->QTIMaterialToString($material);
220 $feedbacksgeneric[$correctness] = $m;
221 }
222 $questiontext = $this->object->getQuestion();
223 if (is_array(ilSession::get("import_mob_xhtml"))) {
224 foreach (ilSession::get("import_mob_xhtml") as $mob) {
225 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
226
227 global $DIC; /* @var ILIAS\DI\Container $DIC */
228 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
229
230 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
231 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
232 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
233 foreach ($feedbacks as $ident => $material) {
234 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
235 }
236 foreach ($feedbacksgeneric as $correctness => $material) {
237 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
238 }
239 }
240 }
241 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
242 foreach ($feedbacks as $ident => $material) {
243 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
244 $this->object->getId(),
245 0,
246 $ident,
248 );
249 }
250 foreach ($feedbacksgeneric as $correctness => $material) {
251 $this->object->feedbackOBJ->importGenericFeedback(
252 $this->object->getId(),
253 $correctness,
255 );
256 }
257 $this->object->saveToDb();
258 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
259 $questionpool_id,
260 $tst_id,
261 $question_counter,
262 $tst_object
263 );
264 return $import_mapping;
265 }
266}
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)
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)
importSuggestedSolutions(int $question_id, array $solution_from_import)
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