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