ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assImagemapQuestionExport.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/export/qti12/class.assQuestionExport.php";
25 
36 {
46  function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
47  {
48  global $ilias;
49 
50  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
51  $a_xml_writer = new ilXmlWriter;
52  // set xml header
53  $a_xml_writer->xmlHeader();
54  $a_xml_writer->xmlStartTag("questestinterop");
55  $attrs = array(
56  "ident" => "il_".IL_INST_ID."_qst_".$this->object->getId(),
57  "title" => $this->object->getTitle(),
58  "maxattempts" => $this->object->getNrOfTries()
59  );
60  $a_xml_writer->xmlStartTag("item", $attrs);
61  // add question description
62  $a_xml_writer->xmlElement("qticomment", NULL, $this->object->getComment());
63  // add estimated working time
64  $workingtime = $this->object->getEstimatedWorkingTime();
65  $duration = sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]);
66  $a_xml_writer->xmlElement("duration", NULL, $duration);
67  // add ILIAS specific metadata
68  $a_xml_writer->xmlStartTag("itemmetadata");
69  $a_xml_writer->xmlStartTag("qtimetadata");
70  $a_xml_writer->xmlStartTag("qtimetadatafield");
71  $a_xml_writer->xmlElement("fieldlabel", NULL, "ILIAS_VERSION");
72  $a_xml_writer->xmlElement("fieldentry", NULL, $ilias->getSetting("ilias_version"));
73  $a_xml_writer->xmlEndTag("qtimetadatafield");
74  $a_xml_writer->xmlStartTag("qtimetadatafield");
75  $a_xml_writer->xmlElement("fieldlabel", NULL, "QUESTIONTYPE");
76  $a_xml_writer->xmlElement("fieldentry", NULL, IMAGEMAP_QUESTION_IDENTIFIER);
77  $a_xml_writer->xmlEndTag("qtimetadatafield");
78  $a_xml_writer->xmlStartTag("qtimetadatafield");
79  $a_xml_writer->xmlElement("fieldlabel", NULL, "AUTHOR");
80  $a_xml_writer->xmlElement("fieldentry", NULL, $this->object->getAuthor());
81  $a_xml_writer->xmlEndTag("qtimetadatafield");
82  $a_xml_writer->xmlEndTag("qtimetadata");
83  $a_xml_writer->xmlEndTag("itemmetadata");
84 
85  // PART I: qti presentation
86  $attrs = array(
87  "label" => $this->object->getTitle()
88  );
89  $a_xml_writer->xmlStartTag("presentation", $attrs);
90  // add flow to presentation
91  $a_xml_writer->xmlStartTag("flow");
92  // add material with question text to presentation
93  $this->object->addQTIMaterial($a_xml_writer, $this->object->getQuestion());
94  // add answers to presentation
95  $attrs = array(
96  "ident" => "IM",
97  "rcardinality" => "Single"
98  );
99  $a_xml_writer->xmlStartTag("response_xy", $attrs);
100  $solution = $this->object->getSuggestedSolution(0);
101  if (count($solution))
102  {
103  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
104  {
105  $a_xml_writer->xmlStartTag("material");
106  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
107  if (strcmp($matches[1], "") != 0)
108  {
109  $intlink = $solution["internal_link"];
110  }
111  $attrs = array(
112  "label" => "suggested_solution"
113  );
114  $a_xml_writer->xmlElement("mattext", $attrs, $intlink);
115  $a_xml_writer->xmlEndTag("material");
116  }
117  }
118  $a_xml_writer->xmlStartTag("render_hotspot");
119  $a_xml_writer->xmlStartTag("material");
120  $imagetype = "image/jpeg";
121  if (preg_match("/.*\.(png|gif)$/", $this->object->getImageFilename(), $matches))
122  {
123  $imagetype = "image/" . $matches[1];
124  }
125  $attrs = array(
126  "imagtype" => $imagetype,
127  "label" => $this->object->getImageFilename()
128  );
129  if ($a_include_binary)
130  {
131  if ($force_image_references)
132  {
133  $attrs["uri"] = $this->object->getImagePathWeb() . $this->object->getImageFilename();
134  $a_xml_writer->xmlElement("matimage", $attrs);
135  }
136  else
137  {
138  $attrs["embedded"] = "base64";
139  $imagepath = $this->object->getImagePath() . $this->object->getImageFilename();
140  $fh = fopen($imagepath, "rb");
141  if ($fh == false)
142  {
143  global $ilErr;
144  $ilErr->raiseError($GLOBALS['lng']->txt("error_open_image_file"), $ilErr->MESSAGE);
145  return;
146  }
147  $imagefile = fread($fh, filesize($imagepath));
148  fclose($fh);
149  $base64 = base64_encode($imagefile);
150  $a_xml_writer->xmlElement("matimage", $attrs, $base64, FALSE, FALSE);
151  }
152  }
153  else
154  {
155  $a_xml_writer->xmlElement("matimage", $attrs);
156  }
157  $a_xml_writer->xmlEndTag("material");
158 
159  // add answers
160  foreach ($this->object->getAnswers() as $index => $answer)
161  {
162  $rared = "";
163  switch ($answer->getArea())
164  {
165  case "rect":
166  $rarea = "Rectangle";
167  break;
168  case "circle":
169  $rarea = "Ellipse";
170  break;
171  case "poly":
172  $rarea = "Bounded";
173  break;
174  }
175  $attrs = array(
176  "ident" => $index,
177  "rarea" => $rarea
178  );
179  $a_xml_writer->xmlStartTag("response_label", $attrs);
180  $a_xml_writer->xmlData($answer->getCoords());
181  $a_xml_writer->xmlStartTag("material");
182  $a_xml_writer->xmlElement("mattext", NULL, $answer->getAnswertext());
183  $a_xml_writer->xmlEndTag("material");
184  $a_xml_writer->xmlEndTag("response_label");
185  }
186  $a_xml_writer->xmlEndTag("render_hotspot");
187  $a_xml_writer->xmlEndTag("response_xy");
188  $a_xml_writer->xmlEndTag("flow");
189  $a_xml_writer->xmlEndTag("presentation");
190 
191  // PART II: qti resprocessing
192  $a_xml_writer->xmlStartTag("resprocessing");
193  $a_xml_writer->xmlStartTag("outcomes");
194  $a_xml_writer->xmlStartTag("decvar");
195  $a_xml_writer->xmlEndTag("decvar");
196  $a_xml_writer->xmlEndTag("outcomes");
197  // add response conditions
198  foreach ($this->object->getAnswers() as $index => $answer)
199  {
200  $attrs = array(
201  "continue" => "Yes"
202  );
203  $a_xml_writer->xmlStartTag("respcondition", $attrs);
204  // qti conditionvar
205  $a_xml_writer->xmlStartTag("conditionvar");
206  if (!$answer->isStateSet())
207  {
208  $a_xml_writer->xmlStartTag("not");
209  }
210  $areatype = "";
211  switch ($answer->getArea())
212  {
213  case "rect":
214  $areatype = "Rectangle";
215  break;
216  case "circle":
217  $areatype = "Ellipse";
218  break;
219  case "poly":
220  $areatype = "Bounded";
221  break;
222  }
223  $attrs = array(
224  "respident" => "IM",
225  "areatype" => $areatype
226  );
227  $a_xml_writer->xmlElement("varinside", $attrs, $answer->getCoords());
228  if (!$answer->isStateSet())
229  {
230  $a_xml_writer->xmlEndTag("not");
231  }
232  $a_xml_writer->xmlEndTag("conditionvar");
233  // qti setvar
234  $attrs = array(
235  "action" => "Add"
236  );
237  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
238  $linkrefid = "response_$index";
239  $attrs = array(
240  "feedbacktype" => "Response",
241  "linkrefid" => $linkrefid
242  );
243  $a_xml_writer->xmlElement("displayfeedback", $attrs);
244  $a_xml_writer->xmlEndTag("respcondition");
245  }
246 
247  $answers = $this->object->getAnswers();
248  $feedback_allcorrect = $this->object->getFeedbackGeneric(1);
249  if (strlen($feedback_allcorrect))
250  {
251  $attrs = array(
252  "continue" => "Yes"
253  );
254  $a_xml_writer->xmlStartTag("respcondition", $attrs);
255  // qti conditionvar
256  $a_xml_writer->xmlStartTag("conditionvar");
257  $bestindex = 0;
258  $maxpoints = 0;
259  foreach ($answers as $index => $answer)
260  {
261  if ($answer->getPoints() > $maxpoints)
262  {
263  $maxpoints = $answer->getPoints();
264  $bestindex = $index;
265  }
266  }
267  $attrs = array(
268  "respident" => "IM"
269  );
270 
271  $areatype = "";
272  $answer = $answers[$bestindex];
273  switch ($answer->getArea())
274  {
275  case "rect":
276  $areatype = "Rectangle";
277  break;
278  case "circle":
279  $areatype = "Ellipse";
280  break;
281  case "poly":
282  $areatype = "Bounded";
283  break;
284  }
285  $attrs = array(
286  "respident" => "IM",
287  "areatype" => $areatype
288  );
289  $a_xml_writer->xmlElement("varinside", $attrs, $answer->getCoords());
290 
291  $a_xml_writer->xmlEndTag("conditionvar");
292  // qti displayfeedback
293  $attrs = array(
294  "feedbacktype" => "Response",
295  "linkrefid" => "response_allcorrect"
296  );
297  $a_xml_writer->xmlElement("displayfeedback", $attrs);
298  $a_xml_writer->xmlEndTag("respcondition");
299  }
300 
301  $feedback_onenotcorrect = $this->object->getFeedbackGeneric(0);
302  if (strlen($feedback_onenotcorrect))
303  {
304  $attrs = array(
305  "continue" => "Yes"
306  );
307  $a_xml_writer->xmlStartTag("respcondition", $attrs);
308  // qti conditionvar
309  $a_xml_writer->xmlStartTag("conditionvar");
310  $bestindex = 0;
311  $maxpoints = 0;
312  foreach ($answers as $index => $answer)
313  {
314  if ($answer->getPoints() > $maxpoints)
315  {
316  $maxpoints = $answer->getPoints();
317  $bestindex = $index;
318  }
319  }
320  $attrs = array(
321  "respident" => "IM"
322  );
323  $a_xml_writer->xmlStartTag("not");
324 
325  $areatype = "";
326  $answer = $answers[$bestindex];
327  switch ($answer->getArea())
328  {
329  case "rect":
330  $areatype = "Rectangle";
331  break;
332  case "circle":
333  $areatype = "Ellipse";
334  break;
335  case "poly":
336  $areatype = "Bounded";
337  break;
338  }
339  $attrs = array(
340  "respident" => "IM",
341  "areatype" => $areatype
342  );
343  $a_xml_writer->xmlElement("varinside", $attrs, $answer->getCoords());
344 
345  $a_xml_writer->xmlEndTag("not");
346  $a_xml_writer->xmlEndTag("conditionvar");
347  // qti displayfeedback
348  $attrs = array(
349  "feedbacktype" => "Response",
350  "linkrefid" => "response_onenotcorrect"
351  );
352  $a_xml_writer->xmlElement("displayfeedback", $attrs);
353  $a_xml_writer->xmlEndTag("respcondition");
354  }
355 
356  $a_xml_writer->xmlEndTag("resprocessing");
357 
358  // PART III: qti itemfeedback
359  foreach ($this->object->getAnswers() as $index => $answer)
360  {
361  $linkrefid = "";
362  $linkrefid = "response_$index";
363  $attrs = array(
364  "ident" => $linkrefid,
365  "view" => "All"
366  );
367  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
368  // qti flow_mat
369  $a_xml_writer->xmlStartTag("flow_mat");
370  $this->object->addQTIMaterial($a_xml_writer, $this->object->getFeedbackSingleAnswer($index));
371  $a_xml_writer->xmlEndTag("flow_mat");
372  $a_xml_writer->xmlEndTag("itemfeedback");
373  }
374  if (strlen($feedback_allcorrect))
375  {
376  $attrs = array(
377  "ident" => "response_allcorrect",
378  "view" => "All"
379  );
380  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
381  // qti flow_mat
382  $a_xml_writer->xmlStartTag("flow_mat");
383  $this->object->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
384  $a_xml_writer->xmlEndTag("flow_mat");
385  $a_xml_writer->xmlEndTag("itemfeedback");
386  }
387  if (strlen($feedback_onenotcorrect))
388  {
389  $attrs = array(
390  "ident" => "response_onenotcorrect",
391  "view" => "All"
392  );
393  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
394  // qti flow_mat
395  $a_xml_writer->xmlStartTag("flow_mat");
396  $this->object->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
397  $a_xml_writer->xmlEndTag("flow_mat");
398  $a_xml_writer->xmlEndTag("itemfeedback");
399  }
400 
401  $a_xml_writer->xmlEndTag("item");
402  $a_xml_writer->xmlEndTag("questestinterop");
403 
404  $xml = $a_xml_writer->xmlDumpMem(FALSE);
405  if (!$a_include_header)
406  {
407  $pos = strpos($xml, "?>");
408  $xml = substr($xml, $pos + 2);
409  }
410  return $xml;
411  }
412 }
413 
414 ?>