ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMatchingPairWizardInputGUI.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 $pairs = [];
32  protected $allowMove = false;
33  protected $terms = [];
34  protected $definitions = [];
35 
36  protected GlyphFactory $glyph_factory;
37  protected Renderer $renderer;
38 
45  public function __construct($a_title = "", $a_postvar = "")
46  {
47  parent::__construct($a_title, $a_postvar);
48 
49  global $DIC;
50  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
51  $this->renderer = $DIC->ui()->renderer();
52  }
53 
54  public function setValue($a_value): void
55  {
56  $this->pairs = array();
57  $this->terms = array();
58  $this->definitions = array();
59  if (is_array($a_value)) {
60  if (isset($a_value['term']) && is_array($a_value['term'])) {
61  foreach ($a_value['term'] as $idx => $term) {
62  $this->pairs[] = new assAnswerMatchingPair(
63  new assAnswerMatchingTerm('', '', $term),
64  new assAnswerMatchingDefinition('', '', $a_value['definition'][$idx]),
65  (float) $a_value['points'][$idx]
66  );
67  }
68  }
69  $term_ids = explode(",", $a_value['term_id']);
70  foreach ($term_ids as $id) {
71  $this->terms[] = new assAnswerMatchingTerm('', '', $id);
72  }
73  $definition_ids = explode(",", $a_value['definition_id']);
74  foreach ($definition_ids as $id) {
75  $this->definitions[] = new assAnswerMatchingDefinition('', '', $id);
76  }
77  }
78  }
79 
85  public function setTerms($a_terms): void
86  {
87  $this->terms = $a_terms;
88  }
89 
95  public function setDefinitions($a_definitions): void
96  {
97  $this->definitions = $a_definitions;
98  }
99 
105  public function setPairs($a_pairs): void
106  {
107  $this->pairs = $a_pairs;
108  }
109 
115  public function setAllowMove($a_allow_move): void
116  {
117  $this->allowMove = $a_allow_move;
118  }
119 
125  public function getAllowMove(): bool
126  {
127  return $this->allowMove;
128  }
129 
134  public function checkInput(): bool
135  {
136  global $DIC;
137  $lng = $DIC['lng'];
138 
139  if (is_array($_POST[$this->getPostVar()])) {
140  $foundvalues = ilArrayUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
141  } else {
142  $foundvalues = $_POST[$this->getPostVar()];
143  }
144  if (is_array($foundvalues)) {
145  // check answers
146  if (isset($foundvalues['term']) && is_array($foundvalues['term'])) {
147  foreach ($foundvalues['term'] as $val) {
148  if ($this->getRequired() && $val < 1) {
149  $this->setAlert($lng->txt("msg_input_is_required"));
150  return false;
151  }
152  }
153  foreach ($foundvalues['definition'] as $val) {
154  if ($this->getRequired() && $val < 1) {
155  $this->setAlert($lng->txt("msg_input_is_required"));
156  return false;
157  }
158  }
159  $max = 0;
160  foreach ($foundvalues['points'] as $val) {
161  if ($this->getRequired() && (strlen($val)) === 0) {
162  $this->setAlert($lng->txt("msg_input_is_required"));
163  return false;
164  }
165  $val = str_replace(",", ".", $val);
166  if (!is_numeric($val)) {
167  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
168  return false;
169  }
170 
171  $val = (float) $val;
172  if ($val > 0) {
173  $max += $val;
174  }
175  }
176  if ($max <= 0) {
177  $this->setAlert($lng->txt("enter_enough_positive_points"));
178  return false;
179  }
180  } else {
181  if ($this->getRequired()) {
182  $this->setAlert($lng->txt("msg_input_is_required"));
183  return false;
184  }
185  }
186  } else {
187  if ($this->getRequired()) {
188  $this->setAlert($lng->txt("msg_input_is_required"));
189  return false;
190  }
191  }
192  return $this->checkSubItemsInput();
193  }
194 
199  public function insert(ilTemplate $a_tpl): void
200  {
201  global $DIC;
202  $lng = $DIC['lng'];
203  $global_tpl = $DIC['tpl'];
204  $global_tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
205  $global_tpl->addJavascript("./Modules/TestQuestionPool/templates/default/matchingpairwizard.js");
206 
207  $tpl = new ilTemplate("tpl.prop_matchingpairinput.html", true, true, "Modules/TestQuestionPool");
208  $i = 0;
209 
210  foreach ($this->pairs as $pair) {
211  $counter = 1;
212  $tpl->setCurrentBlock("option_term");
213  $tpl->setVariable("TEXT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($lng->txt('please_select')));
214  $tpl->setVariable("VALUE_OPTION", 0);
215  $tpl->parseCurrentBlock();
216  foreach ($this->terms as $term) {
217  $tpl->setCurrentBlock("option_term");
218  $tpl->setVariable("VALUE_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($term->getIdentifier()));
219  $tpl->setVariable("TEXT_OPTION", $lng->txt('term') . " " . $counter);
220  if ($pair->getTerm()->getIdentifier() == $term->getIdentifier()) {
221  $tpl->setVariable('SELECTED_OPTION', ' selected="selected"');
222  }
223  $tpl->parseCurrentBlock();
224  $counter++;
225  }
226  $counter = 1;
227  $tpl->setCurrentBlock("option_definition");
228  $tpl->setVariable("TEXT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($lng->txt('please_select')));
229  $tpl->setVariable("VALUE_OPTION", 0);
230  $tpl->parseCurrentBlock();
231  foreach ($this->definitions as $definition) {
232  $tpl->setCurrentBlock("option_definition");
233  $tpl->setVariable("VALUE_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($definition->getIdentifier()));
234  $tpl->setVariable("TEXT_OPTION", $lng->txt('definition') . " " . $counter);
235  if ($pair->getDefinition()->getIdentifier() == $definition->getIdentifier()) {
236  $tpl->setVariable('SELECTED_OPTION', ' selected="selected"');
237  }
238  $tpl->parseCurrentBlock();
239  $counter++;
240  }
241 
242 
243  $tpl->setCurrentBlock('points_value');
244  $tpl->setVariable('POINTS_VALUE', $pair->getPoints());
245  $tpl->parseCurrentBlock();
246 
247  if ($this->getAllowMove()) {
248  $tpl->setCurrentBlock("move");
249  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
250  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
251  $this->glyph_factory->up()->withAction('#')
252  ));
253  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
254  $this->glyph_factory->down()->withAction('#')
255  ));
256  $tpl->parseCurrentBlock();
257  }
258 
259  $tpl->setCurrentBlock("row");
260  $tpl->setVariable("ROW_NUMBER", $i);
261 
262  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
263  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
264  $this->glyph_factory->add()->withAction('#')
265  ));
266  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
267  $this->glyph_factory->remove()->withAction('#')
268  ));
269 
270  $tpl->setVariable("POST_VAR", $this->getPostVar());
271 
272  $tpl->parseCurrentBlock();
273 
274  $i++;
275  }
276 
277  $tpl->setCurrentBlock('term_ids');
278  $ids = array();
279  foreach ($this->terms as $term) {
280  array_push($ids, $term->getIdentifier());
281  }
282  $tpl->setVariable("POST_VAR", $this->getPostVar());
283  $tpl->setVariable("TERM_IDS", join(",", $ids));
284  $tpl->parseCurrentBlock();
285 
286  $tpl->setCurrentBlock('definition_ids');
287  $ids = array();
288  foreach ($this->definitions as $definition) {
289  array_push($ids, $definition->getIdentifier());
290  }
291  $tpl->setVariable("POST_VAR", $this->getPostVar());
292  $tpl->setVariable("DEFINITION_IDS", join(",", $ids));
293  $tpl->parseCurrentBlock();
294 
295  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
296  $tpl->setVariable("TEXT_POINTS", $lng->txt('points'));
297  $tpl->setVariable("TEXT_DEFINITION", $lng->txt('definition'));
298  $tpl->setVariable("TEXT_TERM", $lng->txt('term'));
299  $tpl->setVariable("TEXT_ACTIONS", $lng->txt('actions'));
300 
301  $a_tpl->setCurrentBlock("prop_generic");
302  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
303  $a_tpl->parseCurrentBlock();
304  }
305 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An entity that renders components to a string output.
Definition: Renderer.php:30
setDefinitions($a_definitions)
Set definitions.
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="")
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...
This is how a factory for glyphs looks like.
Definition: Factory.php:26
static prepareFormOutput($a_str, bool $a_strip=false)
checkInput()
Check input, strip slashes etc.
global $DIC
Definition: feed.php:28
insert(ilTemplate $a_tpl)
Insert property html.
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
ilGlobalTemplateInterface $global_tpl
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a key value pair wizard property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.