ILIAS  release_8 Revision v8.24
class.ilUserDefinedFields.php
Go to the documentation of this file.
1<?php
2
19const UDF_TYPE_TEXT = 1;
22const UDF_NO_VALUES = 1;
24
30{
31 protected bool $field_certificate = false;
32 protected bool $field_group_export = false;
33 protected bool $field_course_export = false;
34 protected bool $field_export = false;
35 protected bool $field_searchable = false;
36 protected bool $field_required = false;
37 protected bool $field_changeable_lua = false;
38 protected bool $field_changeable = false;
39 protected bool $field_visib_lua = false;
40 protected array $field_values = []; // Missing array type.
41 protected int $field_type = 0;
42 protected string $field_name = "";
43 protected bool $field_visible = false;
44 public ?ilDBInterface $db = null;
48 public array $definitions = array();
50
51 private function __construct()
52 {
53 global $DIC;
54
55 $this->db = $DIC->database();
56 $this->__read();
57 }
58
59 public static function _getInstance(): self
60 {
61 static $udf = null;
62
63 if (!is_object($udf)) {
64 return $udf = new ilUserDefinedFields();
65 }
66 return $udf;
67 }
68
69 public function fetchFieldIdFromImportId(string $a_import_id): int
70 {
71 global $DIC;
72
73 $ilSetting = $DIC['ilSetting'];
74
75 if (!strlen($a_import_id)) {
76 return 0;
77 }
78 $parts = explode('_', $a_import_id);
79
80 if (($parts[0] ?? '') != 'il') {
81 return 0;
82 }
83 if (($parts[1] ?? '') != $ilSetting->get('inst_id', '0')) {
84 return 0;
85 }
86 if (($parts[2] ?? '') != 'udf') {
87 return 0;
88 }
89 if ($parts[3] ?? false) {
90 // Check if field exists
91 if (is_array(($this->definitions[$parts[3]] ?? false))) {
92 return $parts[3];
93 }
94 }
95 return 0;
96 }
97
98 public function fetchFieldIdFromName(string $a_name): int
99 {
100 foreach ($this->definitions as $definition) {
101 if ($definition['field_name'] == $a_name) {
102 return $definition['field_id'];
103 }
104 }
105 return 0;
106 }
107
108 public function getDefinitions(): array // Missing array type.
109 {
110 return $this->definitions ?: array();
111 }
112
113 public function getDefinition(int $a_id): array // Missing array type.
114 {
115 return $this->definitions[$a_id] ?? array();
116 }
117
118 public function getVisibleDefinitions(): array // Missing array type.
119 {
120 $visible_definition = [];
121 foreach ($this->definitions as $id => $definition) {
122 if ($definition['visible']) {
123 $visible_definition[$id] = $definition;
124 }
125 }
126 return $visible_definition;
127 }
128
129 public function getLocalUserAdministrationDefinitions(): array // Missing array type.
130 {
131 $visible_definition = [];
132 foreach ($this->definitions as $id => $definition) {
133 if ($definition['visib_lua']) {
134 $visible_definition[$id] = $definition;
135 }
136 }
137 return $visible_definition;
138 }
139
140 public function getChangeableLocalUserAdministrationDefinitions(): array // Missing array type.
141 {
142 $visible_definition = [];
143 foreach ($this->definitions as $id => $definition) {
144 if ($definition['changeable_lua']) {
145 $visible_definition[$id] = $definition;
146 }
147 }
148 return $visible_definition;
149 }
150
151 public function getRegistrationDefinitions(): array // Missing array type.
152 {
153 $visible_definition = [];
154 foreach ($this->definitions as $id => $definition) {
155 if ($definition['visib_reg']) {
156 $visible_definition[$id] = $definition;
157 }
158 }
159 return $visible_definition;
160 }
161
162 public function getSearchableDefinitions(): array // Missing array type.
163 {
164 $searchable_definition = [];
165 foreach ($this->definitions as $id => $definition) {
166 if ($definition['searchable']) {
167 $searchable_definition[$id] = $definition;
168 }
169 }
170 return $searchable_definition;
171 }
172
173 public function getRequiredDefinitions(): array // Missing array type.
174 {
175 $required_definition = [];
176 foreach ($this->definitions as $id => $definition) {
177 if ($definition['required']) {
178 $required_definition[$id] = $definition;
179 }
180 }
181 return $required_definition;
182 }
183
184 public function getCourseExportableFields(): array // Missing array type.
185 {
186 $cexp_definition = [];
187 foreach ($this->definitions as $id => $definition) {
188 if ($definition['course_export']) {
189 $cexp_definition[$id] = $definition;
190 }
191 }
192 return $cexp_definition;
193 }
194
195 public function getGroupExportableFields(): array // Missing array type.
196 {
197 $cexp_definition = [];
198 foreach ($this->definitions as $id => $definition) {
199 if ($definition['group_export']) {
200 $cexp_definition[$id] = $definition;
201 }
202 }
203 return $cexp_definition;
204 }
205
209 public function getExportableFields(int $a_obj_id): array // Missing array type.
210 {
211 if (ilObject::_lookupType($a_obj_id) == 'crs') {
212 return $this->getCourseExportableFields();
213 }
214 if (ilObject::_lookupType($a_obj_id) == 'grp') {
215 return $this->getGroupExportableFields();
216 }
217 return array();
218 }
219
220
221 public function setFieldName(string $a_name): void
222 {
223 $this->field_name = $a_name;
224 }
225
226 public function getFieldName(): string
227 {
228 return $this->field_name;
229 }
230
231 public function setFieldType(int $a_type): void
232 {
233 $this->field_type = $a_type;
234 }
235
236 public function isPluginType(): bool
237 {
238 if (!$this->field_type) {
239 return false;
240 }
241 switch ($this->field_type) {
242 case UDF_TYPE_TEXT:
243 case UDF_TYPE_SELECT:
244 case UDF_TYPE_WYSIWYG:
245 return false;
246
247 default:
248 return true;
249 }
250 }
251
252 public function getFieldType(): int
253 {
254 return $this->field_type;
255 }
256
260 public function setFieldValues(array $a_values): void
261 {
262 $this->field_values = array();
263 foreach ($a_values as $value) {
264 if (strlen($value)) {
265 $this->field_values[] = $value;
266 }
267 }
268 }
269
270 public function getFieldValues(): array // Missing array type.
271 {
272 return $this->field_values ?: array();
273 }
274
275 public function enableVisible(bool $a_visible): void
276 {
277 $this->field_visible = $a_visible;
278 }
279
280 public function enabledVisible(): bool
281 {
283 }
284
285 public function enableVisibleLocalUserAdministration(bool $a_visible): void
286 {
287 $this->field_visib_lua = $a_visible;
288 }
289
291 {
293 }
294
295 public function enableChangeable(bool $a_changeable): void
296 {
297 $this->field_changeable = $a_changeable;
298 }
299
300 public function enabledChangeable(): bool
301 {
303 }
304
305 public function enableChangeableLocalUserAdministration(bool $a_changeable): void
306 {
307 $this->field_changeable_lua = $a_changeable;
308 }
309
311 {
313 }
314
315 public function enableRequired(bool $a_required): void
316 {
317 $this->field_required = $a_required;
318 }
319
320 public function enabledRequired(): bool
321 {
323 }
324
325 public function enableSearchable(bool $a_searchable): void
326 {
327 $this->field_searchable = $a_searchable;
328 }
329
330 public function enabledSearchable(): bool
331 {
333 }
334
335 public function enableExport(bool $a_export): void
336 {
337 $this->field_export = $a_export;
338 }
339
340 public function enabledExport(): bool
341 {
342 return $this->field_export;
343 }
344
345 public function enableCourseExport(bool $a_course_export): void
346 {
347 $this->field_course_export = $a_course_export;
348 }
349
350 public function enabledCourseExport(): bool
351 {
353 }
354
355 public function enableGroupExport(bool $a_group_export): void
356 {
357 $this->field_group_export = $a_group_export;
358 }
359
360 public function enabledGroupExport(): bool
361 {
363 }
364
365 public function enableCertificate(bool $a_c): void
366 {
367 $this->field_certificate = $a_c;
368 }
369
370 public function enabledCertificate(): bool
371 {
373 }
374
375 public function enableVisibleRegistration(bool $a_visible_registration): void
376 {
377 $this->field_visible_registration = $a_visible_registration;
378 }
379
380 public function enabledVisibleRegistration(): bool
381 {
383 }
384
386 array $a_values,
387 bool $a_with_selection_info = true
388 ): array {
389 global $DIC;
390
391 $lng = $DIC->language();
392 $values = [];
393 if ($a_with_selection_info) {
394 $values[''] = $lng->txt('please_select');
395 }
396 foreach ($a_values as $value) {
397 $values[$value] = $value;
398 }
399 if (count($values) > (int) $a_with_selection_info) {
400 return $values;
401 }
402 return [];
403 }
404
405 public function validateValues(): int
406 {
407 $number = 0;
408 $unique = array();
409 foreach ($this->getFieldValues() as $value) {
410 if (!strlen($value)) {
411 continue;
412 }
413 $number++;
414 $unique[$value] = $value;
415 }
416
417 if (!count($unique)) {
418 return UDF_NO_VALUES;
419 }
420 if ($number != count($unique)) {
422 }
423 return 0;
424 }
425
426 public function nameExists(string $a_field_name): bool
427 {
428 global $DIC;
429
430 $ilDB = $DIC['ilDB'];
431
432 $query = "SELECT * FROM udf_definition " .
433 "WHERE field_name = " . $this->db->quote($a_field_name, 'text') . " ";
434 $res = $ilDB->query($query);
435
436 return (bool) $res->numRows();
437 }
438
439 public function add(): int
440 {
441 global $DIC;
442
443 $ilDB = $DIC['ilDB'];
444
445 // Add definition entry
446 $next_id = $ilDB->nextId('udf_definition');
447
448 $values = array(
449 'field_id' => array('integer',$next_id),
450 'field_name' => array('text',$this->getFieldName()),
451 'field_type' => array('integer', $this->getFieldType()),
452 'field_values' => array('clob',serialize($this->getFieldValues())),
453 'visible' => array('integer', (int) $this->enabledVisible()),
454 'changeable' => array('integer', (int) $this->enabledChangeable()),
455 'required' => array('integer', (int) $this->enabledRequired()),
456 'searchable' => array('integer', (int) $this->enabledSearchable()),
457 'export' => array('integer', (int) $this->enabledExport()),
458 'course_export' => array('integer', (int) $this->enabledCourseExport()),
459 'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
460 'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
461 'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
462 'group_export' => array('integer', (int) $this->enabledGroupExport()),
463 'certificate' => array('integer', (int) $this->enabledCertificate()),
464 );
465
466 $ilDB->insert('udf_definition', $values);
467
468 // add table field in usr_defined_data
469 $field_id = $next_id;
470
471
472 $this->__read();
473
474 return $field_id;
475 }
476
477 public function delete(int $a_id): void
478 {
479 global $DIC;
480
481 $ilDB = $DIC['ilDB'];
482
483 // Delete definitions
484 $query = "DELETE FROM udf_definition " .
485 "WHERE field_id = " . $this->db->quote($a_id, 'integer') . " ";
486 $ilDB->manipulate($query);
487
488 // Delete usr_data entries
490
491 $this->__read();
492 }
493
494 public function update(int $a_id): void
495 {
496 global $DIC;
497
498 $ilDB = $DIC['ilDB'];
499
500 $values = array(
501 'field_name' => array('text',$this->getFieldName()),
502 'field_type' => array('integer', $this->getFieldType()),
503 'field_values' => array('clob',serialize($this->getFieldValues())),
504 'visible' => array('integer', (int) $this->enabledVisible()),
505 'changeable' => array('integer', (int) $this->enabledChangeable()),
506 'required' => array('integer', (int) $this->enabledRequired()),
507 'searchable' => array('integer', (int) $this->enabledSearchable()),
508 'export' => array('integer', (int) $this->enabledExport()),
509 'course_export' => array('integer', (int) $this->enabledCourseExport()),
510 'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
511 'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
512 'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
513 'group_export' => array('integer', (int) $this->enabledGroupExport()),
514 'certificate' => array('integer', (int) $this->enabledCertificate())
515 );
516 $ilDB->update('udf_definition', $values, array('field_id' => array('integer',$a_id)));
517 $this->__read();
518 }
519
520 protected function __read(): void
521 {
522 global $DIC;
523
524 $ilSetting = $DIC['ilSetting'];
525
526 $query = "SELECT * FROM udf_definition ";
527 $res = $this->db->query($query);
528
529 $this->definitions = array();
530 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
531 $this->definitions[$row->field_id]['field_id'] = $row->field_id;
532 $this->definitions[$row->field_id]['field_name'] = $row->field_name;
533 $this->definitions[$row->field_id]['field_type'] = $row->field_type;
534 $this->definitions[$row->field_id]['il_id'] = 'il_' . $ilSetting->get('inst_id', '0') . '_udf_' . $row->field_id;
535
536 // #16953
537 $tmp = $sort = array();
538 $is_numeric = true;
539 foreach ((array) unserialize($row->field_values, ['allowed_classes' => false]) as $item) {
540 if (!is_numeric($item)) {
541 $is_numeric = false;
542 }
543 $sort[] = array("value" => $item);
544 }
545 foreach (ilArrayUtil::sortArray($sort, "value", "asc", $is_numeric) as $item) {
546 $tmp[] = $item["value"];
547 }
548
549 $this->definitions[$row->field_id]['field_values'] = $tmp;
550 $this->definitions[$row->field_id]['visible'] = $row->visible;
551 $this->definitions[$row->field_id]['changeable'] = $row->changeable;
552 $this->definitions[$row->field_id]['required'] = $row->required;
553 $this->definitions[$row->field_id]['searchable'] = $row->searchable;
554 $this->definitions[$row->field_id]['export'] = $row->export;
555 $this->definitions[$row->field_id]['course_export'] = $row->course_export;
556 $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
557 $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
558 $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
559 $this->definitions[$row->field_id]['group_export'] = $row->group_export;
560 $this->definitions[$row->field_id]['certificate'] = $row->certificate;
561 }
562 }
563
564 public function deleteValue(
565 int $a_field_id,
566 int $a_value_id
567 ): void {
568 global $DIC;
569
570 $ilDB = $DIC['ilDB'];
571 $old_value = "";
572
573 $definition = $this->getDefinition($a_field_id);
574
575 $counter = 0;
576 $new_values = array();
577 foreach ($definition['field_values'] as $value) {
578 if ($counter++ != $a_value_id) {
579 $new_values[] = $value;
580 } else {
581 $old_value = $value;
582 }
583 }
584
585 $values = array(
586 'field_values' => array('clob',serialize($new_values)));
587 $ilDB->update('udf_definition', $values, array('field_id' => array('integer',$a_field_id)));
588
589
590 // sets value to '' where old value is $old_value
591 ilUserDefinedData::deleteFieldValue($a_field_id, $old_value);
592
593 // finally read data
594 $this->__read();
595 }
596
597 public function toXML(): string
598 {
599 $xml_writer = new ilXmlWriter();
600 $this->addToXML($xml_writer);
601 return $xml_writer->xmlDumpMem(false);
602 }
603
607 public function addToXML(ilXmlWriter $xml_writer): void
608 {
609 $xml_writer->xmlStartTag("UDFDefinitions");
610 foreach ($this->getDefinitions() as $definition) {
611 $attributes = array(
612 "Id" => $definition ["il_id"],
613 "Type" => $definition["field_type"] == UDF_TYPE_SELECT ? "SELECT" : "TEXT",
614 "Visible" => $definition["visible"] ? "TRUE" : "FALSE",
615 "Changeable" => $definition["changeable"] ? "TRUE" : "FALSE",
616 "Required" => $definition["required"] ? "TRUE" : "FALSE",
617 "Searchable" => $definition["searchable"] ? "TRUE" : "FALSE",
618 "CourseExport" => $definition["course_export"] ? "TRUE" : "FALSE",
619 "GroupExport" => $definition["group_export"] ? "TRUE" : "FALSE",
620 "Certificate" => $definition["certificate"] ? "TRUE" : "FALSE",
621 "Export" => $definition["export"] ? "TRUE" : "FALSE",
622 "RegistrationVisible" => $definition["visib_reg"] ? "TRUE" : "FALSE",
623 "LocalUserAdministrationVisible" => $definition["visib_lua"] ? "TRUE" : "FALSE",
624 "LocalUserAdministrationChangeable" => $definition["changeable_lua"] ? "TRUE" : "FALSE",
625
626 );
627 $xml_writer->xmlStartTag("UDFDefinition", $attributes);
628 $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
629 if ($definition["field_type"] == UDF_TYPE_SELECT) {
630 $field_values = $definition["field_values"];
631 foreach ($field_values as $field_value) {
632 $xml_writer->xmlElement('UDFValue', null, $field_value);
633 }
634 }
635 $xml_writer->xmlEndTag("UDFDefinition");
636 }
637 $xml_writer->xmlEndTag("UDFDefinitions");
638 }
639
640 public static function _newInstance(): self
641 {
642 static $udf = null;
643 return $udf = new ilUserDefinedFields();
644 }
645}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const UDF_TYPE_SELECT
const UDF_TYPE_WYSIWYG
const UDF_TYPE_TEXT
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const UDF_DUPLICATE_VALUES
const UDF_NO_VALUES
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static _lookupType(int $id, bool $reference=false)
static deleteFieldValue(int $a_field_id, string $a_value)
Delete data of particular value of a (selection) field.
static deleteEntriesOfField(int $a_field_id)
Delete data of particular field.
Additional user data fields definition.
fieldValuesToSelectArray(array $a_values, bool $a_with_selection_info=true)
enableVisibleLocalUserAdministration(bool $a_visible)
deleteValue(int $a_field_id, int $a_value_id)
fetchFieldIdFromImportId(string $a_import_id)
addToXML(ilXmlWriter $xml_writer)
add user defined field data to xml (using usr dtd)
enableChangeableLocalUserAdministration(bool $a_changeable)
nameExists(string $a_field_name)
enableGroupExport(bool $a_group_export)
getExportableFields(int $a_obj_id)
Get exportable field.
enableSearchable(bool $a_searchable)
enableVisibleRegistration(bool $a_visible_registration)
fetchFieldIdFromName(string $a_name)
enableCourseExport(bool $a_course_export)
enableChangeable(bool $a_changeable)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
$attributes
Definition: metadata.php:248
global $ilSetting
Definition: privfeed.php:17
$query
$lng