ILIAS  release_7 Revision v7.30-3-g800a261c036
class.assMatchingQuestionImport.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{
17 public function saveImage($data, $filename)
18 {
19 $image = &base64_decode($data);
20 $imagepath = $this->object->getImagePath();
21 include_once "./Services/Utilities/classes/class.ilUtil.php";
22 if (!file_exists($imagepath)) {
23 ilUtil::makeDirParents($imagepath);
24 }
25 $imagepath .= $filename;
26 $fh = fopen($imagepath, "wb");
27 if ($fh == false) {
28 } else {
29 $imagefile = fwrite($fh, $image);
30 fclose($fh);
31 }
32 }
33
47 public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
48 {
49 global $DIC;
50 $ilUser = $DIC['ilUser'];
51
52 // empty session variable for imported xhtml mobs
53 unset($_SESSION["import_mob_xhtml"]);
54 $presentation = $item->getPresentation();
55 $duration = $item->getDuration();
56 $shuffle = 0;
57 $now = getdate();
58 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
59 $definitions = array();
60 $terms = array();
61 $foundimage = false;
62 foreach ($presentation->order as $entry) {
63 switch ($entry["type"]) {
64 case "response":
65 $response = $presentation->response[$entry["index"]];
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 $answerimage = array();
75 foreach ($response_label->material as $mat) {
76 for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
77 $foundmat = $mat->getMaterial($m);
78 if (strcmp($foundmat["type"], "mattext") == 0) {
79 $answertext .= $foundmat["material"]->getContent();
80 }
81 if (strcmp($foundmat["type"], "matimage") == 0) {
82 $foundimage = true;
83 $answerimage = array(
84 "imagetype" => $foundmat["material"]->getImageType(),
85 "label" => $foundmat["material"]->getLabel(),
86 "content" => $foundmat["material"]->getContent()
87 );
88 }
89 }
90 }
91 if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup()))) {
92 $definitions[$ident] = array(
93 "answertext" => $answertext,
94 "answerimage" => $answerimage,
95 "points" => 0,
96 "answerorder" => $ident,
97 "action" => ""
98 );
99 } else {
100 $terms[$ident] = array(
101 "term" => $answertext,
102 "answerimage" => $answerimage,
103 "points" => 0,
104 "ident" => $ident,
105 "action" => ""
106 );
107 }
108 }
109 break;
110 }
111 break;
112 }
113 }
114 $responses = array();
115 $feedbacksgeneric = array();
116 foreach ($item->resprocessing as $resprocessing) {
117 foreach ($resprocessing->respcondition as $respcondition) {
118 $subset = array();
119 $correctness = 1;
120 $conditionvar = $respcondition->getConditionvar();
121 foreach ($conditionvar->order as $order) {
122 switch ($order["field"]) {
123 case "varsubset":
124 $subset = explode(",", $conditionvar->varsubset[$order["index"]]->getContent());
125 break;
126 }
127 }
128 foreach ($respcondition->setvar as $setvar) {
129 array_push($responses, array("subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()));
130 }
131
132 if (count($respcondition->displayfeedback)) {
133 foreach ($respcondition->displayfeedback as $feedbackpointer) {
134 if (strlen($feedbackpointer->getLinkrefid())) {
135 foreach ($item->itemfeedback as $ifb) {
136 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
137 // found a feedback for the identifier
138 if (count($ifb->material)) {
139 foreach ($ifb->material as $material) {
140 $feedbacksgeneric[1] = $material;
141 }
142 }
143 if ((count($ifb->flow_mat) > 0)) {
144 foreach ($ifb->flow_mat as $fmat) {
145 if (count($fmat->material)) {
146 foreach ($fmat->material as $material) {
147 $feedbacksgeneric[1] = $material;
148 }
149 }
150 }
151 }
152 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
153 // found a feedback for the identifier
154 if (count($ifb->material)) {
155 foreach ($ifb->material as $material) {
156 $feedbacksgeneric[0] = $material;
157 }
158 }
159 if ((count($ifb->flow_mat) > 0)) {
160 foreach ($ifb->flow_mat as $fmat) {
161 if (count($fmat->material)) {
162 foreach ($fmat->material as $material) {
163 $feedbacksgeneric[0] = $material;
164 }
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173 }
174 }
175
176 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
177 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
178 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
179 $this->object->createNewQuestion();
180 $this->addGeneralMetadata($item);
181 $this->object->setTitle($item->getTitle());
182 $this->object->setNrOfTries($item->getMaxattempts());
183 $this->object->setComment($item->getComment());
184 $this->object->setAuthor($item->getAuthor());
185 $this->object->setOwner($ilUser->getId());
186 $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
187 $this->object->setObjId($questionpool_id);
188 $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
189 $extended_shuffle = $item->getMetadataEntry("shuffle");
190 $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
191
192 if (strlen($item->getMetadataEntry('matching_mode'))) {
193 $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
194 } else {
195 $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
196 }
197
198 // save images
199 foreach ($terms as $term) {
200 if (count($term['answerimage'])) {
201 $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
202 }
203 }
204 foreach ($definitions as $definition) {
205 if (count($definition['answerimage'])) {
206 $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
207 }
208 }
209
210 foreach ($terms as $termindex => $term) {
211 $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
212 }
213 foreach ($definitions as $definitionindex => $definition) {
214 $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
215 }
216
217 if (strlen($extended_shuffle) > 0) {
218 $shuffle = $extended_shuffle;
219 }
220 $this->object->setShuffle($shuffle);
221
222 foreach ($responses as $response) {
223 $subset = $response["subset"];
224 foreach ($subset as $ident) {
225 if (array_key_exists($ident, $definitions)) {
226 $definition = $definitions[$ident];
227 }
228 if (array_key_exists($ident, $terms)) {
229 $term = $terms[$ident];
230 }
231 }
232 $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
233 }
234 // additional content editing mode information
235 $this->object->setAdditionalContentEditingMode(
237 );
238 $this->object->saveToDb();
239 if (count($item->suggested_solutions)) {
240 foreach ($item->suggested_solutions as $suggested_solution) {
241 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
242 }
243 $this->object->saveToDb();
244 }
245 foreach ($responses as $response) {
246 $subset = $response["subset"];
247 foreach ($subset as $ident) {
248 if (array_key_exists($ident, $definitions)) {
249 $definition = $definitions[$ident];
250 }
251 if (array_key_exists($ident, $terms)) {
252 $term = $terms[$ident];
253 }
254 }
255 }
256
257 foreach ($feedbacksgeneric as $correctness => $material) {
258 $m = $this->object->QTIMaterialToString($material);
259 $feedbacksgeneric[$correctness] = $m;
260 }
261
262 $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
263
264 // handle the import of media objects in XHTML code
265 $questiontext = $this->object->getQuestion();
266 if (is_array($_SESSION["import_mob_xhtml"])) {
267 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
268 include_once "./Services/RTE/classes/class.ilRTE.php";
269 foreach ($_SESSION["import_mob_xhtml"] as $mob) {
270 if ($tst_id > 0) {
271 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
272 } else {
273 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
274 }
275
276 global $DIC; /* @var ILIAS\DI\Container $DIC */
277 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
278
279 $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
280 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
281 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
282 foreach ($feedbacks as $ident => $material) {
283 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
284 }
285 foreach ($feedbacksgeneric as $correctness => $material) {
286 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
287 }
288 }
289 }
290 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
291 foreach ($feedbacks as $ident => $material) {
292 $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
293
294 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
295 $this->object->getId(),
296 0,
297 $index,
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 if ($tst_id > 0) {
310 $q_1_id = $this->object->getId();
311 $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
312 $tst_object->questions[$question_counter++] = $question_id;
313 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
314 } else {
315 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
316 }
317 }
318
324 protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_')
325 {
326 list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
327
328 foreach ($this->object->getMatchingPairs() as $index => $pair) {
329 /* @var assAnswerMatchingPair $pair */
330
331 if ($pair->term->identifier != $termId) {
332 continue;
333 }
334
335 if ($pair->definition->identifier != $definitionId) {
336 continue;
337 }
338
339 return (int) $index;
340 }
341
342 return -1;
343 }
344}
$filename
Definition: buildRTE.php:89
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class for matching question definitions.
Class for matching question terms.
Class for matching question imports.
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
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)
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
$index
Definition: metadata.php:128
$response
$data
Definition: storeScorm.php:23