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