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