ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.assTextQuestionImport.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
5 
16 {
20  public $object;
21 
35  public function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
36  {
37  global $DIC;
38  $ilUser = $DIC['ilUser'];
39 
40  // empty session variable for imported xhtml mobs
41  unset($_SESSION["import_mob_xhtml"]);
42  $presentation = $item->getPresentation();
43  $duration = $item->getDuration();
44  $now = getdate();
45  $maxchars = 0;
46  $maxpoints = 0;
47  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
48  foreach ($presentation->order as $entry) {
49  switch ($entry["type"]) {
50  case "response":
51  $response = $presentation->response[$entry["index"]];
52  $rendertype = $response->getRenderType();
53  switch (strtolower(get_class($rendertype))) {
54  case "ilqtirenderfib":
55  $maxchars = $rendertype->getMaxchars();
56  break;
57  }
58  break;
59  }
60  }
61 
62  $feedbacksgeneric = array();
63  foreach ($item->resprocessing as $resprocessing) {
64  $outcomes = $resprocessing->getOutcomes();
65  foreach ($outcomes->decvar as $decvar) {
66  $maxpoints = $decvar->getMaxvalue();
67  }
68 
69  foreach ($resprocessing->respcondition as $respcondition) {
70  foreach ($respcondition->displayfeedback as $feedbackpointer) {
71  if (strlen($feedbackpointer->getLinkrefid())) {
72  foreach ($item->itemfeedback as $ifb) {
73  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
74  // found a feedback for the identifier
75  if (count($ifb->material)) {
76  foreach ($ifb->material as $material) {
77  $feedbacksgeneric[1] = $material;
78  }
79  }
80  if ((count($ifb->flow_mat) > 0)) {
81  foreach ($ifb->flow_mat as $fmat) {
82  if (count($fmat->material)) {
83  foreach ($fmat->material as $material) {
84  $feedbacksgeneric[1] = $material;
85  }
86  }
87  }
88  }
89  } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
90  // found a feedback for the identifier
91  if (count($ifb->material)) {
92  foreach ($ifb->material as $material) {
93  $feedbacksgeneric[0] = $material;
94  }
95  }
96  if ((count($ifb->flow_mat) > 0)) {
97  foreach ($ifb->flow_mat as $fmat) {
98  if (count($fmat->material)) {
99  foreach ($fmat->material as $material) {
100  $feedbacksgeneric[0] = $material;
101  }
102  }
103  }
104  }
105  }
106  }
107  }
108  }
109  }
110  }
111  $this->addGeneralMetadata($item);
112  $this->object->setTitle($item->getTitle());
113  $this->object->setNrOfTries($item->getMaxattempts());
114  $this->object->setComment($item->getComment());
115  $this->object->setAuthor($item->getAuthor());
116  $this->object->setOwner($ilUser->getId());
117  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
118  $this->object->setObjId($questionpool_id);
119  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
120  $this->object->setPoints($maxpoints);
121  $this->object->setMaxNumOfChars($maxchars);
122  $this->object->setWordCounterEnabled((bool) $item->getMetadataEntry('wordcounter'));
123  $textrating = $item->getMetadataEntry("textrating");
124  if (strlen($textrating)) {
125  $this->object->setTextRating($textrating);
126  }
127  $this->object->matchcondition = (strlen($item->getMetadataEntry('matchcondition'))) ? $item->getMetadataEntry('matchcondition') : 0;
128 
129  require_once './Modules/TestQuestionPool/classes/class.assAnswerMultipleResponseImage.php';
130  $no_keywords_found = true;
131 
132  $termscoring = $this->fetchTermScoring($item);
133  for ($i = 0; $i < count($termscoring); $i++) {
134  $this->object->addAnswer($termscoring[$i]->getAnswertext(), $termscoring[$i]->getPoints());
135  $no_keywords_found = false;
136  }
137  if (count($termscoring)) {
138  $this->object->setKeywordRelation($item->getMetadataEntry('termrelation'));
139  }
140 
141  $keywords = $item->getMetadataEntry("keywords");
142  if (strlen($keywords)) {
143  #$this->object->setKeywords($keywords);
144  $answers = explode(' ', $keywords);
145  foreach ($answers as $answer) {
146  $this->object->addAnswer($answer, 0);
147  }
148  $this->object->setKeywordRelation('one');
149  $no_keywords_found = false;
150  }
151  if ($no_keywords_found) {
152  $this->object->setKeywordRelation('non');
153  }
154 
155  // additional content editing mode information
156  $this->object->setAdditionalContentEditingMode(
158  );
159  $this->object->saveToDb();
160  if (count($item->suggested_solutions)) {
161  foreach ($item->suggested_solutions as $suggested_solution) {
162  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
163  }
164  $this->object->saveToDb();
165  }
166  foreach ($feedbacksgeneric as $correctness => $material) {
167  $m = $this->object->QTIMaterialToString($material);
168  $feedbacksgeneric[$correctness] = $m;
169  }
170  // handle the import of media objects in XHTML code
171  $questiontext = $this->object->getQuestion();
172 
173  $feedbacks = $this->getFeedbackAnswerSpecific($item);
174 
175  if (is_array($_SESSION["import_mob_xhtml"])) {
176  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
177  include_once "./Services/RTE/classes/class.ilRTE.php";
178  foreach ($_SESSION["import_mob_xhtml"] as $mob) {
179  if ($tst_id > 0) {
180  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
181  } else {
182  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
183  }
184 
185  global $DIC; /* @var ILIAS\DI\Container $DIC */
186  $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
187 
188  $media_object = &ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
189  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
190  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
191  foreach ($feedbacks as $ident => $material) {
192  $feedbacks[$ident] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
193  }
194  foreach ($feedbacksgeneric as $correctness => $material) {
195  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
196  }
197  }
198  }
199  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
200  foreach ($feedbacks as $ident => $material) {
201  $index = $this->fetchIndexFromFeedbackIdent($ident);
202 
203  $this->object->feedbackOBJ->importSpecificAnswerFeedback(
204  $this->object->getId(),
205  0,
206  $index,
208  );
209  }
210  foreach ($feedbacksgeneric as $correctness => $material) {
211  $this->object->feedbackOBJ->importGenericFeedback(
212  $this->object->getId(),
213  $correctness,
215  );
216  }
217  $this->object->saveToDb();
218  if ($tst_id > 0) {
219  $q_1_id = $this->object->getId();
220  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
221  $tst_object->questions[$question_counter++] = $question_id;
222  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
223  } else {
224  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
225  }
226  }
227 
228  protected function fetchTermScoring($item)
229  {
230  $termScoringString = $item->getMetadataEntry('termscoring');
231 
232  if (!strlen($termScoringString)) {
233  return array();
234  }
235 
236  $termScoring = unserialize($termScoringString);
237 
238  if (is_array($termScoring)) {
239  return $termScoring;
240  }
241 
242  $termScoringString = base64_decode($termScoringString);
243  $termScoring = unserialize($termScoringString);
244 
245  if (is_array($termScoring)) {
246  return $termScoring;
247  }
248 
249  return array();
250  }
251 }
getFeedbackAnswerSpecific(ilQTIItem $item, $prefix='response_')
addGeneralMetadata(ilQTIItem $item)
const IL_INST_ID
Definition: constants.php:38
$_SESSION["AccountId"]
Class for essay question imports.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
getQplImportArchivDirectory()
returns the full path to extracted qpl import archiv (qpl import dir + qpl archiv subdir) ...
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=true)
Create new media object and update page in db and return new media object.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
$index
Definition: metadata.php:128
global $DIC
Definition: goto.php:24
fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
Creates a question from a QTI file.
getTstImportArchivDirectory()
returns the full path to extracted tst import archiv (tst import dir + tst archiv subdir) ...
$ilUser
Definition: imgupload.php:18
Class for question imports.
$response
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.
fetchIndexFromFeedbackIdent($feedbackIdent, $prefix='response_')
$i
Definition: metadata.php:24