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