ILIAS  trunk Revision v5.2.0beta1-34132-g2d4d73d4a0
assSingleChoiceImport 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 assSingleChoiceImport:
+ Collaboration diagram for assSingleChoiceImport:

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 single choice question imports

assSingleChoiceImport is a class for single 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.assSingleChoiceImport.php.

Member Function Documentation

◆ fromXML()

assSingleChoiceImport::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.assSingleChoiceImport.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 
48  // empty session variable for imported xhtml mobs
49  ilSession::clear('import_mob_xhtml');
50 
51  $presentation = $item->getPresentation();
52  $shuffle = 0;
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  $answerorder = 0;
65  $foundimage = false;
66  foreach ($rendertype->response_labels as $response_label) {
67  $ident = $response_label->getIdent();
68  $answertext = "";
69  $answerimage = [];
70  foreach ($response_label->material as $mat) {
71  $embedded = false;
72  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
73  $foundmat = $mat->getMaterial($m);
74  if (strcmp($foundmat["type"], "mattext") == 0) {
75  }
76  if (strcmp($foundmat["type"], "matimage") == 0) {
77  if (strlen($foundmat["material"]->getEmbedded())) {
78  $embedded = true;
79  }
80  }
81  }
82  if ($embedded) {
83  for ($m = 0; $m < $mat->getMaterialCount(); $m++) {
84  $foundmat = $mat->getMaterial($m);
85  if (strcmp($foundmat["type"], "mattext") == 0) {
86  $answertext .= $foundmat["material"]->getContent();
87  }
88  if (strcmp($foundmat["type"], "matimage") == 0) {
89  $foundimage = true;
90  $answerimage = [
91  "imagetype" => $foundmat["material"]->getImageType(),
92  "label" => $foundmat["material"]->getLabel(),
93  "content" => $foundmat["material"]->getContent()
94  ];
95  }
96  }
97  } else {
98  $answertext = $this->QTIMaterialToString($mat);
99  }
100  }
101  $answers[$ident] = [
102  "answertext" => $answertext,
103  "imagefile" => $answerimage,
104  "points" => 0,
105  "answerorder" => $answerorder++,
106  "points_unchecked" => 0,
107  "action" => ""
108  ];
109  }
110  break;
111  }
112  break;
113  }
114  }
115  $responses = [];
116  $feedbacks = [];
117  $feedbacksgeneric = [];
118  foreach ($item->resprocessing as $resprocessing) {
119  foreach ($resprocessing->respcondition as $respcondition) {
120  $ident = "";
121  $correctness = 1;
122  $conditionvar = $respcondition->getConditionvar();
123  foreach ($conditionvar->order as $order) {
124  switch ($order["field"]) {
125  case "arr_not":
126  $correctness = 0;
127  break;
128  case "varequal":
129  $ident = $conditionvar->varequal[$order["index"]]->getContent();
130  break;
131  }
132  }
133  foreach ($respcondition->setvar as $setvar) {
134  if (strcmp($ident, "") != 0) {
135  if ($correctness) {
136  $answers[$ident]["action"] = $setvar->getAction();
137  $answers[$ident]["points"] = $setvar->getContent();
138  if (count($respcondition->displayfeedback)) {
139  foreach ($respcondition->displayfeedback as $feedbackpointer) {
140  if (strlen($feedbackpointer->getLinkrefid())) {
141  foreach ($item->itemfeedback as $ifb) {
142  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
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 (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
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  if (strcmp($ifb->getIdent(), $feedbackpointer->getLinkrefid()) == 0) {
176  // found a feedback for the identifier
177  if (count($ifb->material)) {
178  foreach ($ifb->material as $material) {
179  $feedbacks[$ident] = $material;
180  }
181  }
182  if ((count($ifb->flow_mat) > 0)) {
183  foreach ($ifb->flow_mat as $fmat) {
184  if (count($fmat->material)) {
185  foreach ($fmat->material as $material) {
186  $feedbacks[$ident] = $material;
187  }
188  }
189  }
190  }
191  }
192  }
193  }
194  }
195  }
196  } else {
197  $answers[$ident]["action"] = $setvar->getAction();
198  $answers[$ident]["points_unchecked"] = $setvar->getContent();
199  }
200  }
201  }
202  }
203  }
204  $this->addGeneralMetadata($item);
205  $this->object->setTitle($item->getTitle());
206  $this->object->setNrOfTries((int) $item->getMaxattempts());
207  $this->object->setComment($item->getComment());
208  $this->object->setAuthor($item->getAuthor());
209  $this->object->setOwner($ilUser->getId());
210  $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
211  $this->object->setObjId($questionpool_id);
212  $this->object->setShuffle($shuffle);
213  $thumb_size = (int) $item->getMetadataEntry("thumb_size");
214  if ($thumb_size !== null && $thumb_size >= $this->object->getMinimumThumbSize()) {
215  $this->object->setThumbSize($thumb_size);
216  }
217  foreach ($answers as $answer) {
218  if ($item->getMetadataEntry('singleline') || (is_array($answer["imagefile"]) && count($answer["imagefile"]) > 0)) {
219  $this->object->setIsSingleline(true);
220  }
221  if (isset($answer["imagefile"]["label"])) {
222  $this->object->addAnswer(
223  $answer["answertext"],
224  $answer["points"],
225  $answer["answerorder"],
226  $answer["imagefile"]["label"]
227  );
228  } else {
229  $this->object->addAnswer($answer["answertext"], $answer["points"], $answer["answerorder"]);
230  }
231  }
232  // additional content editing mode information
233  $this->object->setAdditionalContentEditingMode(
235  );
236  $this->object->saveToDb();
237  foreach ($answers as $answer) {
238  if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0)) {
239  $image = base64_decode($answer["imagefile"]["content"]);
240  $imagepath = $this->object->getImagePath();
241  if (!file_exists($imagepath)) {
242  ilFileUtils::makeDirParents($imagepath);
243  }
244  $imagepath .= $answer["imagefile"]["label"];
245  $fh = fopen($imagepath, "wb");
246  if ($fh !== false) {
247  $imagefile = fwrite($fh, $image);
248  fclose($fh);
249  $this->object->generateThumbForFile(
250  $answer["imagefile"]["label"],
251  $this->object->getImagePath(),
252  $this->object->getThumbSize()
253  );
254  }
255  }
256  }
257 
258  $feedbackSetting = $item->getMetadataEntry('feedback_setting');
259  if (!is_null($feedbackSetting)) {
260  $this->object->feedbackOBJ->saveSpecificFeedbackSetting($this->object->getId(), $feedbackSetting);
261  }
262 
263  foreach ($feedbacks as $ident => $material) {
264  $m = $this->QTIMaterialToString($material);
265  $feedbacks[$ident] = $m;
266  }
267  foreach ($feedbacksgeneric as $correctness => $material) {
268  $m = $this->QTIMaterialToString($material);
269  $feedbacksgeneric[$correctness] = $m;
270  }
271  // handle the import of media objects in XHTML code
272  $questiontext = $this->object->getQuestion();
273  $answers = &$this->object->getAnswers();
274  if (is_array(ilSession::get("import_mob_xhtml"))) {
275  foreach (ilSession::get("import_mob_xhtml") as $mob) {
276  if ($tst_id > 0) {
277  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
278  } else {
279  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
280  }
281 
282  global $DIC; /* @var ILIAS\DI\Container $DIC */
283  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
284 
285  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
286  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
287  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
288  foreach ($answers as $key => $value) {
289  $answer_obj = $answers[$key];
290  $answer_obj->setAnswertext(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()));
291  }
292  foreach ($feedbacks as $ident => $material) {
293  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
294  }
295  foreach ($feedbacksgeneric as $correctness => $material) {
296  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
297  }
298  }
299  }
300  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
301  foreach ($answers as $key => $value) {
302  $answer_obj = &$answers[$key];
303  $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 1));
304  }
305  foreach ($feedbacks as $ident => $material) {
306  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
307  $this->object->getId(),
308  0,
309  $ident,
311  );
312  }
313  foreach ($feedbacksgeneric as $correctness => $material) {
314  $this->object->feedbackOBJ->importGenericFeedback(
315  $this->object->getId(),
316  $correctness,
318  );
319  }
320  $this->object->saveToDb();
321  if (count($item->suggested_solutions)) {
322  foreach ($item->suggested_solutions as $suggested_solution) {
324  $this->object->getId(),
325  $suggested_solution["solution"]->getContent(),
326  $suggested_solution["gap_index"]
327  );
328  }
329  }
330  if ($tst_id > 0) {
331  $q_1_id = $this->object->getId();
332  $question_id = $this->object->duplicate(true, "", "", -1, $tst_id);
333  $tst_object->questions[$question_counter++] = $question_id;
334  $import_mapping[$item->getIdent()] = ["pool" => $q_1_id, "test" => $question_id];
335  } else {
336  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
337  }
338  return $import_mapping;
339  }
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: