ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assTextQuestionImport.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/TestQuestionPool/classes/import/qti12/class.assQuestionImport.php";
25 
36 {
50  function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
51  {
52  global $ilUser;
53 
54  // empty session variable for imported xhtml mobs
55  unset($_SESSION["import_mob_xhtml"]);
56  $presentation = $item->getPresentation();
57  $duration = $item->getDuration();
58  $now = getdate();
59  $maxchars = 0;
60  $maxpoints = 0;
61  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
62  foreach ($presentation->order as $entry)
63  {
64  switch ($entry["type"])
65  {
66  case "response":
67  $response = $presentation->response[$entry["index"]];
68  $rendertype = $response->getRenderType();
69  switch (strtolower(get_class($rendertype)))
70  {
71  case "ilqtirenderfib":
72  $maxchars = $rendertype->getMaxchars();
73  break;
74  }
75  break;
76  }
77  }
78 
79  $feedbacksgeneric = array();
80  foreach ($item->resprocessing as $resprocessing)
81  {
82  $outcomes = $resprocessing->getOutcomes();
83  foreach ($outcomes->decvar as $decvar)
84  {
85  $maxpoints = $decvar->getMaxvalue();
86  }
87 
88  foreach ($resprocessing->respcondition as $respcondition)
89  {
90  foreach ($respcondition->displayfeedback as $feedbackpointer)
91  {
92  if (strlen($feedbackpointer->getLinkrefid()))
93  {
94  foreach ($item->itemfeedback as $ifb)
95  {
96  if (strcmp($ifb->getIdent(), "response_allcorrect") == 0)
97  {
98  // found a feedback for the identifier
99  if (count($ifb->material))
100  {
101  foreach ($ifb->material as $material)
102  {
103  $feedbacksgeneric[1] = $material;
104  }
105  }
106  if ((count($ifb->flow_mat) > 0))
107  {
108  foreach ($ifb->flow_mat as $fmat)
109  {
110  if (count($fmat->material))
111  {
112  foreach ($fmat->material as $material)
113  {
114  $feedbacksgeneric[1] = $material;
115  }
116  }
117  }
118  }
119  }
120  else if (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0)
121  {
122  // found a feedback for the identifier
123  if (count($ifb->material))
124  {
125  foreach ($ifb->material as $material)
126  {
127  $feedbacksgeneric[0] = $material;
128  }
129  }
130  if ((count($ifb->flow_mat) > 0))
131  {
132  foreach ($ifb->flow_mat as $fmat)
133  {
134  if (count($fmat->material))
135  {
136  foreach ($fmat->material as $material)
137  {
138  $feedbacksgeneric[0] = $material;
139  }
140  }
141  }
142  }
143  }
144  }
145  }
146  }
147  }
148  }
149 
150  $this->object->setTitle($item->getTitle());
151  $this->object->setNrOfTries($item->getMaxattempts());
152  $this->object->setComment($item->getComment());
153  $this->object->setAuthor($item->getAuthor());
154  $this->object->setOwner($ilUser->getId());
155  $this->object->setQuestion($this->object->QTIMaterialToString($item->getQuestiontext()));
156  $this->object->setObjId($questionpool_id);
157  $this->object->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
158  $this->object->setPoints($maxpoints);
159  $this->object->setMaxNumOfChars($maxchars);
160  $textrating = $item->getMetadataEntry("textrating");
161  if (strlen($textrating))
162  {
163  $this->object->setTextRating($textrating);
164  }
165  $this->object->matchcondition = (strlen($item->getMetadataEntry('matchcondition'))) ? $item->getMetadataEntry('matchcondition') : 0;
166 
167  require_once './Modules/TestQuestionPool/classes/class.assAnswerMultipleResponseImage.php';
168  $no_keywords_found=true;
169 
170  $termscoring = $this->fetchTermScoring($item);
171  $termscoring = ( is_array($termscoring) ? $termscoring : array() );
172  for ($i = 0; $i < count($termscoring); $i++ )
173  {
174  $this->object->addAnswer($termscoring[$i]->getAnswertext(), $termscoring[$i]->getPoints() );
175  $no_keywords_found=false;
176  }
177  if(count($termscoring))
178  {
179  $this->object->setKeywordRelation($item->getMetadataEntry('termrelation'));
180  }
181 
182  $keywords = $item->getMetadataEntry("keywords");
183  if (strlen($keywords))
184  {
185  #$this->object->setKeywords($keywords);
186  $answers = explode(' ', $keywords);
187  foreach ($answers as $answer)
188  {
189  $this->object->addAnswer($answer, 0);
190  }
191  $this->object->setKeywordRelation('one');
192  $no_keywords_found=false;
193  }
194  if($no_keywords_found)
195  {
196  $this->object->setKeywordRelation('non');
197  }
198 
199  $this->object->saveToDb();
200  if (count($item->suggested_solutions))
201  {
202  foreach ($item->suggested_solutions as $suggested_solution)
203  {
204  $this->object->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
205  }
206  $this->object->saveToDb();
207  }
208  foreach ($feedbacksgeneric as $correctness => $material)
209  {
210  $m = $this->object->QTIMaterialToString($material);
211  $feedbacksgeneric[$correctness] = $m;
212  }
213  // handle the import of media objects in XHTML code
214  $questiontext = $this->object->getQuestion();
215  if (is_array($_SESSION["import_mob_xhtml"]))
216  {
217  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
218  include_once "./Services/RTE/classes/class.ilRTE.php";
219  foreach ($_SESSION["import_mob_xhtml"] as $mob)
220  {
221  if ($tst_id > 0)
222  {
223  $importfile = $this->getTstImportArchivDirectory() . '/' . $mob["uri"];
224  }
225  else
226  {
227  $importfile = $this->getQplImportArchivDirectory() . '/' . $mob["uri"];
228  }
229 
230  $GLOBALS['ilLog']->write(__METHOD__.': import mob from dir: '. $importfile);
231 
232  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
233  ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
234  $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
235  foreach ($feedbacksgeneric as $correctness => $material)
236  {
237  $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
238  }
239  }
240  }
241  $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
242  foreach ($feedbacksgeneric as $correctness => $material)
243  {
244  $this->object->saveFeedbackGeneric($correctness, ilRTE::_replaceMediaObjectImageSrc($material, 1));
245  }
246  $this->object->saveToDb();
247  if ($tst_id > 0)
248  {
249  $q_1_id = $this->object->getId();
250  $question_id = $this->object->duplicate(true, null, null, null, $tst_id);
251  $tst_object->questions[$question_counter++] = $question_id;
252  $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
253  }
254  else
255  {
256  $import_mapping[$item->getIdent()] = array("pool" => $this->object->getId(), "test" => 0);
257  }
258  }
259 
260  protected function fetchTermScoring($item)
261  {
262  $termScoringString = $item->getMetadataEntry('termscoring');
263 
264  if( !strlen($termScoringString) )
265  {
266  return array();
267  }
268 
269  $termScoring = unserialize($termScoringString);
270 
271  if( is_array($termScoring) )
272  {
273  return $termScoring;
274  }
275 
276  $termScoringString = base64_decode($termScoringString);
277  $termScoring = unserialize($termScoringString);
278 
279  if( is_array($termScoring) )
280  {
281  return $termScoring;
282  }
283 
284  return array();
285  }
286 }
287 
288 ?>