ILIAS  eassessment Revision 61809
 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->setExternalID($item->getIdent());
241  $this->object->setNrOfTries($item->getMaxattempts());
242  $this->object->setComment($item->getComment());
243  $this->object->setAuthor($item->getAuthor());
244  $this->object->setOwner($ilUser->getId());
245  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
246  $this->object->setObjId($questionpool_id);
247  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
248  $extended_shuffle = $item->getMetadataEntry("shuffle");
249  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
250  $this->object->setElementHeight($item->getMetadataEntry("element_height"));
251 
252  // save images
253  foreach ($terms as $term)
254  {
255  if (count($term['answerimage'])) $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
256  }
257  foreach ($definitions as $definition)
258  {
259  if (count($definition['answerimage'])) $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
260  }
261 
262  foreach ($terms as $termindex => $term)
263  {
264  $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
265  }
266  foreach ($definitions as $definitionindex => $definition)
267  {
268  $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
269  }
270 
271  if (strlen($extended_shuffle) > 0)
272  {
273  $shuffle = $extended_shuffle;
274  }
275  $this->object->setShuffle($shuffle);
276 
277  foreach ($responses as $response)
278  {
279  $subset = $response["subset"];
280  foreach ($subset as $ident)
281  {
282  if (array_key_exists($ident, $definitions))
283  {
284  $definition = $definitions[$ident];
285  }
286  if (array_key_exists($ident, $terms))
287  {
288  $term = $terms[$ident];
289  }
290  }
291  $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
292  }
293  $this->object->saveToDb();
294  if (count($item->suggested_solutions))
295  {
296  foreach ($item->suggested_solutions as $suggested_solution)
297  {
298  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
299  }
300  $this->object->saveToDb();
301  }
302  foreach ($responses as $response)
303  {
304  $subset = $response["subset"];
305  foreach ($subset as $ident)
306  {
307  if (array_key_exists($ident, $definitions))
308  {
309  $definition = $definitions[$ident];
310  }
311  if (array_key_exists($ident, $terms))
312  {
313  $term = $terms[$ident];
314  }
315  }
316  }
317 
318  foreach ($feedbacksgeneric as $correctness => $material)
319  {
320  $m = $this->object->QTIMaterialToString($material);
321  $feedbacksgeneric[$correctness] = $m;
322  }
323 
324  // handle the import of media objects in XHTML code
325  $questiontext = $this->object->getQuestion();
326  if (is_array($_SESSION["import_mob_xhtml"]))
327  {
328  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
329  include_once "./Services/RTE/classes/class.ilRTE.php";
330  foreach ($_SESSION["import_mob_xhtml"] as $mob)
331  {
332  if ($tst_id > 0)
333  {
334  include_once "./Modules/Test/classes/class.ilObjTest.php";
335  $importfile = ilObjTest::_getImportDirectory() . "/" . $mob["uri"];
336  }
337  else
338  {
339  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
340  $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
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);
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  $this->safeTempToDB();
370 
371  }
372 
373  //Writes the old imported Term-ID into a temporarely column of qpl_a_mterm for later use to update the imported
374  //answers with the new Term-IDs to make shure that importd Tests are handled correctly
375  private function safeTempToDB()
376  {
377  global $ilDB;
378 
379  $oldIDExists = false;
380  //Get the columns of qpl_a_mterm an check if the column 'old_id' already exists
381  $checkResult = $ilDB->query("SHOW COLUMNS FROM qpl_a_mterm");
382  if ($checkResult->numRows() > 0)
383  {
384  while ($data = $ilDB->fetchAssoc($checkResult))
385  {
386  if($data['field']=='old_id')
387  {
388  $oldIDExists=true;
389  }
390  }
391  }
392  // If it does exist, get rid of it!
393 // if($oldIDExists)
394 // {
395 // $ilDB->dropTableColumn("qpl_a_mterm", "old_id");
396 // }
397  //Now we create the temporal column to hold the old Term-IDs
398  if(!$oldIDExists)
399  {
400  $ilDB->addTableColumn("qpl_a_mterm", "old_id",
401  array("type" => "integer", "length" => 4));
402  }
403  //Get the data stored within the table for the current question
404  $changeResult = $ilDB->queryF("SELECT * FROM qpl_a_mterm WHERE question_fi = %s ORDER BY term_id ASC",
405  array('integer'),
406  array($this->object->id)
407  );
408  //Write for each retrieved row the corresponding old ID from the masterTermIDs Array into the new column
409  $newTerms=array();
410  if ($changeResult->numRows() > 0)
411  {
412  while ($data = $ilDB->fetchAssoc($changeResult))
413  {
414  $data['old_id']=array_search($data['term_id'], $this->object->masterTermIDs);
415  array_push($newTerms, $data);
416  }
417  foreach ($newTerms as $singleTerm)
418  {
419 
420  $ilDB->update("qpl_a_mterm", array(
421  "old_id" =>array("integer", $singleTerm['old_id'])),
422  array(
423  "term_id" =>array("integer", $singleTerm['term_id']),
424  "question_fi" =>array("integer", $singleTerm['question_fi'])
425  ));
426  }
427  }
428  }
429 
430 }
431 
432 ?>