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