ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assSingleChoiceExport.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, SINGLE_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" => "MCSR",
106  "rcardinality" => "Single"
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" => "MCSR"
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  }
239 
240  $feedback_allcorrect = $this->object->getFeedbackGeneric(1);
241  if (strlen($feedback_allcorrect))
242  {
243  $attrs = array(
244  "continue" => "Yes"
245  );
246  $a_xml_writer->xmlStartTag("respcondition", $attrs);
247  // qti conditionvar
248  $a_xml_writer->xmlStartTag("conditionvar");
249  $bestindex = 0;
250  $maxpoints = 0;
251  foreach ($answers as $index => $answer)
252  {
253  if ($answer->getPoints() > $maxpoints)
254  {
255  $maxpoints = $answer->getPoints();
256  $bestindex = $index;
257  }
258  }
259  $attrs = array(
260  "respident" => "MCSR"
261  );
262  $a_xml_writer->xmlElement("varequal", $attrs, $bestindex);
263  $a_xml_writer->xmlEndTag("conditionvar");
264  // qti displayfeedback
265  $attrs = array(
266  "feedbacktype" => "Response",
267  "linkrefid" => "response_allcorrect"
268  );
269  $a_xml_writer->xmlElement("displayfeedback", $attrs);
270  $a_xml_writer->xmlEndTag("respcondition");
271  }
272 
273  $feedback_onenotcorrect = $this->object->getFeedbackGeneric(0);
274  if (strlen($feedback_onenotcorrect))
275  {
276  $attrs = array(
277  "continue" => "Yes"
278  );
279  $a_xml_writer->xmlStartTag("respcondition", $attrs);
280  // qti conditionvar
281  $a_xml_writer->xmlStartTag("conditionvar");
282  $bestindex = 0;
283  $maxpoints = 0;
284  foreach ($answers as $index => $answer)
285  {
286  if ($answer->getPoints() > $maxpoints)
287  {
288  $maxpoints = $answer->getPoints();
289  $bestindex = $index;
290  }
291  }
292  $attrs = array(
293  "respident" => "MCSR"
294  );
295  $a_xml_writer->xmlStartTag("not");
296  $a_xml_writer->xmlElement("varequal", $attrs, $bestindex);
297  $a_xml_writer->xmlEndTag("not");
298  $a_xml_writer->xmlEndTag("conditionvar");
299  // qti displayfeedback
300  $attrs = array(
301  "feedbacktype" => "Response",
302  "linkrefid" => "response_onenotcorrect"
303  );
304  $a_xml_writer->xmlElement("displayfeedback", $attrs);
305  $a_xml_writer->xmlEndTag("respcondition");
306  }
307 
308  $a_xml_writer->xmlEndTag("resprocessing");
309 
310  // PART III: qti itemfeedback
311  foreach ($answers as $index => $answer)
312  {
313  $linkrefid = "response_$index";
314  $attrs = array(
315  "ident" => $linkrefid,
316  "view" => "All"
317  );
318  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
319  // qti flow_mat
320  $a_xml_writer->xmlStartTag("flow_mat");
321  $this->object->addQTIMaterial($a_xml_writer, $this->object->getFeedbackSingleAnswer($index));
322  $a_xml_writer->xmlEndTag("flow_mat");
323  $a_xml_writer->xmlEndTag("itemfeedback");
324  }
325  if (strlen($feedback_allcorrect))
326  {
327  $attrs = array(
328  "ident" => "response_allcorrect",
329  "view" => "All"
330  );
331  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
332  // qti flow_mat
333  $a_xml_writer->xmlStartTag("flow_mat");
334  $this->object->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
335  $a_xml_writer->xmlEndTag("flow_mat");
336  $a_xml_writer->xmlEndTag("itemfeedback");
337  }
338  if (strlen($feedback_onenotcorrect))
339  {
340  $attrs = array(
341  "ident" => "response_onenotcorrect",
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, $feedback_onenotcorrect);
348  $a_xml_writer->xmlEndTag("flow_mat");
349  $a_xml_writer->xmlEndTag("itemfeedback");
350  }
351 
352  $a_xml_writer->xmlEndTag("item");
353  $a_xml_writer->xmlEndTag("questestinterop");
354 
355  $xml = $a_xml_writer->xmlDumpMem(FALSE);
356  if (!$a_include_header)
357  {
358  $pos = strpos($xml, "?>");
359  $xml = substr($xml, $pos + 2);
360  }
361  return $xml;
362  }
363 
364 }
365 
366 ?>