ILIAS  release_8 Revision v8.24
class.ilAddAnswerFormBuilder.php
Go to the documentation of this file.
1<?php
2
19use ILIAS\UI\Factory as UIFactory;
20use ILIAS\Refinery\Factory as RefineryFactory;
22use Psr\Http\Message\RequestInterface;
23
33{
34 protected UIFactory $ui_factory;
37 protected string $form_action;
38
39
40 public function __construct(ilTestCorrectionsGUI $parent_object, UIFactory $ui_factory, RefineryFactory $refinery, ilLanguage $language, ilCtrl $ctrl)
41 {
42 $this->ui_factory = $ui_factory;
43 $this->refinery = $refinery;
44 $this->language = $language;
45
46 $this->form_action = $ctrl->getFormAction(
47 $parent_object,
48 'addAnswer'
49 );
50 }
51
52 public function buildAddAnswerForm(array $data = []): StandardForm
53 {
54 $inputs = $this->buildInputs($data);
55
56 return $this->ui_factory->input()->container()->form()->standard($this->form_action, $inputs);
57 }
58
59 protected function buildInputs(array $data): array
60 {
61 $to_int_trafo = $this->refinery->kindlyTo()->int();
62 $fix_points_trafo = $this->refinery->custom()->transformation(
63 function ($v) {
64 $v = str_replace(',', '.', $v);
65
66 if (is_numeric($v)) {
67 return (float) $v;
68 }
69
70 return false;
71 }
72 );
73
74 $inputs = [];
75 $inputs['question_id'] = $this->ui_factory->input()->field()->hidden()
76 ->withAdditionalTransformation($to_int_trafo)
77 ->withValue((string) ($data['question_id'] ?? ''));
78 $inputs['question_index'] = $this->ui_factory->input()->field()->hidden()
79 ->withAdditionalTransformation($to_int_trafo)
80 ->withValue((string) ($data['question_index'] ?? ''));
81 $inputs['answer_value'] = $this->ui_factory->input()->field()->hidden($this->language->txt('answer'))
82 ->withValue(($data['answer'] ?? ''));
83 $inputs['answer'] = $this->ui_factory->input()->field()->text($this->language->txt('answer'))
84 ->withValue(($data['answer'] ?? ''))
85 ->withDisabled(true);
86 $inputs['points'] = $this->ui_factory->input()->field()->text($this->language->txt('points'))
87 ->withAdditionalTransformation($fix_points_trafo);
88 return $inputs;
89 }
90}
Builds data types.
Definition: Factory.php:21
__construct(ilTestCorrectionsGUI $parent_object, UIFactory $ui_factory, RefineryFactory $refinery, ilLanguage $language, ilCtrl $ctrl)
Class ilCtrl provides processing control methods.
getFormAction(object $a_gui_obj, string $a_fallback_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
language handling
This is how the factory for UI elements looks.
Definition: Factory.php:38
Refinery Factory $refinery
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59