ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.assClozeTestImport.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 {
30  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
31  {
32  global $DIC;
33  $ilUser = $DIC['ilUser'];
34 
35  // empty session variable for imported xhtml mobs
36  unset($_SESSION["import_mob_xhtml"]);
37  $presentation = $item->getPresentation();
38  $duration = $item->getDuration();
39 
40  $packageIliasVersion = $item->getIliasSourceVersion('ILIAS_VERSION');
41  $seperate_question_field = $item->getMetadataEntry("question");
42 
43  $questiontext = null;
44  if (!$packageIliasVersion || version_compare($packageIliasVersion, '5.0.0', '<')) {
45  $questiontext = '&nbsp;';
46  } elseif ($seperate_question_field) {
47  $questiontext = $this->processNonAbstractedImageReferences(
48  $seperate_question_field,
49  $item->getIliasSourceNic()
50  );
51  }
52 
53  $clozetext = array();
54  $shuffle = 0;
55  $now = getdate();
56  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
57  $gaps = array();
58  foreach ($presentation->order as $entry) {
59  switch ($entry["type"]) {
60  case "material":
61 
62  $materialString = $this->object->QTIMaterialToString(
63  $presentation->material[$entry["index"]]
64  );
65 
66  if ($questiontext === null) {
67  $questiontext = $materialString;
68  } else {
69  array_push($clozetext, $materialString);
70  }
71 
72  break;
73  case "response":
74  $response = $presentation->response[$entry["index"]];
75  $rendertype = $response->getRenderType();
76  array_push($clozetext, "<<" . $response->getIdent() . ">>");
77 
78  switch (strtolower(get_class($response->getRenderType()))) {
79  case "ilqtirenderfib":
80  switch ($response->getRenderType()->getFibtype()) {
81  case FIBTYPE_DECIMAL:
82  case FIBTYPE_INTEGER:
83  array_push(
84  $gaps,
85  array(
86  "ident" => $response->getIdent(),
87  "type" => CLOZE_NUMERIC,
88  "answers" => array(),
89  "minnumber" => $response->getRenderType()->getMinnumber(),
90  "maxnumber" => $response->getRenderType()->getMaxnumber(),
91  'gap_size' => $response->getRenderType()->getMaxchars()
92  )
93  );
94  break;
95  default:
96  case FIBTYPE_STRING:
97  array_push(
98  $gaps,
99  array("ident" => $response->getIdent(),
100  "type" => CLOZE_TEXT,
101  "answers" => array(),
102  'gap_size' => $response->getRenderType()->getMaxchars()
103  )
104  );
105  break;
106  }
107  break;
108  case "ilqtirenderchoice":
109  $answers = array();
110  $shuffle = $rendertype->getShuffle();
111  $answerorder = 0;
112  foreach ($rendertype->response_labels as $response_label) {
113  $ident = $response_label->getIdent();
114  $answertext = "";
115  foreach ($response_label->material as $mat) {
116  $answertext .= $this->object->QTIMaterialToString($mat);
117  }
118  $answers[$ident] = array(
119  "answertext" => $answertext,
120  "points" => 0,
121  "answerorder" => $answerorder++,
122  "action" => "",
123  "shuffle" => $rendertype->getShuffle()
124  );
125  }
126  array_push($gaps, array("ident" => $response->getIdent(), "type" => CLOZE_SELECT, "shuffle" => $rendertype->getShuffle(), "answers" => $answers));
127  break;
128  }
129  break;
130  }
131  }
132  $responses = array();
133  $feedbacks = array();
134  $feedbacksgeneric = array();
135  foreach ($item->resprocessing as $resprocessing) {
136  foreach ($resprocessing->respcondition as $respcondition) {
137  $ident = "";
138  $correctness = 1;
139  $conditionvar = $respcondition->getConditionvar();
140  foreach ($conditionvar->order as $order) {
141  switch ($order["field"]) {
142  case "varequal":
143  $equals = $conditionvar->varequal[$order["index"]]->getContent();
144  $gapident = $conditionvar->varequal[$order["index"]]->getRespident();
145  break;
146  }
147  }
148  foreach ($respcondition->setvar as $setvar) {
149  if (strcmp($gapident, "") != 0) {
150  foreach ($gaps as $gi => $g) {
151  if (strcmp($g["ident"], $gapident) == 0) {
152  if ($g["type"] == CLOZE_SELECT) {
153  foreach ($gaps[$gi]["answers"] as $ai => $answer) {
154  if (strcmp($answer["answertext"], $equals) == 0) {
155  $gaps[$gi]["answers"][$ai]["action"] = $setvar->getAction();
156  $gaps[$gi]["answers"][$ai]["points"] = $setvar->getContent();
157  }
158  }
159  } elseif ($g["type"] == CLOZE_TEXT) {
160  array_push($gaps[$gi]["answers"], array(
161  "answertext" => $equals,
162  "points" => $setvar->getContent(),
163  "answerorder" => count($gaps[$gi]["answers"]),
164  "action" => $setvar->getAction()
165 
166  ));
167  } elseif ($g["type"] == CLOZE_NUMERIC) {
168  array_push($gaps[$gi]["answers"], array(
169  "answertext" => $equals,
170  "points" => $setvar->getContent(),
171  "answerorder" => count($gaps[$gi]["answers"]),
172  "action" => $setvar->getAction()
173  ));
174  }
175  }
176  }
177  }
178  }
179 
180  if (count($respcondition->displayfeedback)) {
181  foreach ($respcondition->displayfeedback as $feedbackpointer) {
182  if (strlen($feedbackpointer->getLinkrefid())) {
183  foreach ($item->itemfeedback as $ifb) {
184  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
185  // found a feedback for the identifier
186  if (count($ifb->material)) {
187  foreach ($ifb->material as $material) {
188  $feedbacksgeneric[1] = $material;
189  }
190  }
191  if ((count($ifb->flow_mat) > 0)) {
192  foreach ($ifb->flow_mat as $fmat) {
193  if (count($fmat->material)) {
194  foreach ($fmat->material as $material) {
195  $feedbacksgeneric[1] = $material;
196  }
197  }
198  }
199  }
200  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
201  // found a feedback for the identifier
202  if (count($ifb->material)) {
203  foreach ($ifb->material as $material) {
204  $feedbacksgeneric[0] = $material;
205  }
206  }
207  if ((count($ifb->flow_mat) > 0)) {
208  foreach ($ifb->flow_mat as $fmat) {
209  if (count($fmat->material)) {
210  foreach ($fmat->material as $material) {
211  $feedbacksgeneric[0] = $material;
212  }
213  }
214  }
215  }
216  } else {
217  // found a feedback for the identifier
218  if (count($ifb->material)) {
219  foreach ($ifb->material as $material) {
220  $feedbacks[$ifb->getIdent()] = $material;
221  }
222  }
223  if ((count($ifb->flow_mat) > 0)) {
224  foreach ($ifb->flow_mat as $fmat) {
225  if (count($fmat->material)) {
226  foreach ($fmat->material as $material) {
227  $feedbacks[$ifb->getIdent()] = $material;
228  }
229  }
230  }
231  }
232  }
233  }
234  }
235  }
236  }
237  }
238  }
239 
240  $this->addGeneralMetadata($item);
241  $this->object->setTitle($item->getTitle());
242  $this->object->setNrOfTries($item->getMaxattempts());
243  $this->object->setComment($item->getComment());
244  $this->object->setAuthor($item->getAuthor());
245  $this->object->setOwner($ilUser->getId());
246  $this->object->setObjId($questionpool_id);
247  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
248  $textgap_rating = $item->getMetadataEntry("textgaprating");
249  $this->object->setFixedTextLength($item->getMetadataEntry("fixedTextLength"));
250  $this->object->setIdenticalScoring($item->getMetadataEntry("identicalScoring"));
251  $this->object->setFeedbackMode(
252  strlen($item->getMetadataEntry("feedback_mode")) ?
253  $item->getMetadataEntry("feedback_mode") : ilAssClozeTestFeedback::FB_MODE_GAP_QUESTION
254  );
255  $combination = json_decode(base64_decode($item->getMetadataEntry("combinations")));
256  if (strlen($textgap_rating) == 0) {
257  $textgap_rating = "ci";
258  }
259  $this->object->setTextgapRating($textgap_rating);
260  $gaptext = array();
261  foreach ($gaps as $gapidx => $gap) {
262  $gapcontent = array();
263  include_once "./Modules/TestQuestionPool/classes/class.assClozeGap.php";
264  $clozegap = new assClozeGap($gap["type"]);
265  foreach ($gap["answers"] as $index => $answer) {
266  include_once "./Modules/TestQuestionPool/classes/class.assAnswerCloze.php";
267  $gapanswer = new assAnswerCloze($answer["answertext"], $answer["points"], $answer["answerorder"]);
268  $gapanswer->setGapSize($gap["gap_size"]);
269  switch ($clozegap->getType()) {
270  case CLOZE_SELECT:
271  $clozegap->setShuffle($answer["shuffle"]);
272  break;
273  case CLOZE_NUMERIC:
274  $gapanswer->setLowerBound($gap["minnumber"]);
275  $gapanswer->setUpperBound($gap["maxnumber"]);
276  break;
277  }
278  $clozegap->setGapSize($gap["gap_size"]);
279  $clozegap->addItem($gapanswer);
280  array_push($gapcontent, $answer["answertext"]);
281  }
282  $this->object->addGapAtIndex($clozegap, $gapidx);
283  $gaptext[$gap["ident"]] = "[gap]" . join(",", $gapcontent) . "[/gap]";
284  }
285 
286  $this->object->setQuestion($questiontext);
287  $clozetext = join("", $clozetext);
288 
289  foreach ($gaptext as $idx => $val) {
290  $clozetext = str_replace("<<" . $idx . ">>", $val, $clozetext);
291  }
292  $this->object->setClozeTextValue($clozetext);
293 
294  // additional content editing mode information
295  $this->object->setAdditionalContentEditingMode(
297  );
298  $this->object->saveToDb();
299 
300  // handle the import of media objects in XHTML code
301  foreach ($feedbacks as $ident => $material) {
302  $m = $this->object->QTIMaterialToString($material);
303  $feedbacks[$ident] = $m;
304  }
305  foreach ($feedbacksgeneric as $correctness => $material) {
306  $m = $this->object->QTIMaterialToString($material);
307  $feedbacksgeneric[$correctness] = $m;
308  }
309  $questiontext = $this->object->getQuestion();
310  $clozetext = $this->object->getClozeText();
311  if (is_array($_SESSION["import_mob_xhtml"])) {
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  if ($tst_id > 0) {
316  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
317  } else {
318  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
319  }
320  global $DIC; /* @var ILIAS\DI\Container $DIC */
321  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
322 
323  $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
324  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
325  $clozetext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $clozetext);
326  foreach ($feedbacks as $ident => $material) {
327  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
328  }
329  foreach ($feedbacksgeneric as $correctness => $material) {
330  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
331  }
332  }
333  }
334  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
335  $this->object->setClozeTextValue(ilRTE::_replaceMediaObjectImageSrc($clozetext, 1));
336  foreach ($feedbacks as $ident => $material) {
337  $fbIdentifier = $this->buildFeedbackIdentifier($ident);
338  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
339  $this->object->getId(),
340  $fbIdentifier->getQuestionIndex(),
341  $fbIdentifier->getAnswerIndex(),
343  );
344  }
345  foreach ($feedbacksgeneric as $correctness => $material) {
346  $this->object->feedbackOBJ->importGenericFeedback(
347  $this->object->getId(),
348  $correctness,
350  );
351  }
352  $this->object->saveToDb();
353  if (count($item->suggested_solutions)) {
354  foreach ($item->suggested_solutions as $suggested_solution) {
355  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
356  }
357  $this->object->saveToDb();
358  }
359  if ($tst_id > 0) {
360  $q_1_id = $this->object->getId();
361  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
362  $tst_object->questions[$question_counter++] = $question_id;
363  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
364  } else {
365  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
366  }
367  $this->object->saveToDb();
368  if (is_array($combination) && count($combination) > 0) {
369  require_once './Modules/TestQuestionPool/classes/class.assClozeGapCombination.php';
371  assClozeGapCombination::importGapCombinationToDb($this->object->getId(), $combination);
372  }
373  $this->object->saveToDb();
374  }
375 
380  protected function buildFeedbackIdentifier($ident)
381  {
382  require_once 'Modules/TestQuestionPool/classes/feedback/class.ilAssSpecificFeedbackIdentifier.php';
383  $fbIdentifier = new ilAssSpecificFeedbackIdentifier();
384 
385  $ident = explode('_', $ident);
386 
387  if (count($ident) > 1) {
388  $fbIdentifier->setQuestionIndex($ident[0]);
389  $fbIdentifier->setAnswerIndex($ident[1]);
390  } else {
391  $fbIdentifier->setQuestionIndex($ident[0]);
392  $fbIdentifier->setAnswerIndex(0);
393  }
394 
395  return $fbIdentifier;
396  }
397 }
Class for cloze question numeric answers.
addGeneralMetadata(ilQTIItem $item)
$_SESSION["AccountId"]
static importGapCombinationToDb($question_id, $gap_combinations)
global $DIC
Definition: saml.php:7
const CLOZE_TEXT
Cloze question constants.
processNonAbstractedImageReferences($text, $sourceNic)
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) ...
Class for cloze question gaps.
$index
Definition: metadata.php:60
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=true)
Create new media object and update page in db and return new media object.
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.
$ai
Definition: langwiz.php:238
const CLOZE_SELECT
const FIBTYPE_STRING
$ilUser
Definition: imgupload.php:18
const FIBTYPE_INTEGER
static clearGapCombinationsFromDb($question_id)
const FIBTYPE_DECIMAL
Class for cloze question imports.
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
const CLOZE_NUMERIC
Class for question imports.
$response
const FB_MODE_GAP_QUESTION
constants for different feedback modes (per gap or per gap-answers/options)