ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.assKprimChoiceImport.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 require_once 'Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php';
5 require_once 'Modules/TestQuestionPool/classes/class.ilAssKprimChoiceAnswer.php';
6 
14 {
18  public $object;
19 
20  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
21  {
22  global $DIC;
23  $ilUser = $DIC['ilUser'];
24 
25  unset($_SESSION["import_mob_xhtml"]);
26 
27  $duration = $item->getDuration();
28  $shuffle = 0;
29  $answers = array();
30 
31  $presentation = $item->getPresentation();
32  foreach ($presentation->order as $entry) {
33  switch ($entry["type"]) {
34  case "response":
35  $response = $presentation->response[$entry["index"]];
36  $rendertype = $response->getRenderType();
37  switch (strtolower(get_class($response->getRenderType()))) {
38  case "ilqtirenderchoice":
39  $shuffle = $rendertype->getShuffle();
40  $answerorder = 0;
41  $foundimage = false;
42  foreach ($rendertype->response_labels as $response_label) {
43  $ident = $response_label->getIdent();
44  $answertext = "";
45  $answerimage = array();
46  foreach ($response_label->material as $mat) {
47  $embedded = false;
48  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
49  $foundmat = $mat->getMaterial($m);
50  if (strcmp($foundmat["type"], "mattext") == 0) {
51  }
52  if (strcmp($foundmat["type"], "matimage") == 0) {
53  if (strlen($foundmat["material"]->getEmbedded())) {
54  $embedded = true;
55  }
56  }
57  }
58  if ($embedded) {
59  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
60  $foundmat = $mat->getMaterial($m);
61  if (strcmp($foundmat["type"], "mattext") == 0) {
62  $answertext .= $foundmat["material"]->getContent();
63  }
64  if (strcmp($foundmat["type"], "matimage") == 0) {
65  $foundimage = true;
66  $answerimage = array(
67  "imagetype" => $foundmat["material"]->getImageType(),
68  "label" => $foundmat["material"]->getLabel(),
69  "content" => $foundmat["material"]->getContent()
70  );
71  }
72  }
73  } else {
74  $answertext = $this->object->QTIMaterialToString($mat);
75  }
76  }
77 
78  $answers[$ident] = array(
79  "answertext" => $answertext,
80  "imagefile" => $answerimage,
81  "answerorder" => $ident
82  );
83  }
84  break;
85  }
86  break;
87  }
88  }
89 
90  $feedbacks = array();
91  $feedbacksgeneric = array();
92 
93  foreach ($item->resprocessing as $resprocessing) {
94  foreach ($resprocessing->outcomes->decvar as $decvar) {
95  if ($decvar->getVarname() == 'SCORE') {
96  $this->object->setPoints($decvar->getMaxvalue());
97 
98  if ($decvar->getMinvalue() > 0) {
99  $this->object->setScorePartialSolutionEnabled(true);
100  } else {
101  $this->object->setScorePartialSolutionEnabled(false);
102  }
103  }
104  }
105 
106  foreach ($resprocessing->respcondition as $respcondition) {
107  if (!count($respcondition->setvar)) {
108  foreach ($respcondition->getConditionvar()->varequal as $varequal) {
109  $ident = $varequal->respident;
110  $answers[$ident]['correctness'] = (bool) $varequal->getContent();
111 
112  break;
113  }
114 
115  foreach ($respcondition->displayfeedback as $feedbackpointer) {
116  if (strlen($feedbackpointer->getLinkrefid())) {
117  foreach ($item->itemfeedback as $ifb) {
118  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
119  // found a feedback for the identifier
120  if (count($ifb->material)) {
121  foreach ($ifb->material as $material) {
122  $feedbacks[$ident] = $material;
123  }
124  }
125  if ((count($ifb->flow_mat) > 0)) {
126  foreach ($ifb->flow_mat as $fmat) {
127  if (count($fmat->material)) {
128  foreach ($fmat->material as $material) {
129  $feedbacks[$ident] = $material;
130  }
131  }
132  }
133  }
134  }
135  }
136  }
137  }
138  } else {
139  foreach ($respcondition->displayfeedback as $feedbackpointer) {
140  if (strlen($feedbackpointer->getLinkrefid())) {
141  foreach ($item->itemfeedback as $ifb) {
142  if ($ifb->getIdent() == "response_allcorrect") {
143  // found a feedback for the identifier
144  if (count($ifb->material)) {
145  foreach ($ifb->material as $material) {
146  $feedbacksgeneric[1] = $material;
147  }
148  }
149  if ((count($ifb->flow_mat) > 0)) {
150  foreach ($ifb->flow_mat as $fmat) {
151  if (count($fmat->material)) {
152  foreach ($fmat->material as $material) {
153  $feedbacksgeneric[1] = $material;
154  }
155  }
156  }
157  }
158  } elseif ($ifb->getIdent() == "response_onenotcorrect") {
159  // found a feedback for the identifier
160  if (count($ifb->material)) {
161  foreach ($ifb->material as $material) {
162  $feedbacksgeneric[0] = $material;
163  }
164  }
165  if ((count($ifb->flow_mat) > 0)) {
166  foreach ($ifb->flow_mat as $fmat) {
167  if (count($fmat->material)) {
168  foreach ($fmat->material as $material) {
169  $feedbacksgeneric[0] = $material;
170  }
171  }
172  }
173  }
174  }
175  }
176  }
177  }
178  }
179  }
180  }
181 
182  $this->addGeneralMetadata($item);
183 
184  $this->object->setTitle($item->getTitle());
185  $this->object->setNrOfTries($item->getMaxattempts());
186  $this->object->setComment($item->getComment());
187  $this->object->setAuthor($item->getAuthor());
188  $this->object->setOwner($ilUser->getId());
189  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
190  $this->object->setObjId($questionpool_id);
191  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
192  $this->object->setShuffleAnswersEnabled($shuffle);
193  $this->object->setAnswerType($item->getMetadataEntry("answer_type"));
194  $this->object->setOptionLabel($item->getMetadataEntry("option_label_setting"));
195  $this->object->setCustomTrueOptionLabel($item->getMetadataEntry("custom_true_option_label"));
196  $this->object->setCustomFalseOptionLabel($item->getMetadataEntry("custom_false_option_label"));
197  $this->object->setThumbSize($item->getMetadataEntry("thumb_size"));
198 
199  $this->object->saveToDb();
200 
201  foreach ($answers as $answerData) {
202  $answer = new ilAssKprimChoiceAnswer();
203  $answer->setImageFsDir($this->object->getImagePath());
204  $answer->setImageWebDir($this->object->getImagePathWeb());
205 
206  $answer->setPosition($answerData['answerorder']);
207  $answer->setAnswertext($answerData['answertext']);
208  $answer->setCorrectness($answerData['correctness']);
209 
210  if (isset($answerData['imagefile']['label'])) {
211  $answer->setImageFile($answerData['imagefile']['label']);
212  }
213 
214  $this->object->addAnswer($answer);
215  }
216  // additional content editing mode information
217  $this->object->setAdditionalContentEditingMode(
219  );
220 
221  $this->object->saveToDb();
222 
223  foreach ($answers as $answer) {
224  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
225  $image = &base64_decode($answer["imagefile"]["content"]);
226  $imagepath = $this->object->getImagePath();
227  include_once "./Services/Utilities/classes/class.ilUtil.php";
228  if (!file_exists($imagepath)) {
229  ilUtil::makeDirParents($imagepath);
230  }
231  $imagepath .= $answer["imagefile"]["label"];
232  if ($fh = fopen($imagepath, "wb")) {
233  $imagefile = fwrite($fh, $image);
234  fclose($fh);
235  }
236  }
237  }
238 
239  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
240  if (!is_null($feedbackSetting)) {
241  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
242  }
243 
244  // handle the import of media objects in XHTML code
245  foreach ($feedbacks as $ident => $material) {
246  $m = $this->object->QTIMaterialToString($material);
247  $feedbacks[$ident] = $m;
248  }
249  foreach ($feedbacksgeneric as $correctness => $material) {
250  $m = $this->object->QTIMaterialToString($material);
251  $feedbacksgeneric[$correctness] = $m;
252  }
253  $questiontext = $this->object->getQuestion();
254  $answers = &$this->object->getAnswers();
255  if (is_array($_SESSION["import_mob_xhtml"])) {
256  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
257  include_once "./Services/RTE/classes/class.ilRTE.php";
258  foreach ($_SESSION["import_mob_xhtml"] as $mob) {
259  if ($tst_id > 0) {
260  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
261  } else {
262  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
263  }
264 
265  global $DIC; /* @var ILIAS\DI\Container $DIC */
266  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
267 
268  $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
269  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
270  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
271  foreach ($answers as $key => $value) {
272  $answer_obj = &$answers[$key];
273  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
274  }
275  foreach ($feedbacks as $ident => $material) {
276  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
277  }
278  foreach ($feedbacksgeneric as $correctness => $material) {
279  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
280  }
281  }
282  }
283  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
284  foreach ($answers as $key => $value) {
285  $answer_obj = &$answers[$key];
286  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
287  }
288  foreach ($feedbacks as $ident => $material) {
289  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
290  $this->object->getId(),
291  0,
292  $ident,
294  );
295  }
296  foreach ($feedbacksgeneric as $correctness => $material) {
297  $this->object->feedbackOBJ->importGenericFeedback(
298  $this->object->getId(),
299  $correctness,
301  );
302  }
303  $this->object->saveToDb();
304  if (count($item->suggested_solutions)) {
305  foreach ($item->suggested_solutions as $suggested_solution) {
306  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
307  }
308  $this->object->saveToDb();
309  }
310  if ($tst_id > 0) {
311  $q_1_id = $this->object->getId();
312  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
313  $tst_object->questions[$question_counter++] = $question_id;
314  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
315  } else {
316  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
317  }
318  //$ilLog->write(strftime("%D %T") . ": finished import multiple choice question (single response)");
319  }
320 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
addGeneralMetadata(ilQTIItem $item)
$_SESSION["AccountId"]
global $DIC
Definition: saml.php:7
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) ...
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
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...
$ilUser
Definition: imgupload.php:18
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
Class for question imports.
$response
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.
$key
Definition: croninfo.php:18