ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assClozeTestImport.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  $questiontext = array();
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  $gaps = array();
63  foreach ($presentation->order as $entry)
64  {
65  switch ($entry["type"])
66  {
67  case "material":
68 
69  $material = $presentation->material[$entry["index"]];
70  array_push($questiontext, $this->object->QTIMaterialToString($material));
71  break;
72  case "response":
73  $response = $presentation->response[$entry["index"]];
74  $rendertype = $response->getRenderType();
75  array_push($questiontext, "<<" . $response->getIdent() . ">>");
76  switch (strtolower(get_class($response->getRenderType())))
77  {
78  case "ilqtirenderfib":
79  switch ($response->getRenderType()->getFibtype())
80  {
81  case FIBTYPE_DECIMAL:
82  case FIBTYPE_INTEGER:
83  array_push($gaps,
84  array(
85  "ident" => $response->getIdent(),
86  "type" => CLOZE_NUMERIC,
87  "answers" => array(),
88  "minnumber" => $response->getRenderType()->getMinnumber(),
89  "maxnumber" => $response->getRenderType()->getMaxnumber()
90  )
91  );
92  break;
93  default:
94  case FIBTYPE_STRING:
95  array_push($gaps, array("ident" => $response->getIdent(), "type" => CLOZE_TEXT, "answers" => array()));
96  break;
97  }
98  break;
99  case "ilqtirenderchoice":
100  $answers = array();
101  $shuffle = $rendertype->getShuffle();
102  $answerorder = 0;
103  foreach ($rendertype->response_labels as $response_label)
104  {
105  $ident = $response_label->getIdent();
106  $answertext = "";
107  foreach ($response_label->material as $mat)
108  {
109  $answertext .= $this->object->QTIMaterialToString($mat);
110  }
111  $answers[$ident] = array(
112  "answertext" => $answertext,
113  "points" => 0,
114  "answerorder" => $answerorder++,
115  "action" => "",
116  "shuffle" => $rendertype->getShuffle()
117  );
118  }
119  array_push($gaps, array("ident" => $response->getIdent(), "type" => CLOZE_SELECT, "shuffle" => $rendertype->getShuffle(), "answers" => $answers));
120  break;
121  }
122  break;
123  }
124  }
125  $responses = array();
126  $feedbacks = array();
127  $feedbacksgeneric = array();
128  foreach ($item->resprocessing as $resprocessing)
129  {
130  foreach ($resprocessing->respcondition as $respcondition)
131  {
132  $ident = "";
133  $correctness = 1;
134  $conditionvar = $respcondition->getConditionvar();
135  foreach ($conditionvar->order as $order)
136  {
137  switch ($order["field"])
138  {
139  case "varequal":
140  $equals = $conditionvar->varequal[$order["index"]]->getContent();
141  $gapident = $conditionvar->varequal[$order["index"]]->getRespident();
142  break;
143  }
144  }
145  foreach ($respcondition->setvar as $setvar)
146  {
147  if (strcmp($gapident, "") != 0)
148  {
149  foreach ($gaps as $gi => $g)
150  {
151  if (strcmp($g["ident"], $gapident) == 0)
152  {
153  if ($g["type"] == CLOZE_SELECT)
154  {
155  foreach ($gaps[$gi]["answers"] as $ai => $answer)
156  {
157  if (strcmp($answer["answertext"], $equals) == 0)
158  {
159  $gaps[$gi]["answers"][$ai]["action"] = $setvar->getAction();
160  $gaps[$gi]["answers"][$ai]["points"] = $setvar->getContent();
161  }
162  }
163  }
164  else if ($g["type"] == CLOZE_TEXT)
165  {
166  array_push($gaps[$gi]["answers"], array(
167  "answertext" => $equals,
168  "points" => $setvar->getContent(),
169  "answerorder" => count($gaps[$gi]["answers"]),
170  "action" => $setvar->getAction()
171  ));
172  }
173  else if ($g["type"] == CLOZE_NUMERIC)
174  {
175  array_push($gaps[$gi]["answers"], array(
176  "answertext" => $equals,
177  "points" => $setvar->getContent(),
178  "answerorder" => count($gaps[$gi]["answers"]),
179  "action" => $setvar->getAction()
180  ));
181  }
182  }
183  }
184  }
185  }
186 
187  if (count($respcondition->displayfeedback))
188  {
189  foreach ($respcondition->displayfeedback as $feedbackpointer)
190  {
191  if (strlen($feedbackpointer->getLinkrefid()))
192  {
193  foreach ($item->itemfeedback as $ifb)
194  {
195  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
196  {
197  // found a feedback for the identifier
198  if (count($ifb->material))
199  {
200  foreach ($ifb->material as $material)
201  {
202  $feedbacksgeneric[1] = $material;
203  }
204  }
205  if ((count($ifb->flow_mat) > 0))
206  {
207  foreach ($ifb->flow_mat as $fmat)
208  {
209  if (count($fmat->material))
210  {
211  foreach ($fmat->material as $material)
212  {
213  $feedbacksgeneric[1] = $material;
214  }
215  }
216  }
217  }
218  }
219  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
220  {
221  // found a feedback for the identifier
222  if (count($ifb->material))
223  {
224  foreach ($ifb->material as $material)
225  {
226  $feedbacksgeneric[0] = $material;
227  }
228  }
229  if ((count($ifb->flow_mat) > 0))
230  {
231  foreach ($ifb->flow_mat as $fmat)
232  {
233  if (count($fmat->material))
234  {
235  foreach ($fmat->material as $material)
236  {
237  $feedbacksgeneric[0] = $material;
238  }
239  }
240  }
241  }
242  }
243  }
244  }
245  }
246  }
247 
248  }
249  }
250 
251  $this->object->setTitle($item->getTitle());
252  $this->object->setExternalID($item->getMetadataEntry("externalID"));
253  $this->object->setNrOfTries($item->getMaxattempts());
254  $this->object->setComment($item->getComment());
255  $this->object->setAuthor($item->getAuthor());
256  $this->object->setOwner($ilUser->getId());
257  $this->object->setObjId($questionpool_id);
258  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
259  $textgap_rating = $item->getMetadataEntry("textgaprating");
260  $this->object->setFixedTextLength($item->getMetadataEntry("fixedTextLength"));
261  $this->object->setIdenticalScoring($item->getMetadataEntry("identicalScoring"));
262  if (strlen($textgap_rating) == 0) $textgap_rating = "ci";
263  $this->object->setTextgapRating($textgap_rating);
264  $gaptext = array();
265  foreach ($gaps as $gapidx => $gap)
266  {
267  $gapcontent = array();
268  include_once "./Modules/TestQuestionPool/classes/class.assClozeGap.php";
269  $clozegap = new assClozeGap($gap["type"]);
270  foreach ($gap["answers"] as $index => $answer)
271  {
272  include_once "./Modules/TestQuestionPool/classes/class.assAnswerCloze.php";
273  $gapanswer = new assAnswerCloze($answer["answertext"], $answer["points"], $answer["answerorder"]);
274  switch ($clozegap->getType())
275  {
276  case CLOZE_SELECT:
277  $clozegap->setShuffle($answer["shuffle"]);
278  break;
279  case CLOZE_NUMERIC:
280  $gapanswer->setLowerBound($gap["minnumber"]);
281  $gapanswer->setUpperBound($gap["maxnumber"]);
282  break;
283  }
284  $clozegap->addItem($gapanswer);
285  array_push($gapcontent, $answer["answertext"]);
286  }
287  $this->object->addGapAtIndex($clozegap, $gapidx);
288  $gaptext[$gap["ident"]] = "[gap]" . join(",", $gapcontent). "[/gap]";
289  }
290  $clozetext = join("", $questiontext);
291  foreach ($gaptext as $idx => $val)
292  {
293  $clozetext = str_replace("<<" . $idx . ">>", $val, $clozetext);
294  }
295  $this->object->setQuestion($clozetext);
296  $this->object->saveToDb();
297 
298  // handle the import of media objects in XHTML code
299  foreach ($feedbacks as $ident => $material)
300  {
301  $m = $this->object->QTIMaterialToString($material);
302  $feedbacks[$ident] = $m;
303  }
304  foreach ($feedbacksgeneric as $correctness => $material)
305  {
306  $m = $this->object->QTIMaterialToString($material);
307  $feedbacksgeneric[$correctness] = $m;
308  }
309  $questiontext = $this->object->getQuestion();
310  if (is_array($_SESSION["import_mob_xhtml"]))
311  {
312  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
313  include_once "./Services/RTE/classes/class.ilRTE.php";
314  foreach ($_SESSION["import_mob_xhtml"] as $mob)
315  {
316  if ($tst_id > 0)
317  {
318  include_once "./Modules/Test/classes/class.ilObjTest.php";
319  $importfile = ilObjTest::_getImportDirectory() . "/" . $mob["uri"];
320  }
321  else
322  {
323  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
324  $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
325  }
326  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
327  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
328  foreach ($feedbacks as $ident => $material)
329  {
330  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
331  }
332  foreach ($feedbacksgeneric as $correctness => $material)
333  {
334  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
335  }
336  }
337  }
338  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
339  foreach ($feedbacks as $ident => $material)
340  {
341  //$this->object->saveFeedbackSingleAnswer($ident, ilRTE::_replaceMediaObjectImageSrc($material, 1));
342  }
343  foreach ($feedbacksgeneric as $correctness => $material)
344  {
345  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
346  }
347  $this->object->saveToDb();
348  if (count($item->suggested_solutions))
349  {
350  foreach ($item->suggested_solutions as $suggested_solution)
351  {
352  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
353  }
354  $this->object->saveToDb();
355  }
356  if ($tst_id > 0)
357  {
358  $q_1_id = $this->object->getId();
359  $question_id = $this->object->duplicate(true);
360  $tst_object->questions[$question_counter++] = $question_id;
361  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
362  }
363  else
364  {
365  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
366  }
367  }
368 }
369 
370 ?>