ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.assMultipleChoiceImport.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
5 
16 {
30  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
31  {
32  global $ilUser;
33  // empty session variable for imported xhtml mobs
34  unset($_SESSION["import_mob_xhtml"]);
35  $presentation = $item->getPresentation();
36  $duration = $item->getDuration();
37  $shuffle = 0;
38  $selectionLimit = null;
39  $now = getdate();
40  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
41  $answers = array();
42  foreach ($presentation->order as $entry)
43  {
44  switch ($entry["type"])
45  {
46  case "response":
47  $response = $presentation->response[$entry["index"]];
48  $rendertype = $response->getRenderType();
49  switch (strtolower(get_class($response->getRenderType())))
50  {
51  case "ilqtirenderchoice":
52  $shuffle = $rendertype->getShuffle();
53  if($rendertype->getMaxnumber())
54  {
55  $selectionLimit = $rendertype->getMaxnumber();
56  }
57  $answerorder = 0;
58  $foundimage = FALSE;
59  foreach ($rendertype->response_labels as $response_label)
60  {
61  $ident = $response_label->getIdent();
62  $answertext = "";
63  $answerimage = array();
64  foreach ($response_label->material as $mat)
65  {
66  $embedded = false;
67  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
68  {
69  $foundmat = $mat->getMaterial($m);
70  if (strcmp($foundmat["type"], "mattext") == 0)
71  {
72  }
73  if (strcmp($foundmat["type"], "matimage") == 0)
74  {
75  if (strlen($foundmat["material"]->getEmbedded()))
76  {
77  $embedded = true;
78  }
79  }
80  }
81  if ($embedded)
82  {
83  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
84  {
85  $foundmat = $mat->getMaterial($m);
86  if (strcmp($foundmat["type"], "mattext") == 0)
87  {
88  $answertext .= $foundmat["material"]->getContent();
89  }
90  if (strcmp($foundmat["type"], "matimage") == 0)
91  {
92  $foundimage = TRUE;
93  $answerimage = array(
94  "imagetype" => $foundmat["material"]->getImageType(),
95  "label" => $foundmat["material"]->getLabel(),
96  "content" => $foundmat["material"]->getContent()
97  );
98  }
99  }
100  }
101  else
102  {
103  $answertext = $this->object->QTIMaterialToString($mat);
104  }
105  }
106  $answers[$ident] = array(
107  "answertext" => $answertext,
108  "imagefile" => $answerimage,
109  "points" => 0,
110  "answerorder" => $answerorder++,
111  "points_unchecked" => 0,
112  "action" => ""
113  );
114  }
115  break;
116  }
117  break;
118  }
119  }
120  $responses = array();
121  $feedbacks = array();
122  $feedbacksgeneric = array();
123  foreach ($item->resprocessing as $resprocessing)
124  {
125  foreach ($resprocessing->respcondition as $respcondition)
126  {
127  $ident = "";
128  $correctness = 1;
129  $conditionvar = $respcondition->getConditionvar();
130  foreach ($conditionvar->order as $order)
131  {
132  switch ($order["field"])
133  {
134  case "arr_not":
135  $correctness = 0;
136  break;
137  case "varequal":
138  $ident = $conditionvar->varequal[$order["index"]]->getContent();
139  break;
140  }
141  }
142  foreach ($respcondition->setvar as $setvar)
143  {
144  if (strcmp($ident, "") != 0)
145  {
146  if ($correctness)
147  {
148  $answers[$ident]["action"] = $setvar->getAction();
149  $answers[$ident]["points"] = $setvar->getContent();
150  if (count($respcondition->displayfeedback))
151  {
152  foreach ($respcondition->displayfeedback as $feedbackpointer)
153  {
154  if (strlen($feedbackpointer->getLinkrefid()))
155  {
156  foreach ($item->itemfeedback as $ifb)
157  {
158  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
159  {
160  // found a feedback for the identifier
161  if (count($ifb->material))
162  {
163  foreach ($ifb->material as $material)
164  {
165  $feedbacksgeneric[1] = $material;
166  }
167  }
168  if ((count($ifb->flow_mat) > 0))
169  {
170  foreach ($ifb->flow_mat as $fmat)
171  {
172  if (count($fmat->material))
173  {
174  foreach ($fmat->material as $material)
175  {
176  $feedbacksgeneric[1] = $material;
177  }
178  }
179  }
180  }
181  }
182  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
183  {
184  // found a feedback for the identifier
185  if (count($ifb->material))
186  {
187  foreach ($ifb->material as $material)
188  {
189  $feedbacksgeneric[0] = $material;
190  }
191  }
192  if ((count($ifb->flow_mat) > 0))
193  {
194  foreach ($ifb->flow_mat as $fmat)
195  {
196  if (count($fmat->material))
197  {
198  foreach ($fmat->material as $material)
199  {
200  $feedbacksgeneric[0] = $material;
201  }
202  }
203  }
204  }
205  }
206  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0)
207  {
208  // found a feedback for the identifier
209  if (count($ifb->material))
210  {
211  foreach ($ifb->material as $material)
212  {
213  $feedbacks[$ident] = $material;
214  }
215  }
216  if ((count($ifb->flow_mat) > 0))
217  {
218  foreach ($ifb->flow_mat as $fmat)
219  {
220  if (count($fmat->material))
221  {
222  foreach ($fmat->material as $material)
223  {
224  $feedbacks[$ident] = $material;
225  }
226  }
227  }
228  }
229  }
230  }
231  }
232  }
233  }
234  }
235  else
236  {
237  $answers[$ident]["action"] = $setvar->getAction();
238  $answers[$ident]["points_unchecked"] = $setvar->getContent();
239  }
240  }
241  }
242  }
243  }
244  $this->addGeneralMetadata($item);
245  $this->object->setTitle($item->getTitle());
246  $this->object->setNrOfTries($item->getMaxattempts());
247  $this->object->setComment($item->getComment());
248  $this->object->setAuthor($item->getAuthor());
249  $this->object->setOwner($ilUser->getId());
250  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
251  $this->object->setObjId($questionpool_id);
252  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
253  $this->object->setShuffle($shuffle);
254  $this->object->setSelectionLimit($selectionLimit);
255  $this->object->setThumbSize($item->getMetadataEntry("thumb_size"));
256 
257  foreach ($answers as $answer)
258  {
259  if ($item->getMetadataEntry('singleline') || (is_array($answer["imagefile"]) && count($answer["imagefile"]) > 0))
260  {
261  $this->object->isSingleline = true;
262  }
263  $this->object->addAnswer($answer["answertext"], $answer["points"], $answer["points_unchecked"], $answer["answerorder"], $answer["imagefile"]["label"]);
264  }
265  // additional content editing mode information
266  $this->object->setAdditionalContentEditingMode(
268  );
269  $this->object->saveToDb();
270  foreach ($answers as $answer)
271  {
272  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0))
273  {
274  $image =& base64_decode($answer["imagefile"]["content"]);
275  $imagepath = $this->object->getImagePath();
276  include_once "./Services/Utilities/classes/class.ilUtil.php";
277  if (!file_exists($imagepath))
278  {
279  ilUtil::makeDirParents($imagepath);
280  }
281  $imagepath .= $answer["imagefile"]["label"];
282  $fh = fopen($imagepath, "wb");
283  if ($fh == false)
284  {
285  }
286  else
287  {
288  $imagefile = fwrite($fh, $image);
289  fclose($fh);
290  }
291  }
292  }
293 
294  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
295  if( !is_null($feedbackSetting) )
296  {
297  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
298  }
299 
300  // handle the import of media objects in XHTML code
301  foreach ($feedbacks as $ident => $material)
302  {
303  $m = $this->object->QTIMaterialToString($material);
304  $feedbacks[$ident] = $m;
305  }
306  foreach ($feedbacksgeneric as $correctness => $material)
307  {
308  $m = $this->object->QTIMaterialToString($material);
309  $feedbacksgeneric[$correctness] = $m;
310  }
311  $questiontext = $this->object->getQuestion();
312  $answers =& $this->object->getAnswers();
313  if (is_array($_SESSION["import_mob_xhtml"]))
314  {
315  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
316  include_once "./Services/RTE/classes/class.ilRTE.php";
317  foreach ($_SESSION["import_mob_xhtml"] as $mob)
318  {
319  if ($tst_id > 0)
320  {
321  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
322  }
323  else
324  {
325  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
326  }
327 
328  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
329 
330  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
331  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
332  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
333  foreach ($answers as $key => $value)
334  {
335  $answer_obj =& $answers[$key];
336  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
337  }
338  foreach ($feedbacks as $ident => $material)
339  {
340  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
341  }
342  foreach ($feedbacksgeneric as $correctness => $material)
343  {
344  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
345  }
346  }
347  }
348  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
349  foreach ($answers as $key => $value)
350  {
351  $answer_obj =& $answers[$key];
352  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
353  }
354  foreach ($feedbacks as $ident => $material)
355  {
356  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
357  $this->object->getId(), $ident, ilRTE::_replaceMediaObjectImageSrc($material, 1)
358  );
359  }
360  foreach ($feedbacksgeneric as $correctness => $material)
361  {
362  $this->object->feedbackOBJ->importGenericFeedback(
363  $this->object->getId(), $correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1)
364  );
365  }
366  $this->object->saveToDb();
367  if (count($item->suggested_solutions))
368  {
369  foreach ($item->suggested_solutions as $suggested_solution)
370  {
371  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
372  }
373  $this->object->saveToDb();
374  }
375  if ($tst_id > 0)
376  {
377  $q_1_id = $this->object->getId();
378  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
379  $tst_object->questions[$question_counter++] = $question_id;
380  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
381  }
382  else
383  {
384  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
385  }
386  //$ilLog->write(strftime("%D %T") . ": finished import multiple choice question (single response)");
387  }
388 }
389 
390 ?>
static makeDirParents($a_dir)
Create a new directory and all parent directories.
addGeneralMetadata(ilQTIItem $item)
$_SESSION["AccountId"]
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
Creates a question from a QTI file.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
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 _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
Class for multiple choice question imports.
Create new PHPExcel object
obj_idprivate
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
Class for question imports.
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.