ILIAS  release_8 Revision v8.24
class.ilCategoryWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
25{
27 protected ?SurveyCategories $values = null;
28 protected bool $allowMove = false;
29 protected bool $disabled_scale = true;
30 protected bool $show_wizard = false;
31 protected bool $show_save_phrase = false;
32 protected string $categorytext;
33 protected bool $show_neutral_category = false;
34 protected string $neutral_category_title;
35 protected bool $use_other_answer;
36
37 public function __construct(
38 string $a_title = "",
39 string $a_postvar = ""
40 ) {
41 global $DIC;
42
43 $this->lng = $DIC->language();
44 $this->tpl = $DIC["tpl"];
45 $lng = $DIC->language();
46
47 parent::__construct($a_title, $a_postvar);
48
49 $this->show_wizard = false;
50 $this->show_save_phrase = false;
51 $this->categorytext = $lng->txt('answer');
52 $this->use_other_answer = false;
53
54 $this->setMaxLength(1000); // #6218
55 }
56
57 public function getUseOtherAnswer(): bool
58 {
60 }
61
62 public function setUseOtherAnswer(bool $a_value): void
63 {
64 $this->use_other_answer = $a_value;
65 }
66
67 public function getCategoryCount(): int
68 {
69 if (!is_object($this->values)) {
70 return 0;
71 }
72 return $this->values->getCategoryCount();
73 }
74
75 protected function calcNeutralCategoryScale(): int
76 {
77 if (is_object($this->values)) {
78 $scale = 0;
79 for ($i = 0; $i < $this->values->getCategoryCount(); $i++) {
80 $cat = $this->values->getCategory($i);
81 if ($cat->neutral == 0) {
82 $scale += 1;
83 }
84 }
85 return $scale + 1;
86 }
87
88 return 99;
89 }
90
91 public function setShowNeutralCategory(bool $a_value): void
92 {
93 $this->show_neutral_category = $a_value;
94 }
95
96 public function getShowNeutralCategory(): bool
97 {
99 }
100
101 public function setNeutralCategoryTitle(string $a_title): void
102 {
103 $this->neutral_category_title = $a_title;
104 }
105
106 public function getNeutralCategoryTitle(): string
107 {
109 }
110
114 public function setValue($a_value): void
115 {
116 $this->values = new SurveyCategories();
117 if (is_array($a_value)) {
118 if (is_array($a_value['answer'])) {
119 foreach ($a_value['answer'] as $index => $value) {
120 $this->values->addCategory($value, $a_value['other'][$index] ?? 0, 0, null, $a_value['scale'][$index] ?? null);
121 }
122 }
123 }
124 if (array_key_exists('neutral', $a_value)) {
125 $scale = $this->str($this->postvar . '_neutral_scale');
126 $scale = ($scale === "")
127 ? null
128 : (int) $scale;
129 $this->values->addCategory(
130 $a_value['neutral'],
131 0,
132 1,
133 null,
134 $scale
135 );
136 }
137 }
138
139 public function setValues(SurveyCategories $a_values): void
140 {
141 $this->values = $a_values;
142 }
143
144 public function getValues(): SurveyCategories
145 {
146 return $this->values;
147 }
148
149 public function setAllowMove(bool $a_allow_move): void
150 {
151 $this->allowMove = $a_allow_move;
152 }
153
154 public function getAllowMove(): bool
155 {
156 return $this->allowMove;
157 }
158
159 public function setShowWizard(bool $a_value): void
160 {
161 $this->show_wizard = $a_value;
162 }
163
164 public function getShowWizard(): bool
165 {
166 return $this->show_wizard;
167 }
168
169 public function setCategoryText(string $a_text): void
170 {
171 $this->categorytext = $a_text;
172 }
173
174 public function getCategoryText(): string
175 {
176 return $this->categorytext;
177 }
178
179 public function setShowSavePhrase(bool $a_value): void
180 {
181 $this->show_save_phrase = $a_value;
182 }
183
184 public function getShowSavePhrase(): bool
185 {
187 }
188
189 public function getDisabledScale(): bool
190 {
192 }
193
194 public function setDisabledScale(bool $a_value): void
195 {
196 $this->disabled_scale = $a_value;
197 }
198
199 public function checkInput(): bool
200 {
202 $foundvalues = $this->getInput();
203 $neutral_scale = $this->getNeutralScaleInput();
204 $neutral = $this->getNeutralInput();
205
206 if (count($foundvalues) > 0) {
207 // check answers
208 if (is_array($foundvalues['answer'] ?? false)) {
209 foreach ($foundvalues['answer'] as $idx => $answervalue) {
210 if (((strlen($answervalue)) == 0) && ($this->getRequired() && (!isset($foundvalues['other'][$idx])))) {
211 $this->setAlert($lng->txt("msg_input_is_required"));
212 return false;
213 }
214 }
215 }
216 // check neutral column
217 /* see #33267
218 if (array_key_exists('neutral', $foundvalues)) {
219 if ((strlen($neutral) == 0) && ($this->getRequired())) {
220 $this->setAlert($lng->txt("msg_input_is_required"));
221 return false;
222 }
223 }*/
224 // check scales
225 if (isset($foundvalues['scale'])) {
226 foreach ($foundvalues['scale'] as $scale) {
227 //scales required
228 if ((strlen($scale)) == 0) {
229 $this->setAlert($lng->txt("msg_input_is_required"));
230 return false;
231 }
232 //scales positive number
233 if (!ctype_digit($scale) || $scale <= 0) {
234 $this->setAlert($lng->txt("msg_input_only_positive_numbers"));
235 return false;
236 }
237 }
238 //scales no duplicates.
239 if (count(array_unique($foundvalues['scale'])) !== count($foundvalues['scale'])) {
240 $this->setAlert($lng->txt("msg_duplicate_scale"));
241 return false;
242 }
243 }
244
245 // check neutral column scale
246 if ($neutral_scale != "") {
247 if (isset($foundvalues['scale'])) {
248 if (in_array($neutral_scale, $foundvalues['scale'])) {
249 $this->setAlert($lng->txt("msg_duplicate_scale"));
250 return false;
251 }
252 }
253 }
254 } else {
255 $this->setAlert($lng->txt("msg_input_is_required"));
256 return false;
257 }
258 return $this->checkSubItemsInput();
259 }
260
261 public function getInput(): array
262 {
263 $val = $this->arrayArray($this->getPostVar());
265 return $val;
266 }
267
268 public function getNeutralScaleInput(): string
269 {
270 return $this->str($this->getPostVar() . '_neutral_scale');
271 }
272
273 public function getNeutralInput(): string
274 {
275 $val = $this->strArray($this->getPostVar());
276 return $val["neutral"];
277 }
278
279 public function insert(
280 ilTemplate $a_tpl
281 ): void {
282 $lng = $this->lng;
283
284 $neutral_category = null;
285 $tpl = new ilTemplate("tpl.prop_categorywizardinput.html", true, true, "Modules/SurveyQuestionPool");
286 if (is_object($this->values)) {
287 for ($i = 0; $i < $this->values->getCategoryCount(); $i++) {
288 $cat = $this->values->getCategory($i);
289 if (!$cat->neutral) {
290 $tpl->setCurrentBlock("prop_text_propval");
291 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($cat->title));
293 $tpl->setCurrentBlock("prop_scale_propval");
295 "PROPERTY_VALUE",
296 ilLegacyFormElementsUtil::prepareFormOutput($this->values->getScale($i))
297 );
299
300 if ($this->getUseOtherAnswer()) {
301 $tpl->setCurrentBlock("other_answer_checkbox");
302 $tpl->setVariable("POST_VAR", $this->getPostVar());
303 $tpl->setVariable("OTHER_ID", $this->getPostVar() . "[other][$i]");
304 $tpl->setVariable("ROW_NUMBER", $i);
305 if ($cat->other) {
306 $tpl->setVariable("CHECKED_OTHER", ' checked="checked"');
307 }
309 }
310
311 if ($this->getAllowMove()) {
312 $tpl->setCurrentBlock("move");
313 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
314 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
315 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
316 $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
319 }
320
321 $tpl->setCurrentBlock("row");
322 $tpl->setVariable("POST_VAR", $this->getPostVar());
323 $tpl->setVariable("ROW_NUMBER", $i);
324 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
325 $tpl->setVariable("SIZE", $this->getSize());
326 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
327 if ($this->getDisabled()) {
328 $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
329 }
330
331 $tpl->setVariable("SCALE_ID", $this->getPostVar() . "[scale][$i]");
332 if ($this->getDisabledScale()) {
333 $tpl->setVariable("DISABLED_SCALE", " disabled=\"disabled\"");
334 }
335
336 $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
337 $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
338 $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
341 } else {
342 $neutral_category = $cat;
343 }
344 }
345 }
346
347 if ($this->getShowWizard()) {
348 $tpl->setCurrentBlock("wizard");
349 $tpl->setVariable("CMD_WIZARD", 'cmd[addPhrase]');
350 $tpl->setVariable("WIZARD_BUTTON", ilUtil::getImagePath('wizard.svg'));
351 $tpl->setVariable("WIZARD_TEXT", $lng->txt('add_phrase'));
353 }
354
355 if ($this->getShowSavePhrase()) {
356 $tpl->setCurrentBlock('savephrase');
357 $tpl->setVariable("POST_VAR", $this->getPostVar());
358 $tpl->setVariable("VALUE_SAVE_PHRASE", $lng->txt('save_phrase'));
360 }
361
362 if ($this->getShowNeutralCategory()) {
363 if (is_object($neutral_category) && strlen($neutral_category->title)) {
364 $tpl->setCurrentBlock("prop_text_neutral_propval");
366 "PROPERTY_VALUE",
367 ilLegacyFormElementsUtil::prepareFormOutput($neutral_category->title)
368 );
370 }
371 if ($this->getNeutralCategoryTitle() !== '') {
372 $tpl->setCurrentBlock("neutral_category_title");
373 $tpl->setVariable("NEUTRAL_COLS", ($this->getUseOtherAnswer()) ? 4 : 3);
374 $tpl->setVariable(
375 "CATEGORY_TITLE",
377 );
378 $tpl->parseCurrentBlock();
379 }
380 $tpl->setCurrentBlock("prop_scale_neutral_propval");
381 $scale = (is_object($neutral_category) && $neutral_category->scale > 0) ? $neutral_category->scale : $this->values->getNewScale();
384
385 if ($this->getUseOtherAnswer()) {
386 $tpl->touchBlock('other_answer_neutral');
387 }
388
389 $tpl->setCurrentBlock('neutral_row');
390 $tpl->setVariable("POST_VAR", $this->getPostVar());
391 $tpl->setVariable("ID", $this->getPostVar() . "_neutral");
392 $tpl->setVariable("SIZE", $this->getSize());
393 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
394 if ($this->getDisabled()) {
395 $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
396 }
397 $tpl->setVariable("SCALE_ID", $this->getPostVar() . "_neutral_scale");
398 if ($this->getDisabledScale()) {
399 $tpl->setVariable("DISABLED_SCALE", " disabled=\"disabled\"");
400 }
402 }
403
404 if ($this->getUseOtherAnswer()) {
405 $tpl->setCurrentBlock('other_answer_title');
406 $tpl->setVariable("OTHER_TEXT", $lng->txt('use_other_answer'));
408 }
409
410 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
411 $tpl->setVariable("ANSWER_TEXT", $this->getCategoryText());
412 $tpl->setVariable("SCALE_TEXT", $lng->txt('scale'));
413 $tpl->setVariable("ACTIONS_TEXT", $lng->txt('actions'));
414
415 $a_tpl->setCurrentBlock("prop_generic");
416 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
417 $a_tpl->parseCurrentBlock();
418
420 $tpl->addJavaScript("./Services/Form/js/ServiceFormWizardInput.js");
421 $tpl->addJavaScript("./Modules/SurveyQuestionPool/Categories/js/categorywizard.js");
422 }
423}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(string $a_title="", string $a_postvar="")
checkInput()
Check input, strip slashes etc.
setValues(SurveyCategories $a_values)
static get(string $a_glyph, string $a_text="")
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static prepareFormOutput($a_str, bool $a_strip=false)
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents a text property in a property form.
setMaxLength(?int $a_maxlength)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addJavaScript(string $a_js_file, bool $a_add_version_parameter=true, int $a_batch=2)
Add a javascript file that should be included in the header.
touchBlock(string $block)
overwrites ITX::touchBlock.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc