ILIAS  release_8 Revision v8.24
class.assTextQuestionImport.php
Go to the documentation of this file.
1<?php
2
29{
33 public $object;
34
48 public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping): array
49 {
50 global $DIC;
51 $ilUser = $DIC['ilUser'];
52
53 // empty session variable for imported xhtml mobs
54 ilSession::clear('import_mob_xhtml');
55
56 $presentation = $item->getPresentation();
57 $now = getdate();
58 $maxchars = 0;
59 $maxpoints = 0;
60 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
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 "ilqtirenderfib":
68 $maxchars = $rendertype->getMaxchars();
69 break;
70 }
71 break;
72 }
73 }
74
75 $feedbacksgeneric = array();
76 foreach ($item->resprocessing as $resprocessing) {
77 $outcomes = $resprocessing->getOutcomes();
78 foreach ($outcomes->decvar as $decvar) {
79 $maxpoints = $decvar->getMaxvalue();
80 }
81
82 foreach ($resprocessing->respcondition as $respcondition) {
83 foreach ($respcondition->displayfeedback as $feedbackpointer) {
84 if (strlen($feedbackpointer->getLinkrefid())) {
85 foreach ($item->itemfeedback as $ifb) {
86 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
87 // found a feedback for the identifier
88 if (count($ifb->material)) {
89 foreach ($ifb->material as $material) {
90 $feedbacksgeneric[1] = $material;
91 }
92 }
93 if ((count($ifb->flow_mat) > 0)) {
94 foreach ($ifb->flow_mat as $fmat) {
95 if (count($fmat->material)) {
96 foreach ($fmat->material as $material) {
97 $feedbacksgeneric[1] = $material;
98 }
99 }
100 }
101 }
102 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
103 // found a feedback for the identifier
104 if (count($ifb->material)) {
105 foreach ($ifb->material as $material) {
106 $feedbacksgeneric[0] = $material;
107 }
108 }
109 if ((count($ifb->flow_mat) > 0)) {
110 foreach ($ifb->flow_mat as $fmat) {
111 if (count($fmat->material)) {
112 foreach ($fmat->material as $material) {
113 $feedbacksgeneric[0] = $material;
114 }
115 }
116 }
117 }
118 }
119 }
120 }
121 }
122 }
123 }
124 $this->addGeneralMetadata($item);
125 $this->object->setTitle($item->getTitle());
126 $this->object->setNrOfTries((int) $item->getMaxattempts());
127 $this->object->setComment($item->getComment());
128 $this->object->setAuthor($item->getAuthor());
129 $this->object->setOwner($ilUser->getId());
130 $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
131 $this->object->setObjId($questionpool_id);
132 $this->object->setPoints($maxpoints);
133 $this->object->setMaxNumOfChars($maxchars);
134 $this->object->setWordCounterEnabled((bool) $item->getMetadataEntry('wordcounter'));
135 $textrating = $item->getMetadataEntry("textrating");
136 if (strlen($textrating)) {
137 $this->object->setTextRating($textrating);
138 }
139 $this->object->setMatchcondition((strlen($item->getMetadataEntry('matchcondition'))) ? (int) $item->getMetadataEntry('matchcondition') : 0);
140
141 $no_keywords_found = true;
142
143 if ($item->getMetadataEntry('termrelation') !== 'non'
144 && $item->getMetadataEntry('termrelation') !== null) {
145 $termscoring = $this->fetchTermScoring($item);
146 for ($i = 0, $iMax = count($termscoring); $i < $iMax; $i++) {
147 $this->object->addAnswer($termscoring[$i]->getAnswertext(), $termscoring[$i]->getPoints());
148 $no_keywords_found = false;
149 }
150 }
151
152 if ($item->getMetadataEntry('termrelation') !== null) {
153 $this->object->setKeywordRelation($item->getMetadataEntry('termrelation'));
154 }
155
156 $keywords = $item->getMetadataEntry("keywords");
157 if (strlen($keywords)) {
158 #$this->object->setKeywords($keywords);
159 $answers = explode(' ', $keywords);
160 foreach ($answers as $answer) {
161 $this->object->addAnswer($answer, 0);
162 }
163 $this->object->setKeywordRelation('one');
164 $no_keywords_found = false;
165 }
166 if ($no_keywords_found) {
167 $this->object->setKeywordRelation('non');
168 }
169
170 // additional content editing mode information
171 $this->object->setAdditionalContentEditingMode(
173 );
174 $this->object->saveToDb();
175 if (count($item->suggested_solutions)) {
176 foreach ($item->suggested_solutions as $suggested_solution) {
177 $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
178 }
179 $this->object->saveToDb();
180 }
181 foreach ($feedbacksgeneric as $correctness => $material) {
182 $m = $this->object->QTIMaterialToString($material);
183 $feedbacksgeneric[$correctness] = $m;
184 }
185 // handle the import of media objects in XHTML code
186 $questiontext = $this->object->getQuestion();
187
188 $feedbacks = $this->getFeedbackAnswerSpecific($item);
189
190 if (is_array(ilSession::get("import_mob_xhtml"))) {
191 foreach (ilSession::get("import_mob_xhtml") as $mob) {
192 if ($tst_id > 0) {
193 $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
194 } else {
195 $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
196 }
197
198 global $DIC; /* @var ILIAS\DI\Container $DIC */
199 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
200
201 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
202 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
203 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
204 foreach ($feedbacks as $ident => $material) {
205 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
206 }
207 foreach ($feedbacksgeneric as $correctness => $material) {
208 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
209 }
210 }
211 }
212 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
213 foreach ($feedbacks as $ident => $material) {
214 $index = $this->fetchIndexFromFeedbackIdent($ident);
215
216 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
217 $this->object->getId(),
218 0,
219 $index,
221 );
222 }
223 foreach ($feedbacksgeneric as $correctness => $material) {
224 $this->object->feedbackOBJ->importGenericFeedback(
225 $this->object->getId(),
226 $correctness,
228 );
229 }
230 $this->object->saveToDb();
231 if ($tst_id > 0) {
232 $q_1_id = $this->object->getId();
233 $question_id = $this->object->duplicate(true, "", "", "", $tst_id);
234 $tst_object->questions[$question_counter++] = $question_id;
235 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
236 } else {
237 $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
238 }
239 return $import_mapping;
240 }
241
242 protected function fetchTermScoring($item): array
243 {
244 $termScoringString = $item->getMetadataEntry('termscoring');
245
246 if (!strlen($termScoringString)) {
247 return array();
248 }
249
250 $termScoring = @unserialize($termScoringString, ['allowed_classes' => [ASS_AnswerMultipleResponseImage::class]]);
251
252 if (is_array($termScoring)) {
253 return $termScoring;
254 }
255
256 $termScoringString = base64_decode($termScoringString);
257 $termScoring = unserialize($termScoringString, ['allowed_classes' => [ASS_AnswerMultipleResponseImage::class]]);
258
259 if (is_array($termScoring)) {
260 return $termScoring;
261 }
262
263 return array();
264 }
265}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir)
addGeneralMetadata(ilQTIItem $item)
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, $import_mapping)
Creates a question from a QTI file.
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
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.
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)
static clear(string $a_var)
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
int $created
Timestamp for when the object was created.
Definition: System.php:151
$response