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