ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ConfigurationRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\UUID\Factory as UUIDFactory;
25
27{
28 private const string USER_FIELD_CONFIGURATION_TABLE = 'usr_field_config';
29 private const string UDF_DEFINITIONS_TABLE = 'udf_definition';
31
32 public function __construct(
33 private readonly \ilDBInterface $db,
34 private readonly UUIDFactory $uuid_factory,
35 private readonly array $available_custom_field_types,
36 private readonly array $available_standard_profile_fields
37 ) {
38 $this->available_profile_fields = $this->generateAvailableProfielFields();
39 }
40
41 public function hasMigrationBeenRun(): bool
42 {
43 return $this->db->fetchObject(
44 $this->db->query(
45 'SELECT COUNT(field_id) cnt FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
46 )
47 )?->cnt > 1;
48 }
49
50 public function get(): array
51 {
52 return array_reduce(
53 $this->available_profile_fields,
54 function (array $c, FieldDefinition $v): array {
55 $c[] = $this->buildFieldFromDefinition($v);
56 return $c;
57 },
58 []
59 );
60 }
61
62 public function getByIdentifier(string $identifier): ?Field
63 {
64 foreach ($this->available_profile_fields as $definition) {
65 if ($definition->getIdentifier() === $identifier) {
66 return $this->buildFieldFromDefinition($definition);
67 }
68 }
69 return null;
70 }
71
72 public function getByClass(string $class): ?Field
73 {
74 foreach ($this->available_profile_fields as $definition) {
75 if ($definition::class === $class) {
76 return $this->buildFieldFromDefinition($definition);
77 }
78 }
79 return null;
80 }
81
82 public function storeConfiguration(Field $field): void
83 {
84 $this->db->replace(
85 self::USER_FIELD_CONFIGURATION_TABLE,
86 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
87 [
88 'visible_in_registration' => [
90 $field->isVisibleInRegistration() ? 1 : 0
91 ],
92 'visible_to_user' => [
94 $field->isVisibleToUser() ? 1 : 0
95 ],
96 'visible_in_lua' => [
99 ],
100 'visible_in_crss' => [
102 $field->isVisibleInCourses() ? 1 : 0
103 ],
104 'visible_in_grps' => [
106 $field->isVisibleInGroups() ? 1 : 0
107 ],
108 'visible_in_prgs' => [
110 $field->isVisibleInStudyProgrammes() ? 1 : 0
111 ],
112 'changeable_by_user' => [
114 $field->isChangeableByUser() ? 1 : 0
115 ],
116 'changeable_in_lua' => [
119 ],
120 'required' => [
122 $field->isRequired() ? 1 : 0
123 ],
124 'export' => [
126 $field->export() ? 1 : 0
127 ],
128 'searchable' => [
130 $field->isSearchable() ? 1 : 0
131 ],
132 'available_in_certs' => [
134 $field->isAvailableInCertificates() ? 1 : 0
135 ]
136 ]
137 );
138
139 if ($field->isCustom()) {
140 $this->db->replace(
141 self::UDF_DEFINITIONS_TABLE,
142 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
143 $field->getDefinition()->toStorage()
144 );
145 }
146
147 $this->available_profile_fields = $this->generateAvailableProfielFields();
148 }
149
150 public function getCustomFieldTypes(): array
151 {
152 return array_map(
153 fn(string $v): Custom\Type => new $v(),
154 $this->available_custom_field_types
155 );
156 }
157
159 {
160 return $this->buildFieldFromDefinition(
161 new Custom\Custom(
162 $this->uuid_factory->uuid4()
163 )
164 );
165 }
166
167 public function deleteCustomField(Field $field): void
168 {
169 if (!$field->getDefinition() instanceof Custom\Custom) {
170 return;
171 }
172 $this->db->manipulate(
173 'DELETE FROM ' . DataRepository::USER_VALUES_TABLE
174 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
175 );
176 $this->db->manipulate(
177 'DELETE FROM ' . self::UDF_DEFINITIONS_TABLE
178 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
179 );
180 $this->available_profile_fields = $this->generateAvailableProfielFields();
181 }
182
184 array $available_custom_field_types
185 ): array {
186 $query_result = $this->db->query(
187 'SELECT * FROM ' . self::UDF_DEFINITIONS_TABLE
188 );
189
190 $custom_field_definitions = [];
191 while (($field = $this->db->fetchObject($query_result)) !== null) {
192 $field_type = array_search($field->field_type, $available_custom_field_types);
193 if ($field_type === null) {
194 continue;
195 }
196 $custom_field_definitions[] = new Custom\Custom(
197 $this->uuid_factory->fromString($field->field_id),
198 new $available_custom_field_types[$field_type](),
199 $field->field_name,
200 AvailableSections::tryFrom($field->section) ?? AvailableSections::Other,
201 $field->field_values
202 );
203 }
204 return $custom_field_definitions;
205 }
206
207 private function buildFieldFromDefinition(
208 FieldDefinition $definition
209 ): Field {
210 $values_from_database = $this->db->fetchObject(
211 $this->db->query(
212 'SELECT * FROM ' . self::USER_FIELD_CONFIGURATION_TABLE . " WHERE field_id ='{$definition->getIdentifier()}'"
213 )
214 );
215
216 if ($values_from_database === null) {
217 return new Field(
218 $definition
219 );
220 }
221
222 return new Field(
223 $definition,
224 $values_from_database->visible_in_registration === 1,
225 $values_from_database->visible_to_user === 1,
226 $values_from_database->visible_in_lua === 1,
227 $values_from_database->visible_in_crss === 1,
228 $values_from_database->visible_in_grps === 1,
229 $values_from_database->visible_in_prgs === 1,
230 $values_from_database->changeable_by_user === 1,
231 $values_from_database->changeable_in_lua === 1,
232 $values_from_database->required === 1,
233 $values_from_database->export === 1,
234 $values_from_database->searchable === 1,
235 $values_from_database->available_in_certs === 1
236 );
237 }
238
239 private function generateAvailableProfielFields(): array
240 {
241 return array_merge(
242 $this->available_standard_profile_fields,
243 $this->buildCustomFieldDefinitions($this->available_custom_field_types)
244 );
245 }
246}
buildCustomFieldDefinitions(array $available_custom_field_types)
__construct(private readonly \ilDBInterface $db, private readonly UUIDFactory $uuid_factory, private readonly array $available_custom_field_types, private readonly array $available_standard_profile_fields)
$c
Definition: deliver.php:25
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...