ILIAS  release_7 Revision v7.30-3-g800a261c036
class.assKprimChoiceImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php';
5require_once 'Modules/TestQuestionPool/classes/class.ilAssKprimChoiceAnswer.php';
6
14{
18 public $object;
19
20 public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
21 {
22 global $DIC;
23 $ilUser = $DIC['ilUser'];
24
25 unset($_SESSION["import_mob_xhtml"]);
26
27 $duration = $item->getDuration();
28 $shuffle = 0;
29 $answers = array();
30
31 $presentation = $item->getPresentation();
32 foreach ($presentation->order as $entry) {
33 switch ($entry["type"]) {
34 case "response":
35 $response = $presentation->response[$entry["index"]];
36 $rendertype = $response->getRenderType();
37 switch (strtolower(get_class($response->getRenderType()))) {
38 case "ilqtirenderchoice":
39 $shuffle = $rendertype->getShuffle();
40 $answerorder = 0;
41 $foundimage = false;
42 foreach ($rendertype->response_labels as $response_label) {
43 $ident = $response_label->getIdent();
44 $answertext = "";
45 $answerimage = array();
46 foreach ($response_label->material as $mat) {
47 $embedded = false;
48 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
49 $foundmat = $mat->getMaterial($m);
50 if (strcmp($foundmat["type"], "mattext") == 0) {
51 }
52 if (strcmp($foundmat["type"], "matimage") == 0) {
53 if (strlen($foundmat["material"]->getEmbedded())) {
54 $embedded = true;
55 }
56 }
57 }
58 if ($embedded) {
59 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
60 $foundmat = $mat->getMaterial($m);
61 if (strcmp($foundmat["type"], "mattext") == 0) {
62 $answertext .= $foundmat["material"]->getContent();
63 }
64 if (strcmp($foundmat["type"], "matimage") == 0) {
65 $foundimage = true;
66 $answerimage = array(
67 "imagetype" => $foundmat["material"]->getImageType(),
68 "label" => $foundmat["material"]->getLabel(),
69 "content" => $foundmat["material"]->getContent()
70 );
71 }
72 }
73 } else {
74 $answertext = $this->object->QTIMaterialToString($mat);
75 }
76 }
77
78 $answers[$ident] = array(
79 "answertext" => $answertext,
80 "imagefile" => $answerimage,
81 "answerorder" => $ident
82 );
83 }
84 break;
85 }
86 break;
87 }
88 }
89
90 $feedbacks = array();
91 $feedbacksgeneric = array();
92
93 foreach ($item->resprocessing as $resprocessing) {
94 foreach ($resprocessing->outcomes->decvar as $decvar) {
95 if ($decvar->getVarname() == 'SCORE') {
96 $this->object->setPoints($decvar->getMaxvalue());
97
98 if ($decvar->getMinvalue() > 0) {
99 $this->object->setScorePartialSolutionEnabled(true);
100 } else {
101 $this->object->setScorePartialSolutionEnabled(false);
102 }
103 }
104 }
105
106 foreach ($resprocessing->respcondition as $respcondition) {
107 if (!count($respcondition->setvar)) {
108 foreach ($respcondition->getConditionvar()->varequal as $varequal) {
109 $ident = $varequal->respident;
110 $answers[$ident]['correctness'] = (bool) $varequal->getContent();
111
112 break;
113 }
114
115 foreach ($respcondition->displayfeedback as $feedbackpointer) {
116 if (strlen($feedbackpointer->getLinkrefid())) {
117 foreach ($item->itemfeedback as $ifb) {
118 if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
119 // found a feedback for the identifier
120 if (count($ifb->material)) {
121 foreach ($ifb->material as $material) {
122 $feedbacks[$ident] = $material;
123 }
124 }
125 if ((count($ifb->flow_mat) > 0)) {
126 foreach ($ifb->flow_mat as $fmat) {
127 if (count($fmat->material)) {
128 foreach ($fmat->material as $material) {
129 $feedbacks[$ident] = $material;
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 } else {
139 foreach ($respcondition->displayfeedback as $feedbackpointer) {
140 if (strlen($feedbackpointer->getLinkrefid())) {
141 foreach ($item->itemfeedback as $ifb) {
142 if ($ifb->getIdent() == "response_allcorrect") {
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 ($ifb->getIdent() == "response_onenotcorrect") {
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 $this->addGeneralMetadata($item);
183
184 $this->object->setTitle($item->getTitle());
185 $this->object->setNrOfTries($item->getMaxattempts());
186 $this->object->setComment($item->getComment());
187 $this->object->setAuthor($item->getAuthor());
188 $this->object->setOwner($ilUser->getId());
189 $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
190 $this->object->setObjId($questionpool_id);
191 $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
192 $this->object->setShuffleAnswersEnabled($shuffle);
193 $this->object->setAnswerType($item->getMetadataEntry("answer_type"));
194 $this->object->setOptionLabel($item->getMetadataEntry("option_label_setting"));
195 $this->object->setCustomTrueOptionLabel($item->getMetadataEntry("custom_true_option_label"));
196 $this->object->setCustomFalseOptionLabel($item->getMetadataEntry("custom_false_option_label"));
197 $this->object->setThumbSize($item->getMetadataEntry("thumb_size"));
198
199 $this->object->saveToDb();
200
201 foreach ($answers as $answerData) {
202 $answer = new ilAssKprimChoiceAnswer();
203 $answer->setImageFsDir($this->object->getImagePath());
204 $answer->setImageWebDir($this->object->getImagePathWeb());
205
206 $answer->setPosition($answerData['answerorder']);
207 $answer->setAnswertext($answerData['answertext']);
208 $answer->setCorrectness($answerData['correctness']);
209
210 if (isset($answerData['imagefile']['label'])) {
211 $answer->setImageFile($answerData['imagefile']['label']);
212 }
213
214 $this->object->addAnswer($answer);
215 }
216 // additional content editing mode information
217 $this->object->setAdditionalContentEditingMode(
219 );
220
221 $this->object->saveToDb();
222
223 foreach ($answers as $answer) {
224 if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
225 $image = &base64_decode($answer["imagefile"]["content"]);
226 $imagepath = $this->object->getImagePath();
227 include_once "./Services/Utilities/classes/class.ilUtil.php";
228 if (!file_exists($imagepath)) {
229 ilUtil::makeDirParents($imagepath);
230 }
231 $imagepath .= $answer["imagefile"]["label"];
232 if ($fh = fopen($imagepath, "wb")) {
233 $imagefile = fwrite($fh, $image);
234 fclose($fh);
235 }
236 }
237 }
238
239 $feedbackSetting = $item->getMetadataEntry('feedback_setting');
240 if (!is_null($feedbackSetting)) {
241 $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
242 $this->object->setSpecificFeedbackSetting($feedbackSetting);
243 }
244
245 // handle the import of media objects in XHTML code
246 foreach ($feedbacks as $ident => $material) {
247 $m = $this->object->QTIMaterialToString($material);
248 $feedbacks[$ident] = $m;
249 }
250 foreach ($feedbacksgeneric as $correctness => $material) {
251 $m = $this->object->QTIMaterialToString($material);
252 $feedbacksgeneric[$correctness] = $m;
253 }
254 $questiontext = $this->object->getQuestion();
255 $answers = &$this->object->getAnswers();
256 if (is_array($_SESSION["import_mob_xhtml"])) {
257 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
258 include_once "./Services/RTE/classes/class.ilRTE.php";
259 foreach ($_SESSION["import_mob_xhtml"] as $mob) {
260 if ($tst_id > 0) {
261 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
262 } else {
263 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
264 }
265
266 global $DIC; /* @var ILIAS\DI\Container $DIC */
267 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
268
269 $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
270 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
271 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
272 foreach ($answers as $key => $value) {
273 $answer_obj = &$answers[$key];
274 $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
275 }
276 foreach ($feedbacks as $ident => $material) {
277 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
278 }
279 foreach ($feedbacksgeneric as $correctness => $material) {
280 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
281 }
282 }
283 }
284 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
285 foreach ($answers as $key => $value) {
286 $answer_obj = &$answers[$key];
287 $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
288 }
289 foreach ($feedbacks as $ident => $material) {
290 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
291 $this->object->getId(),
292 0,
293 $ident,
295 );
296 }
297 foreach ($feedbacksgeneric as $correctness => $material) {
298 $this->object->feedbackOBJ->importGenericFeedback(
299 $this->object->getId(),
300 $correctness,
302 );
303 }
304 $this->object->saveToDb();
305 if (count($item->suggested_solutions)) {
306 foreach ($item->suggested_solutions as $suggested_solution) {
307 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
308 }
309 $this->object->saveToDb();
310 }
311 if ($tst_id > 0) {
312 $q_1_id = $this->object->getId();
313 $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
314 $tst_object->questions[$question_counter++] = $question_id;
315 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
316 } else {
317 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
318 }
319 //$ilLog->write(strftime("%D %T") . ": finished import multiple choice question (single response)");
320 }
321}
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
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_...
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)
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 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
$response