ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
VocabValueConditionFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
34 
36 {
39  protected VocabSlotHandler $vocab_slot_handler;
40 
41  public function __construct(
46  PathFactory $path_factory,
47  ElementHelperInterface $element_vocab_helper,
48  VocabSlotHandler $vocab_slot_handler
49  ) {
50  parent::__construct($ui_factory, $presenter, $constraint_dictionary, $types);
51  $this->path_factory = $path_factory;
52  $this->element_vocab_helper = $element_vocab_helper;
53  $this->vocab_slot_handler = $vocab_slot_handler;
54  }
55 
56  public function getConditionInput(
57  ElementInterface $element,
58  ElementInterface $context_element,
59  ElementInterface $conditional_element
60  ): FormInput {
61  $slot = $this->element_vocab_helper->slotForElement($element);
62  $unique_path_to_conditional_element = $this->path_factory->toElement($conditional_element, true);
63  $path_for_conditional_slot = $this->path_factory->toElement($conditional_element);
64  $path_for_condition = $this->path_factory->betweenElements($conditional_element, $element);
65 
66  $data = $this->getDataFromElementOrConstraint($element);
67  $conditional_data = $this->getDataFromElementOrConstraint($conditional_element);
68 
69  $groups = [];
70  foreach ($this->element_vocab_helper->vocabulariesForSlot($slot) as $vocab) {
71  $labels_by_value = $this->getLabelsByValueForVocabulary($vocab);
72  foreach ($vocab->values() as $value) {
73  $conditional_slot = $this->vocab_slot_handler->identiferFromPathAndCondition(
74  $path_for_conditional_slot,
75  $path_for_condition,
76  $value,
77  );
78 
79  $input = $this->getInputInCondition(
80  $conditional_element,
81  $context_element,
82  $conditional_slot
83  );
84 
85  if ($data === $value && isset($conditional_data)) {
86  $input = $input->withValue($conditional_data);
87  }
88 
89  $groups[$value] = $this->ui_factory->group(
90  [$unique_path_to_conditional_element->toString() => $input],
91  $labels_by_value[$value] ?? ''
92  );
93  }
94  }
95 
96  $radios = $this->ui_factory->switchableGroup(
97  $groups,
98  $this->getInputLabelFromElement($this->presenter, $element, $context_element)
99  );
100  if (isset($data)) {
101  $radios = $radios->withValue($data);
102  }
103  return $this->addConstraintsFromElement(
104  $this->constraint_dictionary,
105  $element,
106  $radios
107  );
108  }
109 
110  protected function getLabelsByValueForVocabulary(VocabularyInterface $vocabulary): array
111  {
112  $presentable_labels = $this->presenter->data()->vocabularyValues(
113  $vocabulary->slot(),
114  ...$vocabulary->values()
115  );
116  $labels_by_value = [];
117  foreach ($presentable_labels as $labelled_value) {
118  $labels_by_value[$labelled_value->value()] = $labelled_value->label();
119  }
120  return $labels_by_value;
121  }
122 
123  protected function getDataFromElementOrConstraint(ElementInterface $element): ?string
124  {
125  $data = null;
126  if ($element->getData()->type() !== Type::NULL) {
127  $data = $element->getData()->value();
128  }
129  return $this->getPresetValueFromConstraints($this->constraint_dictionary, $element) ?? $data;
130  }
131 }
getConditionInput(ElementInterface $element, ElementInterface $context_element, ElementInterface $conditional_element)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
$vocab
__construct(Container $dic, ilPlugin $plugin)
This describes inputs that can be used in forms.
Definition: FormInput.php:32
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, FactoryWithoutConditionTypesService $types, PathFactory $path_factory, ElementHelperInterface $element_vocab_helper, VocabSlotHandler $vocab_slot_handler)