ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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';
30
35 private array $fields_data = [];
36
37 public function __construct(
38 private readonly \ilDBInterface $db,
39 private readonly UUIDFactory $uuid_factory,
40 private readonly array $available_custom_field_types,
41 private readonly array $available_standard_profile_fields
42 ) {
43 $this->available_profile_fields = $this->generateAvailableProfielFields();
44 }
45
46 public function hasMigrationBeenRun(): bool
47 {
48 return $this->db->fetchObject(
49 $this->db->query(
50 'SELECT COUNT(field_id) cnt FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
51 )
52 )?->cnt > 1;
53 }
54
55 public function get(): array
56 {
57 if ($this->fields_data === []) {
58 $this->retrieveAllFieldData();
59 }
60
61 return array_reduce(
62 $this->available_profile_fields,
63 function (array $c, FieldDefinition $v): array {
64 $c[] = $this->buildFieldFromDefinition($v);
65 return $c;
66 },
67 []
68 );
69 }
70
71 public function getByIdentifier(string $identifier): ?Field
72 {
73 foreach ($this->available_profile_fields as $definition) {
74 if ($definition->getIdentifier() === $identifier) {
75 return $this->buildFieldFromDefinition($definition);
76 }
77 }
78 return null;
79 }
80
81 public function getByClass(string $class): ?Field
82 {
83 foreach ($this->available_profile_fields as $definition) {
84 if ($definition::class === $class) {
85 return $this->buildFieldFromDefinition($definition);
86 }
87 }
88 return null;
89 }
90
91 public function storeConfiguration(Field $field): void
92 {
93 $this->db->replace(
94 self::USER_FIELD_CONFIGURATION_TABLE,
95 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
96 [
97 'visible_in_registration' => [
99 $field->isVisibleInRegistration() ? 1 : 0
100 ],
101 'visible_to_user' => [
103 $field->isVisibleToUser() ? 1 : 0
104 ],
105 'visible_in_lua' => [
107 $field->isVisibleInLocalUserAdministration() ? 1 : 0
108 ],
109 'visible_in_crss' => [
111 $field->isVisibleInCourses() ? 1 : 0
112 ],
113 'visible_in_grps' => [
115 $field->isVisibleInGroups() ? 1 : 0
116 ],
117 'visible_in_prgs' => [
119 $field->isVisibleInStudyProgrammes() ? 1 : 0
120 ],
121 'changeable_by_user' => [
123 $field->isChangeableByUser() ? 1 : 0
124 ],
125 'changeable_in_lua' => [
128 ],
129 'required' => [
131 $field->isRequired() ? 1 : 0
132 ],
133 'export' => [
135 $field->export() ? 1 : 0
136 ],
137 'searchable' => [
139 $field->isSearchable() ? 1 : 0
140 ],
141 'available_in_certs' => [
143 $field->isAvailableInCertificates() ? 1 : 0
144 ]
145 ]
146 );
147
148 if ($field->isCustom()) {
149 $this->db->replace(
150 self::UDF_DEFINITIONS_TABLE,
151 ['field_id' => [\ilDBConstants::T_TEXT, $field->getIdentifier()]],
152 $field->getDefinition()->toStorage()
153 );
154 }
155
156 $this->available_profile_fields = $this->generateAvailableProfielFields();
157 }
158
159 public function getCustomFieldTypes(): array
160 {
161 return array_map(
162 fn(string $v): Custom\Type => new $v(),
163 $this->available_custom_field_types
164 );
165 }
166
168 {
169 return $this->buildFieldFromDefinition(
170 new Custom\Custom(
171 $this->uuid_factory->uuid4()
172 )
173 );
174 }
175
176 public function deleteCustomField(Field $field): void
177 {
178 if (!$field->getDefinition() instanceof Custom\Custom) {
179 return;
180 }
181 $this->db->manipulate(
182 'DELETE FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
183 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
184 );
185 $this->db->manipulate(
186 'DELETE FROM ' . self::UDF_DEFINITIONS_TABLE
187 . " WHERE field_id={$this->db->quote($field->getIdentifier(), \ilDBConstants::T_TEXT)}"
188 );
189 $this->available_profile_fields = $this->generateAvailableProfielFields();
190 }
191
193 array $available_custom_field_types
194 ): array {
195 $query_result = $this->db->query(
196 'SELECT * FROM ' . self::UDF_DEFINITIONS_TABLE
197 );
198
199 $custom_field_definitions = [];
200 while (($field = $this->db->fetchObject($query_result)) !== null) {
201 $field_type = array_search($field->field_type, $available_custom_field_types);
202 if ($field_type === null) {
203 continue;
204 }
205 $custom_field_definitions[] = new Custom\Custom(
206 $this->uuid_factory->fromString($field->field_id),
207 new $available_custom_field_types[$field_type](),
208 $field->field_name,
209 AvailableSections::tryFrom($field->section) ?? AvailableSections::Other,
210 $field->field_values
211 );
212 }
213 return $custom_field_definitions;
214 }
215
216 private function retrieveAllFieldData(): void
217 {
218 $query = $this->db->query(
219 'SELECT * FROM ' . self::USER_FIELD_CONFIGURATION_TABLE
220 );
221
222 while (($row = $this->db->fetchObject($query)) !== null) {
223 $this->fields_data[$row->field_id] = $row;
224 }
225 }
226
227 private function buildFieldFromDefinition(
228 FieldDefinition $definition
229 ): Field {
230 $values_from_database = $this->fields_data[$definition->getIdentifier()]
231 ?? $this->db->fetchObject(
232 $this->db->query(
233 'SELECT * FROM ' . self::USER_FIELD_CONFIGURATION_TABLE . " WHERE field_id ='{$definition->getIdentifier()}'"
234 )
235 );
236
237 if ($values_from_database === null) {
238 return new Field(
239 $definition
240 );
241 }
242
243 return new Field(
244 $definition,
245 $values_from_database->visible_in_registration === 1,
246 $values_from_database->visible_to_user === 1,
247 $values_from_database->visible_in_lua === 1,
248 $values_from_database->visible_in_crss === 1,
249 $values_from_database->visible_in_grps === 1,
250 $values_from_database->visible_in_prgs === 1,
251 $values_from_database->changeable_by_user === 1,
252 $values_from_database->changeable_in_lua === 1,
253 $values_from_database->required === 1,
254 $values_from_database->export === 1,
255 $values_from_database->searchable === 1,
256 $values_from_database->available_in_certs === 1
257 );
258 }
259
260 private function generateAvailableProfielFields(): array
261 {
262 return array_merge(
263 $this->available_standard_profile_fields,
264 $this->buildCustomFieldDefinitions($this->available_custom_field_types)
265 );
266 }
267}
__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...