ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMultipleChoiceImport.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  // empty session variable for imported xhtml mobs
54  unset($_SESSION["import_mob_xhtml"]);
55  $presentation = $item->getPresentation();
56  $duration = $item->getDuration();
57  $shuffle = 0;
58  $now = getdate();
59  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
60  $answers = array();
61  foreach ($presentation->order as $entry)
62  {
63  switch ($entry["type"])
64  {
65  case "response":
66  $response = $presentation->response[$entry["index"]];
67  $rendertype = $response->getRenderType();
68  switch (strtolower(get_class($response->getRenderType())))
69  {
70  case "ilqtirenderchoice":
71  $shuffle = $rendertype->getShuffle();
72  $answerorder = 0;
73  $foundimage = FALSE;
74  foreach ($rendertype->response_labels as $response_label)
75  {
76  $ident = $response_label->getIdent();
77  $answertext = "";
78  $answerimage = array();
79  foreach ($response_label->material as $mat)
80  {
81  $embedded = false;
82  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
83  {
84  $foundmat = $mat->getMaterial($m);
85  if (strcmp($foundmat["type"], "mattext") == 0)
86  {
87  }
88  if (strcmp($foundmat["type"], "matimage") == 0)
89  {
90  if (strlen($foundmat["material"]->getEmbedded()))
91  {
92  $embedded = true;
93  }
94  }
95  }
96  if ($embedded)
97  {
98  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
99  {
100  $foundmat = $mat->getMaterial($m);
101  if (strcmp($foundmat["type"], "mattext") == 0)
102  {
103  $answertext .= $foundmat["material"]->getContent();
104  }
105  if (strcmp($foundmat["type"], "matimage") == 0)
106  {
107  $foundimage = TRUE;
108  $answerimage = array(
109  "imagetype" => $foundmat["material"]->getImageType(),
110  "label" => $foundmat["material"]->getLabel(),
111  "content" => $foundmat["material"]->getContent()
112  );
113  }
114  }
115  }
116  else
117  {
118  $answertext = $this->object->QTIMaterialToString($mat);
119  }
120  }
121  $answers[$ident] = array(
122  "answertext" => $answertext,
123  "imagefile" => $answerimage,
124  "points" => 0,
125  "answerorder" => $answerorder++,
126  "points_unchecked" => 0,
127  "action" => ""
128  );
129  }
130  break;
131  }
132  break;
133  }
134  }
135  $responses = array();
136  $feedbacks = array();
137  $feedbacksgeneric = array();
138  foreach ($item->resprocessing as $resprocessing)
139  {
140  foreach ($resprocessing->respcondition as $respcondition)
141  {
142  $ident = "";
143  $correctness = 1;
144  $conditionvar = $respcondition->getConditionvar();
145  foreach ($conditionvar->order as $order)
146  {
147  switch ($order["field"])
148  {
149  case "arr_not":
150  $correctness = 0;
151  break;
152  case "varequal":
153  $ident = $conditionvar->varequal[$order["index"]]->getContent();
154  break;
155  }
156  }
157  foreach ($respcondition->setvar as $setvar)
158  {
159  if (strcmp($ident, "") != 0)
160  {
161  if ($correctness)
162  {
163  $answers[$ident]["action"] = $setvar->getAction();
164  $answers[$ident]["points"] = $setvar->getContent();
165  if (count($respcondition->displayfeedback))
166  {
167  foreach ($respcondition->displayfeedback as $feedbackpointer)
168  {
169  if (strlen($feedbackpointer->getLinkrefid()))
170  {
171  foreach ($item->itemfeedback as $ifb)
172  {
173  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
174  {
175  // found a feedback for the identifier
176  if (count($ifb->material))
177  {
178  foreach ($ifb->material as $material)
179  {
180  $feedbacksgeneric[1] = $material;
181  }
182  }
183  if ((count($ifb->flow_mat) > 0))
184  {
185  foreach ($ifb->flow_mat as $fmat)
186  {
187  if (count($fmat->material))
188  {
189  foreach ($fmat->material as $material)
190  {
191  $feedbacksgeneric[1] = $material;
192  }
193  }
194  }
195  }
196  }
197  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
198  {
199  // found a feedback for the identifier
200  if (count($ifb->material))
201  {
202  foreach ($ifb->material as $material)
203  {
204  $feedbacksgeneric[0] = $material;
205  }
206  }
207  if ((count($ifb->flow_mat) > 0))
208  {
209  foreach ($ifb->flow_mat as $fmat)
210  {
211  if (count($fmat->material))
212  {
213  foreach ($fmat->material as $material)
214  {
215  $feedbacksgeneric[0] = $material;
216  }
217  }
218  }
219  }
220  }
221  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0)
222  {
223  // found a feedback for the identifier
224  if (count($ifb->material))
225  {
226  foreach ($ifb->material as $material)
227  {
228  $feedbacks[$ident] = $material;
229  }
230  }
231  if ((count($ifb->flow_mat) > 0))
232  {
233  foreach ($ifb->flow_mat as $fmat)
234  {
235  if (count($fmat->material))
236  {
237  foreach ($fmat->material as $material)
238  {
239  $feedbacks[$ident] = $material;
240  }
241  }
242  }
243  }
244  }
245  }
246  }
247  }
248  }
249  }
250  else
251  {
252  $answers[$ident]["action"] = $setvar->getAction();
253  $answers[$ident]["points_unchecked"] = $setvar->getContent();
254  }
255  }
256  }
257  }
258  }
259  $this->object->setTitle($item->getTitle());
260  $this->object->setExternalID($item->getMetadataEntry("externalID"));
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["points_unchecked"], $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  // handle the import of media objects in XHTML code
303  foreach ($feedbacks as $ident => $material)
304  {
305  $m = $this->object->QTIMaterialToString($material);
306  $feedbacks[$ident] = $m;
307  }
308  foreach ($feedbacksgeneric as $correctness => $material)
309  {
310  $m = $this->object->QTIMaterialToString($material);
311  $feedbacksgeneric[$correctness] = $m;
312  }
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  include_once "./Modules/Test/classes/class.ilObjTest.php";
324  $importfile = ilObjTest::_getImportDirectory() . "/" . $mob["uri"];
325  }
326  else
327  {
328  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
329  $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
330  }
331  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
332  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
333  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
334  foreach ($answers as $key => $value)
335  {
336  $answer_obj =& $answers[$key];
337  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
338  }
339  foreach ($feedbacks as $ident => $material)
340  {
341  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
342  }
343  foreach ($feedbacksgeneric as $correctness => $material)
344  {
345  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
346  }
347  }
348  }
349  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
350  foreach ($answers as $key => $value)
351  {
352  $answer_obj =& $answers[$key];
353  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
354  }
355  foreach ($feedbacks as $ident => $material)
356  {
357  $this->object->saveFeedbackSingleAnswer($ident, ilRTE::_replaceMediaObjectImageSrc($material, 1));
358  }
359  foreach ($feedbacksgeneric as $correctness => $material)
360  {
361  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
362  }
363  $this->object->saveToDb();
364  if (count($item->suggested_solutions))
365  {
366  foreach ($item->suggested_solutions as $suggested_solution)
367  {
368  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
369  }
370  $this->object->saveToDb();
371  }
372  if ($tst_id > 0)
373  {
374  $q_1_id = $this->object->getId();
375  $question_id = $this->object->duplicate(true);
376  $tst_object->questions[$question_counter++] = $question_id;
377  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
378  }
379  else
380  {
381  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
382  }
383  //$ilLog->write(strftime("%D %T") . ": finished import multiple choice question (single response)");
384  }
385 }
386 
387 ?>