ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assOrderingQuestionImport.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  $foundimage = FALSE;
61  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
62  $answers = array();
63  foreach ($presentation->order as $entry)
64  {
65  switch ($entry["type"])
66  {
67  case "response":
68  $response = $presentation->response[$entry["index"]];
69  $rendertype = $response->getRenderType();
70  switch (strtolower(get_class($rendertype)))
71  {
72  case "ilqtirenderchoice":
73  $shuffle = $rendertype->getShuffle();
74  $answerorder = 0;
75  foreach ($rendertype->response_labels as $response_label)
76  {
77  $ident = $response_label->getIdent();
78  $answertext = "";
79  foreach ($response_label->material as $mat)
80  {
81  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
82  {
83  $foundmat = $mat->getMaterial($m);
84  if (strcmp($foundmat["type"], "mattext") == 0)
85  {
86  $answertext .= $foundmat["material"]->getContent();
87  }
88  if (strcmp($foundmat["type"], "matimage") == 0)
89  {
90  $foundimage = TRUE;
91  $answerimage = array(
92  "imagetype" => $foundmat["material"]->getImageType(),
93  "label" => $foundmat["material"]->getLabel(),
94  "content" => $foundmat["material"]->getContent()
95  );
96  }
97  }
98  }
99  $answers[$ident] = array(
100  "answertext" => $answertext,
101  "answerimage" => $answerimage,
102  "points" => 0,
103  "answerorder" => $answerorder++,
104  "correctness" => "",
105  "action" => ""
106  );
107  }
108  break;
109  }
110  break;
111  }
112  }
113  $responses = array();
114  $feedbacksgeneric = array();
115  foreach ($item->resprocessing as $resprocessing)
116  {
117  foreach ($resprocessing->respcondition as $respcondition)
118  {
119  $ident = "";
120  $correctness = 1;
121  $conditionvar = $respcondition->getConditionvar();
122  foreach ($conditionvar->order as $order)
123  {
124  switch ($order["field"])
125  {
126  case "arr_not":
127  $correctness = 0;
128  break;
129  case "varequal":
130  $ident = $conditionvar->varequal[$order["index"]]->getContent();
131  $orderindex = $conditionvar->varequal[$order["index"]]->getIndex();
132  break;
133  }
134  }
135  foreach ($respcondition->setvar as $setvar)
136  {
137  if (strcmp($ident, "") != 0)
138  {
139  $answers[$ident]["solutionorder"] = $orderindex;
140  $answers[$ident]["action"] = $setvar->getAction();
141  $answers[$ident]["points"] = $setvar->getContent();
142  }
143  }
144  if (count($respcondition->displayfeedback))
145  {
146  foreach ($respcondition->displayfeedback as $feedbackpointer)
147  {
148  if (strlen($feedbackpointer->getLinkrefid()))
149  {
150  foreach ($item->itemfeedback as $ifb)
151  {
152  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
153  {
154  // found a feedback for the identifier
155  if (count($ifb->material))
156  {
157  foreach ($ifb->material as $material)
158  {
159  $feedbacksgeneric[1] = $material;
160  }
161  }
162  if ((count($ifb->flow_mat) > 0))
163  {
164  foreach ($ifb->flow_mat as $fmat)
165  {
166  if (count($fmat->material))
167  {
168  foreach ($fmat->material as $material)
169  {
170  $feedbacksgeneric[1] = $material;
171  }
172  }
173  }
174  }
175  }
176  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
177  {
178  // found a feedback for the identifier
179  if (count($ifb->material))
180  {
181  foreach ($ifb->material as $material)
182  {
183  $feedbacksgeneric[0] = $material;
184  }
185  }
186  if ((count($ifb->flow_mat) > 0))
187  {
188  foreach ($ifb->flow_mat as $fmat)
189  {
190  if (count($fmat->material))
191  {
192  foreach ($fmat->material as $material)
193  {
194  $feedbacksgeneric[0] = $material;
195  }
196  }
197  }
198  }
199  }
200  }
201  }
202  }
203  }
204  }
205  }
206  $type = 1; // terms
207  if ($foundimage)
208  {
209  $type = 0; // pictures
210  }
211  $this->object->setTitle($item->getTitle());
212  $this->object->setNrOfTries($item->getMaxattempts());
213  $this->object->setComment($item->getComment());
214  $this->object->setAuthor($item->getAuthor());
215  $this->object->setOwner($ilUser->getId());
216  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
217  $this->object->setOrderingType($type);
218  $this->object->setObjId($questionpool_id);
219  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
220  $this->object->setElementHeight($item->getMetadataEntry("element_height"));
221  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
222  $this->object->setShuffle($shuffle);
223  $points = 0;
224  $solanswers = array();
225  foreach ($answers as $answer)
226  {
227  $solanswers[$answer["solutionorder"]] = $answer;
228  }
229  ksort($solanswers);
230  foreach ($solanswers as $answer)
231  {
232  $points += $answer["points"];
233  if ($type == 1)
234  {
235  $this->object->addAnswer($answer["answertext"]);
236  }
237  else
238  {
239  $this->object->addAnswer($answer["answerimage"]["label"]);
240  }
241  }
242  $points = ($item->getMetadataEntry("points") > 0) ? $item->getMetadataEntry("points") : $points;
243  $this->object->setPoints($points);
244  $this->object->saveToDb();
245  if (count($item->suggested_solutions))
246  {
247  foreach ($item->suggested_solutions as $suggested_solution)
248  {
249  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
250  }
251  $this->object->saveToDb();
252  }
253  foreach ($answers as $answer)
254  {
255  if ($type == 0)
256  {
257  include_once "./Services/Utilities/classes/class.ilUtil.php";
258  $image =& base64_decode($answer["answerimage"]["content"]);
259  $imagepath = $this->object->getImagePath();
260  if (!file_exists($imagepath))
261  {
262  ilUtil::makeDirParents($imagepath);
263  }
264  $imagepath .= $answer["answerimage"]["label"];
265  $fh = fopen($imagepath, "wb");
266  if ($fh == false)
267  {
268 // global $ilErr;
269 // $ilErr->raiseError($this->object->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->MESSAGE);
270 // return;
271  }
272  else
273  {
274  $imagefile = fwrite($fh, $image);
275  fclose($fh);
276  }
277  // create thumbnail file
278  $thumbpath = $imagepath . "." . "thumb.jpg";
279  ilUtil::convertImage($imagepath, $thumbpath, "JPEG", $this->object->getThumbGeometry());
280  }
281  }
282  foreach ($feedbacksgeneric as $correctness => $material)
283  {
284  $m = $this->object->QTIMaterialToString($material);
285  $feedbacksgeneric[$correctness] = $m;
286  }
287  $questiontext = $this->object->getQuestion();
288  $answers =& $this->object->getAnswers();
289  // handle the import of media objects in XHTML code
290  if (is_array($_SESSION["import_mob_xhtml"]))
291  {
292  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
293  include_once "./Services/RTE/classes/class.ilRTE.php";
294  foreach ($_SESSION["import_mob_xhtml"] as $mob)
295  {
296  if ($tst_id > 0)
297  {
298  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
299  }
300  else
301  {
302  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
303  }
304 
305  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
306 
307  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
308  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
309  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
310  foreach ($answers as $key => $value)
311  {
312  $answer_obj =& $answers[$key];
313  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
314  }
315  foreach ($feedbacksgeneric as $correctness => $material)
316  {
317  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
318  }
319  }
320  }
321  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
322  foreach ($answers as $key => $value)
323  {
324  $answer_obj =& $answers[$key];
325  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
326  }
327  foreach ($feedbacksgeneric as $correctness => $material)
328  {
329  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
330  }
331  $this->object->saveToDb();
332  if ($tst_id > 0)
333  {
334  $q_1_id = $this->object->getId();
335  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
336  $tst_object->questions[$question_counter++] = $question_id;
337  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
338  }
339  else
340  {
341  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
342  }
343  }
344 }
345 
346 ?>