ILIAS  release_7 Revision v7.30-3-g800a261c036
class.SurveySingleChoiceQuestionGUI.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
14{
15 protected function initObject()
16 {
17 $this->object = new SurveySingleChoiceQuestion();
18 }
19
20
21 //
22 // EDITOR
23 //
24
25 public function setQuestionTabs()
26 {
27 $this->setQuestionTabsForClass("surveysinglechoicequestiongui");
28 }
29
30 protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
31 {
32 // orientation
33 $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
34 $orientation->setRequired(false);
35 $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
36 $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
37 $orientation->addOption(new ilRadioOption($this->lng->txt('combobox'), 2));
38 $a_form->addItem($orientation);
39
40 // Answers
41 $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
42 $answers->setRequired(false);
43 $answers->setAllowMove(true);
44 $answers->setShowWizard(true);
45 $answers->setShowSavePhrase(true);
46 $answers->setUseOtherAnswer(true);
47 $answers->setShowNeutralCategory(true);
48 $answers->setNeutralCategoryTitle($this->lng->txt('svy_neutral_answer'));
49 $answers->setDisabledScale(false);
50 $a_form->addItem($answers);
51
52 // values
53 $orientation->setValue($this->object->getOrientation());
54 if (!$this->object->getCategories()->getCategoryCount()) {
55 $this->object->getCategories()->addCategory("");
56 }
57 $answers->setValues($this->object->getCategories());
58 }
59
60 protected function importEditFormValues(ilPropertyFormGUI $a_form)
61 {
62 $this->log->debug("importing edit values");
63
64 $this->object->setOrientation($a_form->getInput("orientation"));
65
66 $this->object->categories->flushCategories();
67 foreach ($_POST['answers']['answer'] as $key => $value) {
68 if (strlen($value)) {
69 $this->object->getCategories()->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
70 }
71 }
72 if (strlen($_POST['answers']['neutral'])) {
73 $this->object->getCategories()->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
74 }
75 }
76
77 public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
78 {
79 if (is_array($a_working_data)) {
80 $user_answer = $a_working_data[0];
81 }
82
83 $options = array();
84 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
85 $cat = $this->object->categories->getCategory($i);
86 $value = ($cat->scale) ? ($cat->scale - 1) : $i;
87
88 $checked = "unchecked";
89 $text = null;
90 if (is_array($a_working_data) &&
91 is_array($user_answer)) {
92 if ($value == $user_answer["value"]) {
93 $checked = "checked";
94 if ($user_answer["textanswer"]) {
95 $text = $user_answer["textanswer"];
96 }
97 }
98 }
99
100 // "other" options have to be last or horizontal will be screwed
101 $idx = $cat->other . "_" . $value;
102
103 if (!$a_only_user_anwers || $checked == "checked") {
104 $options[$idx] = array(
105 "value" => $value
106 ,"title" => trim($cat->title)
107 ,"other" => (bool) $cat->other
108 ,"checked" => $checked
109 ,"textanswer" => $text
110 );
111 }
112
113 ksort($options);
114 }
115
116 return array_values($options);
117 }
118
124 public function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
125 {
126 $options = $this->getParsedAnswers($a_working_data);
127
128 // rendering
129
130 $template = new ilTemplate("tpl.il_svy_qpl_sc_printview.html", true, true, "Modules/SurveyQuestionPool");
131 switch ($this->object->orientation) {
132 case 0:
133 // vertical orientation
134 foreach ($options as $option) {
135 if ($option["other"]) {
136 $template->setCurrentBlock("other_row");
137 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
138 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
139 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
140 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
141 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
142 ? ilUtil::prepareFormOutput($option["textanswer"])
143 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
144 $template->parseCurrentBlock();
145 } else {
146 $template->setCurrentBlock("row");
147 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
148 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
149 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
150 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
151 $template->parseCurrentBlock();
152 }
153 }
154 break;
155 case 1:
156 // horizontal orientation
157 foreach ($options as $option) {
158 $template->setCurrentBlock("radio_col");
159 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
160 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
161 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
162 $template->parseCurrentBlock();
163 }
164 foreach ($options as $option) {
165 if ($option["other"]) {
166 $template->setCurrentBlock("other_text_col");
167 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
168 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
169 ? ilUtil::prepareFormOutput($option["textanswer"])
170 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
171 $template->parseCurrentBlock();
172 } else {
173 $template->setCurrentBlock("text_col");
174 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
175 $template->parseCurrentBlock();
176 }
177 }
178 break;
179 case 2:
180 foreach ($options as $option) {
181 $template->setCurrentBlock("comborow");
182 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
183 $template->setVariable("VALUE_SC", $option["value"]);
184 if ($option["checked"] == "checked") {
185 $template->setVariable("SELECTED_SC", ' selected="selected"');
186 }
187 $template->parseCurrentBlock();
188 }
189 $template->setCurrentBlock("combooutput");
190 $template->setVariable("QUESTION_ID", $this->object->getId());
191 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
192 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
193 $template->parseCurrentBlock();
194 break;
195 }
196 if ($question_title) {
197 $template->setVariable("QUESTION_TITLE", $this->getPrintViewQuestionTitle($question_title));
198 }
199 if ($show_questiontext) {
200 $this->outQuestionText($template);
201 }
202 $template->parseCurrentBlock();
203 return $template->get();
204 }
205
206
207 //
208 // EXECUTION
209 //
210
216 public function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null, $compress_view = false)
217 {
218 $orientation = $this->object->orientation;
219 $template_file = "tpl.il_svy_out_sc.html";
220 if ($compress_view && $orientation == 1) {
221 $template_file = "tpl.il_svy_out_sc_comp.html";
222 $orientation = 3;
223 }
224 $template = new ilTemplate($template_file, true, true, "Modules/SurveyQuestionPool");
225 if ($this->getMaterialOutput() != "") {
226 $template->setCurrentBlock("material");
227 $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
228 $template->parseCurrentBlock();
229 }
230 switch ($orientation) {
231 case 0:
232 // vertical orientation
233 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
234 $cat = $this->object->categories->getCategory($i);
235
236 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
237 $this->log->debug("Vertical orientation - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
238
239 if ($cat->other) {
240 $template->setCurrentBlock("other_row");
241 if (strlen($cat->title)) {
242 $template->setVariable("OTHER_LABEL", $cat->title);
243 }
244 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
245 $template->setVariable("QUESTION_ID", $this->object->getId());
246 if (is_array($working_data)) {
247 foreach ($working_data as $value) {
248 if (strlen($value["value"])) {
249 if ($value["value"] == $cat->scale - 1) {
250 if (strlen($value['textanswer'])) {
251 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
252 }
253 if (!$value['uncheck']) {
254 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
255 }
256 }
257 }
258 }
259 }
260 $template->parseCurrentBlock();
261 } else {
262 $template->setCurrentBlock("row");
263 if ($cat->neutral) {
264 $template->setVariable('ROWCLASS', ' class="neutral"');
265 }
266 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
267 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
268 $template->setVariable("QUESTION_ID", $this->object->getId());
269 if (is_array($working_data)) {
270 foreach ($working_data as $value) {
271 if (strcmp($value["value"], "") != 0) {
272 if ($value["value"] == $cat->scale - 1) {
273 if (!$value['uncheck']) {
274 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
275 }
276 }
277 }
278 }
279 }
280 $template->parseCurrentBlock();
281 }
282 $template->touchBlock('outer_row');
283 }
284 break;
285 case 1:
286 // horizontal orientation
287 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
288 $cat = $this->object->categories->getCategory($i);
289
290 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
291 $this->log->debug("Horizontal orientation - Original NEUTRAL scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
292
293 $template->setCurrentBlock("radio_col");
294 if ($cat->neutral) {
295 $template->setVariable('COLCLASS', ' neutral');
296 }
297 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
298 $template->setVariable("QUESTION_ID", $this->object->getId());
299 if (is_array($working_data)) {
300 foreach ($working_data as $value) {
301 if (strcmp($value["value"], "") != 0) {
302 if ($value["value"] == $cat->scale - 1) {
303 if (!$value['uncheck']) {
304 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
305 }
306 }
307 }
308 }
309 }
310 $template->parseCurrentBlock();
311 }
312 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
313 $cat = $this->object->categories->getCategory($i);
314
315 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
316 $this->log->debug("Horizontal orientation - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
317
318 if ($cat->other) {
319 $template->setCurrentBlock("text_other_col");
320 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
321 $template->setVariable("QUESTION_ID", $this->object->getId());
322 if (strlen($cat->title)) {
323 $template->setVariable("OTHER_LABEL", $cat->title);
324 }
325 if (is_array($working_data)) {
326 foreach ($working_data as $value) {
327 if (strlen($value["value"])) {
328 if ($value["value"] == $cat->scale - 1 && strlen($value['textanswer'])) {
329 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
330 }
331 }
332 }
333 }
334 $template->parseCurrentBlock();
335 } else {
336 $template->setCurrentBlock("text_col");
337 if ($cat->neutral) {
338 $template->setVariable('COLCLASS', ' neutral');
339 }
340 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
341 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
342 $template->setVariable("QUESTION_ID", $this->object->getId());
343 $template->parseCurrentBlock();
344 }
345 $template->touchBlock('text_outer_col');
346 }
347 break;
348 case 2:
349 // combobox output
350 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
351 $cat = $this->object->categories->getCategory($i);
352
353 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
354 $this->log->debug("Combobox - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
355
356 $template->setCurrentBlock("comborow");
357 $template->setVariable("TEXT_SC", $cat->title);
358 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
359 if (is_array($working_data)) {
360 if (strcmp($working_data[0]["value"], "") != 0) {
361 if ($working_data[0]["value"] == $cat->scale - 1) {
362 $template->setVariable("SELECTED_SC", " selected=\"selected\"");
363 }
364 }
365 }
366 $template->parseCurrentBlock();
367 }
368 $template->setCurrentBlock("combooutput");
369 $template->setVariable("QUESTION_ID", $this->object->getId());
370 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
371 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
372 $template->parseCurrentBlock();
373 break;
374 case 3:
375 // horizontal orientation, compressed
376 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
377 $cat = $this->object->categories->getCategory($i);
378
379 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
380 $this->log->debug("Horizontal orientation (compressed) - Original NEUTRAL scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
381
382 if ($cat->other) {
383 $template->setCurrentBlock("other");
384 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
385 $template->setVariable("OTHER_Q_ID", $this->object->getId());
386 if (is_array($working_data)) {
387 foreach ($working_data as $value) {
388 if (strlen($value["value"])) {
389 if ($value["value"] == $cat->scale - 1 && strlen($value['textanswer'])) {
390 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
391 }
392 }
393 }
394 }
395 $template->parseCurrentBlock();
396 }
397
398
399 $template->setCurrentBlock("radio_col");
400 if ($cat->neutral) {
401 $template->setVariable('COLCLASS', ' neutral');
402 }
403 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
404 $template->setVariable("QUESTION_ID", $this->object->getId());
405 if (is_array($working_data)) {
406 foreach ($working_data as $value) {
407 if (strcmp($value["value"], "") != 0) {
408 if ($value["value"] == $cat->scale - 1) {
409 if (!$value['uncheck']) {
410 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
411 }
412 }
413 }
414 }
415 }
416 $template->parseCurrentBlock();
417 }
418 $perc = round(70 / $this->object->categories->getCategoryCount(), 2);
419 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
420 $cat = $this->object->categories->getCategory($i);
421
422 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
423 $this->log->debug("Horizontal orientation - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
424
425 $template->setCurrentBlock("text_col");
426 if ($cat->neutral) {
427 $template->setVariable('COLCLASS', ' neutral');
428 }
429 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
430 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
431 $template->setVariable("PERC", $perc);
432 $template->setVariable("QUESTION_ID", $this->object->getId());
433 $template->parseCurrentBlock();
434 }
435 break;
436 }
437 if ($question_title) {
438 $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
439 }
440 $template->setCurrentBlock("question_data");
441 if (strcmp($error_message, "") != 0) {
442 $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
443 }
444 if ($show_questiontext) {
445 $this->outQuestionText($template);
446 }
447 $template->parseCurrentBlock();
448 return $template->get();
449 }
450}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
Basic class for all survey question types.
getPrintViewQuestionTitle($question_title=1)
getMaterialOutput()
Creates the HTML output of the question material(s)
SingleChoice survey question GUI representation.
getParsedAnswers(array $a_working_data=null, $a_only_user_anwers=false)
getPrintView($question_title=1, $show_questiontext=1, $survey_id=null, array $a_working_data=null)
Creates a HTML representation of the question.
getWorkingForm($working_data="", $question_title=1, $show_questiontext=1, $error_message="", $survey_id=null, $compress_view=false)
Creates the question output form for the learner.
This class represents a survey question category wizard property in a property form.
This class represents a property form user interface.
addItem($a_item)
Add Item (Property, SectionHeader).
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
This class represents a property in a property form.
This class represents an option in a radio group.
special template class to simplify handling of ITX/PEAR
static getHtmlPath($relative_path)
get url of path
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$i
Definition: metadata.php:24