ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assSingleChoiceImport.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
25 
36 {
50  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
51  {
52  global $ilUser;
53 
54  // empty session variable for imported xhtml mobs
55  unset($_SESSION["import_mob_xhtml"]);
56  $presentation = $item->getPresentation();
57  $duration = $item->getDuration();
58  $shuffle = 0;
59  $now = getdate();
60  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
61  $answers = array();
62  foreach ($presentation->order as $entry)
63  {
64  switch ($entry["type"])
65  {
66  case "response":
67  $response = $presentation->response[$entry["index"]];
68  $rendertype = $response->getRenderType();
69  switch (strtolower(get_class($response->getRenderType())))
70  {
71  case "ilqtirenderchoice":
72  $shuffle = $rendertype->getShuffle();
73  $answerorder = 0;
74  $foundimage = FALSE;
75  foreach ($rendertype->response_labels as $response_label)
76  {
77  $ident = $response_label->getIdent();
78  $answertext = "";
79  $answerimage = array();
80  foreach ($response_label->material as $mat)
81  {
82  $embedded = false;
83  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
84  {
85  $foundmat = $mat->getMaterial($m);
86  if (strcmp($foundmat["type"], "mattext") == 0)
87  {
88  }
89  if (strcmp($foundmat["type"], "matimage") == 0)
90  {
91  if (strlen($foundmat["material"]->getEmbedded()))
92  {
93  $embedded = true;
94  }
95  }
96  }
97  if ($embedded)
98  {
99  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
100  {
101  $foundmat = $mat->getMaterial($m);
102  if (strcmp($foundmat["type"], "mattext") == 0)
103  {
104  $answertext .= $foundmat["material"]->getContent();
105  }
106  if (strcmp($foundmat["type"], "matimage") == 0)
107  {
108  $foundimage = TRUE;
109  $answerimage = array(
110  "imagetype" => $foundmat["material"]->getImageType(),
111  "label" => $foundmat["material"]->getLabel(),
112  "content" => $foundmat["material"]->getContent()
113  );
114  }
115  }
116  }
117  else
118  {
119  $answertext = $this->object->QTIMaterialToString($mat);
120  }
121  }
122  $answers[$ident] = array(
123  "answertext" => $answertext,
124  "imagefile" => $answerimage,
125  "points" => 0,
126  "answerorder" => $answerorder++,
127  "points_unchecked" => 0,
128  "action" => ""
129  );
130  }
131  break;
132  }
133  break;
134  }
135  }
136  $responses = array();
137  $feedbacks = array();
138  $feedbacksgeneric = array();
139  foreach ($item->resprocessing as $resprocessing)
140  {
141  foreach ($resprocessing->respcondition as $respcondition)
142  {
143  $ident = "";
144  $correctness = 1;
145  $conditionvar = $respcondition->getConditionvar();
146  foreach ($conditionvar->order as $order)
147  {
148  switch ($order["field"])
149  {
150  case "arr_not":
151  $correctness = 0;
152  break;
153  case "varequal":
154  $ident = $conditionvar->varequal[$order["index"]]->getContent();
155  break;
156  }
157  }
158  foreach ($respcondition->setvar as $setvar)
159  {
160  if (strcmp($ident, "") != 0)
161  {
162  if ($correctness)
163  {
164  $answers[$ident]["action"] = $setvar->getAction();
165  $answers[$ident]["points"] = $setvar->getContent();
166  if (count($respcondition->displayfeedback))
167  {
168  foreach ($respcondition->displayfeedback as $feedbackpointer)
169  {
170  if (strlen($feedbackpointer->getLinkrefid()))
171  {
172  foreach ($item->itemfeedback as $ifb)
173  {
174  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
175  {
176  // found a feedback for the identifier
177  if (count($ifb->material))
178  {
179  foreach ($ifb->material as $material)
180  {
181  $feedbacksgeneric[1] = $material;
182  }
183  }
184  if ((count($ifb->flow_mat) > 0))
185  {
186  foreach ($ifb->flow_mat as $fmat)
187  {
188  if (count($fmat->material))
189  {
190  foreach ($fmat->material as $material)
191  {
192  $feedbacksgeneric[1] = $material;
193  }
194  }
195  }
196  }
197  }
198  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
199  {
200  // found a feedback for the identifier
201  if (count($ifb->material))
202  {
203  foreach ($ifb->material as $material)
204  {
205  $feedbacksgeneric[0] = $material;
206  }
207  }
208  if ((count($ifb->flow_mat) > 0))
209  {
210  foreach ($ifb->flow_mat as $fmat)
211  {
212  if (count($fmat->material))
213  {
214  foreach ($fmat->material as $material)
215  {
216  $feedbacksgeneric[0] = $material;
217  }
218  }
219  }
220  }
221  }
222  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0)
223  {
224  // found a feedback for the identifier
225  if (count($ifb->material))
226  {
227  foreach ($ifb->material as $material)
228  {
229  $feedbacks[$ident] = $material;
230  }
231  }
232  if ((count($ifb->flow_mat) > 0))
233  {
234  foreach ($ifb->flow_mat as $fmat)
235  {
236  if (count($fmat->material))
237  {
238  foreach ($fmat->material as $material)
239  {
240  $feedbacks[$ident] = $material;
241  }
242  }
243  }
244  }
245  }
246  }
247  }
248  }
249  }
250  }
251  else
252  {
253  $answers[$ident]["action"] = $setvar->getAction();
254  $answers[$ident]["points_unchecked"] = $setvar->getContent();
255  }
256  }
257  }
258  }
259  }
260  $this->object->setTitle($item->getTitle());
261  $this->object->setNrOfTries($item->getMaxattempts());
262  $this->object->setComment($item->getComment());
263  $this->object->setAuthor($item->getAuthor());
264  $this->object->setOwner($ilUser->getId());
265  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
266  $this->object->setObjId($questionpool_id);
267  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
268  $this->object->setShuffle($shuffle);
269  $this->object->setThumbSize($item->getMetadataEntry("thumb_size"));
270  foreach ($answers as $answer)
271  {
272  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0))
273  {
274  $this->object->isSingleline = true;
275  }
276  $this->object->addAnswer($answer["answertext"], $answer["points"], $answer["answerorder"], $answer["imagefile"]["label"]);
277  }
278  $this->object->saveToDb();
279  foreach ($answers as $answer)
280  {
281  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0))
282  {
283  $image =& base64_decode($answer["imagefile"]["content"]);
284  $imagepath = $this->object->getImagePath();
285  include_once "./Services/Utilities/classes/class.ilUtil.php";
286  if (!file_exists($imagepath))
287  {
288  ilUtil::makeDirParents($imagepath);
289  }
290  $imagepath .= $answer["imagefile"]["label"];
291  $fh = fopen($imagepath, "wb");
292  if ($fh == false)
293  {
294  }
295  else
296  {
297  $imagefile = fwrite($fh, $image);
298  fclose($fh);
299  }
300  }
301  }
302  foreach ($feedbacks as $ident => $material)
303  {
304  $m = $this->object->QTIMaterialToString($material);
305  $feedbacks[$ident] = $m;
306  }
307  foreach ($feedbacksgeneric as $correctness => $material)
308  {
309  $m = $this->object->QTIMaterialToString($material);
310  $feedbacksgeneric[$correctness] = $m;
311  }
312  // handle the import of media objects in XHTML code
313  $questiontext = $this->object->getQuestion();
314  $answers =& $this->object->getAnswers();
315  if (is_array($_SESSION["import_mob_xhtml"]))
316  {
317  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
318  include_once "./Services/RTE/classes/class.ilRTE.php";
319  foreach ($_SESSION["import_mob_xhtml"] as $mob)
320  {
321  if ($tst_id > 0)
322  {
323  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
324  }
325  else
326  {
327  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
328  }
329 
330  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
331 
332  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
333  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
334  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
335  foreach ($answers as $key => $value)
336  {
337  $answer_obj = $answers[$key];
338  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
339  }
340  foreach ($feedbacks as $ident => $material)
341  {
342  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
343  }
344  foreach ($feedbacksgeneric as $correctness => $material)
345  {
346  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
347  }
348  }
349  }
350  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
351  foreach ($answers as $key => $value)
352  {
353  $answer_obj =& $answers[$key];
354  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
355  }
356  foreach ($feedbacks as $ident => $material)
357  {
358  $this->object->saveFeedbackSingleAnswer($ident, ilRTE::_replaceMediaObjectImageSrc($material, 1));
359  }
360  foreach ($feedbacksgeneric as $correctness => $material)
361  {
362  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
363  }
364  $this->object->saveToDb();
365  if (count($item->suggested_solutions))
366  {
367  foreach ($item->suggested_solutions as $suggested_solution)
368  {
369  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
370  }
371  $this->object->saveToDb();
372  }
373  if ($tst_id > 0)
374  {
375  $q_1_id = $this->object->getId();
376  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
377  $tst_object->questions[$question_counter++] = $question_id;
378  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
379  }
380  else
381  {
382  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
383  }
384  }
385 }
386 
387 ?>