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