ILIAS  trunk Revision v5.2.0beta1-34115-g3a2438be29
assMultipleChoiceImport Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ Inheritance diagram for assMultipleChoiceImport:
+ Collaboration diagram for assMultipleChoiceImport:

Public Member Functions

 fromXML (&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
 Creates a question from a QTI file. More...
 
- Public Member Functions inherited from assQuestionImport
 __construct ($a_object)
 assQuestionImport constructor More...
 
 getQuestionId ()
 
 getFeedbackGeneric ($item)
 
 fromXML (&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
 Creates a question from a QTI file. More...
 
 importSuggestedSolution (int $question_id, string $value="", int $subquestion_index=0)
 
 QTIMaterialToString (ilQTIMaterial $a_material)
 Reads an QTI material tag and creates a text or XHTML string. More...
 

Additional Inherited Members

- Data Fields inherited from assQuestionImport
 $object
 
- Protected Member Functions inherited from assQuestionImport
 fetchIndexFromFeedbackIdent ($feedbackIdent, $prefix='response_')
 
 getFeedbackAnswerSpecific (ilQTIItem $item, $prefix='response_')
 
 addGeneralMetadata (ilQTIItem $item)
 
 fetchLifecycle (ilQTIItem $item)
 
 getQplImportArchivDirectory ()
 returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) More...
 
 getTstImportArchivDirectory ()
 returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) More...
 
 processNonAbstractedImageReferences ($text, $sourceNic)
 
 fetchAdditionalContentEditingModeInformation ($qtiItem)
 fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_CONTENT_EDITING_MODE_RTE when no or invalid information is given More...
 
 findSolutionTypeByValue (string $value)
 
 getSuggestedSolutionsRepo ()
 
- Protected Attributes inherited from assQuestionImport
assQuestionSuggestedSolutionsDatabaseRepository $suggestedsolution_repo = null
 

Detailed Description

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning Class for multiple choice question imports

assMultipleChoiceImport is a class for multiple choice question imports

Author
Helmut Schottmüller helmu.nosp@m.t.sc.nosp@m.hottm.nosp@m.uell.nosp@m.er@ma.nosp@m.c.co.nosp@m.m
Version
$Id$ \

Definition at line 28 of file class.assMultipleChoiceImport.php.

Member Function Documentation

◆ fromXML()

assMultipleChoiceImport::fromXML ( $item,
  $questionpool_id,
$tst_id,
$tst_object,
$question_counter,
  $import_mapping 
)

Creates a question from a QTI file.

Receives parameters from a QTI parser and creates a valid ILIAS question object

Parameters
ilQtiItem$itemThe QTI item object
integer$questionpool_idThe id of the parent questionpool
integer$tst_idThe id of the parent test if the question is part of a test
object$tst_objectA reference to the parent test object
integer$question_counterA reference to a question counter to count the questions of an imported question pool
array$import_mappingAn array containing references to included ILIAS objects public

Definition at line 43 of file class.assMultipleChoiceImport.php.

References ILIAS\LTI\ToolProvider\$created, $DIC, ILIAS\LTI\ToolProvider\$key, $response, ilRTE\_replaceMediaObjectImageSrc(), ilObjMediaObject\_saveTempFileAsMediaObject(), ilObjMediaObject\_saveUsage(), assQuestionImport\addGeneralMetadata(), ilSession\clear(), assQuestionImport\fetchAdditionalContentEditingModeInformation(), ilSession\get(), assQuestionImport\getQplImportArchivDirectory(), assQuestionImport\getTstImportArchivDirectory(), IL_INST_ID, assQuestionImport\importSuggestedSolution(), ILIAS\Repository\int(), ilFileUtils\makeDirParents(), ILIAS\Repository\object(), and assQuestionImport\QTIMaterialToString().

43  : array
44  {
45  global $DIC;
46  $ilUser = $DIC['ilUser'];
47  // empty session variable for imported xhtml mobs
48  ilSession::clear('import_mob_xhtml');
49 
50  $presentation = $item->getPresentation();
51  $shuffle = 0;
52  $selectionLimit = null;
53  $now = getdate();
54  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
55  $answers = [];
56  foreach ($presentation->order as $entry) {
57  switch ($entry["type"]) {
58  case "response":
59  $response = $presentation->response[$entry["index"]];
60  $rendertype = $response->getRenderType();
61  switch (strtolower(get_class($response->getRenderType()))) {
62  case "ilqtirenderchoice":
63  $shuffle = $rendertype->getShuffle();
64  if ($rendertype->getMaxnumber()) {
65  $selectionLimit = $rendertype->getMaxnumber();
66  }
67  $answerorder = 0;
68  $foundimage = false;
69  foreach ($rendertype->response_labels as $response_label) {
70  $ident = $response_label->getIdent();
71  $answertext = "";
72  $answerimage = [];
73  foreach ($response_label->material as $mat) {
74  $embedded = false;
75  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
76  $foundmat = $mat->getMaterial($m);
77  if (strcmp($foundmat["type"], "mattext") == 0) {
78  }
79  if (strcmp($foundmat["type"], "matimage") == 0) {
80  if (strlen($foundmat["material"]->getEmbedded())) {
81  $embedded = true;
82  }
83  }
84  }
85  if ($embedded) {
86  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
87  $foundmat = $mat->getMaterial($m);
88  if (strcmp($foundmat["type"], "mattext") == 0) {
89  $answertext .= $foundmat["material"]->getContent();
90  }
91  if (strcmp($foundmat["type"], "matimage") == 0) {
92  $foundimage = true;
93  $answerimage = [
94  "imagetype" => $foundmat["material"]->getImageType(),
95  "label" => $foundmat["material"]->getLabel(),
96  "content" => $foundmat["material"]->getContent()
97  ];
98  }
99  }
100  } else {
101  $answertext = $this->QTIMaterialToString($mat);
102  }
103  }
104  $answers[$ident] = [
105  "answertext" => $answertext,
106  "imagefile" => $answerimage,
107  "points" => 0,
108  "answerorder" => $answerorder++,
109  "points_unchecked" => 0,
110  "action" => ""
111  ];
112  }
113  break;
114  }
115  break;
116  }
117  }
118  $responses = [];
119  $feedbacks = [];
120  $feedbacksgeneric = [];
121  foreach ($item->resprocessing as $resprocessing) {
122  foreach ($resprocessing->respcondition as $respcondition) {
123  $ident = "";
124  $correctness = 1;
125  $conditionvar = $respcondition->getConditionvar();
126  foreach ($conditionvar->order as $order) {
127  switch ($order["field"]) {
128  case "arr_not":
129  $correctness = 0;
130  break;
131  case "varequal":
132  $ident = $conditionvar->varequal[$order["index"]]->getContent();
133  break;
134  }
135  }
136  foreach ($respcondition->setvar as $setvar) {
137  if (strcmp($ident, "") != 0) {
138  if ($correctness) {
139  $answers[$ident]["action"] = $setvar->getAction();
140  $answers[$ident]["points"] = $setvar->getContent();
141  if (count($respcondition->displayfeedback)) {
142  foreach ($respcondition->displayfeedback as $feedbackpointer) {
143  if (strlen($feedbackpointer->getLinkrefid())) {
144  foreach ($item->itemfeedback as $ifb) {
145  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
146  // found a feedback for the identifier
147  if (count($ifb->material)) {
148  foreach ($ifb->material as $material) {
149  $feedbacksgeneric[1] = $material;
150  }
151  }
152  if ((count($ifb->flow_mat) > 0)) {
153  foreach ($ifb->flow_mat as $fmat) {
154  if (count($fmat->material)) {
155  foreach ($fmat->material as $material) {
156  $feedbacksgeneric[1] = $material;
157  }
158  }
159  }
160  }
161  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
162  // found a feedback for the identifier
163  if (count($ifb->material)) {
164  foreach ($ifb->material as $material) {
165  $feedbacksgeneric[0] = $material;
166  }
167  }
168  if ((count($ifb->flow_mat) > 0)) {
169  foreach ($ifb->flow_mat as $fmat) {
170  if (count($fmat->material)) {
171  foreach ($fmat->material as $material) {
172  $feedbacksgeneric[0] = $material;
173  }
174  }
175  }
176  }
177  }
178  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
179  // found a feedback for the identifier
180  if (count($ifb->material)) {
181  foreach ($ifb->material as $material) {
182  $feedbacks[$ident] = $material;
183  }
184  }
185  if ((count($ifb->flow_mat) > 0)) {
186  foreach ($ifb->flow_mat as $fmat) {
187  if (count($fmat->material)) {
188  foreach ($fmat->material as $material) {
189  $feedbacks[$ident] = $material;
190  }
191  }
192  }
193  }
194  }
195  }
196  }
197  }
198  }
199  } else {
200  $answers[$ident]["action"] = $setvar->getAction();
201  $answers[$ident]["points_unchecked"] = $setvar->getContent();
202  }
203  }
204  }
205  }
206  }
207  $this->addGeneralMetadata($item);
208  $this->object->setTitle($item->getTitle());
209  $this->object->setNrOfTries((int) $item->getMaxattempts());
210  $this->object->setComment($item->getComment());
211  $this->object->setAuthor($item->getAuthor());
212  $this->object->setOwner($ilUser->getId());
213  $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
214  $this->object->setObjId($questionpool_id);
215  $this->object->setShuffle($shuffle);
216  $this->object->setSelectionLimit($selectionLimit);
217  $thumb_size = (int) $item->getMetadataEntry("thumb_size");
218  if ($thumb_size !== null && $thumb_size >= $this->object->getMinimumThumbSize()) {
219  $this->object->setThumbSize($thumb_size);
220  }
221 
222  foreach ($answers as $answer) {
223  if ($item->getMetadataEntry('singleline') || (is_array($answer["imagefile"]) && count($answer["imagefile"]) > 0)) {
224  $this->object->setIsSingleline(true);
225  }
226  $this->object->addAnswer(
227  $answer["answertext"],
228  $answer["points"],
229  $answer["points_unchecked"],
230  $answer["answerorder"],
231  $answer["imagefile"]["label"] ?? null
232  );
233  }
234  // additional content editing mode information
235  $this->object->setAdditionalContentEditingMode(
237  );
238  $this->object->saveToDb();
239  foreach ($answers as $answer) {
240  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
241  $image = base64_decode($answer["imagefile"]["content"]);
242  $imagepath = $this->object->getImagePath();
243  if (!file_exists($imagepath)) {
244  ilFileUtils::makeDirParents($imagepath);
245  }
246  $imagepath .= $answer["imagefile"]["label"];
247  $fh = fopen($imagepath, "wb");
248  if ($fh !== false) {
249  $imagefile = fwrite($fh, $image);
250  fclose($fh);
251  $this->object->generateThumbForFile(
252  $answer["imagefile"]["label"],
253  $this->object->getImagePath(),
254  $this->object->getThumbSize()
255  );
256  }
257  }
258  }
259 
260  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
261  if (!is_null($feedbackSetting)) {
262  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
263  }
264 
265  // handle the import of media objects in XHTML code
266  foreach ($feedbacks as $ident => $material) {
267  $m = $this->QTIMaterialToString($material);
268  $feedbacks[$ident] = $m;
269  }
270  foreach ($feedbacksgeneric as $correctness => $material) {
271  $m = $this->QTIMaterialToString($material);
272  $feedbacksgeneric[$correctness] = $m;
273  }
274  $questiontext = $this->object->getQuestion();
275  $answers = &$this->object->getAnswers();
276  if (is_array(ilSession::get("import_mob_xhtml"))) {
277  foreach (ilSession::get("import_mob_xhtml") as $mob) {
278  if ($tst_id > 0) {
279  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
280  } else {
281  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
282  }
283 
284  global $DIC; /* @var ILIAS\DI\Container $DIC */
285  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
286 
287  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
288  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
289  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
290  foreach ($answers as $key => $value) {
291  $answer_obj = &$answers[$key];
292  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
293  }
294  foreach ($feedbacks as $ident => $material) {
295  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
296  }
297  foreach ($feedbacksgeneric as $correctness => $material) {
298  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
299  }
300  }
301  }
302  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
303  foreach ($answers as $key => $value) {
304  $answer_obj = &$answers[$key];
305  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
306  }
307  foreach ($feedbacks as $ident => $material) {
308  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
309  $this->object->getId(),
310  0,
311  $ident,
313  );
314  }
315  foreach ($feedbacksgeneric as $correctness => $material) {
316  $this->object->feedbackOBJ->importGenericFeedback(
317  $this->object->getId(),
318  $correctness,
320  );
321  }
322  $this->object->saveToDb();
323  if (count($item->suggested_solutions)) {
324  foreach ($item->suggested_solutions as $suggested_solution) {
326  $this->object->getId(),
327  $suggested_solution["solution"]->getContent(),
328  $suggested_solution["gap_index"]
329  );
330  }
331  }
332  if ($tst_id > 0) {
333  $q_1_id = $this->object->getId();
334  $question_id = $this->object->duplicate(true, "", "", -1, $tst_id);
335  $tst_object->questions[$question_counter++] = $question_id;
336  $import_mapping[$item->getIdent()] = ["pool" => $q_1_id, "test" => $question_id];
337  } else {
338  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
339  }
340  return $import_mapping;
341  }
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static get(string $a_var)
addGeneralMetadata(ilQTIItem $item)
const IL_INST_ID
Definition: constants.php:40
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
$response
Definition: xapitoken.php:93
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
string $key
Consumer key/client ID value.
Definition: System.php:193
$DIC
Definition: xapitoken.php:62
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
importSuggestedSolution(int $question_id, string $value="", int $subquestion_index=0)
static clear(string $a_var)
int $created
Timestamp for when the object was created.
Definition: System.php:151
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: