ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.assMultipleChoiceExport.php
Go to the documentation of this file.
1 <?php
2 
29 {
36  public function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false): string
37  {
38  global $DIC;
39  $ilias = $DIC['ilias'];
40 
41  $a_xml_writer = new ilXmlWriter();
42  // set xml header
43  $a_xml_writer->xmlHeader();
44  $a_xml_writer->xmlStartTag("questestinterop");
45  $attrs = [
46  "ident" => "il_" . IL_INST_ID . "_qst_" . $this->object->getId(),
47  "title" => $this->object->getTitle(),
48  "maxattempts" => $this->object->getNrOfTries()
49  ];
50  $a_xml_writer->xmlStartTag("item", $attrs);
51  // add question description
52  $a_xml_writer->xmlElement("qticomment", null, $this->object->getComment());
53  $a_xml_writer->xmlStartTag("itemmetadata");
54  $a_xml_writer->xmlStartTag("qtimetadata");
55  $a_xml_writer->xmlStartTag("qtimetadatafield");
56  $a_xml_writer->xmlElement("fieldlabel", null, "ILIAS_VERSION");
57  $a_xml_writer->xmlElement("fieldentry", null, $ilias->getSetting("ilias_version"));
58  $a_xml_writer->xmlEndTag("qtimetadatafield");
59  $a_xml_writer->xmlStartTag("qtimetadatafield");
60  $a_xml_writer->xmlElement("fieldlabel", null, "QUESTIONTYPE");
61  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getQuestionType());
62  $a_xml_writer->xmlEndTag("qtimetadatafield");
63  $a_xml_writer->xmlStartTag("qtimetadatafield");
64  $a_xml_writer->xmlElement("fieldlabel", null, "AUTHOR");
65  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getAuthor());
66  $a_xml_writer->xmlEndTag("qtimetadatafield");
67 
68  // additional content editing information
69  $this->addAdditionalContentEditingModeInformation($a_xml_writer);
70  $this->addGeneralMetadata($a_xml_writer);
71 
72  $a_xml_writer->xmlStartTag("qtimetadatafield");
73  $a_xml_writer->xmlElement("fieldlabel", null, "thumb_size");
74  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getThumbSize());
75  $a_xml_writer->xmlEndTag("qtimetadatafield");
76 
77  $a_xml_writer->xmlStartTag("qtimetadatafield");
78  $a_xml_writer->xmlElement("fieldlabel", null, "feedback_setting");
79  $a_xml_writer->xmlElement("fieldentry", null, $this->object->getSpecificFeedbackSetting());
80  $a_xml_writer->xmlEndTag("qtimetadatafield");
81 
82  $this->addQtiMetaDataField($a_xml_writer, 'singleline', $this->object->isSingleline() ? 1 : 0);
83 
84  $a_xml_writer->xmlEndTag("qtimetadata");
85  $a_xml_writer->xmlEndTag("itemmetadata");
86 
87  // PART I: qti presentation
88  $attrs = [
89  "label" => $this->object->getTitle()
90  ];
91  $a_xml_writer->xmlStartTag("presentation", $attrs);
92  // add flow to presentation
93  $a_xml_writer->xmlStartTag("flow");
94  // add material with question text to presentation
95  $this->addQTIMaterial($a_xml_writer, $this->object->getQuestion());
96  // add answers to presentation
97  $attrs = [
98  "ident" => "MCMR",
99  "rcardinality" => "Multiple"
100  ];
101  $a_xml_writer->xmlStartTag("response_lid", $attrs);
102  $a_xml_writer = $this->addSuggestedSolution($a_xml_writer);
103  // shuffle output and max choice
104  $attrs = ['shuffle' => $this->object->getShuffle() ? 'Yes' : 'No'];
105  if ($this->object->getSelectionLimit()) {
106  $attrs['minnumber'] = '0';
107  $attrs['maxnumber'] = (string) $this->object->getSelectionLimit();
108  }
109  $a_xml_writer->xmlStartTag("render_choice", $attrs);
110  $answers = $this->object->getAnswers();
111  $akeys = array_keys($answers);
112  if ($this->object->getShuffle() && $a_shuffle) {
113  $akeys = shuffle($akeys);
114  }
115  // add answers
116  foreach ($akeys as $index) {
117  $answer = $answers[$index];
118  $attrs = [
119  "ident" => $index
120  ];
121  $a_xml_writer->xmlStartTag("response_label", $attrs);
122 
123  if ($answer->hasImage()) {
124  $this->addQTIMaterial($a_xml_writer, $answer->getAnswertext(), false, false);
125  $imagetype = "image/jpeg";
126  if (preg_match("/.*\.(png|gif)$/", $answer->getImage(), $matches)) {
127  $imagetype = "image/" . $matches[1];
128  }
129  if ($force_image_references) {
130  $attrs = [
131  "imagtype" => $imagetype,
132  "label" => $answer->getImage(),
133  "uri" => $this->object->getImagePathWeb() . $answer->getImage()
134  ];
135  $a_xml_writer->xmlElement("matimage", $attrs);
136  } else {
137  $imagepath = $this->object->getImagePath() . $answer->getImage();
138  $fh = @fopen($imagepath, "rb");
139  if ($fh != false) {
140  $imagefile = fread($fh, filesize($imagepath));
141  fclose($fh);
142  $base64 = base64_encode($imagefile);
143  $attrs = [
144  "imagtype" => $imagetype,
145  "label" => $answer->getImage(),
146  "embedded" => "base64"
147  ];
148  $a_xml_writer->xmlElement("matimage", $attrs, $base64, false, false);
149  }
150  }
151  $a_xml_writer->xmlEndTag("material");
152  } else {
153  $this->addQTIMaterial($a_xml_writer, $answer->getAnswertext());
154  }
155  $a_xml_writer->xmlEndTag("response_label");
156  }
157  $a_xml_writer->xmlEndTag("render_choice");
158  $a_xml_writer->xmlEndTag("response_lid");
159  $a_xml_writer->xmlEndTag("flow");
160  $a_xml_writer->xmlEndTag("presentation");
161 
162  // PART II: qti resprocessing
163  $a_xml_writer->xmlStartTag("resprocessing");
164  $a_xml_writer->xmlStartTag("outcomes");
165  $a_xml_writer->xmlStartTag("decvar");
166  $a_xml_writer->xmlEndTag("decvar");
167  $a_xml_writer->xmlEndTag("outcomes");
168  // add response conditions
169  foreach ($answers as $index => $answer) {
170  $attrs = [
171  "continue" => "Yes"
172  ];
173  $a_xml_writer->xmlStartTag("respcondition", $attrs);
174  // qti conditionvar
175  $a_xml_writer->xmlStartTag("conditionvar");
176  $attrs = [
177  "respident" => "MCMR"
178  ];
179  $a_xml_writer->xmlElement("varequal", $attrs, $index);
180  $a_xml_writer->xmlEndTag("conditionvar");
181  // qti setvar
182  $attrs = [
183  "action" => "Add"
184  ];
185  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
186  // qti displayfeedback
187  $linkrefid = "response_$index";
188  $attrs = [
189  "feedbacktype" => "Response",
190  "linkrefid" => $linkrefid
191  ];
192  $a_xml_writer->xmlElement("displayfeedback", $attrs);
193  $a_xml_writer->xmlEndTag("respcondition");
194  $attrs = [
195  "continue" => "Yes"
196  ];
197  $a_xml_writer->xmlStartTag("respcondition", $attrs);
198 
199  // qti conditionvar
200  $a_xml_writer->xmlStartTag("conditionvar");
201  $attrs = [
202  "respident" => "MCMR"
203  ];
204  $a_xml_writer->xmlStartTag("not");
205  $a_xml_writer->xmlElement("varequal", $attrs, $index);
206  $a_xml_writer->xmlEndTag("not");
207  $a_xml_writer->xmlEndTag("conditionvar");
208  // qti setvar
209  $attrs = [
210  "action" => "Add"
211  ];
212  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPointsUnchecked());
213  $a_xml_writer->xmlEndTag("respcondition");
214  }
215  $feedback_allcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
216  $this->object->getId(),
217  true
218  );
219  if (strlen($feedback_allcorrect)) {
220  $attrs = [
221  "continue" => "Yes"
222  ];
223  $a_xml_writer->xmlStartTag("respcondition", $attrs);
224  // qti conditionvar
225  $a_xml_writer->xmlStartTag("conditionvar");
226  foreach ($answers as $index => $answer) {
227  if ($answer->getPointsChecked() < $answer->getPointsUnchecked()) {
228  $a_xml_writer->xmlStartTag("not");
229  }
230  $attrs = [
231  "respident" => "MCMR"
232  ];
233  $a_xml_writer->xmlElement("varequal", $attrs, $index);
234  if ($answer->getPointsChecked() < $answer->getPointsUnchecked()) {
235  $a_xml_writer->xmlEndTag("not");
236  }
237  }
238  $a_xml_writer->xmlEndTag("conditionvar");
239  // qti displayfeedback
240  $attrs = [
241  "feedbacktype" => "Response",
242  "linkrefid" => "response_allcorrect"
243  ];
244  $a_xml_writer->xmlElement("displayfeedback", $attrs);
245  $a_xml_writer->xmlEndTag("respcondition");
246  }
247  $feedback_onenotcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
248  $this->object->getId(),
249  false
250  );
251  if (strlen($feedback_onenotcorrect)) {
252  $attrs = [
253  "continue" => "Yes"
254  ];
255  $a_xml_writer->xmlStartTag("respcondition", $attrs);
256  // qti conditionvar
257  $a_xml_writer->xmlStartTag("conditionvar");
258  foreach ($answers as $index => $answer) {
259  if ($index > 0) {
260  $a_xml_writer->xmlStartTag("or");
261  }
262  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked()) {
263  $a_xml_writer->xmlStartTag("not");
264  }
265  $attrs = [
266  "respident" => "MCMR"
267  ];
268  $a_xml_writer->xmlElement("varequal", $attrs, $index);
269  if ($answer->getPointsChecked() >= $answer->getPointsUnchecked()) {
270  $a_xml_writer->xmlEndTag("not");
271  }
272  if ($index > 0) {
273  $a_xml_writer->xmlEndTag("or");
274  }
275  }
276  $a_xml_writer->xmlEndTag("conditionvar");
277  // qti displayfeedback
278  $attrs = [
279  "feedbacktype" => "Response",
280  "linkrefid" => "response_onenotcorrect"
281  ];
282  $a_xml_writer->xmlElement("displayfeedback", $attrs);
283  $a_xml_writer->xmlEndTag("respcondition");
284  }
285  $a_xml_writer->xmlEndTag("resprocessing");
286 
287  // PART III: qti itemfeedback
288  foreach ($answers as $index => $answer) {
289  $linkrefid = "response_$index";
290  $attrs = [
291  "ident" => $linkrefid,
292  "view" => "All"
293  ];
294  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
295  // qti flow_mat
296  $a_xml_writer->xmlStartTag("flow_mat");
297  $fb = $this->object->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation(
298  $this->object->getId(),
299  0,
300  $index
301  );
302  $this->addQTIMaterial($a_xml_writer, $fb);
303  $a_xml_writer->xmlEndTag("flow_mat");
304  $a_xml_writer->xmlEndTag("itemfeedback");
305  }
306  if (strlen($feedback_allcorrect)) {
307  $attrs = [
308  "ident" => "response_allcorrect",
309  "view" => "All"
310  ];
311  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
312  // qti flow_mat
313  $a_xml_writer->xmlStartTag("flow_mat");
314  $this->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
315  $a_xml_writer->xmlEndTag("flow_mat");
316  $a_xml_writer->xmlEndTag("itemfeedback");
317  }
318  if (strlen($feedback_onenotcorrect)) {
319  $attrs = [
320  "ident" => "response_onenotcorrect",
321  "view" => "All"
322  ];
323  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
324  // qti flow_mat
325  $a_xml_writer->xmlStartTag("flow_mat");
326  $this->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
327  $a_xml_writer->xmlEndTag("flow_mat");
328  $a_xml_writer->xmlEndTag("itemfeedback");
329  }
330 
331  $a_xml_writer = $this->addSolutionHints($a_xml_writer);
332 
333  $a_xml_writer->xmlEndTag("item");
334  $a_xml_writer->xmlEndTag("questestinterop");
335 
336  $xml = $a_xml_writer->xmlDumpMem(false);
337  if (!$a_include_header) {
338  $pos = strpos($xml, "?>");
339  $xml = substr($xml, $pos + 2);
340  }
341  return $xml;
342  }
343 }
const IL_INST_ID
Definition: constants.php:40
addQTIMaterial(ilXmlWriter $a_xml_writer, string $a_material, bool $close_material_tag=true, bool $add_mobs=true)
Class for question exports.
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 ...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
addAdditionalContentEditingModeInformation(ilXmlWriter $a_xml_writer)
adds a qti meta data field for ilias specific information of "additional content editing mode" (xml w...
addSuggestedSolution(ilXmlWriter $writer)
addSolutionHints(ilXmlWriter $writer)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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 Returns a QTI xml representation of the question and...