ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.assMatchingQuestionImport.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
5 
16 {
17  public function saveImage($data, $filename)
18  {
19  $image =& base64_decode($data);
20  $imagepath = $this->object->getImagePath();
21  include_once "./Services/Utilities/classes/class.ilUtil.php";
22  if (!file_exists($imagepath))
23  {
24  ilUtil::makeDirParents($imagepath);
25  }
26  $imagepath .= $filename;
27  $fh = fopen($imagepath, "wb");
28  if ($fh == false)
29  {
30  }
31  else
32  {
33  $imagefile = fwrite($fh, $image);
34  fclose($fh);
35  }
36  }
37 
51  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
52  {
53  global $ilUser;
54 
55  // empty session variable for imported xhtml mobs
56  unset($_SESSION["import_mob_xhtml"]);
57  $presentation = $item->getPresentation();
58  $duration = $item->getDuration();
59  $shuffle = 0;
60  $now = getdate();
61  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
62  $definitions = array();
63  $terms = array();
64  $foundimage = FALSE;
65  foreach ($presentation->order as $entry)
66  {
67  switch ($entry["type"])
68  {
69  case "response":
70  $response = $presentation->response[$entry["index"]];
71  $rendertype = $response->getRenderType();
72  switch (strtolower(get_class($rendertype)))
73  {
74  case "ilqtirenderchoice":
75  $shuffle = $rendertype->getShuffle();
76  $answerorder = 0;
77  foreach ($rendertype->response_labels as $response_label)
78  {
79  $ident = $response_label->getIdent();
80  $answertext = "";
81  $answerimage = array();
82  foreach ($response_label->material as $mat)
83  {
84  for ($m = 0; $m < $mat->getMaterialCount(); $m++)
85  {
86  $foundmat = $mat->getMaterial($m);
87  if (strcmp($foundmat["type"], "mattext") == 0)
88  {
89  $answertext .= $foundmat["material"]->getContent();
90  }
91  if (strcmp($foundmat["type"], "matimage") == 0)
92  {
93  $foundimage = TRUE;
94  $answerimage = array(
95  "imagetype" => $foundmat["material"]->getImageType(),
96  "label" => $foundmat["material"]->getLabel(),
97  "content" => $foundmat["material"]->getContent()
98  );
99  }
100  }
101  }
102  if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup())))
103  {
104  $definitions[$ident] = array(
105  "answertext" => $answertext,
106  "answerimage" => $answerimage,
107  "points" => 0,
108  "answerorder" => $ident,
109  "action" => ""
110  );
111  }
112  else
113  {
114  $terms[$ident] = array(
115  "term" => $answertext,
116  "answerimage" => $answerimage,
117  "points" => 0,
118  "ident" => $ident,
119  "action" => ""
120  );
121  }
122  }
123  break;
124  }
125  break;
126  }
127  }
128  $responses = array();
129  $feedbacksgeneric = array();
130  foreach ($item->resprocessing as $resprocessing)
131  {
132  foreach ($resprocessing->respcondition as $respcondition)
133  {
134  $subset = array();
135  $correctness = 1;
136  $conditionvar = $respcondition->getConditionvar();
137  foreach ($conditionvar->order as $order)
138  {
139  switch ($order["field"])
140  {
141  case "varsubset":
142  $subset = explode(",", $conditionvar->varsubset[$order["index"]]->getContent());
143  break;
144  }
145  }
146  foreach ($respcondition->setvar as $setvar)
147  {
148  array_push($responses, array("subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()));
149  }
150 
151  if (count($respcondition->displayfeedback))
152  {
153  foreach ($respcondition->displayfeedback as $feedbackpointer)
154  {
155  if (strlen($feedbackpointer->getLinkrefid()))
156  {
157  foreach ($item->itemfeedback as $ifb)
158  {
159  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
160  {
161  // found a feedback for the identifier
162  if (count($ifb->material))
163  {
164  foreach ($ifb->material as $material)
165  {
166  $feedbacksgeneric[1] = $material;
167  }
168  }
169  if ((count($ifb->flow_mat) > 0))
170  {
171  foreach ($ifb->flow_mat as $fmat)
172  {
173  if (count($fmat->material))
174  {
175  foreach ($fmat->material as $material)
176  {
177  $feedbacksgeneric[1] = $material;
178  }
179  }
180  }
181  }
182  }
183  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
184  {
185  // found a feedback for the identifier
186  if (count($ifb->material))
187  {
188  foreach ($ifb->material as $material)
189  {
190  $feedbacksgeneric[0] = $material;
191  }
192  }
193  if ((count($ifb->flow_mat) > 0))
194  {
195  foreach ($ifb->flow_mat as $fmat)
196  {
197  if (count($fmat->material))
198  {
199  foreach ($fmat->material as $material)
200  {
201  $feedbacksgeneric[0] = $material;
202  }
203  }
204  }
205  }
206  }
207  }
208  }
209  }
210  }
211 
212  }
213  }
214 
215  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
216  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
217  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
218  $this->object->createNewQuestion();
219  $this->addGeneralMetadata($item);
220  $this->object->setTitle($item->getTitle());
221  $this->object->setNrOfTries($item->getMaxattempts());
222  $this->object->setComment($item->getComment());
223  $this->object->setAuthor($item->getAuthor());
224  $this->object->setOwner($ilUser->getId());
225  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
226  $this->object->setObjId($questionpool_id);
227  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
228  $extended_shuffle = $item->getMetadataEntry("shuffle");
229  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
230 
231  if( strlen($item->getMetadataEntry('matching_mode')) )
232  {
233  $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
234  }
235  else
236  {
237  $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
238  }
239 
240  // save images
241  foreach ($terms as $term)
242  {
243  if (count($term['answerimage'])) $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
244  }
245  foreach ($definitions as $definition)
246  {
247  if (count($definition['answerimage'])) $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
248  }
249 
250  foreach ($terms as $termindex => $term)
251  {
252  $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
253  }
254  foreach ($definitions as $definitionindex => $definition)
255  {
256  $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
257  }
258 
259  if (strlen($extended_shuffle) > 0)
260  {
261  $shuffle = $extended_shuffle;
262  }
263  $this->object->setShuffle($shuffle);
264 
265  foreach ($responses as $response)
266  {
267  $subset = $response["subset"];
268  foreach ($subset as $ident)
269  {
270  if (array_key_exists($ident, $definitions))
271  {
272  $definition = $definitions[$ident];
273  }
274  if (array_key_exists($ident, $terms))
275  {
276  $term = $terms[$ident];
277  }
278  }
279  $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
280  }
281  // additional content editing mode information
282  $this->object->setAdditionalContentEditingMode(
284  );
285  $this->object->saveToDb();
286  if (count($item->suggested_solutions))
287  {
288  foreach ($item->suggested_solutions as $suggested_solution)
289  {
290  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
291  }
292  $this->object->saveToDb();
293  }
294  foreach ($responses as $response)
295  {
296  $subset = $response["subset"];
297  foreach ($subset as $ident)
298  {
299  if (array_key_exists($ident, $definitions))
300  {
301  $definition = $definitions[$ident];
302  }
303  if (array_key_exists($ident, $terms))
304  {
305  $term = $terms[$ident];
306  }
307  }
308  }
309 
310  foreach ($feedbacksgeneric as $correctness => $material)
311  {
312  $m = $this->object->QTIMaterialToString($material);
313  $feedbacksgeneric[$correctness] = $m;
314  }
315 
316  $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
317 
318  // handle the import of media objects in XHTML code
319  $questiontext = $this->object->getQuestion();
320  if (is_array($_SESSION["import_mob_xhtml"]))
321  {
322  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
323  include_once "./Services/RTE/classes/class.ilRTE.php";
324  foreach ($_SESSION["import_mob_xhtml"] as $mob)
325  {
326  if ($tst_id > 0)
327  {
328  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
329  }
330  else
331  {
332  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
333  }
334 
335  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
336 
337  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
338  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
339  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
340  foreach ($feedbacks as $ident => $material)
341  {
342  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
343  }
344  foreach ($feedbacksgeneric as $correctness => $material)
345  {
346  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
347  }
348  }
349  }
350  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
351  foreach ($feedbacks as $ident => $material)
352  {
353  $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
354 
355  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
356  $this->object->getId(), $index, ilRTE::_replaceMediaObjectImageSrc($material, 1)
357  );
358  }
359  foreach ($feedbacksgeneric as $correctness => $material)
360  {
361  $this->object->feedbackOBJ->importGenericFeedback(
362  $this->object->getId(), $correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1)
363  );
364  }
365  $this->object->saveToDb();
366  if ($tst_id > 0)
367  {
368  $q_1_id = $this->object->getId();
369  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
370  $tst_object->questions[$question_counter++] = $question_id;
371  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
372  }
373  else
374  {
375  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
376  }
377  }
378 
384  protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_')
385  {
386  list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
387 
388  foreach($this->object->getMatchingPairs() as $index => $pair)
389  {
390  /* @var assAnswerMatchingPair $pair */
391 
392  if( $pair->term->identifier != $termId)
393  {
394  continue;
395  }
396 
397  if( $pair->definition->identifier != $definitionId)
398  {
399  continue;
400  }
401 
402  return (int)$index;
403  }
404 
405  return -1;
406  }
407 }
408 
409 ?>
Class for matching question terms.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
addGeneralMetadata(ilQTIItem $item)
$_SESSION["AccountId"]
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
Creates a question from a QTI file.
Class for matching question imports.
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
Create new PHPExcel object
obj_idprivate
Class for matching question definitions.
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
Class for question imports.
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.