ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMultipleTextsInputGUI.php
Go to the documentation of this file.
1 <?php
2 
20 use ILIAS\UI\Component\Symbol\Glyph\Factory as GlyphFactory;
21 
26 {
30  protected $editElementOccuranceEnabled = false;
31 
35  protected $editElementOrderEnabled = false;
36 
37  protected GlyphFactory $glyph_factory;
38  protected Renderer $renderer;
40 
47  public function __construct($a_title = "", $a_postvar = "")
48  {
49  parent::__construct($a_title, $a_postvar);
50 
51  global $DIC;
52  $this->lng = $DIC['lng'];
53  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
54  $this->renderer = $DIC['ui.renderer'];
55  $this->tpl = $DIC['tpl'];
56 
57  $this->validationRegexp = "";
58  }
59 
63  public function isEditElementOccuranceEnabled(): bool
64  {
66  }
67 
72  {
73  $this->editElementOccuranceEnabled = $editElementOccuranceEnabled;
74  }
75 
79  public function isEditElementOrderEnabled(): bool
80  {
82  }
83 
88  {
89  $this->editElementOrderEnabled = $editElementOrderEnabled;
90  }
91 
97  public function onCheckInput(): bool
98  {
99  $lng = $this->lng;
100 
101  $submittedElements = $this->getInput();
102 
103  if ($submittedElements === [] && $this->getRequired()) {
104  $this->setAlert($lng->txt("msg_input_is_required"));
105  return false;
106  }
107 
108  foreach ($submittedElements as $submittedValue) {
109  $submittedContentText = $this->fetchContentTextFromValue($submittedValue);
110 
111  if ($this->getRequired() && trim((string) $submittedContentText) === "") {
112  $this->setAlert($lng->txt('msg_input_is_required'));
113  return false;
114  }
115 
116  if ($this->getValidationRegexp() !== '') {
117  if (!preg_match($this->getValidationRegexp(), (string) $submittedContentText)) {
118  $this->setAlert($lng->txt('msg_wrong_format'));
119  return false;
120  }
121  }
122  }
123 
124  return $this->checkSubItemsInput();
125  }
126 
131  public function render(string $a_mode = ""): string
132  {
133  $tpl = new ilTemplate("tpl.prop_multi_text_inp.html", true, true, "Modules/TestQuestionPool");
134  $i = 0;
135  foreach ($this->getIdentifiedMultiValues() as $identifier => $value) {
136  if ($value !== null) {
137  $tpl->setCurrentBlock("prop_text_propval");
139  $tpl->parseCurrentBlock();
140  }
141  if ($this->isEditElementOrderEnabled()) {
142  $tpl->setCurrentBlock("move");
143  $tpl->setVariable("ID_UP", $this->getMultiValuePosIndexedSubFieldId($identifier, 'up', $i));
144  $tpl->setVariable("ID_DOWN", $this->getMultiValuePosIndexedSubFieldId($identifier, 'down', $i));
145  $tpl->setVariable("ID", $this->getMultiValuePosIndexedFieldId($identifier, $i));
146  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
147  $this->glyph_factory->up()->withAction('#')
148  ));
149  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
150  $this->glyph_factory->down()->withAction('#')
151  ));
152  $tpl->parseCurrentBlock();
153  }
154  $tpl->setCurrentBlock("row");
155  $tpl->setVariable("POST_VAR", $this->getMultiValuePostVarPosIndexed($identifier, $i));
156  $tpl->setVariable("ID", $this->getMultiValuePosIndexedFieldId($identifier, $i));
157  $tpl->setVariable("SIZE", $this->getSize());
158  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
159 
160  if ($this->getDisabled()) {
161  $tpl->setVariable(
162  "DISABLED",
163  " disabled=\"disabled\""
164  );
165  } elseif ($this->isEditElementOccuranceEnabled()) {
166  $tpl->setVariable("ID_ADD", $this->getMultiValuePosIndexedSubFieldId($identifier, 'add', $i));
167  $tpl->setVariable("ID_REMOVE", $this->getMultiValuePosIndexedSubFieldId($identifier, 'remove', $i));
168  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
169  $this->glyph_factory->add()->withAction('#')
170  ));
171  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
172  $this->glyph_factory->remove()->withAction('#')
173  ));
174  }
175 
176  $tpl->parseCurrentBlock();
177  $i++;
178  }
179  $tpl->setVariable("ELEMENT_ID", $this->getFieldId());
180 
181  if (!$this->getDisabled()) {
182  $config = '{'
183  . '"fieldContainerSelector": ".ilWzdContainerText", '
184  . '"reindexingRequiredElementsSelectors": ["input:text", "button"], '
185  . '"handleRowCleanUpCallback": function(rowElem) {$(rowElem).find("input:text").val("")}'
186  . '}';
187  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
188  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/identifiedwizardinput.js");
189  $this->tpl->addOnLoadCode("$.extend({}, AnswerWizardInput, IdentifiedWizardInput).init({$config});");
190  }
191 
192  return $tpl->get();
193  }
194 
199  protected function valueHasContentText($value): bool
200  {
201  if ($value === null || is_array($value) || is_object($value)) {
202  return false;
203  }
204 
205  return (bool) strlen($value);
206  }
207 
212  protected function fetchContentTextFromValue($value)
213  {
214  if ($this->valueHasContentText($value)) {
215  return $value;
216  }
217 
218  return null;
219  }
220 }
An entity that renders components to a string output.
Definition: Renderer.php:30
getMultiValuePosIndexedSubFieldId($identifier, $subFieldIndex, $positionIndex)
__construct($a_title="", $a_postvar="")
Constructor.
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...
onCheckInput()
Check input, strip slashes etc.
This is how a factory for glyphs looks like.
Definition: Factory.php:26
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVariable(string $variable, $value='')
Sets the given variable to the given value.
static prepareFormOutput($a_str, bool $a_strip=false)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
setEditElementOrderEnabled($editElementOrderEnabled)
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setEditElementOccuranceEnabled($editElementOccuranceEnabled)