ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilAdvancedMDFieldDefinitionFloat.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
7 
14 {
15  private const MIN_DECIMALS = 1;
16 
17  protected int $decimals;
18 
19  public function __construct(GenericData $generic_data, string $language = '')
20  {
21  $this->init();
22  parent::__construct($generic_data, $language);
23  }
24 
25  public function getType(): int
26  {
27  return self::TYPE_FLOAT;
28  }
29 
30  protected function init(): void
31  {
32  $this->setDecimals(2);
33  }
34 
35  public function isFilterSupported(): bool
36  {
37  return false;
38  }
39 
40  protected function initADTDefinition(): ilADTDefinition
41  {
42  $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Float");
43 
44  $def->setMin($this->getMin());
45  $def->setMax($this->getMax());
46  $def->setDecimals($this->getDecimals());
47  $def->setSuffix($this->getSuffixTranslations()[$this->language] ?? $this->getSuffix());
48  return $def;
49  }
50 
55  public function setDecimals($a_value)
56  {
57  $this->decimals = max(self::MIN_DECIMALS, abs((int) $a_value));
58  }
59 
64  public function getDecimals()
65  {
66  return $this->decimals;
67  }
68 
69 
70  //
71  // definition (NOT ADT-based)
72  //
73 
74  protected function importFieldDefinition(array $a_def): void
75  {
76  parent::importFieldDefinition($a_def);
77  $this->setDecimals($a_def["decimals"] ?? self::MIN_DECIMALS);
78  }
79 
80  protected function getFieldDefinition(): array
81  {
82  $def = parent::getFieldDefinition();
83  $def["decimals"] = $this->getDecimals();
84  return $def;
85  }
86 
87  public function getFieldDefinitionForTableGUI(string $content_language): array
88  {
89  global $DIC;
90 
91  $lng = $DIC['lng'];
92 
93  $res = parent::getFieldDefinitionForTableGUI($content_language);
94  $res[$lng->txt("md_adv_number_decimals")] = $this->getDecimals();
95  return $res;
96  }
97 
104  protected function addCustomFieldToDefinitionForm(
105  ilPropertyFormGUI $a_form,
106  bool $a_disabled = false,
107  string $language = ''
108  ): void {
109  global $DIC;
110 
111  $lng = $DIC['lng'];
112 
113  // #32
114  parent::addCustomFieldToDefinitionForm($a_form, $a_disabled, $language);
115 
116  $decimals = new ilNumberInputGUI($lng->txt("md_adv_number_decimals"), "dec");
117  $decimals->setRequired(true);
118  $decimals->setValue((string) $this->getDecimals());
119  $decimals->setSize(5);
120  $a_form->addItem($decimals);
121 
122  if ($a_disabled) {
123  $decimals->setDisabled(true);
124  }
125  }
126 
130  public function importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language = ''): void
131  {
132  parent::importCustomDefinitionFormPostValues($a_form, $language);
133  $this->setDecimals((int) $a_form->getInput("dec"));
134  }
135 
136 
137  //
138  // export/import
139  //
140 
141  protected function addPropertiesToXML(ilXmlWriter $a_writer): void
142  {
143  parent::addPropertiesToXML($a_writer);
144  $a_writer->xmlElement('FieldValue', array("id" => "decimals"), $this->getDecimals());
145  }
146 
147  public function importXMLProperty(string $a_key, string $a_value): void
148  {
149  if ($a_key == "decimals") {
150  $this->setDecimals($a_value != "" ? $a_value : null);
151  }
152 
153  parent::importXMLProperty($a_key, $a_value);
154  }
155 }
$res
Definition: ltiservices.php:69
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...
AMD field type float (based on integer)
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language='')
Import custom post values from definition form.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
global $DIC
Definition: shib_login.php:25
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, bool $a_disabled=false, string $language='')
Add input elements to definition form.
__construct(Container $dic, ilPlugin $plugin)
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
language()
description: > Example for rendring a language glyph.
Definition: language.php:25
__construct(GenericData $generic_data, string $language='')
ADT definition base class.