ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.assKprimChoiceImport.php
Go to the documentation of this file.
1<?php
2
28{
32 public $object;
33
34 public function fromXML(
35 string $importdirectory,
36 int $user_id,
37 ilQTIItem $item,
38 int $questionpool_id,
39 ?int $tst_id,
40 ?ilObject &$tst_object,
41 int &$question_counter,
42 array $import_mapping
43 ): array {
44 ilSession::clear('import_mob_xhtml');
45
46 $shuffle = 0;
47 $answers = [];
48
49 $presentation = $item->getPresentation();
50 foreach ($presentation->order as $entry) {
51 if ($entry['type'] !== 'response') {
52 continue;
53 }
54
55 $response = $presentation->response[$entry['index']];
56 $rendertype = $response->getRenderType();
57 if (strtolower(get_class($response->getRenderType())) !== 'ilqtirenderchoice') {
58 continue;
59 }
60
61 $shuffle = $rendertype->getShuffle();
62 $foundimage = false;
63 foreach ($rendertype->response_labels as $response_label) {
64 $ident = $response_label->getIdent();
65 $answertext = '';
66 $answerimage = [];
67 foreach ($response_label->material as $mat) {
68 $embedded = false;
69 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
70 $foundmat = $mat->getMaterial($m);
71 if ($foundmat['type'] === 'matimage'
72 && $foundmat['material']->getEmbedded() !== '') {
73 $embedded = true;
74 }
75 }
76 if (!$embedded) {
77 $answertext = $this->QTIMaterialToString($mat);
78 continue;
79 }
80
81 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
82 $foundmat = $mat->getMaterial($m);
83 if ($foundmat['type'] === 'mattext') {
84 $answertext .= $foundmat['material']->getContent();
85 }
86 if ($foundmat['type'] === 'matimage') {
87 $foundimage = true;
88 $answerimage = [
89 'imagetype' => $foundmat['material']->getImageType(),
90 'label' => $foundmat['material']->getLabel(),
91 'content' => $foundmat['material']->getContent()
92 ];
93 }
94 }
95 }
96
97 $answers[$ident] = [
98 'answertext' => $answertext,
99 'imagefile' => $answerimage,
100 'answerorder' => $ident
101 ];
102 }
103 }
104
105 $feedbacks = [];
106 $feedbacksgeneric = [];
107
108 foreach ($item->resprocessing as $resprocessing) {
109 foreach ($resprocessing->outcomes->decvar as $decvar) {
110 if ($decvar->getVarname() == 'SCORE') {
111 $this->object->setPoints($decvar->getMaxvalue());
112 $this->object->setScorePartialSolutionEnabled(false);
113 if ($decvar->getMinvalue() > 0) {
114 $this->object->setScorePartialSolutionEnabled(true);
115 }
116 }
117 }
118
119 foreach ($resprocessing->respcondition as $respcondition) {
120 if ($respcondition->setvar === []) {
121 foreach ($respcondition->getConditionvar()->varequal as $varequal) {
122 $ident = $varequal->respident;
123 $answers[$ident]['correctness'] = (bool) $varequal->getContent();
124 break;
125 }
126
127 foreach ($respcondition->displayfeedback as $feedbackpointer) {
128 if ($feedbackpointer->getLinkrefid() === '') {
129 continue;
130 }
131 foreach ($item->itemfeedback as $ifb) {
132 if ($ifb->getIdent() !== $feedbackpointer->getLinkrefid()) {
133 continue;
134 }
135
136 foreach ($ifb->material as $material) {
137 $feedbacks[$ident] = $material;
138 }
139 foreach ($ifb->flow_mat as $fmat) {
140 foreach ($fmat->material as $material) {
141 $feedbacks[$ident] = $material;
142 }
143 }
144 }
145 }
146
147 continue;
148 }
149
150 foreach ($respcondition->displayfeedback as $feedbackpointer) {
151 if ($feedbackpointer->getLinkrefid() === '') {
152 continue;
153 }
154
155 foreach ($item->itemfeedback as $ifb) {
156 if ($ifb->getIdent() === 'response_allcorrect') {
157 foreach ($ifb->material as $material) {
158 $feedbacksgeneric[1] = $material;
159 }
160 foreach ($ifb->flow_mat as $fmat) {
161 foreach ($fmat->material as $material) {
162 $feedbacksgeneric[1] = $material;
163 }
164 }
165 continue;
166 }
167
168 if ($ifb->getIdent() === 'response_onenotcorrect') {
169 // found a feedback for the identifier
170 foreach ($ifb->material as $material) {
171 $feedbacksgeneric[0] = $material;
172 }
173 foreach ($ifb->flow_mat as $fmat) {
174 foreach ($fmat->material as $material) {
175 $feedbacksgeneric[0] = $material;
176 }
177 }
178 }
179 }
180 }
181 }
182 }
183
184 $this->addGeneralMetadata($item);
185 $this->object->setTitle($item->getTitle());
186 $this->object->setNrOfTries((int) $item->getMaxattempts());
187 $this->object->setComment($item->getComment());
188 $this->object->setAuthor($item->getAuthor());
189 $this->object->setOwner($user_id);
190 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
191 $this->object->setObjId($questionpool_id);
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(
198 $this->deduceThumbSizeFromImportValue((int) $item->getMetadataEntry('thumb_size'))
199 );
200
201 $this->object->saveToDb();
202
203 $answer_objects = [];
204 foreach ($answers as $answer_data) {
205 $answer = new ilAssKprimChoiceAnswer();
206 $answer->setImageFsDir($this->object->getImagePath());
207 $answer->setImageWebDir($this->object->getImagePathWeb());
208 $answer->setPosition($answer_data['answerorder']);
209 $answer->setAnswertext($answer_data['answertext']);
210 $answer->setCorrectness($answer_data['correctness']);
211 if (isset($answer_data['imagefile']['label'])) {
212 $answer->setImageFile($answer_data['imagefile']['label']);
213 }
214 $answer_objects[] = $answer;
215 }
216 $this->object->setAnswers($answer_objects);
217 // additional content editing mode information
218 $this->object->setAdditionalContentEditingMode(
220 );
221
222 $this->object->saveToDb();
223
224 foreach ($answers as $answer) {
225 if (!is_array($answer['imagefile']) || $answer['imagefile'] === []) {
226 continue;
227 }
228 $image = base64_decode($answer['imagefile']['content']);
229 $imagepath = $this->object->getImagePath();
230 if (!file_exists($imagepath)) {
231 ilFileUtils::makeDirParents($imagepath);
232 }
233 $imagepath .= $answer['imagefile']['label'];
234 if ($fh = fopen($imagepath, 'wb')) {
235 $imagefile = fwrite($fh, $image);
236 fclose($fh);
237 $this->object->generateThumbForFile(
238 $answer['imagefile']['label'],
239 $this->object->getImagePath(),
240 $this->object->getThumbSize()
241 );
242 }
243 }
244
245 $feedback_setting = $item->getMetadataEntry('feedback_setting');
246 if (!is_null($feedback_setting)) {
247 $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedback_setting);
248 $this->object->setSpecificFeedbackSetting($feedback_setting);
249 }
250
251 // handle the import of media objects in XHTML code
252 foreach ($feedbacks as $ident => $material) {
253 $m = $this->QTIMaterialToString($material);
254 $feedbacks[$ident] = $m;
255 }
256 foreach ($feedbacksgeneric as $correctness => $material) {
257 $m = $this->QTIMaterialToString($material);
258 $feedbacksgeneric[$correctness] = $m;
259 }
260 $questiontext = $this->object->getQuestion();
261 $answers = $this->object->getAnswers();
262 if (is_array(ilSession::get("import_mob_xhtml"))) {
263 foreach (ilSession::get("import_mob_xhtml") as $mob) {
264 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
265
266 global $DIC; /* @var ILIAS\DI\Container $DIC */
267 $DIC['ilLog']->info('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 $answer_obj) {
273 if ($answer_obj->getAnswertext() === null) {
274 continue;
275 }
276 $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
277 }
278 foreach ($feedbacks as $ident => $material) {
279 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
280 }
281 foreach ($feedbacksgeneric as $correctness => $material) {
282 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
283 }
284 }
285 }
286 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
287 foreach ($answers as $answer_obj) {
288 if ($answer_obj->getAnswertext() === null) {
289 continue;
290 }
291 $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
292 }
293 foreach ($feedbacks as $ident => $material) {
294 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
295 $this->object->getId(),
296 0,
297 $ident,
299 );
300 }
301 foreach ($feedbacksgeneric as $correctness => $material) {
302 $this->object->feedbackOBJ->importGenericFeedback(
303 $this->object->getId(),
304 $correctness,
306 );
307 }
308 $this->object->saveToDb();
309 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
310 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
311 $questionpool_id,
312 $tst_id,
313 $question_counter,
314 $tst_object
315 );
316 return $import_mapping;
317 }
318}
fromXML(string $importdirectory, int $user_id, ilQTIItem $item, int $questionpool_id, ?int $tst_id, ?ilObject &$tst_object, int &$question_counter, array $import_mapping)
Class for question imports.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
addQuestionToParentObjectAndBuildMappingEntry(int $questionpool_id, ?int $tst_id, int &$question_counter, ?ilObjTest &$tst_object)
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
addGeneralMetadata(ilQTIItem $item)
deduceThumbSizeFromImportValue(?int $size)
importSuggestedSolutions(int $question_id, array $solution_from_import)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
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.
Class ilObject Basic functions for all objects.
getMetadataEntry(string $a_label)
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)
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:92