ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.assTextQuestionImport.php
Go to the documentation of this file.
1<?php
2
31{
35 public $object;
36
37 public function fromXML(
38 string $importdirectory,
39 int $user_id,
40 ilQTIItem $item,
41 int $questionpool_id,
42 ?int $tst_id,
43 ?ilObject &$tst_object,
44 int &$question_counter,
45 array $import_mapping
46 ): array {
47 // empty session variable for imported xhtml mobs
48 ilSession::clear('import_mob_xhtml');
49
50 $presentation = $item->getPresentation();
51 $maxchars = 0;
52 $maxpoints = 0;
53 foreach ($presentation->order as $entry) {
54 switch ($entry["type"]) {
55 case "response":
56 $response = $presentation->response[$entry["index"]];
57 $rendertype = $response->getRenderType();
58 switch (strtolower(get_class($rendertype))) {
59 case "ilqtirenderfib":
60 $maxchars = $rendertype->getMaxchars();
61 break;
62 }
63 break;
64 }
65 }
66
67 $feedbacksgeneric = [];
68 foreach ($item->resprocessing as $resprocessing) {
69 $outcomes = $resprocessing->getOutcomes();
70 foreach ($outcomes->decvar as $decvar) {
71 $maxpoints = $decvar->getMaxvalue();
72 }
73
74 foreach ($resprocessing->respcondition as $respcondition) {
75 foreach ($respcondition->displayfeedback as $feedbackpointer) {
76 if (strlen($feedbackpointer->getLinkrefid())) {
77 foreach ($item->itemfeedback as $ifb) {
78 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
79 // found a feedback for the identifier
80 if (count($ifb->material)) {
81 foreach ($ifb->material as $material) {
82 $feedbacksgeneric[1] = $material;
83 }
84 }
85 if ((count($ifb->flow_mat) > 0)) {
86 foreach ($ifb->flow_mat as $fmat) {
87 if (count($fmat->material)) {
88 foreach ($fmat->material as $material) {
89 $feedbacksgeneric[1] = $material;
90 }
91 }
92 }
93 }
94 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
95 // found a feedback for the identifier
96 if (count($ifb->material)) {
97 foreach ($ifb->material as $material) {
98 $feedbacksgeneric[0] = $material;
99 }
100 }
101 if ((count($ifb->flow_mat) > 0)) {
102 foreach ($ifb->flow_mat as $fmat) {
103 if (count($fmat->material)) {
104 foreach ($fmat->material as $material) {
105 $feedbacksgeneric[0] = $material;
106 }
107 }
108 }
109 }
110 }
111 }
112 }
113 }
114 }
115 }
116 $this->addGeneralMetadata($item);
117 $this->object->setTitle($item->getTitle());
118 $this->object->setNrOfTries((int) $item->getMaxattempts());
119 $this->object->setComment($item->getComment());
120 $this->object->setAuthor($item->getAuthor());
121 $this->object->setOwner($user_id);
122 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
123 $this->object->setObjId($questionpool_id);
124 $this->object->setPoints($maxpoints);
125 $this->object->setMaxNumOfChars($maxchars ?? 0);
126 $this->object->setWordCounterEnabled((bool) $item->getMetadataEntry('wordcounter'));
127 $textrating = $item->getMetadataEntry("textrating");
128 if (strlen($textrating)) {
129 $this->object->setTextRating($textrating);
130 }
131 $this->object->setMatchcondition((strlen($item->getMetadataEntry('matchcondition'))) ? (int) $item->getMetadataEntry('matchcondition') : 0);
132
133 $no_keywords_found = true;
134
135 if ($item->getMetadataEntry('termrelation') !== 'non'
136 && $item->getMetadataEntry('termrelation') !== null) {
137 $termscoring = $this->fetchTermScoring($item);
138 for ($i = 0, $iMax = count($termscoring); $i < $iMax; $i++) {
139 $this->object->addAnswer($termscoring[$i]->getAnswertext(), $termscoring[$i]->getPoints());
140 $no_keywords_found = false;
141 }
142 }
143
144 if ($item->getMetadataEntry('termrelation') !== null) {
145 $this->object->setKeywordRelation($item->getMetadataEntry('termrelation'));
146 }
147
148 $keywords = $item->getMetadataEntry("keywords");
149 if ($keywords !== null) {
150 $answers = explode(' ', $keywords);
151 foreach ($answers as $answer) {
152 $this->object->addAnswer($answer, 0);
153 }
154 $this->object->setKeywordRelation('one');
155 $no_keywords_found = false;
156 }
157 if ($no_keywords_found) {
158 $this->object->setKeywordRelation('non');
159 }
160
161 // additional content editing mode information
162 $this->object->setAdditionalContentEditingMode(
164 );
165 $this->object->saveToDb();
166 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
167 foreach ($feedbacksgeneric as $correctness => $material) {
168 $m = $this->QTIMaterialToString($material);
169 $feedbacksgeneric[$correctness] = $m;
170 }
171 // handle the import of media objects in XHTML code
172 $questiontext = $this->object->getQuestion();
173
174 $feedbacks = $this->getFeedbackAnswerSpecific($item);
175
176 if (is_array(ilSession::get("import_mob_xhtml"))) {
177 foreach (ilSession::get("import_mob_xhtml") as $mob) {
178 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
179
180 global $DIC; /* @var ILIAS\DI\Container $DIC */
181 $DIC['ilLog']->info('import mob from dir: ' . $importfile);
182
183 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
184 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
185 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
186 foreach ($feedbacks as $ident => $material) {
187 $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
188 }
189 foreach ($feedbacksgeneric as $correctness => $material) {
190 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
191 }
192 }
193 }
194 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
195 foreach ($feedbacks as $ident => $material) {
196 $index = $this->fetchIndexFromFeedbackIdent($ident);
197
198 $this->object->feedbackOBJ->importSpecificAnswerFeedback(
199 $this->object->getId(),
200 0,
201 $index,
203 );
204 }
205 foreach ($feedbacksgeneric as $correctness => $material) {
206 $this->object->feedbackOBJ->importGenericFeedback(
207 $this->object->getId(),
208 $correctness,
210 );
211 }
212 $this->object->saveToDb();
213 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
214 $questionpool_id,
215 $tst_id,
216 $question_counter,
217 $tst_object
218 );
219 return $import_mapping;
220 }
221
222 protected function fetchTermScoring($item): array
223 {
224 $termScoringString = $item->getMetadataEntry('termscoring');
225
226 if (!strlen($termScoringString)) {
227 return [];
228 }
229
230 $termScoring = @unserialize($termScoringString, ['allowed_classes' => [ASS_AnswerMultipleResponseImage::class]]);
231
232 if (is_array($termScoring)) {
233 return $termScoring;
234 }
235
236 $termScoringString = base64_decode($termScoringString);
237 $termScoring = unserialize($termScoringString, ['allowed_classes' => [ASS_AnswerMultipleResponseImage::class]]);
238
239 if (is_array($termScoring)) {
240 return $termScoring;
241 }
242
243 return [];
244 }
245}
Class for question imports.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
addQuestionToParentObjectAndBuildMappingEntry(int $questionpool_id, ?int $tst_id, int &$question_counter, ?ilObjTest &$tst_object)
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
addGeneralMetadata(ilQTIItem $item)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
importSuggestedSolutions(int $question_id, array $solution_from_import)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fromXML(string $importdirectory, int $user_id, ilQTIItem $item, int $questionpool_id, ?int $tst_id, ?ilObject &$tst_object, int &$question_counter, array $import_mapping)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
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.
Class ilObject Basic functions for all objects.
getMetadataEntry(string $a_label)
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)
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:92