ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.assSingleChoiceExport.php
Go to the documentation of this file.
1 <?php
2 
30 {
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, SINGLE_CHOICE_QUESTION_IDENTIFIER);
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" => "MCSR",
99  "rcardinality" => "Single"
100  ];
101  $a_xml_writer->xmlStartTag("response_lid", $attrs);
102  $a_xml_writer = $this->addSuggestedSolution($a_xml_writer);
103  // shuffle output
104  $attrs = [];
105  if ($this->object->getShuffle()) {
106  $attrs = [
107  "shuffle" => "Yes"
108  ];
109  } else {
110  $attrs = [
111  "shuffle" => "No"
112  ];
113  }
114  $a_xml_writer->xmlStartTag("render_choice", $attrs);
115  $answers = &$this->object->getAnswers();
116  $akeys = array_keys($answers);
117  if ($this->object->getShuffle() && $a_shuffle) {
118  $akeys = shuffle($akeys);
119  }
120  // add answers
121  foreach ($akeys as $index) {
122  $answer = $answers[$index];
123  $attrs = [
124  "ident" => $index
125  ];
126  $a_xml_writer->xmlStartTag("response_label", $attrs);
127 
128  if ($answer->hasImage()) {
129  $this->addQTIMaterial($a_xml_writer, $answer->getAnswertext(), false, false);
130  $imagetype = "image/jpeg";
131  if (preg_match("/.*\.(png|gif)$/", $answer->getImage(), $matches)) {
132  $imagetype = "image/" . $matches[1];
133  }
134  if ($force_image_references) {
135  $attrs = [
136  "imagtype" => $imagetype,
137  "label" => $answer->getImage(),
138  "uri" => $this->object->getImagePathWeb() . $answer->getImage()
139  ];
140  $a_xml_writer->xmlElement("matimage", $attrs);
141  } else {
142  $imagepath = $this->object->getImagePath() . $answer->getImage();
143  $fh = @fopen($imagepath, "rb");
144  if ($fh != false) {
145  $imagefile = fread($fh, filesize($imagepath));
146  fclose($fh);
147  $base64 = base64_encode($imagefile);
148  $attrs = [
149  "imagtype" => $imagetype,
150  "label" => $answer->getImage(),
151  "embedded" => "base64"
152  ];
153  $a_xml_writer->xmlElement("matimage", $attrs, $base64, false, false);
154  }
155  }
156  $a_xml_writer->xmlEndTag("material");
157  } else {
158  $this->addQTIMaterial($a_xml_writer, $answer->getAnswertext());
159  }
160  $a_xml_writer->xmlEndTag("response_label");
161  }
162  $a_xml_writer->xmlEndTag("render_choice");
163  $a_xml_writer->xmlEndTag("response_lid");
164  $a_xml_writer->xmlEndTag("flow");
165  $a_xml_writer->xmlEndTag("presentation");
166 
167  // PART II: qti resprocessing
168  $a_xml_writer->xmlStartTag("resprocessing");
169  $a_xml_writer->xmlStartTag("outcomes");
170  $a_xml_writer->xmlStartTag("decvar");
171  $a_xml_writer->xmlEndTag("decvar");
172  $a_xml_writer->xmlEndTag("outcomes");
173  // add response conditions
174  foreach ($answers as $index => $answer) {
175  $attrs = [
176  "continue" => "Yes"
177  ];
178  $a_xml_writer->xmlStartTag("respcondition", $attrs);
179  // qti conditionvar
180  $a_xml_writer->xmlStartTag("conditionvar");
181  $attrs = [
182  "respident" => "MCSR"
183  ];
184  $a_xml_writer->xmlElement("varequal", $attrs, $index);
185  $a_xml_writer->xmlEndTag("conditionvar");
186  // qti setvar
187  $attrs = [
188  "action" => "Add"
189  ];
190  $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
191  // qti displayfeedback
192  $linkrefid = "response_$index";
193  $attrs = [
194  "feedbacktype" => "Response",
195  "linkrefid" => $linkrefid
196  ];
197  $a_xml_writer->xmlElement("displayfeedback", $attrs);
198  $a_xml_writer->xmlEndTag("respcondition");
199  }
200 
201  $feedback_allcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
202  $this->object->getId(),
203  true
204  );
205  if (strlen($feedback_allcorrect)) {
206  $attrs = [
207  "continue" => "Yes"
208  ];
209  $a_xml_writer->xmlStartTag("respcondition", $attrs);
210  // qti conditionvar
211  $a_xml_writer->xmlStartTag("conditionvar");
212  $bestindex = 0;
213  $maxpoints = 0;
214  foreach ($answers as $index => $answer) {
215  if ($answer->getPoints() > $maxpoints) {
216  $maxpoints = $answer->getPoints();
217  $bestindex = $index;
218  }
219  }
220  $attrs = [
221  "respident" => "MCSR"
222  ];
223  $a_xml_writer->xmlElement("varequal", $attrs, $bestindex);
224  $a_xml_writer->xmlEndTag("conditionvar");
225  // qti displayfeedback
226  $attrs = [
227  "feedbacktype" => "Response",
228  "linkrefid" => "response_allcorrect"
229  ];
230  $a_xml_writer->xmlElement("displayfeedback", $attrs);
231  $a_xml_writer->xmlEndTag("respcondition");
232  }
233 
234  $feedback_onenotcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
235  $this->object->getId(),
236  false
237  );
238  if (strlen($feedback_onenotcorrect)) {
239  $attrs = [
240  "continue" => "Yes"
241  ];
242  $a_xml_writer->xmlStartTag("respcondition", $attrs);
243  // qti conditionvar
244  $a_xml_writer->xmlStartTag("conditionvar");
245  $bestindex = 0;
246  $maxpoints = 0;
247  foreach ($answers as $index => $answer) {
248  if ($answer->getPoints() > $maxpoints) {
249  $maxpoints = $answer->getPoints();
250  $bestindex = $index;
251  }
252  }
253  $attrs = [
254  "respident" => "MCSR"
255  ];
256  $a_xml_writer->xmlStartTag("not");
257  $a_xml_writer->xmlElement("varequal", $attrs, $bestindex);
258  $a_xml_writer->xmlEndTag("not");
259  $a_xml_writer->xmlEndTag("conditionvar");
260  // qti displayfeedback
261  $attrs = [
262  "feedbacktype" => "Response",
263  "linkrefid" => "response_onenotcorrect"
264  ];
265  $a_xml_writer->xmlElement("displayfeedback", $attrs);
266  $a_xml_writer->xmlEndTag("respcondition");
267  }
268 
269  $a_xml_writer->xmlEndTag("resprocessing");
270 
271  // PART III: qti itemfeedback
272  foreach ($answers as $index => $answer) {
273  $linkrefid = "response_$index";
274  $attrs = [
275  "ident" => $linkrefid,
276  "view" => "All"
277  ];
278  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
279  // qti flow_mat
280  $a_xml_writer->xmlStartTag("flow_mat");
281  $fb = $this->object->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation(
282  $this->object->getId(),
283  0,
284  $index
285  );
286  $this->addQTIMaterial($a_xml_writer, $fb);
287  $a_xml_writer->xmlEndTag("flow_mat");
288  $a_xml_writer->xmlEndTag("itemfeedback");
289  }
290  if (strlen($feedback_allcorrect)) {
291  $attrs = [
292  "ident" => "response_allcorrect",
293  "view" => "All"
294  ];
295  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
296  // qti flow_mat
297  $a_xml_writer->xmlStartTag("flow_mat");
298  $this->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
299  $a_xml_writer->xmlEndTag("flow_mat");
300  $a_xml_writer->xmlEndTag("itemfeedback");
301  }
302  if (strlen($feedback_onenotcorrect)) {
303  $attrs = [
304  "ident" => "response_onenotcorrect",
305  "view" => "All"
306  ];
307  $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
308  // qti flow_mat
309  $a_xml_writer->xmlStartTag("flow_mat");
310  $this->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
311  $a_xml_writer->xmlEndTag("flow_mat");
312  $a_xml_writer->xmlEndTag("itemfeedback");
313  }
314 
315  $a_xml_writer = $this->addSolutionHints($a_xml_writer);
316 
317  $a_xml_writer->xmlEndTag("item");
318  $a_xml_writer->xmlEndTag("questestinterop");
319  $xml = $a_xml_writer->xmlDumpMem(false);
320  if (!$a_include_header) {
321  $pos = strpos($xml, "?>");
322  $xml = substr($xml, $pos + 2);
323  }
324  return $xml;
325  }
326 }
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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 ...
global $DIC
Definition: feed.php:28
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...
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)
const SINGLE_CHOICE_QUESTION_IDENTIFIER