ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 $this->object->createNewQuestion();
176 $this->addGeneralMetadata($item);
177 $this->object->setTitle($item->getTitle());
178 $this->object->setNrOfTries((int) $item->getMaxattempts());
179 $this->object->setComment($item->getComment());
180 $this->object->setAuthor($item->getAuthor());
181 $this->object->setOwner($user_id);
182 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
183 $this->object->setObjId($questionpool_id);
184 $shuffle_mode = $item->getMetadataEntry('shuffle');
185 if (is_numeric($shuffle_mode) && $shuffle_mode > 0) {
186 $this->object->setShuffleMode($shuffle_mode);
187 }
188 $this->object->setThumbGeometry(
189 $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_geometry'))
190 );
191
192 if (strlen($item->getMetadataEntry('matching_mode'))) {
193 $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
194 } else {
195 $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
196 }
197
198 // save images
199 foreach ($terms as $term) {
200 if (count($term['answerimage'])) {
201 $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
202 }
203 }
204 foreach ($definitions as $definition) {
205 if (count($definition['answerimage'])) {
206 $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
207 }
208 }
209
210 foreach ($terms as $termindex => $term) {
211 // @PHP8-CR: If you look above, how $this->object->addDefinition does in fact take an object, I take this
212 // issue as an indicator for a bigger issue and won't suppress / "quickfix" this but postpone further
213 // analysis, eventually involving T&A TechSquad (see also remark in assMatchingQuestionGUI
214 $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'] ?? '', $term["ident"]));
215 }
216 foreach ($definitions as $definitionindex => $definition) {
217 $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'] ?? '', $definition["answerorder"]));
218 }
219
220 foreach ($responses as $response) {
221 $subset = $response["subset"];
222 foreach ($subset as $ident) {
223 if (array_key_exists($ident, $definitions)) {
224 $definition = $definitions[$ident];
225 }
226 if (array_key_exists($ident, $terms)) {
227 $term = $terms[$ident];
228 }
229 }
230 $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', (float) $term["ident"]), new assAnswerMatchingDefinition('', '', (int) $definition["answerorder"]), (float) $response['points']);
231 }
232 // additional content editing mode information
233 $this->object->setAdditionalContentEditingMode(
235 );
236 $this->object->saveToDb();
237 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
238 foreach ($responses as $response) {
239 $subset = $response["subset"];
240 foreach ($subset as $ident) {
241 if (array_key_exists($ident, $definitions)) {
242 $definition = $definitions[$ident];
243 }
244 if (array_key_exists($ident, $terms)) {
245 $term = $terms[$ident];
246 }
247 }
248 }
249
250 foreach ($feedbacksgeneric as $correctness => $material) {
251 $m = $this->QTIMaterialToString($material);
252 $feedbacksgeneric[$correctness] = $m;
253 }
254
255 $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
256
257 // handle the import of media objects in XHTML code
258 $questiontext = $this->object->getQuestion();
259 if (is_array(ilSession::get("import_mob_xhtml"))) {
260 foreach (ilSession::get("import_mob_xhtml") as $mob) {
261 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
262
263 global $DIC; /* @var ILIAS\DI\Container $DIC */
264 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
265
266 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
267 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
268 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
269 foreach ($feedbacks as $ident => $material) {
270 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
271 }
272 foreach ($feedbacksgeneric as $correctness => $material) {
273 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
274 }
275 }
276 }
277 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
278 foreach ($feedbacks as $ident => $material) {
279 $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
280
281 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
282 $this->object->getId(),
283 0,
284 $index,
286 );
287 }
288 foreach ($feedbacksgeneric as $correctness => $material) {
289 $this->object->feedbackOBJ->importGenericFeedback(
290 $this->object->getId(),
291 $correctness,
293 );
294 }
295 $this->object->saveToDb();
296 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
297 $questionpool_id,
298 $tst_id,
299 $question_counter,
300 $tst_object
301 );
302 return $import_mapping;
303 }
304
310 protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_'): int
311 {
312 list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
313
314 foreach ($this->object->getMatchingPairs() as $index => $pair) {
315 /* @var assAnswerMatchingPair $pair */
316
317 if ($pair->getTerm()->getIdentifier() != $termId) {
318 continue;
319 }
320
321 if ($pair->getDefinition()->getIdentifier() != $definitionId) {
322 continue;
323 }
324
325 return (int) $index;
326 }
327
328 return -1;
329 }
330}
$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:93