ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.assLongMenuImport.php
Go to the documentation of this file.
1 <?php
2 
35 {
36  public $object;
37 
38  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
39  {
40  global $DIC;
41  $ilUser = $DIC['ilUser'];
42 
43  ilSession::clear('import_mob_xhtml');
44 
45  $presentation = $item->getPresentation();
46  $questiontext = [];
47  $seperate_question_field = $item->getMetadataEntry("question");
48  $clozetext = [];
49  $now = getdate();
50  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
51  $answers = [];
52  $correct_answers = [];
53  $presentation = $item->getPresentation();
54  $gap_types = json_decode($item->getMetadataEntry("gapTypes"));
55  foreach ($presentation->order as $entry) {
56  switch ($entry["type"]) {
57  case "material":
58 
59  $material = $presentation->material[$entry["index"]];
60  if (preg_match('/\[Longmenu \d\]/', $this->QTIMaterialToString($material))) {
61  $this->object->setLongMenuTextValue($this->QTIMaterialToString($material));
62  } else {
63  $this->object->setQuestion($this->QTIMaterialToString($material));
64  }
65 
66 
67  break;
68  }
69  }
70 
71  // fixLongMenuImageImport - process images in question and long menu text when question is imported
72  $questiontext = $this->object->getQuestion();
73  $longmenutext = $this->object->getLongMenuTextValue();
74  if (is_array(ilSession::get("import_mob_xhtml"))) {
75  foreach (ilSession::get("import_mob_xhtml") as $mob) {
76  if ($tst_id > 0) {
77  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
78  } else {
79  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
80  }
81 
82  global $DIC; /* @var ILIAS\DI\Container $DIC */
83  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
84 
85  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
86  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
87 
88  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
89  $longmenutext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $longmenutext);
90  }
91  }
92  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
93  $this->object->setLongMenuTextValue(ilRTE::_replaceMediaObjectImageSrc($longmenutext, 1));
94  // fau.
95 
96  foreach ($item->resprocessing as $resprocessing) {
97  foreach ($resprocessing->respcondition as $respcondition) {
98  $correctness = 1;
99  $conditionvar = $respcondition->getConditionvar();
100  foreach ($conditionvar->order as $order) {
101  switch ($order['field']) {
102  case 'varequal':
103  $equals = $conditionvar->varequal[$order["index"]]->getContent();
104  $gapident = $conditionvar->varequal[$order["index"]]->getRespident();
105  $id = $this->getIdFromGapIdent($gapident);
106  if (!isset($answers[$id]) || !in_array($equals, $answers[$id])) {
107  $answers[$id][] = $equals;
108  }
109  break;
110  }
111  }
112  foreach ($respcondition->setvar as $setvar) {
113  if ($gapident !== '') {
114  if ($setvar->getContent() > 0) {
115  $id = $this->getIdFromGapIdent($gapident);
116  $correct_answers[$id][0][] = $equals;
117  $correct_answers[$id][1] = $setvar->getContent();
118  if (is_array($gap_types) && key_exists($id, $gap_types)) {
119  $correct_answers[$id][2] = $gap_types[$id];
120  }
121  }
122  }
123  }
124  foreach ($respcondition->displayfeedback as $feedbackpointer) {
125  if (strlen($feedbackpointer->getLinkrefid())) {
126  foreach ($item->itemfeedback as $ifb) {
127  if ($ifb->getIdent() === 'response_allcorrect') {
128  foreach ($ifb->material as $material) {
129  $feedbacksgeneric[1] = $material;
130  }
131  foreach ($ifb->flow_mat as $fmat) {
132  if (count($fmat->material)) {
133  foreach ($fmat->material as $material) {
134  $feedbacksgeneric[1] = $material;
135  }
136  }
137  }
138  } elseif ($ifb->getIdent() === 'response_onenotcorrect') {
139  foreach ($ifb->material as $material) {
140  $feedbacksgeneric[0] = $material;
141  }
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  } else {
150  foreach ($ifb->material as $material) {
151  $feedbacks[$ifb->getIdent()] = $material;
152  }
153  foreach ($ifb->flow_mat as $fmat) {
154  if (count($fmat->material)) {
155  foreach ($fmat->material as $material) {
156  $feedbacks[$ifb->getIdent()] = $material;
157  }
158  }
159  }
160  }
161  }
162  }
163  }
164  }
165  }
166 
167  $sum = 0;
168  foreach ($correct_answers as $row) {
169  $sum += $row[1];
170  }
171  $this->object->setAnswers($answers);
172  // handle the import of media objects in XHTML code
173  if (isset($feedbacks) && count($feedbacks) > 0) {
174  foreach ($feedbacks as $ident => $material) {
175  $m = $this->QTIMaterialToString($material);
176  $feedbacks[$ident] = $m;
177  }
178  }
179  if (isset($feedbacksgeneric) && is_array($feedbacksgeneric) && count($feedbacksgeneric) > 0) {
180  foreach ($feedbacksgeneric as $correctness => $material) {
181  $m = $this->QTIMaterialToString($material);
182  $feedbacksgeneric[$correctness] = $m;
183  }
184  }
185 
186  $this->addGeneralMetadata($item);
187  $this->object->setTitle($item->getTitle());
188  $this->object->setNrOfTries((int) $item->getMaxattempts());
189  $this->object->setComment($item->getComment());
190  $this->object->setAuthor($item->getAuthor());
191  $this->object->setOwner($ilUser->getId());
192  $this->object->setObjId($questionpool_id);
193  $this->object->setMinAutoComplete($item->getMetadataEntry("minAutoCompleteLength"));
194  $this->object->setIdenticalscoring((int) $item->getMetadataEntry("identical_scoring"));
195  $this->object->setCorrectAnswers($correct_answers);
196  $this->object->setPoints($sum);
197  // additional content editing mode information
198  $this->object->setAdditionalContentEditingMode(
200  );
201  $this->object->saveToDb();
202  $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
203  if (isset($feedbacks) && count($feedbacks) > 0) {
204  foreach ($feedbacks as $ident => $material) {
205  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
206  $this->object->getId(),
207  0,
208  $ident,
210  );
211  }
212  }
213  if (isset($feedbacksgeneric) && is_array($feedbacksgeneric) && count($feedbacksgeneric) > 0) {
214  foreach ($feedbacksgeneric as $correctness => $material) {
215  $this->object->feedbackOBJ->importGenericFeedback(
216  $this->object->getId(),
217  $correctness,
219  );
220  }
221  }
222  $this->object->saveToDb();
223 
224  if ($tst_id > 0) {
225  $q_1_id = $this->object->getId();
226  $question_id = $this->object->duplicate(true, "", "", -1, $tst_id);
227  $tst_object->questions[$question_counter++] = $question_id;
228  $import_mapping[$item->getIdent()] = ["pool" => $q_1_id, "test" => $question_id];
229  } else {
230  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
231  }
232  return $import_mapping;
233  }
234 
235  private function getIdFromGapIdent($ident)
236  {
237  $id = preg_split('/_/', $ident);
238  return $id[1] - 1;
239  }
240 }
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)
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.
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
global $DIC
Definition: feed.php:28
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
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...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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