ILIAS  release_8 Revision v8.23
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 = array();
71  $terms = array();
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 = array();
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 = array(
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] = array(
104  "answertext" => $answertext,
105  "answerimage" => $answerimage,
106  "points" => 0,
107  "answerorder" => $ident,
108  "action" => ""
109  );
110  } else {
111  $terms[$ident] = array(
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 = array();
126  $feedbacksgeneric = array();
127  foreach ($item->resprocessing as $resprocessing) {
128  foreach ($resprocessing->respcondition as $respcondition) {
129  $subset = array();
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, array("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->object->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  if (count($item->suggested_solutions)) {
252  foreach ($item->suggested_solutions as $suggested_solution) {
253  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
254  }
255  $this->object->saveToDb();
256  }
257  foreach ($responses as $response) {
258  $subset = $response["subset"];
259  foreach ($subset as $ident) {
260  if (array_key_exists($ident, $definitions)) {
261  $definition = $definitions[$ident];
262  }
263  if (array_key_exists($ident, $terms)) {
264  $term = $terms[$ident];
265  }
266  }
267  }
268 
269  foreach ($feedbacksgeneric as $correctness => $material) {
270  $m = $this->object->QTIMaterialToString($material);
271  $feedbacksgeneric[$correctness] = $m;
272  }
273 
274  $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
275 
276  // handle the import of media objects in XHTML code
277  $questiontext = $this->object->getQuestion();
278  if (is_array(ilSession::get("import_mob_xhtml"))) {
279  foreach (ilSession::get("import_mob_xhtml") as $mob) {
280  if ($tst_id > 0) {
281  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
282  } else {
283  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
284  }
285 
286  global $DIC; /* @var ILIAS\DI\Container $DIC */
287  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
288 
289  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
290  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
291  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
292  foreach ($feedbacks as $ident => $material) {
293  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
294  }
295  foreach ($feedbacksgeneric as $correctness => $material) {
296  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
297  }
298  }
299  }
300  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
301  foreach ($feedbacks as $ident => $material) {
302  $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
303 
304  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
305  $this->object->getId(),
306  0,
307  $index,
309  );
310  }
311  foreach ($feedbacksgeneric as $correctness => $material) {
312  $this->object->feedbackOBJ->importGenericFeedback(
313  $this->object->getId(),
314  $correctness,
316  );
317  }
318  $this->object->saveToDb();
319  if ($tst_id > 0) {
320  $q_1_id = $this->object->getId();
321  $question_id = $this->object->duplicate(true, "", "", "", $tst_id);
322  $tst_object->questions[$question_counter++] = $question_id;
323  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
324  } else {
325  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
326  }
327  return $import_mapping;
328  }
329 
335  protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_'): int
336  {
337  list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
338 
339  foreach ($this->object->getMatchingPairs() as $index => $pair) {
340  /* @var assAnswerMatchingPair $pair */
341 
342  if ($pair->getTerm()->getIdentifier() != $termId) {
343  continue;
344  }
345 
346  if ($pair->getDefinition()->getIdentifier() != $definitionId) {
347  continue;
348  }
349 
350  return (int) $index;
351  }
352 
353  return -1;
354  }
355 }
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
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.
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.
$index
Definition: metadata.php:145
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
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
$ilUser
Definition: imgupload.php:34
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) ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$response
static clear(string $a_var)
int $created
Timestamp for when the object was created.
Definition: System.php:151