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