ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.assMatchingQuestionImport.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 {
17  public function saveImage($data, $filename)
18  {
19  $image =&base64_decode($data);
20  $imagepath = $this->object->getImagePath();
21  include_once "./Services/Utilities/classes/class.ilUtil.php";
22  if (!file_exists($imagepath)) {
23  ilUtil::makeDirParents($imagepath);
24  }
25  $imagepath .= $filename;
26  $fh = fopen($imagepath, "wb");
27  if ($fh == false) {
28  } else {
29  $imagefile = fwrite($fh, $image);
30  fclose($fh);
31  }
32  }
33 
47  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
48  {
49  global $ilUser;
50 
51  // empty session variable for imported xhtml mobs
52  unset($_SESSION["import_mob_xhtml"]);
53  $presentation = $item->getPresentation();
54  $duration = $item->getDuration();
55  $shuffle = 0;
56  $now = getdate();
57  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
58  $definitions = array();
59  $terms = array();
60  $foundimage = false;
61  foreach ($presentation->order as $entry) {
62  switch ($entry["type"]) {
63  case "response":
64  $response = $presentation->response[$entry["index"]];
65  $rendertype = $response->getRenderType();
66  switch (strtolower(get_class($rendertype))) {
67  case "ilqtirenderchoice":
68  $shuffle = $rendertype->getShuffle();
69  $answerorder = 0;
70  foreach ($rendertype->response_labels as $response_label) {
71  $ident = $response_label->getIdent();
72  $answertext = "";
73  $answerimage = array();
74  foreach ($response_label->material as $mat) {
75  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
76  $foundmat = $mat->getMaterial($m);
77  if (strcmp($foundmat["type"], "mattext") == 0) {
78  $answertext .= $foundmat["material"]->getContent();
79  }
80  if (strcmp($foundmat["type"], "matimage") == 0) {
81  $foundimage = true;
82  $answerimage = array(
83  "imagetype" => $foundmat["material"]->getImageType(),
84  "label" => $foundmat["material"]->getLabel(),
85  "content" => $foundmat["material"]->getContent()
86  );
87  }
88  }
89  }
90  if (($response_label->getMatchMax() == 1) && (strlen($response_label->getMatchGroup()))) {
91  $definitions[$ident] = array(
92  "answertext" => $answertext,
93  "answerimage" => $answerimage,
94  "points" => 0,
95  "answerorder" => $ident,
96  "action" => ""
97  );
98  } else {
99  $terms[$ident] = array(
100  "term" => $answertext,
101  "answerimage" => $answerimage,
102  "points" => 0,
103  "ident" => $ident,
104  "action" => ""
105  );
106  }
107  }
108  break;
109  }
110  break;
111  }
112  }
113  $responses = array();
114  $feedbacksgeneric = array();
115  foreach ($item->resprocessing as $resprocessing) {
116  foreach ($resprocessing->respcondition as $respcondition) {
117  $subset = array();
118  $correctness = 1;
119  $conditionvar = $respcondition->getConditionvar();
120  foreach ($conditionvar->order as $order) {
121  switch ($order["field"]) {
122  case "varsubset":
123  $subset = explode(",", $conditionvar->varsubset[$order["index"]]->getContent());
124  break;
125  }
126  }
127  foreach ($respcondition->setvar as $setvar) {
128  array_push($responses, array("subset" => $subset, "action" => $setvar->getAction(), "points" => $setvar->getContent()));
129  }
130 
131  if (count($respcondition->displayfeedback)) {
132  foreach ($respcondition->displayfeedback as $feedbackpointer) {
133  if (strlen($feedbackpointer->getLinkrefid())) {
134  foreach ($item->itemfeedback as $ifb) {
135  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
136  // found a feedback for the identifier
137  if (count($ifb->material)) {
138  foreach ($ifb->material as $material) {
139  $feedbacksgeneric[1] = $material;
140  }
141  }
142  if ((count($ifb->flow_mat) > 0)) {
143  foreach ($ifb->flow_mat as $fmat) {
144  if (count($fmat->material)) {
145  foreach ($fmat->material as $material) {
146  $feedbacksgeneric[1] = $material;
147  }
148  }
149  }
150  }
151  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
152  // found a feedback for the identifier
153  if (count($ifb->material)) {
154  foreach ($ifb->material as $material) {
155  $feedbacksgeneric[0] = $material;
156  }
157  }
158  if ((count($ifb->flow_mat) > 0)) {
159  foreach ($ifb->flow_mat as $fmat) {
160  if (count($fmat->material)) {
161  foreach ($fmat->material as $material) {
162  $feedbacksgeneric[0] = $material;
163  }
164  }
165  }
166  }
167  }
168  }
169  }
170  }
171  }
172  }
173  }
174 
175  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
176  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
177  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
178  $this->object->createNewQuestion();
179  $this->addGeneralMetadata($item);
180  $this->object->setTitle($item->getTitle());
181  $this->object->setNrOfTries($item->getMaxattempts());
182  $this->object->setComment($item->getComment());
183  $this->object->setAuthor($item->getAuthor());
184  $this->object->setOwner($ilUser->getId());
185  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
186  $this->object->setObjId($questionpool_id);
187  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
188  $extended_shuffle = $item->getMetadataEntry("shuffle");
189  $this->object->setThumbGeometry($item->getMetadataEntry("thumb_geometry"));
190 
191  if (strlen($item->getMetadataEntry('matching_mode'))) {
192  $this->object->setMatchingMode($item->getMetadataEntry('matching_mode'));
193  } else {
194  $this->object->setMatchingMode(assMatchingQuestion::MATCHING_MODE_1_ON_1);
195  }
196 
197  // save images
198  foreach ($terms as $term) {
199  if (count($term['answerimage'])) {
200  $this->saveImage($term['answerimage']['content'], $term['answerimage']['label']);
201  }
202  }
203  foreach ($definitions as $definition) {
204  if (count($definition['answerimage'])) {
205  $this->saveImage($definition['answerimage']['content'], $definition['answerimage']['label']);
206  }
207  }
208 
209  foreach ($terms as $termindex => $term) {
210  $this->object->addTerm(new assAnswerMatchingTerm($term["term"], $term['answerimage']['label'], $term["ident"]));
211  }
212  foreach ($definitions as $definitionindex => $definition) {
213  $this->object->addDefinition(new assAnswerMatchingDefinition($definition["answertext"], $definition['answerimage']['label'], $definition["answerorder"]));
214  }
215 
216  if (strlen($extended_shuffle) > 0) {
217  $shuffle = $extended_shuffle;
218  }
219  $this->object->setShuffle($shuffle);
220 
221  foreach ($responses as $response) {
222  $subset = $response["subset"];
223  foreach ($subset as $ident) {
224  if (array_key_exists($ident, $definitions)) {
225  $definition = $definitions[$ident];
226  }
227  if (array_key_exists($ident, $terms)) {
228  $term = $terms[$ident];
229  }
230  }
231  $this->object->addMatchingPair(new assAnswerMatchingTerm('', '', $term["ident"]), new assAnswerMatchingDefinition('', '', $definition["answerorder"]), $response['points']);
232  }
233  // additional content editing mode information
234  $this->object->setAdditionalContentEditingMode(
236  );
237  $this->object->saveToDb();
238  if (count($item->suggested_solutions)) {
239  foreach ($item->suggested_solutions as $suggested_solution) {
240  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
241  }
242  $this->object->saveToDb();
243  }
244  foreach ($responses as $response) {
245  $subset = $response["subset"];
246  foreach ($subset as $ident) {
247  if (array_key_exists($ident, $definitions)) {
248  $definition = $definitions[$ident];
249  }
250  if (array_key_exists($ident, $terms)) {
251  $term = $terms[$ident];
252  }
253  }
254  }
255 
256  foreach ($feedbacksgeneric as $correctness => $material) {
257  $m = $this->object->QTIMaterialToString($material);
258  $feedbacksgeneric[$correctness] = $m;
259  }
260 
261  $feedbacks = $this->getFeedbackAnswerSpecific($item, 'correct_');
262 
263  // handle the import of media objects in XHTML code
264  $questiontext = $this->object->getQuestion();
265  if (is_array($_SESSION["import_mob_xhtml"])) {
266  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
267  include_once "./Services/RTE/classes/class.ilRTE.php";
268  foreach ($_SESSION["import_mob_xhtml"] as $mob) {
269  if ($tst_id > 0) {
270  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
271  } else {
272  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
273  }
274 
275  $GLOBALS['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
276 
277  $media_object =&ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
278  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
279  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
280  foreach ($feedbacks as $ident => $material) {
281  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
282  }
283  foreach ($feedbacksgeneric as $correctness => $material) {
284  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
285  }
286  }
287  }
288  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
289  foreach ($feedbacks as $ident => $material) {
290  $index = $this->fetchIndexFromFeedbackIdent($ident, 'correct_');
291 
292  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
293  $this->object->getId(),
294  $index,
296  );
297  }
298  foreach ($feedbacksgeneric as $correctness => $material) {
299  $this->object->feedbackOBJ->importGenericFeedback(
300  $this->object->getId(),
301  $correctness,
303  );
304  }
305  $this->object->saveToDb();
306  if ($tst_id > 0) {
307  $q_1_id = $this->object->getId();
308  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
309  $tst_object->questions[$question_counter++] = $question_id;
310  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
311  } else {
312  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
313  }
314  }
315 
321  protected function fetchIndexFromFeedbackIdent($feedbackIdent, $prefix = 'response_')
322  {
323  list($termId, $definitionId) = explode('_', str_replace($prefix, '', $feedbackIdent));
324 
325  foreach ($this->object->getMatchingPairs() as $index => $pair) {
326  /* @var assAnswerMatchingPair $pair */
327 
328  if ($pair->term->identifier != $termId) {
329  continue;
330  }
331 
332  if ($pair->definition->identifier != $definitionId) {
333  continue;
334  }
335 
336  return (int) $index;
337  }
338 
339  return -1;
340  }
341 }
Class for matching question terms.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
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) ...
$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.
Class for matching question imports.
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
Create new PHPExcel object
obj_idprivate
Class for matching question definitions.
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.