ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilAnswerWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 
20 use ILIAS\UI\Component\Symbol\Glyph\Factory as GlyphFactory;
21 
30 {
31  protected $values = array();
32  protected $allowMove = false;
33  protected $allowAddRemove = true;
34  protected $singleline = true;
35  protected $qstObject = null;
36  protected $minvalue = false;
37  protected $minvalueShouldBeGreater = false;
38 
39  protected GlyphFactory $glyph_factory;
40  protected Renderer $renderer;
41 
48  public function __construct($a_title = "", $a_postvar = "")
49  {
50  parent::__construct($a_title, $a_postvar);
51 
52  global $DIC;
53  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
54  $this->renderer = $DIC->ui()->renderer();
55 
56  $this->setSize('25');
57  $this->validationRegexp = "";
58  }
59 
60  public function setValue($a_value): void
61  {
62  $this->values = array();
63  if (is_array($a_value)) {
64  if (is_array($a_value['answer'])) {
65  foreach ($a_value['answer'] as $index => $value) {
66  $answer = new ASS_AnswerBinaryStateImage($value, $a_value['points'][$index], $index, true, null, -1);
67  if (isset($a_value['imagename'][$index])) {
68  $answer->setImage($a_value['imagename'][$index]);
69  }
70  array_push($this->values, $answer);
71  }
72  }
73  }
74  }
75 
81  public function setValues($a_values): void
82  {
83  $this->values = $a_values;
84  }
85 
91  public function getValues(): array
92  {
93  return $this->values;
94  }
95 
101  public function setSingleline($a_value): void
102  {
103  $this->singleline = $a_value;
104  }
105 
111  public function getSingleline(): bool
112  {
113  return $this->singleline;
114  }
115 
121  public function setQuestionObject($a_value): void
122  {
123  $this->qstObject = &$a_value;
124  }
125 
131  public function getQuestionObject(): ?object
132  {
133  return $this->qstObject;
134  }
135 
141  public function setAllowMove($a_allow_move): void
142  {
143  $this->allowMove = $a_allow_move;
144  }
145 
151  public function getAllowMove(): bool
152  {
153  return $this->allowMove;
154  }
155 
159  public function isAddRemoveAllowed(): bool
160  {
161  return $this->allowAddRemove;
162  }
163 
167  public function setAllowAddRemove($allowAddRemove): void
168  {
169  $this->allowAddRemove = $allowAddRemove;
170  }
171 
177  public function setMinvalueShouldBeGreater($a_bool): void
178  {
179  $this->minvalueShouldBeGreater = $a_bool;
180  }
181 
187  public function minvalueShouldBeGreater(): bool
188  {
190  }
196  public function setMinValue($a_minvalue): void
197  {
198  $this->minvalue = $a_minvalue;
199  }
200 
206  public function getMinValue()
207  {
208  return $this->minvalue;
209  }
214  public function checkInput(): bool
215  {
216  global $DIC;
217  $lng = $DIC['lng'];
218 
219  $foundvalues = $_POST[$this->getPostVar()];
220  if (is_array($foundvalues)) {
221  $foundvalues = ilArrayUtil::stripSlashesRecursive($foundvalues);
222  // check answers
223  if (is_array($foundvalues['answer'])) {
224  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
225  if ((strlen($answervalue)) == 0) {
226  $this->setAlert($lng->txt("msg_input_is_required"));
227  return false;
228  }
229  }
230  }
231  // check points
232  $max = 0;
233  if (is_array($foundvalues['points'])) {
234  foreach ($foundvalues['points'] as $points) {
235  if ($points > $max) {
236  $max = $points;
237  }
238  if (((strlen($points)) == 0) || (!is_numeric($points))) {
239  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
240  return false;
241  }
242  if ($this->minvalueShouldBeGreater()) {
243  if (trim($points) != "" &&
244  $this->getMinValue() !== false &&
245  $points <= $this->getMinValue()) {
246  $this->setAlert($lng->txt("form_msg_value_too_low"));
247 
248  return false;
249  }
250  } else {
251  if (trim($points) != "" &&
252  $this->getMinValue() !== false &&
253  $points < $this->getMinValue()) {
254  $this->setAlert($lng->txt("form_msg_value_too_low"));
255 
256  return false;
257  }
258  }
259  }
260  }
261  if ($max == 0) {
262  $this->setAlert($lng->txt("enter_enough_positive_points"));
263  return false;
264  }
265  } else {
266  $this->setAlert($lng->txt("msg_input_is_required"));
267  return false;
268  }
269 
270  return $this->checkSubItemsInput();
271  }
272 
273  public function insert(ilTemplate $a_tpl): void
274  {
275  global $DIC;
276  $lng = $DIC['lng'];
277 
278  $tpl = new ilTemplate($this->getTemplate(), true, true, "Modules/TestQuestionPool");
279  $i = 0;
280  foreach ($this->values as $value) {
281  if ($this->getSingleline()) {
282  if (is_object($value)) {
283  $tpl->setCurrentBlock("prop_text_propval");
284  $tpl->setVariable(
285  "PROPERTY_VALUE",
286  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
287  );
288  $tpl->parseCurrentBlock();
289  $tpl->setCurrentBlock("prop_points_propval");
290  $tpl->setVariable(
291  "PROPERTY_VALUE",
293  );
294  $tpl->parseCurrentBlock();
295  }
296  $tpl->setCurrentBlock('singleline');
297  $tpl->setVariable("SIZE", $this->getSize());
298  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
299  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
300  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
301  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
302  if ($this->getDisabled()) {
303  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
304  }
305  $tpl->parseCurrentBlock();
306  } elseif (!$this->getSingleline()) {
307  if (is_object($value)) {
308  $tpl->setCurrentBlock("prop_points_propval");
309  $tpl->setVariable(
310  "PROPERTY_VALUE",
312  );
313  $tpl->parseCurrentBlock();
314  }
315  $tpl->setCurrentBlock('multiline');
316  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareTextareaOutput($value->getAnswertext()));
317  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
318  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
319  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
320  if ($this->getDisabled()) {
321  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
322  }
323  $tpl->parseCurrentBlock();
324  }
325  if ($this->getAllowMove()) {
326  $tpl->setCurrentBlock("move");
327  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
328  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
329  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
330  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
331  $this->glyph_factory->up()->withAction('#')
332  ));
333  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
334  $this->glyph_factory->down()->withAction('#')
335  ));
336  $tpl->parseCurrentBlock();
337  }
338  $tpl->setCurrentBlock("row");
339  $tpl->setVariable("POST_VAR", $this->getPostVar());
340  $tpl->setVariable("ROW_NUMBER", $i);
341  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
342  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
343  if ($this->isAddRemoveAllowed()) {
344  $tpl->setVariable("ADD_REMOVE_ID", $this->getPostVar() . "[$i]");
345  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
346  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
347  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
348  $this->glyph_factory->add()->withAction('#')
349  ));
350  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
351  $this->glyph_factory->remove()->withAction('#')
352  ));
353  }
354  if ($this->getDisabled()) {
355  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
356  }
357  $tpl->parseCurrentBlock();
358  $i++;
359  }
360 
361  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
362  $tpl->setVariable("ANSWER_TEXT", $this->getTextInputLabel($lng));
363  $tpl->setVariable("POINTS_TEXT", $this->getPointsInputLabel($lng));
364  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
365 
366  $a_tpl->setCurrentBlock("prop_generic");
367  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
368  $a_tpl->parseCurrentBlock();
369 
370  global $DIC;
371  $tpl = $DIC['tpl'];
372  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
373  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizard.js");
374  }
375 
380  protected function getTextInputLabel($lng)
381  {
382  return $lng->txt('answer_text');
383  }
384 
389  protected function getPointsInputLabel($lng)
390  {
391  return $lng->txt('points');
392  }
393 
397  protected function getTemplate(): string
398  {
399  return "tpl.prop_answerwizardinput.html";
400  }
401 
402  protected function prepareFormOutput(float|string $input, bool $strip = false): string
403  {
404  $input = $this->refinery->kindlyTo()->string()->transform($input);
405  $input = $strip ? ilUtil::stripSlashes($input) : $input;
406  $input = htmlspecialchars($input, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, null, false);
407  return str_replace(['{', '}', '\\'], ['&#123;', '&#125;', '&#92;'], $input);
408  }
409 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
An entity that renders components to a string output.
Definition: Renderer.php:30
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
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...
Class for answers with a binary state indicator.
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:26
static prepareFormOutput($a_str, bool $a_strip=false)
setMinValue($a_minvalue)
Set Minimum Value.
setMinvalueShouldBeGreater($a_bool)
Set minvalueShouldBeGreater.
This class represents a single choice wizard property in a property form.
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
setAllowMove($a_allow_move)
Set allow move.
minvalueShouldBeGreater()
Get minvalueShouldBeGreater.
__construct($a_title="", $a_postvar="")
Constructor.
prepareFormOutput(float|string $input, bool $strip=false)
getQuestionObject()
Get question object.
checkInput()
Check input, strip slashes etc.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setSingleline($a_value)
Set singleline.
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...
setQuestionObject($a_value)
Set question object.