ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMatchingQuestionImport.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 {
37  public function saveImage($data, $filename)
38  {
39  $image =& base64_decode($data);
40  $imagepath = $this->object->getImagePath();
41  include_once "./Services/Utilities/classes/class.ilUtil.php";
42  if (!file_exists($imagepath))
43  {
44  ilUtil::makeDirParents($imagepath);
45  }
46  $imagepath .= $filename;
47  $fh = fopen($imagepath, "wb");
48  if ($fh == false)
49  {
50  }
51  else
52  {
53  $imagefile = fwrite($fh, $image);
54  fclose($fh);
55  }
56  }
57 
71  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
72  {
73  global $ilUser;
74 
75  // empty session variable for imported xhtml mobs
76  unset($_SESSION["import_mob_xhtml"]);
77  $presentation = $item->getPresentation();
78  $duration = $item->getDuration();
79  $shuffle = 0;
80  $now = getdate();
81  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
82  $definitions = array();
83  $terms = array();
84  $foundimage = FALSE;
85  foreach ($presentation->order as $entry)
86  {
87  switch ($entry["type"])
88  {
89  case "response":
90  $response = $presentation->response[$entry["index"]];
91  $rendertype = $response->getRenderType();
92  switch (strtolower(get_class($rendertype)))
93  {
94  case "ilqtirenderchoice":
95  $shuffle = $rendertype->getShuffle();
96  $answerorder = 0;
97  foreach ($rendertype->response_labels as $response_label)
98  {
99  $ident = $response_label->getIdent();
100  $answertext = "";
101  $answerimage = array();
102  foreach ($response_label->material as $mat)
103  {
104  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
105  {
106  $foundmat = $mat->getMaterial($m);
107  if (strcmp($foundmat["type"], "mattext") == 0)
108  {
109  $answertext .= $foundmat["material"]->getContent();
110  }
111  if (strcmp($foundmat["type"], "matimage") == 0)
112  {
113  $foundimage = TRUE;
114  $answerimage = array(
115  "imagetype" => $foundmat["material"]->getImageType(),
116  "label" => $foundmat["material"]->getLabel(),
117  "content" => $foundmat["material"]->getContent()
118  );
119  }
120  }
121  }
122  if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup())))
123  {
124  $definitions[$ident] = array(
125  "answertext" => $answertext,
126  "answerimage" => $answerimage,
127  "points" => 0,
128  "answerorder" => $ident,
129  "action" => ""
130  );
131  }
132  else
133  {
134  $terms[$ident] = array(
135  "term" => $answertext,
136  "answerimage" => $answerimage,
137  "points" => 0,
138  "ident" => $ident,
139  "action" => ""
140  );
141  }
142  }
143  break;
144  }
145  break;
146  }
147  }
148  $responses = array();
149  $feedbacksgeneric = array();
150  foreach ($item->resprocessing as $resprocessing)
151  {
152  foreach ($resprocessing->respcondition as $respcondition)
153  {
154  $subset = array();
155  $correctness = 1;
156  $conditionvar = $respcondition->getConditionvar();
157  foreach ($conditionvar->order as $order)
158  {
159  switch ($order["field"])
160  {
161  case "varsubset":
162  $subset = split(",", $conditionvar->varsubset[$order["index"]]->getContent());
163  break;
164  }
165  }
166  foreach ($respcondition->setvar as $setvar)
167  {
168  array_push($responses, array("subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()));
169  }
170 
171  if (count($respcondition->displayfeedback))
172  {
173  foreach ($respcondition->displayfeedback as $feedbackpointer)
174  {
175  if (strlen($feedbackpointer->getLinkrefid()))
176  {
177  foreach ($item->itemfeedback as $ifb)
178  {
179  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
180  {
181  // found a feedback for the identifier
182  if (count($ifb->material))
183  {
184  foreach ($ifb->material as $material)
185  {
186  $feedbacksgeneric[1] = $material;
187  }
188  }
189  if ((count($ifb->flow_mat) > 0))
190  {
191  foreach ($ifb->flow_mat as $fmat)
192  {
193  if (count($fmat->material))
194  {
195  foreach ($fmat->material as $material)
196  {
197  $feedbacksgeneric[1] = $material;
198  }
199  }
200  }
201  }
202  }
203  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
204  {
205  // found a feedback for the identifier
206  if (count($ifb->material))
207  {
208  foreach ($ifb->material as $material)
209  {
210  $feedbacksgeneric[0] = $material;
211  }
212  }
213  if ((count($ifb->flow_mat) > 0))
214  {
215  foreach ($ifb->flow_mat as $fmat)
216  {
217  if (count($fmat->material))
218  {
219  foreach ($fmat->material as $material)
220  {
221  $feedbacksgeneric[0] = $material;
222  }
223  }
224  }
225  }
226  }
227  }
228  }
229  }
230  }
231 
232  }
233  }
234 
235  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
236  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
237  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
238  $this->object->createNewQuestion();
239  $this->object->setTitle($item->getTitle());
240  $this->object->setNrOfTries($item->getMaxattempts());
241  $this->object->setComment($item->getComment());
242  $this->object->setAuthor($item->getAuthor());
243  $this->object->setOwner($ilUser->getId());
244  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
245  $this->object->setObjId($questionpool_id);
246  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
247  $extended_shuffle = $item->getMetadataEntry("shuffle");
248  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
249  $this->object->setElementHeight($item->getMetadataEntry("element_height"));
250 
251  // save images
252  foreach ($terms as $term)
253  {
254  if (count($term['answerimage'])) $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
255  }
256  foreach ($definitions as $definition)
257  {
258  if (count($definition['answerimage'])) $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
259  }
260 
261  foreach ($terms as $termindex => $term)
262  {
263  $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
264  }
265  foreach ($definitions as $definitionindex => $definition)
266  {
267  $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
268  }
269 
270  if (strlen($extended_shuffle) > 0)
271  {
272  $shuffle = $extended_shuffle;
273  }
274  $this->object->setShuffle($shuffle);
275 
276  foreach ($responses as $response)
277  {
278  $subset = $response["subset"];
279  foreach ($subset as $ident)
280  {
281  if (array_key_exists($ident, $definitions))
282  {
283  $definition = $definitions[$ident];
284  }
285  if (array_key_exists($ident, $terms))
286  {
287  $term = $terms[$ident];
288  }
289  }
290  $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
291  }
292  $this->object->saveToDb();
293  if (count($item->suggested_solutions))
294  {
295  foreach ($item->suggested_solutions as $suggested_solution)
296  {
297  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
298  }
299  $this->object->saveToDb();
300  }
301  foreach ($responses as $response)
302  {
303  $subset = $response["subset"];
304  foreach ($subset as $ident)
305  {
306  if (array_key_exists($ident, $definitions))
307  {
308  $definition = $definitions[$ident];
309  }
310  if (array_key_exists($ident, $terms))
311  {
312  $term = $terms[$ident];
313  }
314  }
315  }
316 
317  foreach ($feedbacksgeneric as $correctness => $material)
318  {
319  $m = $this->object->QTIMaterialToString($material);
320  $feedbacksgeneric[$correctness] = $m;
321  }
322 
323  // handle the import of media objects in XHTML code
324  $questiontext = $this->object->getQuestion();
325  if (is_array($_SESSION["import_mob_xhtml"]))
326  {
327  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
328  include_once "./Services/RTE/classes/class.ilRTE.php";
329  foreach ($_SESSION["import_mob_xhtml"] as $mob)
330  {
331  if ($tst_id > 0)
332  {
333  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
334  }
335  else
336  {
337  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
338  }
339 
340  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
341 
342  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
343  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
344  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
345  foreach ($feedbacksgeneric as $correctness => $material)
346  {
347  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
348  }
349  }
350  }
351  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
352  foreach ($feedbacksgeneric as $correctness => $material)
353  {
354  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
355  }
356  $this->object->saveToDb();
357  if ($tst_id > 0)
358  {
359  $q_1_id = $this->object->getId();
360  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
361  $tst_object->questions[$question_counter++] = $question_id;
362  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
363  }
364  else
365  {
366  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
367  }
368  }
369 }
370 
371 ?>