ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.assOrderingQuestionImport.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 {
20  public $object;
21 
35  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
36  {
37  global $ilUser;
38 
39  // empty session variable for imported xhtml mobs
40  unset($_SESSION["import_mob_xhtml"]);
41  $presentation = $item->getPresentation();
42  $duration = $item->getDuration();
43  $shuffle = 0;
44  $now = getdate();
45  $foundimage = FALSE;
46  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
47  $answers = array();
48  $type = OQ_TERMS;
49 
50  foreach ($presentation->order as $entry)
51  {
52  switch ($entry["type"])
53  {
54  case "response":
55  $response = $presentation->response[$entry["index"]];
56  $type = $response->getIdent();
57  if($response->getIdent() == 'OQP' )
58  $type = OQ_PICTURES;
59  else if($response->getIdent() == 'OQNP')
60  $type = OQ_NESTED_PICTURES;
61  else if($response->getIdent() == 'OQNT')
62  $type = OQ_NESTED_TERMS;
63  else if($response->getIdent() == 'OQT')
64  $type = OQ_TERMS;
65 
66  $rendertype = $response->getRenderType();
67  switch (strtolower(get_class($rendertype)))
68  {
69  case "ilqtirenderchoice":
70  $shuffle = $rendertype->getShuffle();
71  $answerorder = 0;
72  foreach ($rendertype->response_labels as $response_label)
73  {
74  $ident = $response_label->getIdent();
75  $answertext = "";
76  foreach ($response_label->material as $mat)
77  {
78  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
79  {
80  $foundmat = $mat->getMaterial($m);
81 
82  if (strcmp($foundmat["material"]->getLabel(), "answerdepth") == 0)
83  {
84  $answerdepth = $foundmat["material"]->getContent();
85  }
86  if (strcmp($foundmat["type"], "mattext") == 0
87  && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0)
88  {
89  $answertext .= $foundmat["material"]->getContent();
90  }
91  if (strcmp($foundmat["type"], "matimage") == 0
92  && strcmp($foundmat["material"]->getLabel(), "answerdepth") != 0)
93  {
94  $foundimage = TRUE;
95  $answerimage = array(
96  "imagetype" => $foundmat["material"]->getImageType(),
97  "label" => $foundmat["material"]->getLabel(),
98  "content" => $foundmat["material"]->getContent()
99  );
100  }
101  }
102  }
103  $answers[$answerorder] = array(
104  'ident' => $ident,
105  "answertext" => $answertext,
106  "answerimage" => $answerimage,
107  "points" => 0,
108  "answerorder" => $answerorder,
109  "answerdepth" => $answerdepth,
110  "correctness" => "",
111  "action" => ""
112  );
113  $answerorder++;
114  }
115  break;
116  }
117  break;
118  }
119  }
120  $responses = array();
121  $feedbacksgeneric = array();
122  foreach ($item->resprocessing as $resprocessing)
123  {
124  foreach ($resprocessing->respcondition as $respcondition)
125  {
126  $ident = "";
127  $correctness = 1;
128  $conditionvar = $respcondition->getConditionvar();
129  foreach ($conditionvar->order as $order)
130  {
131  switch ($order["field"])
132  {
133  case "arr_not":
134  $correctness = 0;
135  break;
136  case "varequal":
137  $ident = $conditionvar->varequal[$order["index"]]->getContent();
138  $orderindex = $conditionvar->varequal[$order["index"]]->getIndex();
139  break;
140  }
141  }
142  foreach ($respcondition->setvar as $setvar)
143  {
144  if (strcmp($ident, "") != 0)
145  {
146  $answers[$ident]["solutionorder"] = $orderindex;
147  $answers[$ident]["action"] = $setvar->getAction();
148  $answers[$ident]["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  $itemfeedbacks = $this->getFeedbackAnswerSpecific($item, 'link_');
215 
216  $this->addGeneralMetadata($item);
217  $this->object->setTitle($item->getTitle());
218  $this->object->setNrOfTries($item->getMaxattempts());
219  $this->object->setComment($item->getComment());
220  $this->object->setAuthor($item->getAuthor());
221  $this->object->setOwner($ilUser->getId());
222  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
223  $this->object->setOrderingType($type);
224  $this->object->setObjId($questionpool_id);
225  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
226  $this->object->setElementHeight($item->getMetadataEntry("element_height"));
227  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
228  $this->object->setShuffle($shuffle);
229  $points = 0;
230  $solanswers = array();
231 
232  foreach ($answers as $answer)
233  {
234  $solanswers[$answer["solutionorder"]] = $answer;
235  }
236  ksort($solanswers);
237  $position = 0;
238  foreach ($solanswers as $answer)
239  {
240  $points += $answer["points"];
241 
242  $element = new ilAssOrderingElement();
243 
244  if( $element->isExportIdent($answer['ident']) )
245  {
246  $element->setExportIdent($answer['ident']);
247  }
248  else
249  {
250  $element->setPosition($position++);
251  if(isset($answer['answerdepth']))
252  {
253  $element->setIndentation($answer['answerdepth']);
254  }
255  }
256 
257  if( $this->object->isImageOrderingType() )
258  {
259  $element->setContent($answer["answerimage"]["label"]);
260  }
261  else
262  {
263  $element->setContent($answer["answertext"]);
264  }
265 
266  $this->object->getOrderingElementList()->addElement($element);
267  }
268  $points = ($item->getMetadataEntry("points") > 0) ? $item->getMetadataEntry("points") : $points;
269  $this->object->setPoints($points);
270  // additional content editing mode information
271  $this->object->setAdditionalContentEditingMode(
273  );
274  $this->object->saveToDb();
275  if (count($item->suggested_solutions))
276  {
277  foreach ($item->suggested_solutions as $suggested_solution)
278  {
279  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
280  }
281  $this->object->saveToDb();
282  }
283  foreach ($answers as $answer)
284  {
285  if ($type == OQ_PICTURES || $type == OQ_NESTED_PICTURES)
286  {
287  include_once "./Services/Utilities/classes/class.ilUtil.php";
288  if( strlen($answer['answerimage']['label']) && strlen($answer['answerimage']['content']) )
289  {
290  $image =& base64_decode($answer["answerimage"]["content"]);
291  $imagepath = $this->object->getImagePath();
292  if (!file_exists($imagepath))
293  {
294  ilUtil::makeDirParents($imagepath);
295  }
296  $imagepath .= $answer["answerimage"]["label"];
297  $fh = fopen($imagepath, "wb");
298  if ($fh == false)
299  {
300 // global $ilErr;
301 // $ilErr->raiseError($this->object->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->MESSAGE);
302 // return;
303  }
304  else
305  {
306  $imagefile = fwrite($fh, $image);
307  fclose($fh);
308  }
309  // create thumbnail file
310  $thumbpath = $imagepath . "." . "thumb.jpg";
311  ilUtil::convertImage($imagepath, $thumbpath, "JPEG", $this->object->getThumbGeometry());
312  }
313  }
314  }
315  foreach ($feedbacksgeneric as $correctness => $material)
316  {
317  $m = $this->object->QTIMaterialToString($material);
318  $feedbacksgeneric[$correctness] = $m;
319  }
320  $questiontext = $this->object->getQuestion();
321 
322  // handle the import of media objects in XHTML code
323  if (is_array($_SESSION["import_mob_xhtml"]))
324  {
325  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
326  include_once "./Services/RTE/classes/class.ilRTE.php";
327  foreach ($_SESSION["import_mob_xhtml"] as $mob)
328  {
329  if ($tst_id > 0)
330  {
331  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
332  }
333  else
334  {
335  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
336  }
337 
338  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
339 
340  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
341  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
342  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
343  foreach($this->object->getOrderingElementList() as $element)
344  {
345  $element->setContent(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $element->getContent()));
346  }
347  foreach ($feedbacksgeneric as $correctness => $material)
348  {
349  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
350  }
351  foreach ($itemfeedbacks as $ident => $material)
352  {
353  $itemfeedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
354  }
355  }
356  }
357  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
358  foreach($this->object->getOrderingElementList() as $element)
359  {
360  $element->setContent(ilRTE::_replaceMediaObjectImageSrc($element->getContent(), 1));
361  }
362  foreach ($feedbacksgeneric as $correctness => $material)
363  {
364  $this->object->feedbackOBJ->importGenericFeedback(
365  $this->object->getId(), $correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1)
366  );
367  }
368  foreach ($itemfeedbacks as $ident => $material)
369  {
370  $index = $this->fetchIndexFromFeedbackIdent($ident, 'link_');
371 
372  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
373  $this->object->getId(), $index, ilRTE::_replaceMediaObjectImageSrc($material, 1)
374  );
375  }
376  $this->object->saveToDb();
377  if ($tst_id > 0)
378  {
379  $this->object->setObjId($tst_id);
380  $tstQid = $this->object->getId();
381  $qplQid = $this->object->duplicate(true, null, null, null, $questionpool_id);
383  assQuestion::saveOriginalId($tstQid, $qplQid);
384  $tst_object->questions[$question_counter++] = $tstQid;
385  $import_mapping[$item->getIdent()] = array("pool" => $qplQid, "test" => $tstQid);
386  }
387  else
388  {
389  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
390  }
391  }
392 }
393 
394 ?>
static resetOriginalId($questionId)
static makeDirParents($a_dir)
Create a new directory and all parent directories.
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
addGeneralMetadata(ilQTIItem $item)
$_SESSION["AccountId"]
const OQ_NESTED_TERMS
$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...
Class for ordering question imports.
const OQ_PICTURES
Ordering question constants.
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
static saveOriginalId($questionId, $originalId)
Create styles array
The data for the language used.
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
Creates a question from a QTI file.
const OQ_TERMS
Create new PHPExcel object
obj_idprivate
static convertImage($a_from, $a_to, $a_target_format="", $a_geometry="", $a_background_color="")
convert image
const OQ_NESTED_PICTURES
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.
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')