ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilQuestionExporter.php
Go to the documentation of this file.
1<?php
2
20
31{
35 protected ilLanguage $lng;
36
37 public static array $exported = []; //json data for all exported questions (class variable)
38 public static array $mobs = []; //json data for all mobs (class variable)
39 public static array $media_files = []; //json data for all files (class variable)
40
41 public ilDBInterface $db; // database object
42 public int $ref_id; // reference ID
43 public int $inst_id; // installation id
44 public ?assQuestionGUI $q_gui; // Question GUI object
45 public ilTemplate $tpl; // question template
46 public string $json; // json object for current question
47 public mixed $json_decoded; // json object (decoded) for current question
48 public bool $preview_mode; // preview mode activated yes/no
49
54 public function __construct(bool $a_preview_mode = false)
55 {
56 global $DIC;
57
58 $ilDB = $DIC->database();
59 $lng = $DIC->language();
60
61 $this->db = $ilDB;
62 $this->lng = $lng;
63
64 $this->lng->loadLanguageModule('assessment');
65
66 $this->inst_id = IL_INST_ID;
67
68 $this->preview_mode = $a_preview_mode;
69
70 $this->tpl = new ilTemplate("tpl.question_export.html", true, true, "components/ILIAS/COPage");
71
72 // fix for bug 5386, alex 29.10.2009
73 if (!$a_preview_mode) {
74 $this->tpl->setVariable("FORM_BEGIN", "<form onsubmit='return false;'>");
75 $this->tpl->setVariable("FORM_END", "</form>");
76 }
77 }
78
79
80 public function exportQuestion($a_ref_id, $a_image_path = null, $a_output_mode = "presentation")
81 {
82 if ($a_ref_id != "") {
84 if (!($inst_id > 0)) {
86 }
87 }
88
89 $this->q_gui = assQuestionGUI::_getQuestionGUI("", $q_id);
90
91 if (!is_object($this->q_gui->getObject())) {
92 return "Error: Question not found.";
93 }
94
95 $type = $this->q_gui->getObject()->getQuestionType();
96 if ($this->q_gui->getObject() instanceof QuestionLMExportable) {
97 $this->q_gui->getObject()->setExportImagePath($a_image_path);
98 $this->q_gui->getObject()->feedbackOBJ->setPageObjectOutputMode($a_output_mode);
99 $this->json = $this->q_gui->getObject()->toJSON();
100 $this->json_decoded = json_decode($this->json);
101 self::$exported[$this->json_decoded->id] = $this->json;
102 self::$mobs[$this->json_decoded->id] = $this->json_decoded->mobs;
103 return $this->$type();
104 } else {
105 return "Error: Question Type not implemented/Question editing not finished";
106 }
107 }
108
109 public static function questionsJS(?array $a_qids = null): string
110 {
111 $exportstring = '';
112 if (!is_array($a_qids)) {
113 $exportstring = 'var questions = new Array();';
114 }
115 foreach (self::$exported as $key => $value) {
116 if (!is_array($a_qids) || in_array($key, $a_qids)) {
117 $exportstring .= "questions[$key]= $value;";
118 }
119 }
120 return $exportstring;
121 }
122
123 private function assSingleChoice(): string
124 {
125 $this->tpl->setCurrentBlock("singlechoice");
126 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
127 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
128 if ($this->preview_mode) {
129 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
130 }
131 if ($this->json_decoded->path ?? false) {
132 $this->tpl->setVariable(
133 "HANDLE_IMAGES",
134 "ilias.questions.handleMCImages(" . $this->json_decoded->id . ");"
135 );
136 }
137 $this->tpl->parseCurrentBlock();
138 foreach ($this->json_decoded->answers as $answer) {
139 if ($answer->image != "") {
140 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . $answer->image);
141 if (is_file($this->q_gui->getObject()->getImagePath() . "thumb." . $answer->image)) {
142 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . "thumb." . $answer->image);
143 }
144 }
145 }
146 return $this->tpl->get();
147 }
148
149 private function assMultipleChoice(): string
150 {
151 $this->tpl->setCurrentBlock("multiplechoice");
152 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
153 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
154 if ($this->json_decoded->selection_limit) {
155 $this->tpl->setVariable('SELECTION_LIMIT_HINT', sprintf(
156 $this->lng->txt('ass_mc_sel_lim_hint'),
157 $this->json_decoded->selection_limit,
158 count($this->json_decoded->answers)
159 ));
160
161 $this->tpl->setVariable('SELECTION_LIMIT_VALUE', $this->json_decoded->selection_limit);
162 } else {
163 $this->tpl->setVariable('SELECTION_LIMIT_VALUE', 'null');
164 }
165 if ($this->preview_mode) {
166 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
167 }
168 if (isset($this->json_decoded->path)) {
169 $this->tpl->setVariable(
170 "HANDLE_IMAGES",
171 "ilias.questions.handleMCImages(" . $this->json_decoded->id . ");"
172 );
173 }
174 $this->tpl->parseCurrentBlock();
175 foreach ($this->json_decoded->answers as $answer) {
176 if ($answer->image != "") {
177 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . $answer->image);
178 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . "thumb." . $answer->image);
179 }
180 }
181 return $this->tpl->get();
182 }
183
184
185 private function assKprimChoice(): string
186 {
187 $this->tpl->setCurrentBlock("kprimchoice");
188
189 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
190 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
191
192 if ($this->preview_mode) {
193 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
194 }
195
196 if ($this->json_decoded->path ?? false) {
197 $this->tpl->setVariable(
198 "HANDLE_IMAGES",
199 "ilias.questions.handleKprimImages(" . $this->json_decoded->id . ");"
200 );
201 }
202
203 $this->tpl->setVariable('OPTION_LABEL_TRUE', $this->json_decoded->trueOptionLabel);
204 $this->tpl->setVariable('OPTION_LABEL_FALSE', $this->json_decoded->falseOptionLabel);
205
206 $this->tpl->setVariable('VALUE_TRUE', 1);
207 $this->tpl->setVariable('VALUE_FALSE', 0);
208
209 $this->tpl->parseCurrentBlock();
210
211 foreach ($this->json_decoded->answers as $answer) {
212 if (is_object($answer->image)) {
213 self::$media_files[] = $answer->getImageFsPath();
214 self::$media_files[] = $answer->getThumbFsPath();
215 } elseif (is_string($answer->image)) {
216 self::$media_files[] = $this->q_gui->getObject()->getImagePath() . $answer->image;
217 if (is_file($this->q_gui->getObject()->getImagePath() . "thumb." . $answer->image)) {
218 self::$media_files[] = $this->q_gui->getObject()->getImagePath() . "thumb." . $answer->image;
219 }
220 }
221 }
222
223 return $this->tpl->get();
224 }
225
226 private function assTextQuestion(): string
227 {
228 $maxlength = $this->json_decoded->maxlength == 0 ? 4096 : $this->json_decoded->maxlength;
229 $this->tpl->setCurrentBlock("textquestion");
230 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
231 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
232 $this->tpl->setVariable("VAL_MAXLENGTH", $maxlength);
233 if ($this->preview_mode) {
234 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
235 }
236 $this->tpl->parseCurrentBlock();
237 return $this->tpl->get();
238 }
239
240 private function assClozeTest(): string
241 {
242 $this->tpl->setCurrentBlock("clozequestion");
243 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
244 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
245 if ($this->preview_mode) {
246 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
247 }
248 $this->tpl->parseCurrentBlock();
249 return $this->tpl->get();
250 }
251
252 private function assLongMenu(): string
253 {
254 $this->tpl->setCurrentBlock("longmenu");
255 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
256 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
257 if ($this->preview_mode) {
258 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
259 }
260 $this->tpl->parseCurrentBlock();
261 return $this->tpl->get();
262 }
263
264 private function assOrderingQuestion(): string
265 {
266 $this->tpl->setCurrentBlock("orderingquestion");
267 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
268 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
269 if ($this->preview_mode) {
270 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
271 }
272 if ($this->q_gui->getObject()->getOrderingType() == assOrderingQuestion::OQ_PICTURES) {
273 $this->tpl->setVariable("VAL_SUBTYPE", "_images");
274 $this->tpl->setVariable(
275 "HANDLE_IMAGES",
276 "ilias.questions.handleOrderingImages(" . $this->json_decoded->id . ");"
277 );
278
279 foreach ($this->json_decoded->answers as $answer) {
280 if ($answer->answertext != "") {
281 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . $answer->answertext);
282 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . "thumb." . $answer->answertext);
283 }
284 }
285 } else {
286 $this->tpl->setVariable("VAL_SUBTYPE", "_terms");
287 }
288 $this->tpl->parseCurrentBlock();
289 return $this->tpl->get();
290 }
291
292 private function assMatchingQuestion(): string
293 {
294 $this->tpl->setCurrentBlock("matchingquestion");
295 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
296 $this->tpl->setVariable("BTN_LABEL_RESET", $this->lng->txt("reset_terms"));
297 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
298 if ($this->preview_mode) {
299 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
300 }
301 $this->tpl->parseCurrentBlock();
302 return $this->tpl->get();
303 }
304
305 private function assImagemapQuestion(): string
306 {
307 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
308 array_push(self::$media_files, $this->q_gui->getObject()->getImagePath() . $this->q_gui->getObject()->getImageFilename());
309 $this->tpl->setCurrentBlock("mapareas");
310 $areas = $this->json_decoded->answers;
311 //set areas in PHP cause of inteference between pure and highlighter
312 foreach ($areas as $area) {
313 $this->tpl->setVariable("VAL_TOOLTIP", htmlspecialchars($area->answertext));
314 $this->tpl->setVariable("VAL_COORDS", $area->coords);
315 $this->tpl->setVariable("VAL_ORDER", $area->order);
316 $this->tpl->setVariable("VAL_AREA", $area->area);
317 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
318 $this->tpl->parseCurrentBlock();
319 }
320 $this->tpl->setCurrentBlock("imagemapquestion");
321 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
322 if ($this->preview_mode) {
323 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
324 }
325 $this->tpl->parseCurrentBlock();
326 return $this->tpl->get();
327 }
328
329 private function assTextSubset(): string
330 {
331 $this->tpl->setCurrentBlock("textsubset");
332 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
333 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
334 if ($this->preview_mode) {
335 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
336 }
337 $this->tpl->parseCurrentBlock();
338 return $this->tpl->get();
339 }
340
341 private function assOrderingHorizontal(): string
342 {
343 $this->tpl->setCurrentBlock("orderinghorizontal");
344 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
345 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
346 if ($this->preview_mode) {
347 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
348 }
349 $this->tpl->parseCurrentBlock();
350 return $this->tpl->get();
351 }
352
353 private function assErrorText(): string
354 {
355 $this->tpl->setCurrentBlock("errortext");
356 $this->tpl->setVariable("VAL_ID", $this->json_decoded->id);
357 $this->tpl->setVariable("TXT_SUBMIT_ANSWERS", $this->lng->txt("cont_submit_answers"));
358 if ($this->preview_mode) {
359 $this->tpl->setVariable("VAL_NO_DISPLAY", "style=\"display:none\"");
360 }
361 $this->tpl->parseCurrentBlock();
362 return $this->tpl->get();
363 }
364}
static _getQuestionGUI(string $question_type='', int $question_id=-1)
Creates a question gui representation and returns the alias to the question gui.
language handling
Scorm 2004 Question Exporter.
exportQuestion($a_ref_id, $a_image_path=null, $a_output_mode="presentation")
static questionsJS(?array $a_qids=null)
__construct(bool $a_preview_mode=false)
Constructor @access public.
special template class to simplify handling of ITX/PEAR
const IL_INST_ID
Definition: constants.php:40
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26