ILIAS  release_7 Revision v7.30-3-g800a261c036
class.assOrderingQuestionImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
5
16{
20 public $object;
21
35 public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
36 {
37 global $DIC;
38 $ilUser = $DIC['ilUser'];
39
40 // empty session variable for imported xhtml mobs
41 unset($_SESSION["import_mob_xhtml"]);
42 $presentation = $item->getPresentation();
43 $duration = $item->getDuration();
44 $shuffle = 0;
45 $now = getdate();
46 $foundimage = false;
47 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
48 $answers = array();
50
51 foreach ($presentation->order as $entry) {
52 switch ($entry["type"]) {
53 case "response":
54 $response = $presentation->response[$entry["index"]];
55 $type = $response->getIdent();
56 if ($response->getIdent() == 'OQP') {
58 } elseif ($response->getIdent() == 'OQNP') {
60 } elseif ($response->getIdent() == 'OQNT') {
62 } elseif ($response->getIdent() == 'OQT') {
64 }
65
66 $rendertype = $response->getRenderType();
67 switch (strtolower(get_class($rendertype))) {
68 case "ilqtirenderchoice":
69 $shuffle = $rendertype->getShuffle();
70 $answerorder = 0;
71 foreach ($rendertype->response_labels as $response_label) {
72 $ident = $response_label->getIdent();
73 $answertext = "";
74 foreach ($response_label->material as $mat) {
75 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
76 $foundmat = $mat->getMaterial($m);
77
78 if (strcmp($foundmat["material"]->getLabel(), "answerdepth") == 0) {
79 $answerdepth = $foundmat["material"]->getContent();
80 }
81 if (strcmp($foundmat["type"], "mattext") == 0
82 && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0) {
83 $answertext .= $foundmat["material"]->getContent();
84 }
85 if (strcmp($foundmat["type"], "matimage") == 0
86 && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0) {
87 $foundimage = true;
88 $answerimage = array(
89 "imagetype" => $foundmat["material"]->getImageType(),
90 "label" => $foundmat["material"]->getLabel(),
91 "content" => $foundmat["material"]->getContent()
92 );
93 }
94 }
95 }
96 $answers[$answerorder] = array(
97 'ident' => $ident,
98 "answertext" => $answertext,
99 "answerimage" => $answerimage,
100 "points" => 0,
101 "answerorder" => $answerorder,
102 "answerdepth" => $answerdepth,
103 "correctness" => "",
104 "action" => ""
105 );
106 $answerorder++;
107 }
108 break;
109 }
110 break;
111 }
112 }
113
114 $feedbacksgeneric = array();
115 foreach ($item->resprocessing as $resprocessing) {
116 foreach ($resprocessing->respcondition as $respcondition) {
117 $ident = "";
118 $correctness = 1;
119 $conditionvar = $respcondition->getConditionvar();
120 foreach ($conditionvar->order as $order) {
121 switch ($order["field"]) {
122 case "arr_not":
123 $correctness = 0;
124 break;
125 case "varequal":
126 $ident = $conditionvar->varequal[$order["index"]]->getContent();
127 $orderindex = $conditionvar->varequal[$order["index"]]->getIndex();
128 break;
129 }
130 }
131 foreach ($respcondition->setvar as $setvar) {
132 if (strcmp($ident, "") != 0) {
133 $answers[$ident]["solutionorder"] = $orderindex;
134 $answers[$ident]["action"] = $setvar->getAction();
135 $answers[$ident]["points"] = $setvar->getContent();
136 }
137 }
138 if (count($respcondition->displayfeedback)) {
139 foreach ($respcondition->displayfeedback as $feedbackpointer) {
140 if (strlen($feedbackpointer->getLinkrefid())) {
141 foreach ($item->itemfeedback as $ifb) {
142 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
143 // found a feedback for the identifier
144 if (count($ifb->material)) {
145 foreach ($ifb->material as $material) {
146 $feedbacksgeneric[1] = $material;
147 }
148 }
149 if ((count($ifb->flow_mat) > 0)) {
150 foreach ($ifb->flow_mat as $fmat) {
151 if (count($fmat->material)) {
152 foreach ($fmat->material as $material) {
153 $feedbacksgeneric[1] = $material;
154 }
155 }
156 }
157 }
158 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
159 // found a feedback for the identifier
160 if (count($ifb->material)) {
161 foreach ($ifb->material as $material) {
162 $feedbacksgeneric[0] = $material;
163 }
164 }
165 if ((count($ifb->flow_mat) > 0)) {
166 foreach ($ifb->flow_mat as $fmat) {
167 if (count($fmat->material)) {
168 foreach ($fmat->material as $material) {
169 $feedbacksgeneric[0] = $material;
170 }
171 }
172 }
173 }
174 }
175 }
176 }
177 }
178 }
179 }
180 }
181
182 $itemfeedbacks = $this->getFeedbackAnswerSpecific($item, 'link_');
183
184 $this->addGeneralMetadata($item);
185 $this->object->setTitle($item->getTitle());
186 $this->object->setNrOfTries($item->getMaxattempts());
187 $this->object->setComment($item->getComment());
188 $this->object->setAuthor($item->getAuthor());
189 $this->object->setOwner($ilUser->getId());
190 $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
191 $this->object->setOrderingType($type);
192 $this->object->setObjId($questionpool_id);
193 $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
194 $this->object->setElementHeight($item->getMetadataEntry("element_height"));
195 $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
196 $this->object->setShuffle($shuffle);
197 $this->object->saveQuestionDataToDb();
198 $points = 0;
199 $solanswers = array();
200
201 foreach ($answers as $answer) {
202 $solanswers[$answer["solutionorder"]] = $answer;
203 }
204 ksort($solanswers);
205 $position = 0;
206 $element_list = $this->object->getOrderingElementList();
207 foreach ($solanswers as $answer) {
208 $points += $answer["points"];
209
210 $element = new ilAssOrderingElement();
211
212 if ($element->isExportIdent($answer['ident'])) {
213 $element->setExportIdent($answer['ident']);
214 } else {
215 $element = $element->withPosition($position++);
216 if (isset($answer['answerdepth'])) {
217 $element = $element->withIndentation((int) $answer['answerdepth']);
218 }
219 }
220
221 if ($this->object->isImageOrderingType()) {
222 $filename = $this->handleUploadedfile($answer);
223 if ($filename !== null) {
224 $element = $element->withContent($filename);
225 }
226 } else {
227 $element = $element->withContent($answer["answertext"]);
228 }
229
230 $element_list->addElement($element);
231 }
232 $this->object->setOrderingElementList($element_list);
233 $points = ($item->getMetadataEntry("points") > 0) ? $item->getMetadataEntry("points") : $points;
234 $this->object->setPoints($points);
235 // additional content editing mode information
236 $this->object->setAdditionalContentEditingMode(
238 );
239 $this->object->saveToDb();
240 if (count($item->suggested_solutions)) {
241 foreach ($item->suggested_solutions as $suggested_solution) {
242 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
243 }
244 $this->object->saveToDb();
245 }
246
247 foreach ($feedbacksgeneric as $correctness => $material) {
248 $m = $this->object->QTIMaterialToString($material);
249 $feedbacksgeneric[$correctness] = $m;
250 }
251 $questiontext = $this->object->getQuestion();
252
253 // handle the import of media objects in XHTML code
254 if (is_array($_SESSION["import_mob_xhtml"])) {
255 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
256 include_once "./Services/RTE/classes/class.ilRTE.php";
257 foreach ($_SESSION["import_mob_xhtml"] as $mob) {
258 if ($tst_id > 0) {
259 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
260 } else {
261 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
262 }
263
264 global $DIC; /* @var ILIAS\DI\Container $DIC */
265 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
266
267 $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
268 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
269 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
270 foreach ($this->object->getOrderingElementList() as $element) {
271 $element->setContent(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $element->getContent()));
272 }
273 foreach ($feedbacksgeneric as $correctness => $material) {
274 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
275 }
276 foreach ($itemfeedbacks as $ident => $material) {
277 $itemfeedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
278 }
279 }
280 }
281 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
282 foreach ($this->object->getOrderingElementList() as $element) {
283 $element->setContent(ilRTE::_replaceMediaObjectImageSrc($element->getContent(), 1));
284 }
285 foreach ($feedbacksgeneric as $correctness => $material) {
286 $this->object->feedbackOBJ->importGenericFeedback(
287 $this->object->getId(),
288 $correctness,
290 );
291 }
292 foreach ($itemfeedbacks as $ident => $material) {
293 $index = $this->fetchIndexFromFeedbackIdent($ident, 'link_');
294
295 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
296 $this->object->getId(),
297 0,
298 $index,
300 );
301 }
302 $this->object->saveToDb();
303 if (isset($tst_id) && $tst_id !== $questionpool_id) {
304 $qplQid = $this->object->getId();
305 $tstQid = $this->object->duplicate(true, '', '', '', $tst_id);
306 $tst_object->questions[$question_counter++] = $tstQid;
307 $import_mapping[$item->getIdent()] = array("pool" => $qplQid, "test" => $tstQid);
308 return;
309 }
310
311 if ($tst_id > 0) {
312 $tst_object->questions[$question_counter++] = $this->object->getId();
313 $import_mapping[$item->getIdent()] = array("pool" => 0, "test" => $this->object->getId());
314 return;
315 }
316 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
317 }
318
319 protected function handleUploadedFile(array $answer) : ?string
320 {
321 $image = &base64_decode($answer["answerimage"]["content"]);
322 $image_file_name = $answer['answerimage']['label'];
323 $tmp_path = ilUtil::ilTempnam();
324
325 $file_handle = fopen($tmp_path, "wb");
326 if ($file_handle === false) {
327 return null;
328 }
329 fwrite($file_handle, $image);
330 fclose($file_handle);
331
332 $suffix = strtolower(array_pop(explode(".", $image_file_name)));
333 if (!in_array($suffix, assOrderingQuestion::VALID_UPLOAD_SUFFIXES)) {
334 return null;
335 }
336
337 $this->ensureImagePathExists();
338 $target_filename = $this->object->buildHashedImageFilename($image_file_name, true);
339 $target_filepath = $this->object->getImagePath() . $target_filename;
340 if (rename($tmp_path, $target_filepath)) {
341 $thumb_path = $this->object->getImagePath() . $this->object->getThumbPrefix() . $target_filename;
342 if ($this->object->getThumbGeometry()) {
343 ilUtil::convertImage($target_filepath, $thumb_path, "JPEG", $this->object->getThumbGeometry());
344 }
345 return $target_filename;
346 }
347
348 return null;
349 }
350
351 protected function ensureImagePathExists()
352 {
353 if (!file_exists($this->object->getImagePath())) {
354 ilUtil::makeDirParents($this->object->getImagePath());
355 }
356 }
357}
$filename
Definition: buildRTE.php:89
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class for ordering question imports.
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
Creates a question from a QTI file.
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_')
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir)
addGeneralMetadata(ilQTIItem $item)
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=true)
Create new media object and update page in db and return new media object.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static convertImage( $a_from, $a_to, $a_target_format="", $a_geometry="", $a_background_color="")
convert image
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
const IL_INST_ID
Definition: constants.php:38
global $DIC
Definition: goto.php:24
$ilUser
Definition: imgupload.php:18
const OQ_NESTED_PICTURES
const OQ_TERMS
const OQ_NESTED_TERMS
const OQ_PICTURES
Ordering question constants.
$index
Definition: metadata.php:128
$type
$response