ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilTestQuestionNavigationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\UI\Implementation\Component\Signal as SignalImplementation;
23use ILIAS\UI\Factory as UIFactory;
24use ILIAS\UI\Renderer as UIRenderer;
26
34{
35 public const SHOW_DISABLED_COMMANDS = false;
36
37 public const CSS_CLASS_SUBMIT_BUTTONS = 'ilc_qsubmit_Submit';
38 private \ILIAS\DI\UIServices $ui;
39
40 private string $edit_solution_command = '';
41 private bool $question_worked_through = false;
42 private string $revert_changes_link_target = '';
44 private string $skip_question_link_target = '';
45 private string $instant_feedback_command = '';
46 private bool $answer_freezing_enabled = false;
47 private bool $force_instant_response_enabled = false;
48 private string $question_mark_link_target = '';
49 private bool $question_marked = false;
50 private bool $anything_rendered = false;
52 private bool $feedback_button_enabled = false;
53
54 public function __construct(
55 protected ilLanguage $lng,
56 protected UIFactory $ui_factory,
57 protected UIRenderer $ui_renderer
58 ) {
59 }
60
61 public function setEditSolutionCommand(string $edit_solution_command): void
62 {
63 $this->edit_solution_command = $edit_solution_command;
64 }
65
67 {
68 $this->question_worked_through = $question_worked_through;
69 }
70
72 {
73 $this->revert_changes_link_target = $revert_changes_link_target;
74 }
75
77 {
78 $this->discard_solution_button_enabled = $discard_solution_button_enabled;
79 }
80
82 {
83 $this->skip_question_link_target = $skip_question_link_target;
84 }
85
87 {
88 $this->instant_feedback_command = $instant_feedback_command;
89 }
90
92 {
93 $this->force_instant_response_enabled = $force_instant_response_enabled;
94 }
95
97 {
98 $this->answer_freezing_enabled = $answer_freezing_enabled;
99 }
100
102 {
103 $this->question_mark_link_target = $question_mark_link_target;
104 }
105
106 public function setQuestionMarked(bool $question_marked): void
107 {
108 $this->question_marked = $question_marked;
109 }
110
111 public function setAnythingRendered(): void
112 {
113 $this->anything_rendered = true;
114 }
115
116 public function setShowDiscardModalSignal(Signal $signal): void
117 {
118 $this->show_discard_modal_signal = $signal;
119 }
120
122 {
123 return $this->show_discard_modal_signal ?? new SignalImplementation('');
124 }
125
127 {
128 $this->feedback_button_enabled = $feedback_button_enabled;
129 }
130
131 public function isFeedbackButtonEnabled(): bool
132 {
134 }
135
136 public function getActionsHTML(): string
137 {
138 $tpl = $this->getTemplate('actions');
139 $actions = [];
140
141 if ($this->question_mark_link_target) {
142 $this->renderActionsIcon(
143 $tpl,
146 'ilTestMarkQuestionIcon'
147 );
149 $actions[] = $this->ui_factory->button()->shy(
151 ''
152 )->withAdditionalOnLoadCode(
153 static function (string $id) use ($target): string {
154 return "document.getElementById('$id').addEventListener('click', "
155 . '(e) => {'
156 . " il.TestPlayerQuestionEditControl.checkNavigation('{$target}', 'show', e);"
157 . '});';
158 }
159 );
160 }
161
162 if ($this->skip_question_link_target) {
163 $actions[] = $this->ui_factory->button()->shy(
164 $this->lng->txt('postpone_question'),
165 $this->skip_question_link_target
166 );
167 }
168
169 if ($actions !== []) {
170 $actions[] = $this->ui_factory->divider()->horizontal();
171 }
172
173 $actions[] = $this->ui_factory->button()->shy(
174 $this->lng->txt('tst_revert_changes'),
175 $this->revert_changes_link_target
176 )
177 ->withUnavailableAction(!$this->revert_changes_link_target)
178 ->withAdditionalOnLoadCode(
179 static fn(string $id): string => "document.getElementById('{$id}').classList.add('ilTestRevertChangesAction')"
180 );
181
182 $actions[] = $this->ui_factory->button()->shy($this->lng->txt('discard_answer'), '#')
183 ->withUnavailableAction(!$this->discard_solution_button_enabled)
184 ->withOnClick($this->getShowDiscardModalSignal());
185
186 $list = $this->ui_factory->dropdown()->standard($actions)->withLabel($this->lng->txt('actions'));
187 $tpl->setVariable('ACTION_MENU', $this->ui_renderer->render($list));
188
189 return $tpl->get();
190 }
191
192 public function getHTML(): string
193 {
194 $tpl = $this->getTemplate('toolbar');
195 if ($this->edit_solution_command) {
196 $this->renderSubmitButton(
197 $tpl,
198 $this->edit_solution_command,
200 );
201 }
202
203 if ($this->isFeedbackButtonEnabled()) {
205 $tpl,
206 $this->instant_feedback_command,
207 $this->getCheckButtonLabel(),
208 $this->force_instant_response_enabled
209 );
210 }
211
212 if ($this->anything_rendered) {
213 $this->parseNavigation($tpl);
214 }
215
216 return $tpl->get();
217 }
218
219 private function getEditSolutionButtonLabel(): string
220 {
221 if ($this->question_worked_through) {
222 return $this->lng->txt('edit_answer');
223 }
224
225 return $this->lng->txt('answer_question');
226 }
227
228 private function getCheckButtonLabel(): string
229 {
230 if ($this->answer_freezing_enabled) {
231 return $this->lng->txt('submit_and_check');
232 }
233
234 return $this->lng->txt('check');
235 }
236
237 private function getQuestionMarkActionLabel(): string
238 {
239 if ($this->question_marked) {
240 return $this->lng->txt('tst_remove_mark');
241 }
242
243 return $this->lng->txt('tst_question_mark');
244 }
245
246
247 private function getQuestionMarkIconLabel(): string
248 {
249 if ($this->question_marked) {
250 return $this->lng->txt('tst_question_marked');
251 }
252
253 return$this->lng->txt('tst_question_not_marked');
254 }
255
256 private function getQuestionMarkIconSource(): string
257 {
258 if ($this->question_marked) {
259 return ilUtil::getImagePath('object/marked.svg');
260 }
261
262 return ilUtil::getImagePath('object/marked_.svg');
263 }
264
265 private function getTemplate($a_purpose = 'toolbar'): ilTemplate
266 {
267 switch ($a_purpose) {
268 case 'toolbar':
269 return new ilTemplate(
270 'tpl.tst_question_navigation.html',
271 true,
272 true,
273 'components/ILIAS/Test'
274 );
275 default:
276 case 'actions':
277 return new ilTemplate(
278 'tpl.tst_question_actions.html',
279 true,
280 true,
281 'components/ILIAS/Test'
282 );
283 }
284 }
285
286 private function parseNavigation(ilTemplate $tpl): void
287 {
288 $tpl->setCurrentBlock('question_related_navigation');
289 $tpl->parseCurrentBlock();
290 }
291
292 private function parseButtonsBlock(ilTemplate $tpl): void
293 {
294 $tpl->setCurrentBlock('buttons');
295 $tpl->parseCurrentBlock();
296 }
297
298 private function renderButtonInstance(ilTemplate $tpl, Button $button): void
299 {
300 $tpl->setCurrentBlock('button_instance');
301 $tpl->setVariable('BUTTON_INSTANCE', $this->ui_renderer->render($button));
302 $tpl->parseCurrentBlock();
303
304 $this->parseButtonsBlock($tpl);
305 $this->setAnythingRendered();
306 }
307
308 private function renderSubmitButton(
309 ilTemplate $tpl,
310 string $command,
311 string $label
312 ): void {
313 $on_load_code = $this->getOnLoadCode($command);
315 $tpl,
316 $this->ui_factory->button()->standard($label, '')->withAdditionalOnLoadCode($on_load_code)
317 );
318 }
319
321 ilTemplate $tpl,
322 string $command,
323 string $label,
324 bool $is_primary
325 ): void {
326 $on_load_code = $this->getOnLoadCode($command);
327 if ($is_primary) {
328 $this->renderButtonInstance(
329 $tpl,
330 $this->ui_factory->button()->primary($label, '')->withAdditionalOnLoadCode($on_load_code)
331 );
332 return;
333 }
334
335 $this->renderButtonInstance(
336 $tpl,
337 $this->ui_factory->button()->standard($label, '')->withAdditionalOnLoadCode($on_load_code)
338 );
339 }
340
341 private function getOnLoadCode(string $command): Closure
342 {
343 return static function ($id) use ($command): string {
344 return "document.getElementById('$id').addEventListener('click', "
345 . '(e) => {'
346 . " e.target.setAttribute('name', 'cmd[$command]');"
347 . ' e.target.form.requestSubmit(e.target);'
348 . '});';
349 };
350 }
351
352 private function renderActionsIcon(
353 ilTemplate $tpl,
354 string $icon_src,
355 string $label,
356 string $css_class
357 ): void {
358 $tpl->setCurrentBlock('actions_icon');
359 $tpl->setVariable('ICON_SRC', $icon_src);
360 $tpl->setVariable('ICON_TEXT', $label);
361 $tpl->setVariable('ICON_CLASS', $css_class);
362 $tpl->parseCurrentBlock();
363 }
364}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
language handling
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setForceInstantResponseEnabled(bool $force_instant_response_enabled)
setInstantFeedbackCommand(string $instant_feedback_command)
setFeedbackButtonEnabled(bool $feedback_button_enabled)
renderButtonInstance(ilTemplate $tpl, Button $button)
renderSubmitButton(ilTemplate $tpl, string $command, string $label)
setRevertChangesLinkTarget(string $revert_changes_link_target)
__construct(protected ilLanguage $lng, protected UIFactory $ui_factory, protected UIRenderer $ui_renderer)
setQuestionWorkedThrough(bool $question_worked_through)
setQuestionMarkLinkTarget(string $question_mark_link_target)
setAnswerFreezingEnabled(bool $answer_freezing_enabled)
setEditSolutionCommand(string $edit_solution_command)
renderActionsIcon(ilTemplate $tpl, string $icon_src, string $label, string $css_class)
renderInstantFeedbackButton(ilTemplate $tpl, string $command, string $label, bool $is_primary)
setDiscardSolutionButtonEnabled(bool $discard_solution_button_enabled)
setSkipQuestionLinkTarget(string $skip_question_link_target)
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
This describes commonalities between standard and primary buttons.
Definition: Button.php:34
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
global $lng
Definition: privfeed.php:31