ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.assTextQuestionImport.php
Go to the documentation of this file.
1 <?php
2 
29 {
33  public $object;
34 
35  public function fromXML(
36  string $importdirectory,
37  int $user_id,
38  ilQTIItem $item,
39  int $questionpool_id,
40  ?int $tst_id,
41  ?ilObject &$tst_object,
42  int &$question_counter,
43  array $import_mapping
44  ): array {
45  // empty session variable for imported xhtml mobs
46  ilSession::clear('import_mob_xhtml');
47 
48  $presentation = $item->getPresentation();
49  $maxchars = 0;
50  $maxpoints = 0;
51  foreach ($presentation->order as $entry) {
52  switch ($entry["type"]) {
53  case "response":
54  $response = $presentation->response[$entry["index"]];
55  $rendertype = $response->getRenderType();
56  switch (strtolower(get_class($rendertype))) {
57  case "ilqtirenderfib":
58  $maxchars = $rendertype->getMaxchars();
59  break;
60  }
61  break;
62  }
63  }
64 
65  $feedbacksgeneric = [];
66  foreach ($item->resprocessing as $resprocessing) {
67  $outcomes = $resprocessing->getOutcomes();
68  foreach ($outcomes->decvar as $decvar) {
69  $maxpoints = $decvar->getMaxvalue();
70  }
71 
72  foreach ($resprocessing->respcondition as $respcondition) {
73  foreach ($respcondition->displayfeedback as $feedbackpointer) {
74  if (strlen($feedbackpointer->getLinkrefid())) {
75  foreach ($item->itemfeedback as $ifb) {
76  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
77  // found a feedback for the identifier
78  if (count($ifb->material)) {
79  foreach ($ifb->material as $material) {
80  $feedbacksgeneric[1] = $material;
81  }
82  }
83  if ((count($ifb->flow_mat) > 0)) {
84  foreach ($ifb->flow_mat as $fmat) {
85  if (count($fmat->material)) {
86  foreach ($fmat->material as $material) {
87  $feedbacksgeneric[1] = $material;
88  }
89  }
90  }
91  }
92  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
93  // found a feedback for the identifier
94  if (count($ifb->material)) {
95  foreach ($ifb->material as $material) {
96  $feedbacksgeneric[0] = $material;
97  }
98  }
99  if ((count($ifb->flow_mat) > 0)) {
100  foreach ($ifb->flow_mat as $fmat) {
101  if (count($fmat->material)) {
102  foreach ($fmat->material as $material) {
103  $feedbacksgeneric[0] = $material;
104  }
105  }
106  }
107  }
108  }
109  }
110  }
111  }
112  }
113  }
114  $this->addGeneralMetadata($item);
115  $this->object->setTitle($item->getTitle());
116  $this->object->setNrOfTries((int) $item->getMaxattempts());
117  $this->object->setComment($item->getComment());
118  $this->object->setAuthor($item->getAuthor());
119  $this->object->setOwner($user_id);
120  $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
121  $this->object->setObjId($questionpool_id);
122  $this->object->setPoints($maxpoints);
123  $this->object->setMaxNumOfChars($maxchars ?? 0);
124  $this->object->setWordCounterEnabled((bool) $item->getMetadataEntry('wordcounter'));
125  $textrating = $item->getMetadataEntry("textrating");
126  if (strlen($textrating)) {
127  $this->object->setTextRating($textrating);
128  }
129  $this->object->setMatchcondition((strlen($item->getMetadataEntry('matchcondition'))) ? (int) $item->getMetadataEntry('matchcondition') : 0);
130 
131  $no_keywords_found = true;
132 
133  if ($item->getMetadataEntry('termrelation') !== 'non'
134  && $item->getMetadataEntry('termrelation') !== null) {
135  $termscoring = $this->fetchTermScoring($item);
136  for ($i = 0, $iMax = count($termscoring); $i < $iMax; $i++) {
137  $this->object->addAnswer($termscoring[$i]->getAnswertext(), $termscoring[$i]->getPoints());
138  $no_keywords_found = false;
139  }
140  }
141 
142  if ($item->getMetadataEntry('termrelation') !== null) {
143  $this->object->setKeywordRelation($item->getMetadataEntry('termrelation'));
144  }
145 
146  $keywords = $item->getMetadataEntry("keywords");
147  if ($keywords !== null) {
148  $answers = explode(' ', $keywords);
149  foreach ($answers as $answer) {
150  $this->object->addAnswer($answer, 0);
151  }
152  $this->object->setKeywordRelation('one');
153  $no_keywords_found = false;
154  }
155  if ($no_keywords_found) {
156  $this->object->setKeywordRelation('non');
157  }
158 
159  // additional content editing mode information
160  $this->object->setAdditionalContentEditingMode(
162  );
163  $this->object->saveToDb();
164  $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
165  foreach ($feedbacksgeneric as $correctness => $material) {
166  $m = $this->QTIMaterialToString($material);
167  $feedbacksgeneric[$correctness] = $m;
168  }
169  // handle the import of media objects in XHTML code
170  $questiontext = $this->object->getQuestion();
171 
172  $feedbacks = $this->getFeedbackAnswerSpecific($item);
173 
174  if (is_array(ilSession::get("import_mob_xhtml"))) {
175  foreach (ilSession::get("import_mob_xhtml") as $mob) {
176  $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
177 
178  global $DIC; /* @var ILIAS\DI\Container $DIC */
179  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
180 
181  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
182  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
183  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
184  foreach ($feedbacks as $ident => $material) {
185  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
186  }
187  foreach ($feedbacksgeneric as $correctness => $material) {
188  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
189  }
190  }
191  }
192  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
193  foreach ($feedbacks as $ident => $material) {
194  $index = $this->fetchIndexFromFeedbackIdent($ident);
195 
196  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
197  $this->object->getId(),
198  0,
199  $index,
201  );
202  }
203  foreach ($feedbacksgeneric as $correctness => $material) {
204  $this->object->feedbackOBJ->importGenericFeedback(
205  $this->object->getId(),
206  $correctness,
208  );
209  }
210  $this->object->saveToDb();
211  if ($tst_id > 0) {
212  $q_1_id = $this->object->getId();
213  $question_id = $this->object->duplicate(true, "", "", -1, $tst_id);
214  $tst_object->questions[$question_counter++] = $question_id;
215  $import_mapping[$item->getIdent()] = ["pool" => $q_1_id, "test" => $question_id];
216  } else {
217  $import_mapping[$item->getIdent()] = ["pool" => $this->object->getId(), "test" => 0];
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 }
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)
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
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.
getMetadataEntry(string $a_label)
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_...
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
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
fromXML(string $importdirectory, int $user_id, ilQTIItem $item, int $questionpool_id, ?int $tst_id, ?ilObject &$tst_object, int &$question_counter, array $import_mapping)
global $DIC
Definition: shib_login.php:22
importSuggestedSolutions(int $question_id, array $solution_from_import)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Class for question imports.
static clear(string $a_var)
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')