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