ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
24 ilUtil::makeDirParents($imagepath);
25 }
26 $imagepath .= $filename;
27 $fh = fopen($imagepath, "wb");
28 if ($fh == false)
29 {
30 }
31 else
32 {
33 $imagefile = fwrite($fh, $image);
34 fclose($fh);
35 }
36 }
37
51 function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
52 {
53 global $ilUser;
54
55 // empty session variable for imported xhtml mobs
56 unset($_SESSION["import_mob_xhtml"]);
57 $presentation = $item->getPresentation();
58 $duration = $item->getDuration();
59 $shuffle = 0;
60 $now = getdate();
61 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
62 $definitions = array();
63 $terms = array();
64 $foundimage = FALSE;
65 foreach ($presentation->order as $entry)
66 {
67 switch ($entry["type"])
68 {
69 case "response":
70 $response = $presentation->response[$entry["index"]];
71 $rendertype = $response->getRenderType();
72 switch (strtolower(get_class($rendertype)))
73 {
74 case "ilqtirenderchoice":
75 $shuffle = $rendertype->getShuffle();
76 $answerorder = 0;
77 foreach ($rendertype->response_labels as $response_label)
78 {
79 $ident = $response_label->getIdent();
80 $answertext = "";
81 $answerimage = array();
82 foreach ($response_label->material as $mat)
83 {
84 for ($m = 0; $m < $mat->getMaterialCount(); $m++)
85 {
86 $foundmat = $mat->getMaterial($m);
87 if (strcmp($foundmat["type"], "mattext") == 0)
88 {
89 $answertext .= $foundmat["material"]->getContent();
90 }
91 if (strcmp($foundmat["type"], "matimage") == 0)
92 {
93 $foundimage = TRUE;
94 $answerimage = array(
95 "imagetype" => $foundmat["material"]->getImageType(),
96 "label" => $foundmat["material"]->getLabel(),
97 "content" => $foundmat["material"]->getContent()
98 );
99 }
100 }
101 }
102 if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup())))
103 {
104 $definitions[$ident] = array(
105 "answertext" => $answertext,
106 "answerimage" => $answerimage,
107 "points" => 0,
108 "answerorder" => $ident,
109 "action" => ""
110 );
111 }
112 else
113 {
114 $terms[$ident] = array(
115 "term" => $answertext,
116 "answerimage" => $answerimage,
117 "points" => 0,
118 "ident" => $ident,
119 "action" => ""
120 );
121 }
122 }
123 break;
124 }
125 break;
126 }
127 }
128 $responses = array();
129 $feedbacksgeneric = array();
130 foreach ($item->resprocessing as $resprocessing)
131 {
132 foreach ($resprocessing->respcondition as $respcondition)
133 {
134 $subset = array();
135 $correctness = 1;
136 $conditionvar = $respcondition->getConditionvar();
137 foreach ($conditionvar->order as $order)
138 {
139 switch ($order["field"])
140 {
141 case "varsubset":
142 $subset = split(",", $conditionvar->varsubset[$order["index"]]->getContent());
143 break;
144 }
145 }
146 foreach ($respcondition->setvar as $setvar)
147 {
148 array_push($responses, array("subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()));
149 }
150
151 if (count($respcondition->displayfeedback))
152 {
153 foreach ($respcondition->displayfeedback as $feedbackpointer)
154 {
155 if (strlen($feedbackpointer->getLinkrefid()))
156 {
157 foreach ($item->itemfeedback as $ifb)
158 {
159 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
160 {
161 // found a feedback for the identifier
162 if (count($ifb->material))
163 {
164 foreach ($ifb->material as $material)
165 {
166 $feedbacksgeneric[1] = $material;
167 }
168 }
169 if ((count($ifb->flow_mat) > 0))
170 {
171 foreach ($ifb->flow_mat as $fmat)
172 {
173 if (count($fmat->material))
174 {
175 foreach ($fmat->material as $material)
176 {
177 $feedbacksgeneric[1] = $material;
178 }
179 }
180 }
181 }
182 }
183 else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
184 {
185 // found a feedback for the identifier
186 if (count($ifb->material))
187 {
188 foreach ($ifb->material as $material)
189 {
190 $feedbacksgeneric[0] = $material;
191 }
192 }
193 if ((count($ifb->flow_mat) > 0))
194 {
195 foreach ($ifb->flow_mat as $fmat)
196 {
197 if (count($fmat->material))
198 {
199 foreach ($fmat->material as $material)
200 {
201 $feedbacksgeneric[0] = $material;
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211
212 }
213 }
214
215 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
216 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
217 include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
218 $this->object->createNewQuestion();
219 $this->addGeneralMetadata($item);
220 $this->object->setTitle($item->getTitle());
221 $this->object->setNrOfTries($item->getMaxattempts());
222 $this->object->setComment($item->getComment());
223 $this->object->setAuthor($item->getAuthor());
224 $this->object->setOwner($ilUser->getId());
225 $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
226 $this->object->setObjId($questionpool_id);
227 $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
228 $extended_shuffle = $item->getMetadataEntry("shuffle");
229 $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
230
231 if( strlen($item->getMetadataEntry('matching_mode')) )
232 {
233 $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
234 }
235 else
236 {
237 $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
238 }
239
240 // save images
241 foreach ($terms as $term)
242 {
243 if (count($term['answerimage'])) $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
244 }
245 foreach ($definitions as $definition)
246 {
247 if (count($definition['answerimage'])) $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
248 }
249
250 foreach ($terms as $termindex => $term)
251 {
252 $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
253 }
254 foreach ($definitions as $definitionindex => $definition)
255 {
256 $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
257 }
258
259 if (strlen($extended_shuffle) > 0)
260 {
261 $shuffle = $extended_shuffle;
262 }
263 $this->object->setShuffle($shuffle);
264
265 foreach ($responses as $response)
266 {
267 $subset = $response["subset"];
268 foreach ($subset as $ident)
269 {
270 if (array_key_exists($ident, $definitions))
271 {
272 $definition = $definitions[$ident];
273 }
274 if (array_key_exists($ident, $terms))
275 {
276 $term = $terms[$ident];
277 }
278 }
279 $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
280 }
281 // additional content editing mode information
282 $this->object->setAdditionalContentEditingMode(
284 );
285 $this->object->saveToDb();
286 if (count($item->suggested_solutions))
287 {
288 foreach ($item->suggested_solutions as $suggested_solution)
289 {
290 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
291 }
292 $this->object->saveToDb();
293 }
294 foreach ($responses as $response)
295 {
296 $subset = $response["subset"];
297 foreach ($subset as $ident)
298 {
299 if (array_key_exists($ident, $definitions))
300 {
301 $definition = $definitions[$ident];
302 }
303 if (array_key_exists($ident, $terms))
304 {
305 $term = $terms[$ident];
306 }
307 }
308 }
309
310 foreach ($feedbacksgeneric as $correctness => $material)
311 {
312 $m = $this->object->QTIMaterialToString($material);
313 $feedbacksgeneric[$correctness] = $m;
314 }
315
316 // handle the import of media objects in XHTML code
317 $questiontext = $this->object->getQuestion();
318 if (is_array($_SESSION["import_mob_xhtml"]))
319 {
320 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
321 include_once "./Services/RTE/classes/class.ilRTE.php";
322 foreach ($_SESSION["import_mob_xhtml"] as $mob)
323 {
324 if ($tst_id > 0)
325 {
326 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
327 }
328 else
329 {
330 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
331 }
332
333 $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
334
335 $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
336 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
337 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
338 foreach ($feedbacksgeneric as $correctness => $material)
339 {
340 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
341 }
342 }
343 }
344 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
345 foreach ($feedbacksgeneric as $correctness => $material)
346 {
347 $this->object->feedbackOBJ->importGenericFeedback(
348 $this->object->getId(), $correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1)
349 );
350 }
351 $this->object->saveToDb();
352 if ($tst_id > 0)
353 {
354 $q_1_id = $this->object->getId();
355 $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
356 $tst_object->questions[$question_counter++] = $question_id;
357 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
358 }
359 else
360 {
361 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
362 }
363 }
364}
365
366?>
$filename
Definition: buildRTE.php:89
$_SESSION["AccountId"]
Class for matching question definitions.
Class for matching question terms.
Class for matching 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_...
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)
_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.
$data
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
global $ilUser
Definition: imgupload.php:15