ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilMatchingPairWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
22use ILIAS\UI\Component\Button\Factory as ButtonFactory;
24
33{
34 protected $pairs = [];
35 protected $allowMove = false;
36 protected $terms = [];
37 protected $definitions = [];
38
40 protected GlyphFactory $glyph_factory;
41 protected ButtonFactory $button_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->button_factory = $DIC->ui()->factory()->button();
59 $this->renderer = $DIC->ui()->renderer();
60 }
61
62 public function setValue($a_value): void
63 {
64 $this->pairs = [];
65 $this->terms = [];
66 $this->definitions = [];
67
68 $to_int = $this->refinery->kindlyTo()->int();
69 $points = $this->forms_helper->transformPoints($a_value);
70 $terms = $this->forms_helper->transformArray($a_value, 'term', $to_int);
71 $definitions = $this->forms_helper->transformArray($a_value, 'definition', $to_int);
72
73 foreach ($terms as $index => $term) {
74 $this->pairs[] = new assAnswerMatchingPair(
75 new assAnswerMatchingTerm('', '', $term),
76 new assAnswerMatchingDefinition('', '', $definitions[$index] ?? 0),
77 $points[$index]
78 );
79 }
80
81 $term_ids = explode(',', $a_value['term_id']);
82 foreach ($term_ids as $id) {
83 $this->terms[] = new assAnswerMatchingTerm('', '', (int) $id);
84 }
85
86 $definition_ids = explode(',', $a_value['definition_id']);
87 foreach ($definition_ids as $id) {
88 $this->definitions[] = new assAnswerMatchingDefinition('', '', (int) $id);
89 }
90 }
91
97 public function setTerms($a_terms): void
98 {
99 $this->terms = $a_terms;
100 }
101
107 public function setDefinitions($a_definitions): void
108 {
109 $this->definitions = $a_definitions;
110 }
111
117 public function setPairs($a_pairs): void
118 {
119 $this->pairs = $a_pairs;
120 }
121
127 public function setAllowMove($a_allow_move): void
128 {
129 $this->allowMove = $a_allow_move;
130 }
131
137 public function getAllowMove(): bool
138 {
139 return $this->allowMove;
140 }
141
146 public function checkInput(): bool
147 {
148 $to_int = $this->refinery->kindlyTo()->int();
149 $data = $this->raw($this->getPostVar());
150
151 if (!is_array($data)) {
152 $this->setAlert($this->lng->txt('msg_input_is_required'));
153 return false;
154 }
155
156 // check points
157 $result_points = $this->forms_helper->checkPointsInputEnoughPositive($data, true);
158 if (!is_array($result_points)) {
159 $this->setAlert($this->lng->txt($result_points));
160 return false;
161 }
162
163 // check answers
164 $terms = $this->forms_helper->transformArray($data, 'term', $to_int);
165 $definitions = $this->forms_helper->transformArray($data, 'definition', $to_int);
166 foreach ([$terms, $definitions] as $value) {
167 if ($value < 1 && $this->getRequired()) {
168 $this->setAlert($this->lng->txt('msg_input_is_required'));
169 return false;
170 }
171 }
172
173 return $this->checkSubItemsInput();
174 }
175
180 public function insert(ilTemplate $a_tpl): void
181 {
182 global $DIC;
183 $lng = $DIC['lng'];
184 $global_tpl = $DIC['tpl'];
185 $global_tpl->addJavascript("assets/js/answerwizardinput.js");
186 $global_tpl->addJavascript("assets/js/matchingpairwizard.js");
187
188 $tpl = new ilTemplate("tpl.prop_matchingpairinput.html", true, true, "components/ILIAS/TestQuestionPool");
189 $i = 0;
190
191 foreach ($this->pairs as $pair) {
192 $counter = 1;
193 $tpl->setCurrentBlock("option_term");
194 $tpl->setVariable("TEXT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($lng->txt('please_select')));
195 $tpl->setVariable("VALUE_OPTION", 0);
196 $tpl->parseCurrentBlock();
197 foreach ($this->terms as $term) {
198 $tpl->setCurrentBlock("option_term");
199 $tpl->setVariable("VALUE_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($term->getIdentifier()));
200 $tpl->setVariable("TEXT_OPTION", $lng->txt('term') . " " . $counter);
201 if ($pair->getTerm()->getIdentifier() == $term->getIdentifier()) {
202 $tpl->setVariable('SELECTED_OPTION', ' selected="selected"');
203 }
204 $tpl->parseCurrentBlock();
205 $counter++;
206 }
207 $counter = 1;
208 $tpl->setCurrentBlock("option_definition");
209 $tpl->setVariable("TEXT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($lng->txt('please_select')));
210 $tpl->setVariable("VALUE_OPTION", 0);
211 $tpl->parseCurrentBlock();
212 foreach ($this->definitions as $definition) {
213 $tpl->setCurrentBlock("option_definition");
214 $tpl->setVariable("VALUE_OPTION", ilLegacyFormElementsUtil::prepareFormOutput($definition->getIdentifier()));
215 $tpl->setVariable("TEXT_OPTION", $lng->txt('definition') . " " . $counter);
216 if ($pair->getDefinition()->getIdentifier() == $definition->getIdentifier()) {
217 $tpl->setVariable('SELECTED_OPTION', ' selected="selected"');
218 }
219 $tpl->parseCurrentBlock();
220 $counter++;
221 }
222
223
224 $tpl->setCurrentBlock('points_value');
225 $tpl->setVariable('POINTS_VALUE', $pair->getPoints());
226 $tpl->parseCurrentBlock();
227
228 if ($this->getAllowMove()) {
229 $tpl->setCurrentBlock("move");
230 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
231 $tpl->setVariable("UP_BUTTON", $this->renderer->render(
232 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->up())
233 ));
234 $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
235 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->down())
236 ));
237 $tpl->parseCurrentBlock();
238 }
239
240 $tpl->setCurrentBlock("row");
241 $tpl->setVariable("ROW_NUMBER", $i);
242
243 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
244 $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
245 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->add())
246 ));
247 $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
248 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->remove())
249 ));
250
251 $tpl->setVariable("POST_VAR", $this->getPostVar());
252
253 $tpl->parseCurrentBlock();
254
255 $i++;
256 }
257
258 $tpl->setCurrentBlock('term_ids');
259 $ids = [];
260 foreach ($this->terms as $term) {
261 array_push($ids, $term->getIdentifier());
262 }
263 $tpl->setVariable("POST_VAR", $this->getPostVar());
264 $tpl->setVariable("TERM_IDS", join(",", $ids));
265 $tpl->parseCurrentBlock();
266
267 $tpl->setCurrentBlock('definition_ids');
268 $ids = [];
269 foreach ($this->definitions as $definition) {
270 array_push($ids, $definition->getIdentifier());
271 }
272 $tpl->setVariable("POST_VAR", $this->getPostVar());
273 $tpl->setVariable("DEFINITION_IDS", join(",", $ids));
274 $tpl->parseCurrentBlock();
275
276 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
277 $tpl->setVariable("TEXT_POINTS", $lng->txt('points'));
278 $tpl->setVariable("TEXT_DEFINITION", $lng->txt('definition'));
279 $tpl->setVariable("TEXT_TERM", $lng->txt('term'));
280 $tpl->setVariable("TEXT_ACTIONS", $lng->txt('actions'));
281
282 $a_tpl->setCurrentBlock("prop_generic");
283 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
284 $a_tpl->parseCurrentBlock();
285 }
286}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderer()
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Class for matching question definitions.
Class for matching question pairs.
Class for matching question terms.
ilGlobalTemplateInterface $global_tpl
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 prepareFormOutput($a_str, bool $a_strip=false)
This class represents a key value pair wizard property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.
setDefinitions($a_definitions)
Set definitions.
checkInput()
Check input, strip slashes etc.
insert(ilTemplate $a_tpl)
Insert property html.
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.
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
$counter