ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.assKprimChoiceExport.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 require_once 'Modules/TestQuestionPool/classes/export/qti12/class.assQuestionExport.php';
5 
13 {
17  public $object;
18 
19  public function toXML($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
20  {
21  global $ilias;
22 
23  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
24  $xml = new ilXmlWriter;
25  // set xml header
26  $xml->xmlHeader();
27  $xml->xmlStartTag("questestinterop");
28  $attrs = array(
29  "ident" => "il_" . IL_INST_ID . "_qst_" . $this->object->getId(),
30  "title" => $this->object->getTitle(),
31  "maxattempts" => $this->object->getNrOfTries()
32  );
33  $xml->xmlStartTag("item", $attrs);
34  // add question description
35  $xml->xmlElement("qticomment", null, $this->object->getComment());
36  // add estimated working time
37  $workingtime = $this->object->getEstimatedWorkingTime();
38  $duration = sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]);
39  $xml->xmlElement("duration", null, $duration);
40  // add ILIAS specific metadata
41  $xml->xmlStartTag("itemmetadata");
42  $xml->xmlStartTag("qtimetadata");
43  $xml->xmlStartTag("qtimetadatafield");
44  $xml->xmlElement("fieldlabel", null, "ILIAS_VERSION");
45  $xml->xmlElement("fieldentry", null, $ilias->getSetting("ilias_version"));
46  $xml->xmlEndTag("qtimetadatafield");
47  $xml->xmlStartTag("qtimetadatafield");
48  $xml->xmlElement("fieldlabel", null, "QUESTIONTYPE");
49  $xml->xmlElement("fieldentry", null, KPRIM_CHOICE_QUESTION_IDENTIFIER);
50  $xml->xmlEndTag("qtimetadatafield");
51  $xml->xmlStartTag("qtimetadatafield");
52  $xml->xmlElement("fieldlabel", null, "AUTHOR");
53  $xml->xmlElement("fieldentry", null, $this->object->getAuthor());
54  $xml->xmlEndTag("qtimetadatafield");
55 
56  // additional content editing information
58  $this->addGeneralMetadata($xml);
59 
60  $xml->xmlStartTag("qtimetadatafield");
61  $xml->xmlElement("fieldlabel", null, "answer_type");
62  $xml->xmlElement("fieldentry", null, $this->object->getAnswerType());
63  $xml->xmlEndTag("qtimetadatafield");
64 
65  $xml->xmlStartTag("qtimetadatafield");
66  $xml->xmlElement("fieldlabel", null, "thumb_size");
67  $xml->xmlElement("fieldentry", null, $this->object->getThumbSize());
68  $xml->xmlEndTag("qtimetadatafield");
69 
70  $xml->xmlStartTag("qtimetadatafield");
71  $xml->xmlElement("fieldlabel", null, "option_label_setting");
72  $xml->xmlElement("fieldentry", null, $this->object->getOptionLabel());
73  $xml->xmlEndTag("qtimetadatafield");
74  $xml->xmlStartTag("qtimetadatafield");
75  $xml->xmlElement("fieldlabel", null, "custom_true_option_label");
76  $xml->xmlElement("fieldentry", null, $this->object->getCustomTrueOptionLabel());
77  $xml->xmlEndTag("qtimetadatafield");
78  $xml->xmlStartTag("qtimetadatafield");
79  $xml->xmlElement("fieldlabel", null, "custom_false_option_label");
80  $xml->xmlElement("fieldentry", null, $this->object->getCustomFalseOptionLabel());
81  $xml->xmlEndTag("qtimetadatafield");
82 
83  $xml->xmlStartTag("qtimetadatafield");
84  $xml->xmlElement("fieldlabel", null, "feedback_setting");
85  $xml->xmlElement("fieldentry", null, $this->object->getSpecificFeedbackSetting());
86  $xml->xmlEndTag("qtimetadatafield");
87 
88  $xml->xmlEndTag("qtimetadata");
89  $xml->xmlEndTag("itemmetadata");
90 
91  // PART I: qti presentation
92  $attrs = array(
93  "label" => $this->object->getTitle()
94  );
95  $xml->xmlStartTag("presentation", $attrs);
96  // add flow to presentation
97  $xml->xmlStartTag("flow");
98  // add material with question text to presentation
99  $this->object->addQTIMaterial($xml, $this->object->getQuestion());
100  // add answers to presentation
101  $attrs = array(
102  "ident" => "MCMR",
103  "rcardinality" => "Multiple"
104  );
105  $xml->xmlStartTag("response_lid", $attrs);
106  $solution = $this->object->getSuggestedSolution(0);
107  if (count($solution)) {
108  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches)) {
109  $xml->xmlStartTag("material");
110  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
111  if (strcmp($matches[1], "") != 0) {
112  $intlink = $solution["internal_link"];
113  }
114  $attrs = array(
115  "label" => "suggested_solution"
116  );
117  $xml->xmlElement("mattext", $attrs, $intlink);
118  $xml->xmlEndTag("material");
119  }
120  }
121  // shuffle output
122  $attrs = array();
123  if ($this->object->isShuffleAnswersEnabled()) {
124  $attrs = array(
125  "shuffle" => "Yes"
126  );
127  } else {
128  $attrs = array(
129  "shuffle" => "No"
130  );
131  }
132  $xml->xmlStartTag("render_choice", $attrs);
133 
134  // add answers
135  $answers =&$this->object->getAnswers();
136  $akeys = array_keys($answers);
137  foreach ($akeys as $index) {
138  $answer = $this->object->getAnswer($index);
139 
140  $xml->xmlStartTag('response_label', array('ident' => $answer->getPosition()));
141 
142  if (strlen($answer->getImageFile())) {
143  $this->object->addQTIMaterial($xml, $answer->getAnswertext(), false, false);
144  $imagetype = "image/jpeg";
145  if (preg_match("/.*\.(png|gif)$/", $answer->getImageFile(), $matches)) {
146  $imagetype = "image/" . $matches[1];
147  }
148  if ($force_image_references) {
149  $attrs = array(
150  "imagtype" => $imagetype,
151  "label" => $answer->getImageFile(),
152  "uri" => $answer->getImageWebPath()
153  );
154  $xml->xmlElement("matimage", $attrs);
155  } else {
156  $imagepath = $answer->getImageFsPath();
157  $fh = @fopen($imagepath, "rb");
158  if ($fh != false) {
159  $imagefile = fread($fh, filesize($imagepath));
160  fclose($fh);
161  $base64 = base64_encode($imagefile);
162  $attrs = array(
163  "imagtype" => $imagetype,
164  "label" => $answer->getImageFile(),
165  "embedded" => "base64"
166  );
167  $xml->xmlElement("matimage", $attrs, $base64, false, false);
168  }
169  }
170  $xml->xmlEndTag("material");
171  } else {
172  $this->object->addQTIMaterial($xml, $answer->getAnswertext());
173  }
174  $xml->xmlEndTag("response_label");
175  }
176  $xml->xmlEndTag("render_choice");
177  $xml->xmlEndTag("response_lid");
178  $xml->xmlEndTag("flow");
179  $xml->xmlEndTag("presentation");
180 
181  // PART II: qti resprocessing
182 
183  $xml->xmlStartTag('resprocessing');
184 
185  $xml->xmlStartTag('outcomes');
186  $xml->xmlElement('decvar', array(
187  'varname' => 'SCORE', 'vartype' => 'Decimal', 'defaultval' => '0',
188  'minvalue' => $this->getMinPoints(), 'maxvalue' => $this->getMaxPoints()
189  ));
190  $xml->xmlEndTag('outcomes');
191 
192 
193  foreach ($answers as $answer) {
194  $xml->xmlStartTag('respcondition', array('continue' => 'Yes'));
195 
196  $xml->xmlStartTag('conditionvar');
197  $xml->xmlElement('varequal', array('respident' => $answer->getPosition()), $answer->getCorrectness());
198  $xml->xmlEndTag('conditionvar');
199 
200  $xml->xmlElement('displayfeedback', array(
201  'feedbacktype' => 'Response', 'linkrefid' => "response_{$answer->getPosition()}"
202  ));
203 
204  $xml->xmlEndTag('respcondition');
205  }
206 
207  $feedback_allcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
208  $this->object->getId(),
209  true
210  );
211 
212  $xml->xmlStartTag('respcondition', array('continue' => 'Yes'));
213 
214  $xml->xmlStartTag('conditionvar');
215  $xml->xmlStartTag('and');
216  foreach ($answers as $answer) {
217  $xml->xmlElement('varequal', array('respident' => $answer->getPosition()), $answer->getCorrectness());
218  }
219  $xml->xmlEndTag('and');
220  $xml->xmlEndTag('conditionvar');
221 
222  $xml->xmlElement('setvar', array('action' => 'Add'), $this->object->getPoints());
223 
224  if (strlen($feedback_allcorrect)) {
225  $xml->xmlElement('displayfeedback', array('feedbacktype' => 'Response', 'linkrefid' => 'response_allcorrect'));
226  }
227 
228  $xml->xmlEndTag('respcondition');
229 
230  $feedback_onenotcorrect = $this->object->feedbackOBJ->getGenericFeedbackExportPresentation(
231  $this->object->getId(),
232  false
233  );
234 
235  $xml->xmlStartTag('respcondition', array('continue' => 'Yes'));
236 
237  $xml->xmlStartTag('conditionvar');
238  $xml->xmlStartTag('or');
239  foreach ($answers as $answer) {
240  $xml->xmlStartTag('not');
241  $xml->xmlElement('varequal', array('respident' => $answer->getPosition()), $answer->getCorrectness());
242  $xml->xmlEndTag('not');
243  }
244  $xml->xmlEndTag('or');
245  $xml->xmlEndTag('conditionvar');
246 
247  $xml->xmlElement('setvar', array('action' => 'Add'), 0);
248 
249  if (strlen($feedback_onenotcorrect)) {
250  $xml->xmlElement('displayfeedback', array('feedbacktype' => 'Response', 'linkrefid' => 'response_onenotcorrect'));
251  }
252 
253  $xml->xmlEndTag('respcondition');
254 
255  $xml->xmlEndTag('resprocessing');
256 
257  foreach ($answers as $answer) {
258  $xml->xmlStartTag('itemfeedback', array('ident' => "response_{$answer->getPosition()}", 'view' => 'All'));
259  $xml->xmlStartTag('flow_mat');
260 
261  $this->object->addQTIMaterial($xml, $this->object->feedbackOBJ->getSpecificAnswerFeedbackExportPresentation(
262  $this->object->getId(),
263  $answer->getPosition()
264  ));
265 
266  $xml->xmlEndTag('flow_mat');
267  $xml->xmlEndTag('itemfeedback');
268  }
269  if (strlen($feedback_allcorrect)) {
270  $xml->xmlStartTag('itemfeedback', array('ident' => 'response_allcorrect', 'view' => 'All'));
271  $xml->xmlStartTag('flow_mat');
272 
273  $this->object->addQTIMaterial($xml, $feedback_allcorrect);
274 
275  $xml->xmlEndTag('flow_mat');
276  $xml->xmlEndTag('itemfeedback');
277  }
278  if (strlen($feedback_onenotcorrect)) {
279  $xml->xmlStartTag('itemfeedback', array('ident' => 'response_onenotcorrect', 'view' => 'All'));
280  $xml->xmlStartTag('flow_mat');
281 
282  $this->object->addQTIMaterial($xml, $feedback_onenotcorrect);
283 
284  $xml->xmlEndTag('flow_mat');
285  $xml->xmlEndTag('itemfeedback');
286  }
287 
288  $xml->xmlEndTag("item");
289  $xml->xmlEndTag("questestinterop");
290 
291  $xml = $xml->xmlDumpMem(false);
292  if (!$a_include_header) {
293  $pos = strpos($xml, "?>");
294  $xml = substr($xml, $pos + 2);
295  }
296  return $xml;
297  }
298 
299  private function getMinPoints()
300  {
301  if ($this->object->isScorePartialSolutionEnabled()) {
302  return ($this->object->getPoints() / 2);
303  }
304 
305  return 0;
306  }
307 
308  private function getMaxPoints()
309  {
310  return $this->object->getPoints();
311  }
312 }
const KPRIM_CHOICE_QUESTION_IDENTIFIER
XML writer class.
Class for question exports.
$index
Definition: metadata.php:60
addGeneralMetadata(ilXmlWriter $xmlwriter)
$xml
Definition: metadata.php:240
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...
toXML($a_include_header=true, $a_include_binary=true, $a_shuffle=false, $test_output=false, $force_image_references=false)
Create new PHPExcel object
obj_idprivate