ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.assMatchingQuestionImport.php
Go to the documentation of this file.
1<?php
2
29{
30 public function saveImage($data, $filename): void
31 {
32 $image = base64_decode($data);
33 $imagepath = $this->object->getImagePath();
34 if (!file_exists($imagepath)) {
36 }
37 $imagepath .= $filename;
38 $fh = fopen($imagepath, "wb");
39 if ($fh == false) {
40 } else {
41 $imagefile = fwrite($fh, $image);
42 fclose($fh);
43 }
44 }
45
46 public function fromXML(
47 string $importdirectory,
48 int $user_id,
49 ilQTIItem $item,
50 int $questionpool_id,
51 ?int $tst_id,
52 ?ilObject &$tst_object,
53 int &$question_counter,
54 array $import_mapping
55 ): array {
56 // empty session variable for imported xhtml mobs
57 ilSession::clear('import_mob_xhtml');
58 $presentation = $item->getPresentation();
59 $definitions = [];
60 $terms = [];
61 $foundimage = false;
62 foreach ($presentation->order as $entry) {
63 switch ($entry["type"]) {
64 case "response":
65 $response = $presentation->response[$entry["index"]];
66 $rendertype = $response->getRenderType();
67 switch (strtolower(get_class($rendertype))) {
68 case "ilqtirenderchoice":
69 $answerorder = 0;
70 foreach ($rendertype->response_labels as $response_label) {
71 $ident = $response_label->getIdent();
72 $answertext = "";
73 $answerimage = [];
74 foreach ($response_label->material as $mat) {
75 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
76 $foundmat = $mat->getMaterial($m);
77 if (strcmp($foundmat["type"], "mattext") == 0) {
78 $answertext .= $foundmat["material"]->getContent();
79 }
80 if (strcmp($foundmat["type"], "matimage") == 0) {
81 $foundimage = true;
82 $answerimage = [
83 "imagetype" => $foundmat["material"]->getImageType(),
84 "label" => $foundmat["material"]->getLabel(),
85 "content" => $foundmat["material"]->getContent()
86 ];
87 }
88 }
89 }
90 if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup()))) {
91 $definitions[$ident] = [
92 "answertext" => $answertext,
93 "answerimage" => $answerimage,
94 "points" => 0,
95 "answerorder" => $ident,
96 "action" => ""
97 ];
98 } else {
99 $terms[$ident] = [
100 "term" => $answertext,
101 "answerimage" => $answerimage,
102 "points" => 0,
103 "ident" => $ident,
104 "action" => ""
105 ];
106 }
107 }
108 break;
109 }
110 break;
111 }
112 }
113 $responses = [];
114 $feedbacksgeneric = [];
115 foreach ($item->resprocessing as $resprocessing) {
116 foreach ($resprocessing->respcondition as $respcondition) {
117 $subset = [];
118 $correctness = 1;
119 $conditionvar = $respcondition->getConditionvar();
120 foreach ($conditionvar->order as $order) {
121 switch ($order["field"]) {
122 case "varsubset":
123 $subset = explode(",", $conditionvar->varsubset[$order["index"]]->getContent());
124 break;
125 }
126 }
127 foreach ($respcondition->setvar as $setvar) {
128 array_push($responses, ["subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()]);
129 }
130
131 if (count($respcondition->displayfeedback)) {
132 foreach ($respcondition->displayfeedback as $feedbackpointer) {
133 if (strlen($feedbackpointer->getLinkrefid())) {
134 foreach ($item->itemfeedback as $ifb) {
135 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
136 // found a feedback for the identifier
137 if (count($ifb->material)) {
138 foreach ($ifb->material as $material) {
139 $feedbacksgeneric[1] = $material;
140 }
141 }
142 if ((count($ifb->flow_mat) > 0)) {
143 foreach ($ifb->flow_mat as $fmat) {
144 if (count($fmat->material)) {
145 foreach ($fmat->material as $material) {
146 $feedbacksgeneric[1] = $material;
147 }
148 }
149 }
150 }
151 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
152 // found a feedback for the identifier
153 if (count($ifb->material)) {
154 foreach ($ifb->material as $material) {
155 $feedbacksgeneric[0] = $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 $feedbacksgeneric[0] = $material;
163 }
164 }
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173 }
174
175 // additional content editing mode information
176 $this->object->setAdditionalContentEditingMode(
178 );
179
180 $this->object->createNewQuestion();
181 $this->addGeneralMetadata($item);
182 $this->object->setTitle($item->getTitle());
183 $this->object->setNrOfTries((int) $item->getMaxattempts());
184 $this->object->setComment($item->getComment());
185 $this->object->setAuthor($item->getAuthor());
186 $this->object->setOwner($user_id);
187 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
188 $this->object->setObjId($questionpool_id);
189 $shuffle_mode = $item->getMetadataEntry('shuffle');
190 if (is_numeric($shuffle_mode) && $shuffle_mode > 0) {
191 $this->object->setShuffleMode($shuffle_mode);
192 }
193 $this->object->setThumbGeometry(
194 $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_geometry'))
195 );
196
197 if (strlen($item->getMetadataEntry('matching_mode'))) {
198 $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
199 } else {
200 $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
201 }
202
203 // save images
204 foreach ($terms as $term) {
205 if (count($term['answerimage'])) {
206 $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
207 }
208 }
209 foreach ($definitions as $definition) {
210 if (count($definition['answerimage'])) {
211 $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
212 }
213 }
214
215 foreach ($terms as $termindex => $term) {
216 // @PHP8-CR: If you look above, how $this->object->addDefinition does in fact take an object, I take this
217 // issue as an indicator for a bigger issue and won't suppress / "quickfix" this but postpone further
218 // analysis, eventually involving T&A TechSquad (see also remark in assMatchingQuestionGUI
219 $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'] ?? '', $term["ident"]));
220 }
221 foreach ($definitions as $definitionindex => $definition) {
222 $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'] ?? '', $definition["answerorder"]));
223 }
224
225 foreach ($responses as $response) {
226 $subset = $response["subset"];
227 foreach ($subset as $ident) {
228 if (array_key_exists($ident, $definitions)) {
229 $definition = $definitions[$ident];
230 }
231 if (array_key_exists($ident, $terms)) {
232 $term = $terms[$ident];
233 }
234 }
235 $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', (float) $term["ident"]), new assAnswerMatchingDefinition('', '', (int) $definition["answerorder"]), (float) $response['points']);
236 }
237
238 $this->object->saveToDb();
239 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
240 foreach ($responses as $response) {
241 $subset = $response["subset"];
242 foreach ($subset as $ident) {
243 if (array_key_exists($ident, $definitions)) {
244 $definition = $definitions[$ident];
245 }
246 if (array_key_exists($ident, $terms)) {
247 $term = $terms[$ident];
248 }
249 }
250 }
251
252 foreach ($feedbacksgeneric as $correctness => $material) {
253 $m = $this->QTIMaterialToString($material);
254 $feedbacksgeneric[$correctness] = $m;
255 }
256
257 $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
258
259 // handle the import of media objects in XHTML code
260 $questiontext = $this->object->getQuestion();
261 if (is_array(ilSession::get("import_mob_xhtml"))) {
262 foreach (ilSession::get("import_mob_xhtml") as $mob) {
263 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
264
265 global $DIC; /* @var ILIAS\DI\Container $DIC */
266 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
267
268 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
269 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
270 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
271 foreach ($feedbacks as $ident => $material) {
272 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
273 }
274 foreach ($feedbacksgeneric as $correctness => $material) {
275 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
276 }
277 }
278 }
279 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
280 foreach ($feedbacks as $ident => $material) {
281 $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
282
283 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
284 $this->object->getId(),
285 0,
286 $index,
288 );
289 }
290 foreach ($feedbacksgeneric as $correctness => $material) {
291 $this->object->feedbackOBJ->importGenericFeedback(
292 $this->object->getId(),
293 $correctness,
295 );
296 }
297 $this->object->saveToDb();
298 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
299 $questionpool_id,
300 $tst_id,
301 $question_counter,
302 $tst_object
303 );
304 return $import_mapping;
305 }
306
312 protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_'): int
313 {
314 list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
315
316 foreach ($this->object->getMatchingPairs() as $index => $pair) {
317 /* @var assAnswerMatchingPair $pair */
318
319 if ($pair->getTerm()->getIdentifier() != $termId) {
320 continue;
321 }
322
323 if ($pair->getDefinition()->getIdentifier() != $definitionId) {
324 continue;
325 }
326
327 return (int) $index;
328 }
329
330 return -1;
331 }
332}
$filename
Definition: buildRTE.php:78
Class for matching question definitions.
Class for matching question terms.
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)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
Class for question imports.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
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)
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:90