ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.assMultipleChoiceExport.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/export/qti12/class.assQuestionExport.php";
5 
16 {
26  public function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
27  {
28  global $ilias;
29 
30  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
31  $a_xml_writer = new ilXmlWriter;
32  // set xml header
33  $a_xml_writer->xmlHeader();
34  $a_xml_writer->xmlStartTag("questestinterop");
35  $attrs = array(
36  "ident" => "il_" . IL_INST_ID . "_qst_" . $this->object->getId(),
37  "title" => $this->object->getTitle(),
38  "maxattempts" => $this->object->getNrOfTries()
39  );
40  $a_xml_writer->xmlStartTag("item", $attrs);
41  // add question description
42  $a_xml_writer->xmlElement("qticomment", null, $this->object->getComment());
43  // add estimated working time
44  $workingtime = $this->object->getEstimatedWorkingTime();
45  $duration = sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]);
46  $a_xml_writer->xmlElement("duration", null, $duration);
47  // add ILIAS specific metadata
48  $a_xml_writer->xmlStartTag("itemmetadata");
49  $a_xml_writer->xmlStartTag("qtimetadata");
50  $a_xml_writer->xmlStartTag("qtimetadatafield");
51  $a_xml_writer->xmlElement("fieldlabel", null, "ILIAS_VERSION");
52  $a_xml_writer->xmlElement("fieldentry", null, $ilias->getSetting("ilias_version"));
53  $a_xml_writer->xmlEndTag("qtimetadatafield");
54  $a_xml_writer->xmlStartTag("qtimetadatafield");
55  $a_xml_writer->xmlElement("fieldlabel", null, "QUESTIONTYPE");
56  $a_xml_writer->xmlElement("fieldentry", null, MULTIPLE_CHOICE_QUESTION_IDENTIFIER);
57  $a_xml_writer->xmlEndTag("qtimetadatafield");
58  $a_xml_writer->xmlStartTag("qtimetadatafield");
59  $a_xml_writer->xmlElement("fieldlabel", null, "AUTHOR");
60  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getAuthor());
61  $a_xml_writer->xmlEndTag("qtimetadatafield");
62 
63  // additional content editing information
64  $this->addAdditionalContentEditingModeInformation($a_xml_writer);
65  $this->addGeneralMetadata($a_xml_writer);
66 
67  $a_xml_writer->xmlStartTag("qtimetadatafield");
68  $a_xml_writer->xmlElement("fieldlabel", null, "thumb_size");
69  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getThumbSize());
70  $a_xml_writer->xmlEndTag("qtimetadatafield");
71 
72  $a_xml_writer->xmlStartTag("qtimetadatafield");
73  $a_xml_writer->xmlElement("fieldlabel", null, "feedback_setting");
74  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getSpecificFeedbackSetting());
75  $a_xml_writer->xmlEndTag("qtimetadatafield");
76 
77  $this->addQtiMetaDataField($a_xml_writer, 'singleline', $this->object->isSingleline ? 1 : 0);
78 
79  $a_xml_writer->xmlEndTag("qtimetadata");
80  $a_xml_writer->xmlEndTag("itemmetadata");
81 
82  // PART I: qti presentation
83  $attrs = array(
84  "label" => $this->object->getTitle()
85  );
86  $a_xml_writer->xmlStartTag("presentation", $attrs);
87  // add flow to presentation
88  $a_xml_writer->xmlStartTag("flow");
89  // add material with question text to presentation
90  $this->object->addQTIMaterial($a_xml_writer, $this->object->getQuestion());
91  // add answers to presentation
92  $attrs = array();
93  $attrs = array(
94  "ident" => "MCMR",
95  "rcardinality" => "Multiple"
96  );
97  $a_xml_writer->xmlStartTag("response_lid", $attrs);
98  $solution = $this->object->getSuggestedSolution(0);
99  if (count($solution)) {
100  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches)) {
101  $a_xml_writer->xmlStartTag("material");
102  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
103  if (strcmp($matches[1], "") != 0) {
104  $intlink = $solution["internal_link"];
105  }
106  $attrs = array(
107  "label" => "suggested_solution"
108  );
109  $a_xml_writer->xmlElement("mattext", $attrs, $intlink);
110  $a_xml_writer->xmlEndTag("material");
111  }
112  }
113  // shuffle output and max choice
114  $attrs = array('shuffle' => $this->object->getShuffle() ? 'Yes' : 'No');
115  if ($this->object->getSelectionLimit()) {
116  $attrs['minnumber'] = '0';
117  $attrs['maxnumber'] = (string) $this->object->getSelectionLimit();
118  }
119  $a_xml_writer->xmlStartTag("render_choice", $attrs);
120  $answers =&$this->object->getAnswers();
121  $akeys = array_keys($answers);
122  if ($this->object->getShuffle() && $a_shuffle) {
123  $akeys = $this->object->pcArrayShuffle($akeys);
124  }
125  // add answers
126  foreach ($akeys as $index) {
127  $answer = $answers[$index];
128  $attrs = array(
129  "ident" => $index
130  );
131  $a_xml_writer->xmlStartTag("response_label", $attrs);
132 
133  if (strlen($answer->getImage())) {
134  $this->object->addQTIMaterial($a_xml_writer, $answer->getAnswertext(), false, false);
135  $imagetype = "image/jpeg";
136  if (preg_match("/.*\.(png|gif)$/", $answer->getImage(), $matches)) {
137  $imagetype = "image/" . $matches[1];
138  }
139  if ($force_image_references) {
140  $attrs = array(
141  "imagtype" => $imagetype,
142  "label" => $answer->getImage(),
143  "uri" => $this->object->getImagePathWeb() . $answer->getImage()
144  );
145  $a_xml_writer->xmlElement("matimage", $attrs);
146  } else {
147  $imagepath = $this->object->getImagePath() . $answer->getImage();
148  $fh = @fopen($imagepath, "rb");
149  if ($fh != false) {
150  $imagefile = fread($fh, filesize($imagepath));
151  fclose($fh);
152  $base64 = base64_encode($imagefile);
153  $attrs = array(
154  "imagtype" => $imagetype,
155  "label" => $answer->getImage(),
156  "embedded" => "base64"
157  );
158  $a_xml_writer->xmlElement("matimage", $attrs, $base64, false, false);
159  }
160  }
161  $a_xml_writer->xmlEndTag("material");
162  } else {
163  $this->object->addQTIMaterial($a_xml_writer, $answer->getAnswertext());
164  }
165  $a_xml_writer->xmlEndTag("response_label");
166  }
167  $a_xml_writer->xmlEndTag("render_choice");
168  $a_xml_writer->xmlEndTag("response_lid");
169  $a_xml_writer->xmlEndTag("flow");
170  $a_xml_writer->xmlEndTag("presentation");
171 
172  // PART II: qti resprocessing
173  $a_xml_writer->xmlStartTag("resprocessing");
174  $a_xml_writer->xmlStartTag("outcomes");
175  $a_xml_writer->xmlStartTag("decvar");
176  $a_xml_writer->xmlEndTag("decvar");
177  $a_xml_writer->xmlEndTag("outcomes");
178  // add response conditions
179  foreach ($answers as $index => $answer) {
180  $attrs = array(
181  "continue" => "Yes"
182  );
183  $a_xml_writer->xmlStartTag("respcondition", $attrs);
184  // qti conditionvar
185  $a_xml_writer->xmlStartTag("conditionvar");
186  $attrs = array();
187  $attrs = array(
188  "respident" => "MCMR"
189  );
190  $a_xml_writer->xmlElement("varequal", $attrs, $index);
191  $a_xml_writer->xmlEndTag("conditionvar");
192  // qti setvar
193  $attrs = array(
194  "action" => "Add"
195  );
196  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
197  // qti displayfeedback
198  $linkrefid = "response_$index";
199  $attrs = array(
200  "feedbacktype" => "Response",
201  "linkrefid" => $linkrefid
202  );
203  $a_xml_writer->xmlElement("displayfeedback", $attrs);
204  $a_xml_writer->xmlEndTag("respcondition");
205  $attrs = array(
206  "continue" => "Yes"
207  );
208  $a_xml_writer->xmlStartTag("respcondition", $attrs);
209 
210  // qti conditionvar
211  $a_xml_writer->xmlStartTag("conditionvar");
212  $attrs = array();
213  $attrs = array(
214  "respident" => "MCMR"
215  );
216  $a_xml_writer->xmlStartTag("not");
217  $a_xml_writer->xmlElement("varequal", $attrs, $index);
218  $a_xml_writer->xmlEndTag("not");
219  $a_xml_writer->xmlEndTag("conditionvar");
220  // qti setvar
221  $attrs = array(
222  "action" => "Add"
223  );
224  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPointsUnchecked());
225  $a_xml_writer->xmlEndTag("respcondition");
226  }
227  $feedback_allcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
228  $this->object->getId(),
229  true
230  );
231  if (strlen($feedback_allcorrect)) {
232  $attrs = array(
233  "continue" => "Yes"
234  );
235  $a_xml_writer->xmlStartTag("respcondition", $attrs);
236  // qti conditionvar
237  $a_xml_writer->xmlStartTag("conditionvar");
238  foreach ($answers as $index => $answer) {
239  if ($answer->getPointsChecked() < $answer->getPointsUnchecked()) {
240  $a_xml_writer->xmlStartTag("not");
241  }
242  $attrs = array(
243  "respident" => "MCMR"
244  );
245  $a_xml_writer->xmlElement("varequal", $attrs, $index);
246  if ($answer->getPointsChecked() < $answer->getPointsUnchecked()) {
247  $a_xml_writer->xmlEndTag("not");
248  }
249  }
250  $a_xml_writer->xmlEndTag("conditionvar");
251  // qti displayfeedback
252  $attrs = array(
253  "feedbacktype" => "Response",
254  "linkrefid" => "response_allcorrect"
255  );
256  $a_xml_writer->xmlElement("displayfeedback", $attrs);
257  $a_xml_writer->xmlEndTag("respcondition");
258  }
259  $feedback_onenotcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
260  $this->object->getId(),
261  false
262  );
263  if (strlen($feedback_onenotcorrect)) {
264  $attrs = array(
265  "continue" => "Yes"
266  );
267  $a_xml_writer->xmlStartTag("respcondition", $attrs);
268  // qti conditionvar
269  $a_xml_writer->xmlStartTag("conditionvar");
270  foreach ($answers as $index => $answer) {
271  if ($index > 0) {
272  $a_xml_writer->xmlStartTag("or");
273  }
274  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked()) {
275  $a_xml_writer->xmlStartTag("not");
276  }
277  $attrs = array(
278  "respident" => "MCMR"
279  );
280  $a_xml_writer->xmlElement("varequal", $attrs, $index);
281  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked()) {
282  $a_xml_writer->xmlEndTag("not");
283  }
284  if ($index > 0) {
285  $a_xml_writer->xmlEndTag("or");
286  }
287  }
288  $a_xml_writer->xmlEndTag("conditionvar");
289  // qti displayfeedback
290  $attrs = array(
291  "feedbacktype" => "Response",
292  "linkrefid" => "response_onenotcorrect"
293  );
294  $a_xml_writer->xmlElement("displayfeedback", $attrs);
295  $a_xml_writer->xmlEndTag("respcondition");
296  }
297  $a_xml_writer->xmlEndTag("resprocessing");
298 
299  // PART III: qti itemfeedback
300  foreach ($answers as $index => $answer) {
301  $linkrefid = "response_$index";
302  $attrs = array(
303  "ident" => $linkrefid,
304  "view" => "All"
305  );
306  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
307  // qti flow_mat
308  $a_xml_writer->xmlStartTag("flow_mat");
309  $fb = $this->object->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation(
310  $this->object->getId(),
311  $index
312  );
313  $this->object->addQTIMaterial($a_xml_writer, $fb);
314  $a_xml_writer->xmlEndTag("flow_mat");
315  $a_xml_writer->xmlEndTag("itemfeedback");
316  }
317  if (strlen($feedback_allcorrect)) {
318  $attrs = array(
319  "ident" => "response_allcorrect",
320  "view" => "All"
321  );
322  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
323  // qti flow_mat
324  $a_xml_writer->xmlStartTag("flow_mat");
325  $this->object->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
326  $a_xml_writer->xmlEndTag("flow_mat");
327  $a_xml_writer->xmlEndTag("itemfeedback");
328  }
329  if (strlen($feedback_onenotcorrect)) {
330  $attrs = array(
331  "ident" => "response_onenotcorrect",
332  "view" => "All"
333  );
334  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
335  // qti flow_mat
336  $a_xml_writer->xmlStartTag("flow_mat");
337  $this->object->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
338  $a_xml_writer->xmlEndTag("flow_mat");
339  $a_xml_writer->xmlEndTag("itemfeedback");
340  }
341 
342  $a_xml_writer->xmlEndTag("item");
343  $a_xml_writer->xmlEndTag("questestinterop");
344 
345  $xml = $a_xml_writer->xmlDumpMem(false);
346  if (!$a_include_header) {
347  $pos = strpos($xml, "?>");
348  $xml = substr($xml, $pos + 2);
349  }
350  return $xml;
351  }
352 }
const MULTIPLE_CHOICE_QUESTION_IDENTIFIER
Add rich text string
XML writer class.
Class for question exports.
$index
Definition: metadata.php:60
addGeneralMetadata(ilXmlWriter $xmlwriter)
addQtiMetaDataField(ilXmlWriter $a_xml_writer, $fieldLabel, $fieldValue)
adds a qti meta data field with given name and value to the passed xml writer (xml writer must be in ...
$xml
Definition: metadata.php:240
xmlHeader()
Writes xml header public.
Create styles array
The data for the language used.
addAdditionalContentEditingModeInformation(ilXmlWriter $a_xml_writer)
adds a qti meta data field for ilias specific information of "additional content editing mode" (xml w...
Create new PHPExcel object
obj_idprivate
Class for multiple choice question exports.
toXML($a_include_header=true, $a_include_binary=true, $a_shuffle=false, $test_output=false, $force_image_references=false)
Returns a QTI xml representation of the question.