ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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)) {
35  ilFileUtils::makeDirParents($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 
59  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
60  {
61  global $DIC;
62  $ilUser = $DIC['ilUser'];
63 
64  // empty session variable for imported xhtml mobs
65  ilSession::clear('import_mob_xhtml');
66  $presentation = $item->getPresentation();
67  $shuffle = 0;
68  $now = getdate();
69  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
70  $definitions = [];
71  $terms = [];
72  $foundimage = false;
73  foreach ($presentation->order as $entry) {
74  switch ($entry["type"]) {
75  case "response":
76  $response = $presentation->response[$entry["index"]];
77  $rendertype = $response->getRenderType();
78  switch (strtolower(get_class($rendertype))) {
79  case "ilqtirenderchoice":
80  $shuffle = $rendertype->getShuffle();
81  $answerorder = 0;
82  foreach ($rendertype->response_labels as $response_label) {
83  $ident = $response_label->getIdent();
84  $answertext = "";
85  $answerimage = [];
86  foreach ($response_label->material as $mat) {
87  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
88  $foundmat = $mat->getMaterial($m);
89  if (strcmp($foundmat["type"], "mattext") == 0) {
90  $answertext .= $foundmat["material"]->getContent();
91  }
92  if (strcmp($foundmat["type"], "matimage") == 0) {
93  $foundimage = true;
94  $answerimage = [
95  "imagetype" => $foundmat["material"]->getImageType(),
96  "label" => $foundmat["material"]->getLabel(),
97  "content" => $foundmat["material"]->getContent()
98  ];
99  }
100  }
101  }
102  if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup()))) {
103  $definitions[$ident] = [
104  "answertext" => $answertext,
105  "answerimage" => $answerimage,
106  "points" => 0,
107  "answerorder" => $ident,
108  "action" => ""
109  ];
110  } else {
111  $terms[$ident] = [
112  "term" => $answertext,
113  "answerimage" => $answerimage,
114  "points" => 0,
115  "ident" => $ident,
116  "action" => ""
117  ];
118  }
119  }
120  break;
121  }
122  break;
123  }
124  }
125  $responses = [];
126  $feedbacksgeneric = [];
127  foreach ($item->resprocessing as $resprocessing) {
128  foreach ($resprocessing->respcondition as $respcondition) {
129  $subset = [];
130  $correctness = 1;
131  $conditionvar = $respcondition->getConditionvar();
132  foreach ($conditionvar->order as $order) {
133  switch ($order["field"]) {
134  case "varsubset":
135  $subset = explode(",", $conditionvar->varsubset[$order["index"]]->getContent());
136  break;
137  }
138  }
139  foreach ($respcondition->setvar as $setvar) {
140  array_push($responses, ["subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()]);
141  }
142 
143  if (count($respcondition->displayfeedback)) {
144  foreach ($respcondition->displayfeedback as $feedbackpointer) {
145  if (strlen($feedbackpointer->getLinkrefid())) {
146  foreach ($item->itemfeedback as $ifb) {
147  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
148  // found a feedback for the identifier
149  if (count($ifb->material)) {
150  foreach ($ifb->material as $material) {
151  $feedbacksgeneric[1] = $material;
152  }
153  }
154  if ((count($ifb->flow_mat) > 0)) {
155  foreach ($ifb->flow_mat as $fmat) {
156  if (count($fmat->material)) {
157  foreach ($fmat->material as $material) {
158  $feedbacksgeneric[1] = $material;
159  }
160  }
161  }
162  }
163  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
164  // found a feedback for the identifier
165  if (count($ifb->material)) {
166  foreach ($ifb->material as $material) {
167  $feedbacksgeneric[0] = $material;
168  }
169  }
170  if ((count($ifb->flow_mat) > 0)) {
171  foreach ($ifb->flow_mat as $fmat) {
172  if (count($fmat->material)) {
173  foreach ($fmat->material as $material) {
174  $feedbacksgeneric[0] = $material;
175  }
176  }
177  }
178  }
179  }
180  }
181  }
182  }
183  }
184  }
185  }
186 
187  $this->object->createNewQuestion();
188  $this->addGeneralMetadata($item);
189  $this->object->setTitle($item->getTitle());
190  $this->object->setNrOfTries((int) $item->getMaxattempts());
191  $this->object->setComment($item->getComment());
192  $this->object->setAuthor($item->getAuthor());
193  $this->object->setOwner($ilUser->getId());
194  $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
195  $this->object->setObjId($questionpool_id);
196  $extended_shuffle = $item->getMetadataEntry('shuffle');
197  $this->object->setThumbGeometry(
198  $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_geometry'))
199  );
200 
201  if (strlen($item->getMetadataEntry('matching_mode'))) {
202  $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
203  } else {
204  $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
205  }
206 
207  // save images
208  foreach ($terms as $term) {
209  if (count($term['answerimage'])) {
210  $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
211  }
212  }
213  foreach ($definitions as $definition) {
214  if (count($definition['answerimage'])) {
215  $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
216  }
217  }
218 
219  foreach ($terms as $termindex => $term) {
220  // @PHP8-CR: If you look above, how $this->object->addDefinition does in fact take an object, I take this
221  // issue as an indicator for a bigger issue and won't suppress / "quickfix" this but postpone further
222  // analysis, eventually involving T&A TechSquad (see also remark in assMatchingQuestionGUI
223  $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'] ?? '', $term["ident"]));
224  }
225  foreach ($definitions as $definitionindex => $definition) {
226  $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'] ?? '', $definition["answerorder"]));
227  }
228 
229  if (strlen($extended_shuffle) > 0) {
230  $shuffle = $extended_shuffle;
231  }
232  $this->object->setShuffle($shuffle);
233 
234  foreach ($responses as $response) {
235  $subset = $response["subset"];
236  foreach ($subset as $ident) {
237  if (array_key_exists($ident, $definitions)) {
238  $definition = $definitions[$ident];
239  }
240  if (array_key_exists($ident, $terms)) {
241  $term = $terms[$ident];
242  }
243  }
244  $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', (float) $term["ident"]), new assAnswerMatchingDefinition('', '', (int) $definition["answerorder"]), (float) $response['points']);
245  }
246  // additional content editing mode information
247  $this->object->setAdditionalContentEditingMode(
249  );
250  $this->object->saveToDb();
251  $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
252  foreach ($responses as $response) {
253  $subset = $response["subset"];
254  foreach ($subset as $ident) {
255  if (array_key_exists($ident, $definitions)) {
256  $definition = $definitions[$ident];
257  }
258  if (array_key_exists($ident, $terms)) {
259  $term = $terms[$ident];
260  }
261  }
262  }
263 
264  foreach ($feedbacksgeneric as $correctness => $material) {
265  $m = $this->QTIMaterialToString($material);
266  $feedbacksgeneric[$correctness] = $m;
267  }
268 
269  $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
270 
271  // handle the import of media objects in XHTML code
272  $questiontext = $this->object->getQuestion();
273  if (is_array(ilSession::get("import_mob_xhtml"))) {
274  foreach (ilSession::get("import_mob_xhtml") as $mob) {
275  if ($tst_id > 0) {
276  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
277  } else {
278  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
279  }
280 
281  global $DIC; /* @var ILIAS\DI\Container $DIC */
282  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
283 
284  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
285  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
286  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
287  foreach ($feedbacks as $ident => $material) {
288  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
289  }
290  foreach ($feedbacksgeneric as $correctness => $material) {
291  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
292  }
293  }
294  }
295  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
296  foreach ($feedbacks as $ident => $material) {
297  $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
298 
299  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
300  $this->object->getId(),
301  0,
302  $index,
304  );
305  }
306  foreach ($feedbacksgeneric as $correctness => $material) {
307  $this->object->feedbackOBJ->importGenericFeedback(
308  $this->object->getId(),
309  $correctness,
311  );
312  }
313  $this->object->saveToDb();
314  if ($tst_id > 0) {
315  $q_1_id = $this->object->getId();
316  $question_id = $this->object->duplicate(true, "", "", -1, $tst_id);
317  $tst_object->questions[$question_counter++] = $question_id;
318  $import_mapping[$item->getIdent()] = ["pool" => $q_1_id, "test" => $question_id];
319  } else {
320  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
321  }
322  return $import_mapping;
323  }
324 
330  protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_'): int
331  {
332  list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
333 
334  foreach ($this->object->getMatchingPairs() as $index => $pair) {
335  /* @var assAnswerMatchingPair $pair */
336 
337  if ($pair->getTerm()->getIdentifier() != $termId) {
338  continue;
339  }
340 
341  if ($pair->getDefinition()->getIdentifier() != $definitionId) {
342  continue;
343  }
344 
345  return (int) $index;
346  }
347 
348  return -1;
349  }
350 }
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static get(string $a_var)
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
deduceThumbSizeFromImportValue(?int $size)
addGeneralMetadata(ilQTIItem $item)
const IL_INST_ID
Definition: constants.php:40
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
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.
$response
Definition: xapitoken.php:93
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.
global $DIC
Definition: feed.php:28
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...
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
$filename
Definition: buildRTE.php:78
importSuggestedSolutions(int $question_id, array $solution_from_import)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
Class for question imports.
static clear(string $a_var)
int $created
Timestamp for when the object was created.
Definition: System.php:151