ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilAnswerWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
22use ILIAS\UI\Component\Button\Factory as ButtonFactory;
23
32{
33 protected $values = [];
34 protected $allowMove = false;
35 protected $allowAddRemove = true;
36 protected $singleline = true;
37 protected $qstObject = null;
38 protected $minvalue = false;
39 protected $minvalueShouldBeGreater = false;
40
42 protected GlyphFactory $glyph_factory;
43 protected ButtonFactory $button_factory;
45
52 public function __construct($a_title = "", $a_postvar = "")
53 {
54 parent::__construct($a_title, $a_postvar);
55
56 global $DIC;
57
58 $this->forms_helper = new ilTestLegacyFormsHelper();
59 $this->button_factory = $DIC->ui()->factory()->button();
60 $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
61 $this->renderer = $DIC->ui()->renderer();
62
63 $this->setSize('25');
64 $this->validationRegexp = "";
65 }
66
67 public function setValue($a_value): void
68 {
69 $this->values = [];
70
71 $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
72 $images = $this->forms_helper->transformArray($a_value, 'imagename', $this->refinery->kindlyTo()->string());
73 $points = $this->forms_helper->transformPoints($a_value);
74
75 foreach ($answers as $index => $value) {
76 $answer = new ASS_AnswerBinaryStateImage(
77 $value,
78 $points[$index] ?? 0.0,
79 $index,
80 true,
81 null,
82 -1
83 );
84 if ($this->forms_helper->inArray($images, $index)) {
85 $answer->setImage($images[$index]);
86 }
87 $this->values[] = $answer;
88 }
89 }
90
96 public function setValues($a_values): void
97 {
98 $this->values = $a_values;
99 }
100
106 public function getValues(): array
107 {
108 return $this->values;
109 }
110
116 public function setSingleline($a_value): void
117 {
118 $this->singleline = $a_value;
119 }
120
126 public function getSingleline(): bool
127 {
128 return $this->singleline;
129 }
130
136 public function setQuestionObject($a_value): void
137 {
138 $this->qstObject = &$a_value;
139 }
140
146 public function getQuestionObject(): ?object
147 {
148 return $this->qstObject;
149 }
150
156 public function setAllowMove($a_allow_move): void
157 {
158 $this->allowMove = $a_allow_move;
159 }
160
166 public function getAllowMove(): bool
167 {
168 return $this->allowMove;
169 }
170
174 public function isAddRemoveAllowed(): bool
175 {
177 }
178
182 public function setAllowAddRemove($allowAddRemove): void
183 {
184 $this->allowAddRemove = $allowAddRemove;
185 }
186
192 public function setMinvalueShouldBeGreater($a_bool): void
193 {
194 $this->minvalueShouldBeGreater = $a_bool;
195 }
196
202 public function minvalueShouldBeGreater(): bool
203 {
205 }
211 public function setMinValue($a_minvalue): void
212 {
213 $this->minvalue = $a_minvalue;
214 }
215
221 public function getMinValue()
222 {
223 return $this->minvalue;
224 }
229 public function checkInput(): bool
230 {
231 $data = $this->raw($this->getPostVar());
232
233 if (!is_array($data)) {
234 $this->setAlert($this->lng->txt('msg_input_is_required'));
235 return false;
236 }
237
238 // check points
239 $points = $this->forms_helper->checkPointsInputEnoughPositive($data, true);
240 if (!is_array($points)) {
241 $this->setAlert($this->lng->txt($points));
242 return false;
243 }
244 foreach ($points as $value) {
245 if ($value !== 0.0 && $this->getMinValue() !== false) {
246 if (($this->minvalueShouldBeGreater() && $points <= $this->getMinValue()) ||
247 (!$this->minvalueShouldBeGreater() && $points < $this->getMinValue())) {
248 $this->setAlert($this->lng->txt('form_msg_value_too_low'));
249 return false;
250 }
251 }
252 }
253
254 // check answers
255 $answers = $this->forms_helper->transformArray($data, 'answer', $this->refinery->kindlyTo()->string());
256 foreach ($answers as $answer) {
257 if ($answer === '') {
258 $this->setAlert($this->lng->txt('msg_input_is_required'));
259 return false;
260 }
261 }
262
263 return $this->checkSubItemsInput();
264 }
265
266 public function insert(ilTemplate $a_tpl): void
267 {
268 $tpl = new ilTemplate($this->getTemplate(), true, true, "components/ILIAS/TestQuestionPool");
269 $i = 0;
270 foreach ($this->values as $value) {
271 if ($this->getSingleline()) {
272 if (is_object($value)) {
273 $tpl->setCurrentBlock("prop_text_propval");
274 $tpl->setVariable(
275 "PROPERTY_VALUE",
277 );
278 $tpl->parseCurrentBlock();
279 $tpl->setCurrentBlock("prop_points_propval");
280 $tpl->setVariable(
281 "PROPERTY_VALUE",
283 );
284 $tpl->parseCurrentBlock();
285 }
286 $tpl->setCurrentBlock('singleline');
287 $tpl->setVariable("SIZE", $this->getSize());
288 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
289 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
290 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
291 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
292 if ($this->getDisabled()) {
293 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
294 }
295 $tpl->parseCurrentBlock();
296 } elseif (!$this->getSingleline()) {
297 if (is_object($value)) {
298 $tpl->setCurrentBlock("prop_points_propval");
299 $tpl->setVariable(
300 "PROPERTY_VALUE",
302 );
303 $tpl->parseCurrentBlock();
304 }
305 $tpl->setCurrentBlock('multiline');
306 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareTextareaOutput($value->getAnswertext()));
307 $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
308 $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
309 $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
310 if ($this->getDisabled()) {
311 $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
312 }
313 $tpl->parseCurrentBlock();
314 }
315 if ($this->getAllowMove()) {
316 $tpl->setCurrentBlock("move");
317 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
318 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
319 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
320 $tpl->setVariable("UP_BUTTON", $this->renderer->render(
321 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->up())
322 ));
323 $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
324 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->down())
325 ));
326 $tpl->parseCurrentBlock();
327 }
328 $tpl->setCurrentBlock("row");
329 $tpl->setVariable("POST_VAR", $this->getPostVar());
330 $tpl->setVariable("ROW_NUMBER", $i);
331 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
332 $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
333 if ($this->isAddRemoveAllowed()) {
334 $tpl->setVariable("ADD_REMOVE_ID", $this->getPostVar() . "[$i]");
335 $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
336 $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
337 $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
338 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->add())
339 ));
340 $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
341 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->remove())
342 ));
343 }
344 if ($this->getDisabled()) {
345 $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
346 }
347 $tpl->parseCurrentBlock();
348 $i++;
349 }
350
351 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
352 $tpl->setVariable("ANSWER_TEXT", $this->getTextInputLabel($this->lng));
353 $tpl->setVariable("POINTS_TEXT", $this->getPointsInputLabel($this->lng));
354 $tpl->setVariable("COMMANDS_TEXT", $this->lng->txt('actions'));
355
356 $a_tpl->setCurrentBlock("prop_generic");
357 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
358 $a_tpl->parseCurrentBlock();
359
360 global $DIC;
361 $tpl = $DIC['tpl'];
362 $tpl->addJavascript("assets/js/answerwizardinput.js");
363 $tpl->addJavascript("assets/js/answerwizard.js");
364 }
365
370 protected function getTextInputLabel($lng)
371 {
372 return $lng->txt('answer_text');
373 }
374
379 protected function getPointsInputLabel($lng)
380 {
381 return $lng->txt('points');
382 }
383
387 protected function getTemplate(): string
388 {
389 return "tpl.prop_answerwizardinput.html";
390 }
391
392 protected function prepareFormOutput(float|string $input, bool $strip = false): string
393 {
394 $input_string = $this->refinery->kindlyTo()->string()->transform($input);
395 return str_replace(
396 ['{', '}', '\\'],
397 ['&#123;', '&#125;', '&#92;'],
398 htmlspecialchars(
399 $strip ? ilUtil::stripSlashes($input_string) : $input_string,
400 ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
401 null,
402 false
403 )
404 );
405 }
406}
renderer()
Class for answers with a binary state indicator.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
This class represents a single choice wizard property in a property form.
getQuestionObject()
Get question object.
checkInput()
Check input, strip slashes etc.
ilTestLegacyFormsHelper $forms_helper
prepareFormOutput(float|string $input, bool $strip=false)
setMinvalueShouldBeGreater($a_bool)
Set minvalueShouldBeGreater.
setAllowMove($a_allow_move)
Set allow move.
minvalueShouldBeGreater()
Get minvalueShouldBeGreater.
__construct($a_title="", $a_postvar="")
Constructor.
setSingleline($a_value)
Set singleline.
setMinValue($a_minvalue)
Set Minimum Value.
setQuestionObject($a_value)
Set question object.
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 prepareTextareaOutput(string $txt_output, bool $prepare_for_latex_output=false, bool $omitNl2BrWhenTextArea=false)
Prepares a string for a text area output where latex code may be in it If the text is HTML-free,...
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.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This is how a factory for glyphs looks like.
Definition: Factory.php:27
An entity that renders components to a string output.
Definition: Renderer.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26