ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.assMatchingQuestionExport.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
37 public function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false): string
38 {
39 global $DIC;
40 $ilias = $DIC['ilias'];
41
42 $a_xml_writer = new ilXmlWriter();
43 // set xml header
44 $a_xml_writer->xmlHeader();
45 $a_xml_writer->xmlStartTag("questestinterop");
46 $attrs = [
47 "ident" => "il_" . IL_INST_ID . "_qst_" . $this->object->getId(),
48 "title" => $this->object->getTitle(),
49 "maxattempts" => $this->object->getNrOfTries()
50 ];
51 $a_xml_writer->xmlStartTag("item", $attrs);
52 // add question description
53 $a_xml_writer->xmlElement("qticomment", null, $this->object->getComment());
54 $a_xml_writer->xmlStartTag("itemmetadata");
55 $a_xml_writer->xmlStartTag("qtimetadata");
56 $a_xml_writer->xmlStartTag("qtimetadatafield");
57 $a_xml_writer->xmlElement("fieldlabel", null, "ILIAS_VERSION");
58 $a_xml_writer->xmlElement("fieldentry", null, $ilias->getSetting("ilias_version"));
59 $a_xml_writer->xmlEndTag("qtimetadatafield");
60 $a_xml_writer->xmlStartTag("qtimetadatafield");
61 $a_xml_writer->xmlElement("fieldlabel", null, "QUESTIONTYPE");
62 $a_xml_writer->xmlElement("fieldentry", null, $this->object->getQuestionType());
63 $a_xml_writer->xmlEndTag("qtimetadatafield");
64 $a_xml_writer->xmlStartTag("qtimetadatafield");
65 $a_xml_writer->xmlElement("fieldlabel", null, "AUTHOR");
66 $a_xml_writer->xmlElement("fieldentry", null, $this->object->getAuthor());
67 $a_xml_writer->xmlEndTag("qtimetadatafield");
68
69 // additional content editing information
71 $this->addGeneralMetadata($a_xml_writer);
72
73 $a_xml_writer->xmlStartTag("qtimetadatafield");
74 $a_xml_writer->xmlElement("fieldlabel", null, "shuffle");
75 $a_xml_writer->xmlElement("fieldentry", null, $this->object->getShuffleMode());
76 $a_xml_writer->xmlEndTag("qtimetadatafield");
77 $a_xml_writer->xmlStartTag("qtimetadatafield");
78 $a_xml_writer->xmlElement("fieldlabel", null, "thumb_geometry");
79 $a_xml_writer->xmlElement("fieldentry", null, $this->object->getThumbGeometry());
80 $a_xml_writer->xmlEndTag("qtimetadatafield");
81 $a_xml_writer->xmlStartTag("qtimetadatafield");
82 $a_xml_writer->xmlElement("fieldlabel", null, 'matching_mode');
83 $a_xml_writer->xmlElement("fieldentry", null, $this->object->getMatchingMode());
84 $a_xml_writer->xmlEndTag("qtimetadatafield");
85 $a_xml_writer->xmlEndTag("qtimetadata");
86 $a_xml_writer->xmlEndTag("itemmetadata");
87
88 // PART I: qti presentation
89 $attrs = [
90 "label" => $this->object->getTitle()
91 ];
92 $a_xml_writer->xmlStartTag("presentation", $attrs);
93 // add flow to presentation
94 $a_xml_writer->xmlStartTag("flow");
95 // add material with question text to presentation
96 $this->addQTIMaterial($a_xml_writer, $this->object->getQuestion());
97 // add answers to presentation
98 $attrs = [
99 "ident" => "MQ",
100 "rcardinality" => "Multiple"
101 ];
102 $a_xml_writer->xmlStartTag("response_grp", $attrs);
103 $a_xml_writer = $this->addSuggestedSolution($a_xml_writer);
104 // shuffle output
105 $attrs = [];
106 if ($this->object->getShuffle()) {
107 $attrs = [
108 "shuffle" => "Yes"
109 ];
110 } else {
111 $attrs = [
112 "shuffle" => "No"
113 ];
114 }
115 $a_xml_writer->xmlStartTag("render_choice", $attrs);
116 // add answertext
117 $matchingtext_orders = [];
118 foreach ($this->object->getMatchingPairs() as $index => $matchingpair) {
119 array_push($matchingtext_orders, $matchingpair->getTerm()->getIdentifier());
120 }
121
122 $termids = [];
123 foreach ($this->object->getTerms() as $term) {
124 array_push($termids, $term->getidentifier());
125 }
126 // add answers
127 foreach ($this->object->getDefinitions() as $definition) {
128 $attrs = [
129 "ident" => $definition->getIdentifier(),
130 "match_max" => "1",
131 "match_group" => join(",", $termids)
132 ];
133 $a_xml_writer->xmlStartTag("response_label", $attrs);
134 $a_xml_writer->xmlStartTag("material");
135 if (strlen($definition->getPicture())) {
136 if ($force_image_references) {
137 $attrs = [
138 "imagtype" => "image/jpeg",
139 "label" => $definition->getPicture(),
140 "uri" => $this->object->getImagePathWeb() . $definition->getPicture()
141 ];
142 $a_xml_writer->xmlElement("matimage", $attrs);
143 } else {
144 $imagepath = $this->object->getImagePath() . $definition->getPicture();
145 $fh = @fopen($imagepath, "rb");
146 if ($fh != false) {
147 $imagefile = fread($fh, filesize($imagepath));
148 fclose($fh);
149 $base64 = base64_encode($imagefile);
150 $attrs = [
151 "imagtype" => "image/jpeg",
152 "label" => $definition->getPicture(),
153 "embedded" => "base64"
154 ];
155 $a_xml_writer->xmlElement("matimage", $attrs, $base64, false, false);
156 }
157 }
158 }
159 if (strlen($definition->getText())) {
160 $attrs = [
161 "texttype" => "text/plain"
162 ];
163 if (ilUtil::isHTML($definition->getText())) {
164 $attrs["texttype"] = "text/xhtml";
165 }
166 $a_xml_writer->xmlElement("mattext", $attrs, $definition->getText());
167 }
168 $a_xml_writer->xmlEndTag("material");
169 $a_xml_writer->xmlEndTag("response_label");
170 }
171 // add matchingtext
172 foreach ($this->object->getTerms() as $term) {
173 $attrs = [
174 "ident" => $term->getIdentifier()
175 ];
176 $a_xml_writer->xmlStartTag("response_label", $attrs);
177 $a_xml_writer->xmlStartTag("material");
178 if (strlen($term->getPicture())) {
179 if ($force_image_references) {
180 $attrs = [
181 "imagtype" => "image/jpeg",
182 "label" => $term->getPicture(),
183 "uri" => $this->object->getImagePathWeb() . $term->getPicture()
184 ];
185 $a_xml_writer->xmlElement("matimage", $attrs);
186 } else {
187 $imagepath = $this->object->getImagePath() . $term->getPicture();
188 $fh = @fopen($imagepath, "rb");
189 if ($fh != false) {
190 $imagefile = fread($fh, filesize($imagepath));
191 fclose($fh);
192 $base64 = base64_encode($imagefile);
193 $attrs = [
194 "imagtype" => "image/jpeg",
195 "label" => $term->getPicture(),
196 "embedded" => "base64"
197 ];
198 $a_xml_writer->xmlElement("matimage", $attrs, $base64, false, false);
199 }
200 }
201 }
202 if (strlen($term->getText())) {
203 $attrs = [
204 "texttype" => "text/plain"
205 ];
206 if (method_exists($this->object, 'isHTML') && $this->object->isHTML($term->text)) {
207 $attrs["texttype"] = "text/xhtml";
208 }
209 $a_xml_writer->xmlElement("mattext", $attrs, $term->getText());
210 }
211 $a_xml_writer->xmlEndTag("material");
212 $a_xml_writer->xmlEndTag("response_label");
213 }
214 $a_xml_writer->xmlEndTag("render_choice");
215 $a_xml_writer->xmlEndTag("response_grp");
216 $a_xml_writer->xmlEndTag("flow");
217 $a_xml_writer->xmlEndTag("presentation");
218
219 // PART II: qti resprocessing
220 $a_xml_writer->xmlStartTag("resprocessing");
221 $a_xml_writer->xmlStartTag("outcomes");
222 $a_xml_writer->xmlStartTag("decvar");
223 $a_xml_writer->xmlEndTag("decvar");
224 $a_xml_writer->xmlEndTag("outcomes");
225 // add response conditions
226 foreach ($this->object->getMatchingPairs() as $matchingpair) {
227 $attrs = [
228 "continue" => "Yes"
229 ];
230 $a_xml_writer->xmlStartTag("respcondition", $attrs);
231 // qti conditionvar
232 $a_xml_writer->xmlStartTag("conditionvar");
233 $attrs = [
234 "respident" => "MQ"
235 ];
236 $a_xml_writer->xmlElement("varsubset", $attrs, $matchingpair->getTerm()->getIdentifier() . "," . $matchingpair->getDefinition()->getidentifier());
237 $a_xml_writer->xmlEndTag("conditionvar");
238
239 // qti setvar
240 $attrs = [
241 "action" => "Add"
242 ];
243 $a_xml_writer->xmlElement("setvar", $attrs, $matchingpair->getPoints());
244 // qti displayfeedback
245 $attrs = [
246 "feedbacktype" => "Response",
247 "linkrefid" => "correct_" . $matchingpair->getTerm()->getIdentifier() . "_" . $matchingpair->getDefinition()->getIdentifier()
248 ];
249 $a_xml_writer->xmlElement("displayfeedback", $attrs);
250 $a_xml_writer->xmlEndTag("respcondition");
251 }
252
253 $feedback_allcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
254 $this->object->getId(),
255 true
256 );
257 if (strlen($feedback_allcorrect)) {
258 $attrs = [
259 "continue" => "Yes"
260 ];
261 $a_xml_writer->xmlStartTag("respcondition", $attrs);
262 // qti conditionvar
263 $a_xml_writer->xmlStartTag("conditionvar");
264
265 foreach ($this->object->getMatchingPairs() as $matchingpair) {
266 $attrs = [
267 "respident" => "MQ"
268 ];
269 $a_xml_writer->xmlElement("varsubset", $attrs, $matchingpair->getTerm()->getIdentifier() . "," . $matchingpair->getDefinition()->getIdentifier());
270 }
271 $a_xml_writer->xmlEndTag("conditionvar");
272 // qti displayfeedback
273 $attrs = [
274 "feedbacktype" => "Response",
275 "linkrefid" => "response_allcorrect"
276 ];
277 $a_xml_writer->xmlElement("displayfeedback", $attrs);
278 $a_xml_writer->xmlEndTag("respcondition");
279 }
280 $feedback_onenotcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
281 $this->object->getId(),
282 false
283 );
284 if (strlen($feedback_onenotcorrect)) {
285 $attrs = [
286 "continue" => "Yes"
287 ];
288 $a_xml_writer->xmlStartTag("respcondition", $attrs);
289 // qti conditionvar
290 $a_xml_writer->xmlStartTag("conditionvar");
291 $a_xml_writer->xmlStartTag("not");
292 foreach ($this->object->getMatchingPairs() as $matchingpair) {
293 $attrs = [
294 "respident" => "MQ"
295 ];
296 $a_xml_writer->xmlElement("varsubset", $attrs, $matchingpair->getTerm()->getIdentifier() . "," . $matchingpair->getDefinition()->getIdentifier());
297 }
298 $a_xml_writer->xmlEndTag("not");
299 $a_xml_writer->xmlEndTag("conditionvar");
300 // qti displayfeedback
301 $attrs = [
302 "feedbacktype" => "Response",
303 "linkrefid" => "response_onenotcorrect"
304 ];
305 $a_xml_writer->xmlElement("displayfeedback", $attrs);
306 $a_xml_writer->xmlEndTag("respcondition");
307 }
308
309 $a_xml_writer->xmlEndTag("resprocessing");
310
311 // PART III: qti itemfeedback
312 foreach ($this->object->getMatchingPairs() as $index => $matchingpair) {
313 $attrs = [
314 "ident" => "correct_" . $matchingpair->getTerm()->getIdentifier() . "_" . $matchingpair->getDefinition()->getIdentifier(),
315 "view" => "All"
316 ];
317 $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
318 // qti flow_mat
319 $a_xml_writer->xmlStartTag("flow_mat");
320 $a_xml_writer->xmlStartTag("material");
321 $a_xml_writer->xmlElement("mattext", null, $this->object->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation(
322 $this->object->getId(),
323 0,
324 $index
325 ));
326 $a_xml_writer->xmlEndTag("material");
327 $a_xml_writer->xmlEndTag("flow_mat");
328 $a_xml_writer->xmlEndTag("itemfeedback");
329 }
330
331 if (strlen($feedback_allcorrect)) {
332 $attrs = [
333 "ident" => "response_allcorrect",
334 "view" => "All"
335 ];
336 $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
337 // qti flow_mat
338 $a_xml_writer->xmlStartTag("flow_mat");
339 $this->addQTIMaterial($a_xml_writer, $feedback_allcorrect);
340 $a_xml_writer->xmlEndTag("flow_mat");
341 $a_xml_writer->xmlEndTag("itemfeedback");
342 }
343 if (strlen($feedback_onenotcorrect)) {
344 $attrs = [
345 "ident" => "response_onenotcorrect",
346 "view" => "All"
347 ];
348 $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
349 // qti flow_mat
350 $a_xml_writer->xmlStartTag("flow_mat");
351 $this->addQTIMaterial($a_xml_writer, $feedback_onenotcorrect);
352 $a_xml_writer->xmlEndTag("flow_mat");
353 $a_xml_writer->xmlEndTag("itemfeedback");
354 }
355
356 $a_xml_writer->xmlEndTag("item");
357 $a_xml_writer->xmlEndTag("questestinterop");
358
359 $xml = $a_xml_writer->xmlDumpMem(false);
360 if (!$a_include_header) {
361 $pos = strpos($xml, "?>");
362 $xml = substr($xml, $pos + 2);
363 }
364 return $xml;
365 }
366}
Class for matching 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 Returns a QTI xml representation of the question and...
Class for question exports.
addGeneralMetadata(ilXmlWriter $xmlwriter)
addQTIMaterial(ilXmlWriter $a_xml_writer, string $a_material, bool $close_material_tag=true, bool $add_mobs=true)
addSuggestedSolution(ilXmlWriter $writer)
addAdditionalContentEditingModeInformation(ilXmlWriter $a_xml_writer)
adds a qti meta data field for ilias specific information of "additional content editing mode" (xml w...
static isHTML(string $a_text)
Checks if a given string contains HTML or not.
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: shib_login.php:26