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