ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
CachedConfigurationRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\UUID\Factory as UUIDFactory;
24
26{
27 private const string USER_FIELD_CONFIGURATION_TABLE = 'usr_field_config';
28 private const string UDF_DEFINITIONS_TABLE = 'udf_definition';
33
37 private array $fields_data = [];
38
43 public function __construct(
44 private readonly \ilDBInterface $db,
45 private readonly UUIDFactory $uuid_factory,
46 private readonly array $available_custom_field_types,
47 private readonly array $available_standard_profile_fields
48 ) {
49 $this->available_profile_fields = $this->generateAvailableProfielFields();
50 }
51
52 public function hasMigrationBeenRun(): bool
53 {
54 return $this->db->fetchObject(
55 $this->db->query(
56 'SELECT COUNT(field_id) cnt FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
57 )
58 )?->cnt > 1;
59 }
60
61 public function get(): array
62 {
63 if ($this->fields_data === []) {
64 $this->retrieveAllFieldData();
65 }
66
67 return array_reduce(
68 $this->available_profile_fields,
69 function (array $c, FieldDefinition $v): array {
70 $c[] = $this->buildFieldFromDefinition($v);
71 return $c;
72 },
73 []
74 );
75 }
76
77 public function getByIdentifier(string $identifier): ?Field
78 {
79 foreach ($this->available_profile_fields as $definition) {
80 if ($definition->getIdentifier() === $identifier) {
81 return $this->buildFieldFromDefinition($definition);
82 }
83 }
84 return null;
85 }
86
87 public function getByClass(string $class): ?Field
88 {
89 foreach ($this->available_profile_fields as $definition) {
90 if ($definition::class === $class) {
91 return $this->buildFieldFromDefinition($definition);
92 }
93 }
94 return null;
95 }
96
97 public function storeConfiguration(Field $field): void
98 {
99 $this->db->replace(
100 self::USER_FIELD_CONFIGURATION_TABLE,
101 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
102 [
103 'visible_in_registration' => [
105 $field->isVisibleInRegistration() ? 1 : 0
106 ],
107 'visible_to_user' => [
109 $field->isVisibleToUser() ? 1 : 0
110 ],
111 'visible_in_lua' => [
113 $field->isVisibleInLocalUserAdministration() ? 1 : 0
114 ],
115 'visible_in_crss' => [
117 $field->isVisibleInCourses() ? 1 : 0
118 ],
119 'visible_in_grps' => [
121 $field->isVisibleInGroups() ? 1 : 0
122 ],
123 'visible_in_prgs' => [
125 $field->isVisibleInStudyProgrammes() ? 1 : 0
126 ],
127 'changeable_by_user' => [
129 $field->isChangeableByUser() ? 1 : 0
130 ],
131 'changeable_in_lua' => [
134 ],
135 'required' => [
137 $field->isRequired() ? 1 : 0
138 ],
139 'export' => [
141 $field->export() ? 1 : 0
142 ],
143 'searchable' => [
145 $field->isSearchable() ? 1 : 0
146 ],
147 'available_in_certs' => [
149 $field->isAvailableInCertificates() ? 1 : 0
150 ]
151 ]
152 );
153
154 if ($field->isCustom()) {
155 $this->db->replace(
156 self::UDF_DEFINITIONS_TABLE,
157 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
158 $field->getDefinition()->toStorage()
159 );
160 }
161
162 $this->available_profile_fields = $this->generateAvailableProfielFields();
163 }
164
165 public function getCustomFieldTypes(): array
166 {
167 return array_map(
168 static fn(string $v): Custom\Type => new $v(),
169 $this->available_custom_field_types
170 );
171 }
172
174 {
175 return $this->buildFieldFromDefinition(
176 new Custom\Custom(
177 $this->uuid_factory->uuid4()
178 )
179 );
180 }
181
182 public function deleteCustomField(Field $field): void
183 {
184 if (!$field->getDefinition() instanceof Custom\Custom) {
185 return;
186 }
187 $this->db->manipulate(
188 'DELETE FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
189 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
190 );
191 $this->db->manipulate(
192 'DELETE FROM ' . self::UDF_DEFINITIONS_TABLE
193 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
194 );
195 $this->available_profile_fields = $this->generateAvailableProfielFields();
196 }
197
203 array $available_custom_field_types
204 ): array {
205 $query_result = $this->db->query(
206 'SELECT * FROM ' . self::UDF_DEFINITIONS_TABLE
207 );
208
209 $custom_field_definitions = [];
210 while (($field = $this->db->fetchObject($query_result)) !== null) {
211 $field_type = array_search($field->field_type, $available_custom_field_types);
212 if ($field_type === false) {
213 continue;
214 }
215
216 $custom_field_definitions[] = new Custom\Custom(
217 $this->uuid_factory->fromString($field->field_id),
218 new $available_custom_field_types[$field_type](),
219 $field->field_name,
220 AvailableSections::tryFrom($field->section) ?? AvailableSections::Other,
221 $field->field_values
222 );
223 }
224 return $custom_field_definitions;
225 }
226
227 private function retrieveAllFieldData(): void
228 {
229 $query = $this->db->query(
230 'SELECT * FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
231 );
232
233 while (($row = $this->db->fetchObject($query)) !== null) {
234 $this->fields_data[$row->field_id] = $row;
235 }
236 }
237
238 private function buildFieldFromDefinition(
239 FieldDefinition $definition
240 ): Field {
241 $values_from_database = $this->fields_data[$definition->getIdentifier()]
242 ?? $this->db->fetchObject(
243 $this->db->query(
244 'SELECT * FROM ' . self::USER_FIELD_CONFIGURATION_TABLE . " WHERE field_id ='{$definition->getIdentifier()}'"
245 )
246 );
247
248 if ($values_from_database === null) {
249 return new Field(
250 $definition
251 );
252 }
253
254 return new Field(
255 $definition,
256 $values_from_database->visible_in_registration === 1,
257 $values_from_database->visible_to_user === 1,
258 $values_from_database->visible_in_lua === 1,
259 $values_from_database->visible_in_crss === 1,
260 $values_from_database->visible_in_grps === 1,
261 $values_from_database->visible_in_prgs === 1,
262 $values_from_database->changeable_by_user === 1,
263 $values_from_database->changeable_in_lua === 1,
264 $values_from_database->required === 1,
265 $values_from_database->export === 1,
266 $values_from_database->searchable === 1,
267 $values_from_database->available_in_certs === 1
268 );
269 }
270
271 private function generateAvailableProfielFields(): array
272 {
273 return array_merge(
274 $this->available_standard_profile_fields,
275 $this->buildCustomFieldDefinitions($this->available_custom_field_types)
276 );
277 }
278}
__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...