ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAnswerWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
22
31{
32 protected $values = [];
33 protected $allowMove = false;
34 protected $allowAddRemove = true;
35 protected $singleline = true;
36 protected $qstObject = null;
37 protected $minvalue = false;
38 protected $minvalueShouldBeGreater = false;
39
41 protected GlyphFactory $glyph_factory;
43
50 public function __construct($a_title = "", $a_postvar = "")
51 {
52 parent::__construct($a_title, $a_postvar);
53
54 global $DIC;
55
56 $this->forms_helper = new ilTestLegacyFormsHelper();
57 $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
58 $this->renderer = $DIC->ui()->renderer();
59
60 $this->setSize('25');
61 $this->validationRegexp = "";
62 }
63
64 public function setValue($a_value): void
65 {
66 $this->values = [];
67
68 $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
69 $images = $this->forms_helper->transformArray($a_value, 'imagename', $this->refinery->kindlyTo()->string());
70 $points = $this->forms_helper->transformPoints($a_value);
71
72 foreach ($answers as $index => $value) {
73 $answer = new ASS_AnswerBinaryStateImage(
74 $value,
75 $points[$index] ?? 0.0,
76 $index,
77 true,
78 null,
79 -1
80 );
81 if ($this->forms_helper->inArray($images, $index)) {
82 $answer->setImage($images[$index]);
83 }
84 $this->values[] = $answer;
85 }
86 }
87
93 public function setValues($a_values): void
94 {
95 $this->values = $a_values;
96 }
97
103 public function getValues(): array
104 {
105 return $this->values;
106 }
107
113 public function setSingleline($a_value): void
114 {
115 $this->singleline = $a_value;
116 }
117
123 public function getSingleline(): bool
124 {
125 return $this->singleline;
126 }
127
133 public function setQuestionObject($a_value): void
134 {
135 $this->qstObject = &$a_value;
136 }
137
143 public function getQuestionObject(): ?object
144 {
145 return $this->qstObject;
146 }
147
153 public function setAllowMove($a_allow_move): void
154 {
155 $this->allowMove = $a_allow_move;
156 }
157
163 public function getAllowMove(): bool
164 {
165 return $this->allowMove;
166 }
167
171 public function isAddRemoveAllowed(): bool
172 {
174 }
175
179 public function setAllowAddRemove($allowAddRemove): void
180 {
181 $this->allowAddRemove = $allowAddRemove;
182 }
183
189 public function setMinvalueShouldBeGreater($a_bool): void
190 {
191 $this->minvalueShouldBeGreater = $a_bool;
192 }
193
199 public function minvalueShouldBeGreater(): bool
200 {
202 }
208 public function setMinValue($a_minvalue): void
209 {
210 $this->minvalue = $a_minvalue;
211 }
212
218 public function getMinValue()
219 {
220 return $this->minvalue;
221 }
226 public function checkInput(): bool
227 {
228 $data = $this->raw($this->getPostVar());
229
230 if (!is_array($data)) {
231 $this->setAlert($this->lng->txt('msg_input_is_required'));
232 return false;
233 }
234
235 // check points
236 $points = $this->forms_helper->checkPointsInputEnoughPositive($data, true);
237 if (!is_array($points)) {
238 $this->setAlert($this->lng->txt($points));
239 return false;
240 }
241 foreach ($points as $value) {
242 if ($value !== 0.0 && $this->getMinValue() !== false) {
243 if (($this->minvalueShouldBeGreater() && $points <= $this->getMinValue()) ||
244 (!$this->minvalueShouldBeGreater() && $points < $this->getMinValue())) {
245 $this->setAlert($this->lng->txt('form_msg_value_too_low'));
246 return false;
247 }
248 }
249 }
250
251 // check answers
252 $answers = $this->forms_helper->transformArray($data, 'answer', $this->refinery->kindlyTo()->string());
253 foreach ($answers as $answer) {
254 if ($answer === '') {
255 $this->setAlert($this->lng->txt('msg_input_is_required'));
256 return false;
257 }
258 }
259
260 return $this->checkSubItemsInput();
261 }
262
263 public function insert(ilTemplate $a_tpl): void
264 {
265 $tpl = new ilTemplate($this->getTemplate(), true, true, "components/ILIAS/TestQuestionPool");
266 $i = 0;
267 foreach ($this->values as $value) {
268 if ($this->getSingleline()) {
269 if (is_object($value)) {
270 $tpl->setCurrentBlock("prop_text_propval");
271 $tpl->setVariable(
272 "PROPERTY_VALUE",
274 );
275 $tpl->parseCurrentBlock();
276 $tpl->setCurrentBlock("prop_points_propval");
277 $tpl->setVariable(
278 "PROPERTY_VALUE",
280 );
281 $tpl->parseCurrentBlock();
282 }
283 $tpl->setCurrentBlock('singleline');
284 $tpl->setVariable("SIZE", $this->getSize());
285 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
286 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
287 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
288 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
289 if ($this->getDisabled()) {
290 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
291 }
292 $tpl->parseCurrentBlock();
293 } elseif (!$this->getSingleline()) {
294 if (is_object($value)) {
295 $tpl->setCurrentBlock("prop_points_propval");
296 $tpl->setVariable(
297 "PROPERTY_VALUE",
299 );
300 $tpl->parseCurrentBlock();
301 }
302 $tpl->setCurrentBlock('multiline');
303 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareTextareaOutput($value->getAnswertext()));
304 $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
305 $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
306 $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
307 if ($this->getDisabled()) {
308 $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
309 }
310 $tpl->parseCurrentBlock();
311 }
312 if ($this->getAllowMove()) {
313 $tpl->setCurrentBlock("move");
314 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
315 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
316 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
317 $tpl->setVariable("UP_BUTTON", $this->renderer->render(
318 $this->glyph_factory->up()->withAction('#')
319 ));
320 $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
321 $this->glyph_factory->down()->withAction('#')
322 ));
323 $tpl->parseCurrentBlock();
324 }
325 $tpl->setCurrentBlock("row");
326 $tpl->setVariable("POST_VAR", $this->getPostVar());
327 $tpl->setVariable("ROW_NUMBER", $i);
328 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
329 $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
330 if ($this->isAddRemoveAllowed()) {
331 $tpl->setVariable("ADD_REMOVE_ID", $this->getPostVar() . "[$i]");
332 $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
333 $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
334 $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
335 $this->glyph_factory->add()->withAction('#')
336 ));
337 $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
338 $this->glyph_factory->remove()->withAction('#')
339 ));
340 }
341 if ($this->getDisabled()) {
342 $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
343 }
344 $tpl->parseCurrentBlock();
345 $i++;
346 }
347
348 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
349 $tpl->setVariable("ANSWER_TEXT", $this->getTextInputLabel($this->lng));
350 $tpl->setVariable("POINTS_TEXT", $this->getPointsInputLabel($this->lng));
351 $tpl->setVariable("COMMANDS_TEXT", $this->lng->txt('actions'));
352
353 $a_tpl->setCurrentBlock("prop_generic");
354 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
355 $a_tpl->parseCurrentBlock();
356
357 global $DIC;
358 $tpl = $DIC['tpl'];
359 $tpl->addJavascript("assets/js/answerwizardinput.js");
360 $tpl->addJavascript("assets/js/answerwizard.js");
361 }
362
367 protected function getTextInputLabel($lng)
368 {
369 return $lng->txt('answer_text');
370 }
371
376 protected function getPointsInputLabel($lng)
377 {
378 return $lng->txt('points');
379 }
380
384 protected function getTemplate(): string
385 {
386 return "tpl.prop_answerwizardinput.html";
387 }
388
389 protected function prepareFormOutput(float|string $input, bool $strip = false): string
390 {
391 $input_string = $this->refinery->kindlyTo()->string()->transform($input);
392 return str_replace(
393 ['{', '}', '\\'],
394 ['&#123;', '&#125;', '&#92;'],
395 htmlspecialchars(
396 $strip ? ilUtil::stripSlashes($input_string) : $input_string,
397 ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
398 null,
399 false
400 )
401 );
402 }
403}
renderer()
Class for answers with a binary state indicator.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
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