ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMultipleChoiceExport.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, MULTIPLE_CHOICE_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, "thumb_size");
84  $a_xml_writer->xmlElement("fieldentry", NULL, $this->object->getThumbSize());
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  $attrs = array(
101  "ident" => "MCMR",
102  "rcardinality" => "Multiple"
103  );
104  $a_xml_writer->xmlStartTag("response_lid", $attrs);
105  $solution = $this->object->getSuggestedSolution(0);
106  if (count($solution))
107  {
108  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
109  {
110  $a_xml_writer->xmlStartTag("material");
111  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
112  if (strcmp($matches[1], "") != 0)
113  {
114  $intlink = $solution["internal_link"];
115  }
116  $attrs = array(
117  "label" => "suggested_solution"
118  );
119  $a_xml_writer->xmlElement("mattext", $attrs, $intlink);
120  $a_xml_writer->xmlEndTag("material");
121  }
122  }
123  // shuffle output
124  $attrs = array();
125  if ($this->object->getShuffle())
126  {
127  $attrs = array(
128  "shuffle" => "Yes"
129  );
130  }
131  else
132  {
133  $attrs = array(
134  "shuffle" => "No"
135  );
136  }
137  $a_xml_writer->xmlStartTag("render_choice", $attrs);
138  $answers =& $this->object->getAnswers();
139  $akeys = array_keys($answers);
140  if ($this->object->getShuffle() && $a_shuffle)
141  {
142  $akeys = $this->object->pcArrayShuffle($akeys);
143  }
144  // add answers
145  foreach ($akeys as $index)
146  {
147  $answer = $answers[$index];
148  $attrs = array(
149  "ident" => $index
150  );
151  $a_xml_writer->xmlStartTag("response_label", $attrs);
152 
153  if (strlen($answer->getImage()))
154  {
155  $this->object->addQTIMaterial($a_xml_writer, $answer->getAnswertext(), FALSE, FALSE);
156  $imagetype = "image/jpeg";
157  if (preg_match("/.*\.(png|gif)$/", $answer->getImage(), $matches))
158  {
159  $imagetype = "image/" . $matches[1];
160  }
161  if ($force_image_references)
162  {
163  $attrs = array(
164  "imagtype" => $imagetype,
165  "label" => $answer->getImage(),
166  "uri" => $this->object->getImagePathWeb() . $answer->getImage()
167  );
168  $a_xml_writer->xmlElement("matimage", $attrs);
169  }
170  else
171  {
172  $imagepath = $this->object->getImagePath() . $answer->getImage();
173  $fh = @fopen($imagepath, "rb");
174  if ($fh != false)
175  {
176  $imagefile = fread($fh, filesize($imagepath));
177  fclose($fh);
178  $base64 = base64_encode($imagefile);
179  $attrs = array(
180  "imagtype" => $imagetype,
181  "label" => $answer->getImage(),
182  "embedded" => "base64"
183  );
184  $a_xml_writer->xmlElement("matimage", $attrs, $base64, FALSE, FALSE);
185  }
186  }
187  $a_xml_writer->xmlEndTag("material");
188  }
189  else
190  {
191  $this->object->addQTIMaterial($a_xml_writer, $answer->getAnswertext());
192  }
193  $a_xml_writer->xmlEndTag("response_label");
194  }
195  $a_xml_writer->xmlEndTag("render_choice");
196  $a_xml_writer->xmlEndTag("response_lid");
197  $a_xml_writer->xmlEndTag("flow");
198  $a_xml_writer->xmlEndTag("presentation");
199 
200  // PART II: qti resprocessing
201  $a_xml_writer->xmlStartTag("resprocessing");
202  $a_xml_writer->xmlStartTag("outcomes");
203  $a_xml_writer->xmlStartTag("decvar");
204  $a_xml_writer->xmlEndTag("decvar");
205  $a_xml_writer->xmlEndTag("outcomes");
206  // add response conditions
207  foreach ($answers as $index => $answer)
208  {
209  $attrs = array(
210  "continue" => "Yes"
211  );
212  $a_xml_writer->xmlStartTag("respcondition", $attrs);
213  // qti conditionvar
214  $a_xml_writer->xmlStartTag("conditionvar");
215  $attrs = array();
216  $attrs = array(
217  "respident" => "MCMR"
218  );
219  $a_xml_writer->xmlElement("varequal", $attrs, $index);
220  $a_xml_writer->xmlEndTag("conditionvar");
221  // qti setvar
222  $attrs = array(
223  "action" => "Add"
224  );
225  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
226  // qti displayfeedback
227  $linkrefid = "response_$index";
228  $attrs = array(
229  "feedbacktype" => "Response",
230  "linkrefid" => $linkrefid
231  );
232  $a_xml_writer->xmlElement("displayfeedback", $attrs);
233  $a_xml_writer->xmlEndTag("respcondition");
234  $attrs = array(
235  "continue" => "Yes"
236  );
237  $a_xml_writer->xmlStartTag("respcondition", $attrs);
238 
239  // qti conditionvar
240  $a_xml_writer->xmlStartTag("conditionvar");
241  $attrs = array();
242  $attrs = array(
243  "respident" => "MCMR"
244  );
245  $a_xml_writer->xmlStartTag("not");
246  $a_xml_writer->xmlElement("varequal", $attrs, $index);
247  $a_xml_writer->xmlEndTag("not");
248  $a_xml_writer->xmlEndTag("conditionvar");
249  // qti setvar
250  $attrs = array(
251  "action" => "Add"
252  );
253  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPointsUnchecked());
254  $a_xml_writer->xmlEndTag("respcondition");
255  }
256  $feedback_allcorrect = $this->object->getFeedbackGeneric(1);
257  if (strlen($feedback_allcorrect))
258  {
259  $attrs = array(
260  "continue" => "Yes"
261  );
262  $a_xml_writer->xmlStartTag("respcondition", $attrs);
263  // qti conditionvar
264  $a_xml_writer->xmlStartTag("conditionvar");
265  foreach ($answers as $index => $answer)
266  {
267  if ($answer->getPointsChecked() < $answer->getPointsUnchecked())
268  {
269  $a_xml_writer->xmlStartTag("not");
270  }
271  $attrs = array(
272  "respident" => "MCMR"
273  );
274  $a_xml_writer->xmlElement("varequal", $attrs, $index);
275  if ($answer->getPointsChecked() < $answer->getPointsUnchecked())
276  {
277  $a_xml_writer->xmlEndTag("not");
278  }
279  }
280  $a_xml_writer->xmlEndTag("conditionvar");
281  // qti displayfeedback
282  $attrs = array(
283  "feedbacktype" => "Response",
284  "linkrefid" => "response_allcorrect"
285  );
286  $a_xml_writer->xmlElement("displayfeedback", $attrs);
287  $a_xml_writer->xmlEndTag("respcondition");
288  }
289  $feedback_onenotcorrect = $this->object->getFeedbackGeneric(0);
290  if (strlen($feedback_onenotcorrect))
291  {
292  $attrs = array(
293  "continue" => "Yes"
294  );
295  $a_xml_writer->xmlStartTag("respcondition", $attrs);
296  // qti conditionvar
297  $a_xml_writer->xmlStartTag("conditionvar");
298  foreach ($answers as $index => $answer)
299  {
300  if ($index > 0)
301  {
302  $a_xml_writer->xmlStartTag("or");
303  }
304  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked())
305  {
306  $a_xml_writer->xmlStartTag("not");
307  }
308  $attrs = array(
309  "respident" => "MCMR"
310  );
311  $a_xml_writer->xmlElement("varequal", $attrs, $index);
312  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked())
313  {
314  $a_xml_writer->xmlEndTag("not");
315  }
316  if ($index > 0)
317  {
318  $a_xml_writer->xmlEndTag("or");
319  }
320  }
321  $a_xml_writer->xmlEndTag("conditionvar");
322  // qti displayfeedback
323  $attrs = array(
324  "feedbacktype" => "Response",
325  "linkrefid" => "response_onenotcorrect"
326  );
327  $a_xml_writer->xmlElement("displayfeedback", $attrs);
328  $a_xml_writer->xmlEndTag("respcondition");
329  }
330  $a_xml_writer->xmlEndTag("resprocessing");
331 
332  // PART III: qti itemfeedback
333  foreach ($answers as $index => $answer)
334  {
335  $linkrefid = "response_$index";
336  $attrs = array(
337  "ident" => $linkrefid,
338  "view" => "All"
339  );
340  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
341  // qti flow_mat
342  $a_xml_writer->xmlStartTag("flow_mat");
343  $this->object->addQTIMaterial($a_xml_writer, $this->object->getFeedbackSingleAnswer($index));
344  $a_xml_writer->xmlEndTag("flow_mat");
345  $a_xml_writer->xmlEndTag("itemfeedback");
346  }
347  if (strlen($feedback_allcorrect))
348  {
349  $attrs = array(
350  "ident" => "response_allcorrect",
351  "view" => "All"
352  );
353  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
354  // qti flow_mat
355  $a_xml_writer->xmlStartTag("flow_mat");
356  $this->object->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
357  $a_xml_writer->xmlEndTag("flow_mat");
358  $a_xml_writer->xmlEndTag("itemfeedback");
359  }
360  if (strlen($feedback_onenotcorrect))
361  {
362  $attrs = array(
363  "ident" => "response_onenotcorrect",
364  "view" => "All"
365  );
366  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
367  // qti flow_mat
368  $a_xml_writer->xmlStartTag("flow_mat");
369  $this->object->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
370  $a_xml_writer->xmlEndTag("flow_mat");
371  $a_xml_writer->xmlEndTag("itemfeedback");
372  }
373 
374  $a_xml_writer->xmlEndTag("item");
375  $a_xml_writer->xmlEndTag("questestinterop");
376 
377  $xml = $a_xml_writer->xmlDumpMem(FALSE);
378  if (!$a_include_header)
379  {
380  $pos = strpos($xml, "?>");
381  $xml = substr($xml, $pos + 2);
382  }
383  return $xml;
384  }
385 }
386 
387 ?>