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