ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assImagemapQuestionImport.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  $now = getdate();
59  $questionimage = array();
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($rendertype)))
70  {
71  case "ilqtirenderhotspot":
72  foreach ($rendertype->material as $mat)
73  {
74  for ($i = 0; $i < $mat->getMaterialCount(); $i++)
75  {
76  $m = $mat->getMaterial($i);
77  if (strcmp($m["type"], "matimage") == 0)
78  {
79  $questionimage = array(
80  "imagetype" => $m["material"]->getImageType(),
81  "label" => $m["material"]->getLabel(),
82  "content" => $m["material"]->getContent()
83  );
84  }
85  }
86  }
87  foreach ($rendertype->response_labels as $response_label)
88  {
89  $ident = $response_label->getIdent();
90  $answerhint = "";
91  foreach ($response_label->material as $mat)
92  {
93  $answerhint .= $this->object->QTIMaterialToString($mat);
94  }
95  $answers[$ident] = array(
96  "answerhint" => $answerhint,
97  "areatype" => $response_label->getRarea(),
98  "coordinates" => $response_label->getContent(),
99  "points" => 0,
100  "answerorder" => $response_label->getIdent(),
101  "correctness" => "1",
102  "action" => ""
103  );
104  }
105  break;
106  }
107  break;
108  }
109  }
110  $responses = array();
111  $feedbacks = array();
112  $feedbacksgeneric = array();
113  foreach ($item->resprocessing as $resprocessing)
114  {
115  foreach ($resprocessing->respcondition as $respcondition)
116  {
117  $coordinates = "";
118  $conditionvar = $respcondition->getConditionvar();
119  foreach ($conditionvar->order as $order)
120  {
121  switch ($order["field"])
122  {
123  case "varinside":
124  $coordinates = $conditionvar->varinside[$order["index"]]->getContent();
125  break;
126  }
127  }
128  foreach ($respcondition->setvar as $setvar)
129  {
130  foreach ($answers as $ident => $answer)
131  {
132  if (strcmp($answer["coordinates"], $coordinates) == 0)
133  {
134  $answers[$ident]["action"] = $setvar->getAction();
135  $answers[$ident]["points"] = $setvar->getContent();
136  if (count($respcondition->displayfeedback))
137  {
138  foreach ($respcondition->displayfeedback as $feedbackpointer)
139  {
140  if (strlen($feedbackpointer->getLinkrefid()))
141  {
142  foreach ($item->itemfeedback as $ifb)
143  {
144  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
145  {
146  // found a feedback for the identifier
147  if (count($ifb->material))
148  {
149  foreach ($ifb->material as $material)
150  {
151  $feedbacksgeneric[1] = $material;
152  }
153  }
154  if ((count($ifb->flow_mat) > 0))
155  {
156  foreach ($ifb->flow_mat as $fmat)
157  {
158  if (count($fmat->material))
159  {
160  foreach ($fmat->material as $material)
161  {
162  $feedbacksgeneric[1] = $material;
163  }
164  }
165  }
166  }
167  }
168  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
169  {
170  // found a feedback for the identifier
171  if (count($ifb->material))
172  {
173  foreach ($ifb->material as $material)
174  {
175  $feedbacksgeneric[0] = $material;
176  }
177  }
178  if ((count($ifb->flow_mat) > 0))
179  {
180  foreach ($ifb->flow_mat as $fmat)
181  {
182  if (count($fmat->material))
183  {
184  foreach ($fmat->material as $material)
185  {
186  $feedbacksgeneric[0] = $material;
187  }
188  }
189  }
190  }
191  }
192  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0)
193  {
194  // found a feedback for the identifier
195  if (count($ifb->material))
196  {
197  foreach ($ifb->material as $material)
198  {
199  $feedbacks[$ident] = $material;
200  }
201  }
202  if ((count($ifb->flow_mat) > 0))
203  {
204  foreach ($ifb->flow_mat as $fmat)
205  {
206  if (count($fmat->material))
207  {
208  foreach ($fmat->material as $material)
209  {
210  $feedbacks[$ident] = $material;
211  }
212  }
213  }
214  }
215  }
216  }
217  }
218  }
219  }
220  }
221  }
222  }
223  }
224  }
225 
226  $this->object->setTitle($item->getTitle());
227  $this->object->setExternalID($item->getMetadataEntry("externalID"));
228  $this->object->setNrOfTries($item->getMaxattempts());
229  $this->object->setComment($item->getComment());
230  $this->object->setAuthor($item->getAuthor());
231  $this->object->setOwner($ilUser->getId());
232  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
233  $this->object->setObjId($questionpool_id);
234  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
235  $areas = array("2" => "rect", "1" => "circle", "3" => "poly");
236  $this->object->setImageFilename($questionimage["label"]);
237  foreach ($answers as $answer)
238  {
239  $this->object->addAnswer($answer["answerhint"], $answer["points"], $answer["answerorder"], $answer["coordinates"], $areas[$answer["areatype"]]);
240  }
241  $this->object->saveToDb();
242  if (count($item->suggested_solutions))
243  {
244  foreach ($item->suggested_solutions as $suggested_solution)
245  {
246  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
247  }
248  $this->object->saveToDb();
249  }
250  $image =& base64_decode($questionimage["content"]);
251  $imagepath = $this->object->getImagePath();
252  if (!file_exists($imagepath))
253  {
254  include_once "./Services/Utilities/classes/class.ilUtil.php";
255  ilUtil::makeDirParents($imagepath);
256  }
257  $imagepath .= $questionimage["label"];
258  $fh = fopen($imagepath, "wb");
259  if ($fh == false)
260  {
261 // global $ilErr;
262 // $ilErr->raiseError($this->object->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->MESSAGE);
263 // return;
264  }
265  else
266  {
267  $imagefile = fwrite($fh, $image);
268  fclose($fh);
269  }
270  // handle the import of media objects in XHTML code
271  foreach ($feedbacks as $ident => $material)
272  {
273  $m = $this->object->QTIMaterialToString($material);
274  $feedbacks[$ident] = $m;
275  }
276  foreach ($feedbacksgeneric as $correctness => $material)
277  {
278  $m = $this->object->QTIMaterialToString($material);
279  $feedbacksgeneric[$correctness] = $m;
280  }
281  $questiontext = $this->object->getQuestion();
282  if (is_array($_SESSION["import_mob_xhtml"]))
283  {
284  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
285  include_once "./Services/RTE/classes/class.ilRTE.php";
286  foreach ($_SESSION["import_mob_xhtml"] as $mob)
287  {
288  if ($tst_id > 0)
289  {
290  include_once "./Modules/Test/classes/class.ilObjTest.php";
291  $importfile = ilObjTest::_getImportDirectory() . "/" . $mob["uri"];
292  }
293  else
294  {
295  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
296  $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
297  }
298  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
299  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
300  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
301  foreach ($feedbacks as $ident => $material)
302  {
303  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
304  }
305  foreach ($feedbacksgeneric as $correctness => $material)
306  {
307  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
308  }
309  }
310  }
311  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
312  foreach ($feedbacks as $ident => $material)
313  {
314  $this->object->saveFeedbackSingleAnswer($ident, ilRTE::_replaceMediaObjectImageSrc($material, 1));
315  }
316  foreach ($feedbacksgeneric as $correctness => $material)
317  {
318  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
319  }
320  $this->object->saveToDb();
321  if ($tst_id > 0)
322  {
323  $q_1_id = $this->object->getId();
324  $question_id = $this->object->duplicate(true);
325  $tst_object->questions[$question_counter++] = $question_id;
326  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
327  }
328  else
329  {
330  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
331  }
332  }
333 }
334 
335 ?>