ILIAS  eassessment Revision 61809
 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->setExternalID($item->getMetadataEntry("externalID"));
213  $this->object->setNrOfTries($item->getMaxattempts());
214  $this->object->setComment($item->getComment());
215  $this->object->setAuthor($item->getAuthor());
216  $this->object->setOwner($ilUser->getId());
217  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
218  $this->object->setOrderingType($type);
219  $this->object->setObjId($questionpool_id);
220  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
221  $this->object->setElementHeight($item->getMetadataEntry("element_height"));
222  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
223  $this->object->setShuffle($shuffle);
224  $points = 0;
225  $solanswers = array();
226  foreach ($answers as $answer)
227  {
228  $solanswers[$answer["solutionorder"]] = $answer;
229  }
230  ksort($solanswers);
231  foreach ($solanswers as $answer)
232  {
233  $points += $answer["points"];
234  if ($type == 1)
235  {
236  $this->object->addAnswer($answer["answertext"]);
237  }
238  else
239  {
240  $this->object->addAnswer($answer["answerimage"]["label"]);
241  }
242  }
243  $points = ($item->getMetadataEntry("points") > 0) ? $item->getMetadataEntry("points") : $points;
244  $this->object->setPoints($points);
245  $this->object->saveToDb();
246  if (count($item->suggested_solutions))
247  {
248  foreach ($item->suggested_solutions as $suggested_solution)
249  {
250  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
251  }
252  $this->object->saveToDb();
253  }
254  foreach ($answers as $answer)
255  {
256  if ($type == 0)
257  {
258  include_once "./Services/Utilities/classes/class.ilUtil.php";
259  $image =& base64_decode($answer["answerimage"]["content"]);
260  $imagepath = $this->object->getImagePath();
261  if (!file_exists($imagepath))
262  {
263  ilUtil::makeDirParents($imagepath);
264  }
265  $imagepath .= $answer["answerimage"]["label"];
266  $fh = fopen($imagepath, "wb");
267  if ($fh == false)
268  {
269 // global $ilErr;
270 // $ilErr->raiseError($this->object->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->MESSAGE);
271 // return;
272  }
273  else
274  {
275  $imagefile = fwrite($fh, $image);
276  fclose($fh);
277  }
278  // create thumbnail file
279  $thumbpath = $imagepath . "." . "thumb.jpg";
280  ilUtil::convertImage($imagepath, $thumbpath, "JPEG", $this->object->getThumbGeometry());
281  }
282  }
283  foreach ($feedbacksgeneric as $correctness => $material)
284  {
285  $m = $this->object->QTIMaterialToString($material);
286  $feedbacksgeneric[$correctness] = $m;
287  }
288  $questiontext = $this->object->getQuestion();
289  $answers =& $this->object->getAnswers();
290  // handle the import of media objects in XHTML code
291  if (is_array($_SESSION["import_mob_xhtml"]))
292  {
293  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
294  include_once "./Services/RTE/classes/class.ilRTE.php";
295  foreach ($_SESSION["import_mob_xhtml"] as $mob)
296  {
297  if ($tst_id > 0)
298  {
299  include_once "./Modules/Test/classes/class.ilObjTest.php";
300  $importfile = ilObjTest::_getImportDirectory() . "/" . $mob["uri"];
301  }
302  else
303  {
304  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
305  $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
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);
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 ?>