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