ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDataCollectionDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
27  protected array $record_field_ids_2_storage = [];
44  protected array $caches
45  = [
46  'dcl' => [],
47  'il_dcl_table' => [],
48  'il_dcl_field' => [],
49  'il_dcl_field_prop' => [],
50  'il_dcl_sel_opts' => [],
51  'il_dcl_record' => [],
52  'il_dcl_record_field' => [],
53  'il_dcl_stloc1_value' => [],
54  'il_dcl_stloc2_value' => [],
55  'il_dcl_stloc3_value' => [],
56  'il_dcl_stloc1_default' => [],
57  'il_dcl_stloc2_default' => [],
58  'il_dcl_stloc3_default' => [],
59  'il_dcl_tfield_set' => [],
60  'il_dcl_tableview' => [],
61  'il_dcl_tview_set' => [],
62  ];
63 
65 
69  protected array $import_record_field_cache = [];
70  protected ilObjUser $user;
71  protected array $import_temp_refs = [];
72  protected array $import_temp_refs_props = [];
73  protected array $import_temp_new_mob_ids = [];
74 
75  public function __construct()
76  {
77  global $DIC;
79  $this->db = $DIC->database();
80  $this->user = $DIC->user();
81  }
82 
83  public function getSupportedVersions(): array
84  {
85  return ['4.5.0', '8.13'];
86  }
87 
92  public function getCache(string $a_entity): array
93  {
94  if (!in_array($a_entity, array_keys($this->caches))) {
95  throw new ilException("Entity '$a_entity' does not exist in Cache");
96  }
97 
98  return $this->caches[$a_entity];
99  }
100 
101  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
102  {
103  return 'https://www.ilias.de/xml/Modules/DataCollection/' . $a_entity;
104  }
105 
106  public function importRecord(
107  string $a_entity,
108  array $a_types,
109  array $a_rec,
110  ilImportMapping $a_mapping,
111  string $a_schema_version
112  ): void {
113  foreach ($a_rec as $key => &$value) {
114  $array = json_decode($value, true);
115  if ($key === 'title' || $key === 'description') {
116  $value = strip_tags($value, ilObjectGUI::ALLOWED_TAGS_IN_TITLE_AND_DESCRIPTION);
117  } elseif (is_array($array)) {
118  $value = json_encode($this->escapeArray($array));
119  } else {
120  $value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8');
121  }
122  }
123  switch ($a_entity) {
124  case 'dcl':
125  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_rec['id'])) {
126  $new_obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
127  } else {
128  $new_obj = new ilObjDataCollection();
129  $new_obj->create(true);
130  }
131  $new_obj->setTitle($a_rec['title']);
132  $new_obj->setDescription($a_rec['description']);
133  $new_obj->setApproval($a_rec['approval'] === '1');
134  $new_obj->setPublicNotes($a_rec['public_notes'] === '1');
135  $new_obj->setNotification($a_rec['notification'] === '1');
136  $new_obj->setRating($a_rec['rating'] === '1');
137  $new_obj->update(); //clone mode, so no table will be created
138  $this->import_dc_object = $new_obj;
139  $a_mapping->addMapping('Modules/DataCollection', 'dcl', $a_rec['id'], (string) $new_obj->getId());
140  break;
141  case 'il_dcl_table':
142  $table = new ilDclTable();
143  $table->setTitle($a_rec['title']);
144  $table->setObjId($this->import_dc_object->getId());
145  $table->setDescription($a_rec['description']);
146  $table->setAddPerm($a_rec['add_perm'] === '1');
147  $table->setEditPerm($a_rec['edit_perm'] === '1');
148  $table->setDeletePerm($a_rec['delete_perm'] === '1');
149  $table->setEditByOwner($a_rec['edit_by_owner'] === '1');
150  $table->setLimited($a_rec['limited'] === '1');
151  $table->setLimitStart($a_rec['limit_start']);
152  $table->setLimitEnd($a_rec['limit_end']);
153  $table->setIsVisible($a_rec['is_visible'] === '1');
154  $table->setExportEnabled($a_rec['export_enabled'] === '1');
155  $table->setImportEnabled($a_rec['import_enabled'] === '1');
156  $table->setDefaultSortField($a_rec['default_sort_field_id']);
157  $table->setDefaultSortFieldOrder($a_rec['default_sort_field_order']);
158  $table->setPublicCommentsEnabled($a_rec['public_comments'] === '1');
159  $table->setViewOwnRecordsPerm($a_rec['view_own_records_perm'] === '1');
160  $table->setDeleteByOwner($a_rec['delete_by_owner'] === '1');
161  $table->setSaveConfirmation($a_rec['save_confirmation'] === '1');
162  $table->setOrder((int) $a_rec['table_order']);
163  $table->doCreate(false, false); // false => Do not create views! They are imported later
164  $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['id'], (string) $table->getId());
165  break;
166  case 'il_dcl_tableview':
167  $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
168  if ($new_table_id) {
169  $tableview = new ilDclTableView();
170  $tableview->setTitle($a_rec['title']);
171  $tableview->setTableId((int) $new_table_id);
172  $tableview->setDescription($a_rec['description']);
173  $tableview->setTableviewOrder((int) $a_rec['tableview_order']);
174  if (!is_array($a_rec['roles'])) {
175  $a_rec['roles'] = json_decode($a_rec['roles']);
176  }
177  $tableview->setRoles($a_rec['roles']);
178  $tableview->create(false); //do not create default setting as they are imported too
179 
180  $a_mapping->addMapping(
181  'Modules/DataCollection',
182  'il_dcl_tableview',
183  $a_rec['id'],
184  (string) $tableview->getId()
185  );
186  $a_mapping->addMapping('Services/COPage', 'pg', 'dclf:' . $a_rec['id'], 'dclf:' . $tableview->getId());
187  }
188  break;
189  case 'il_dcl_field':
190  $new_table_id = (int) $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
191  if ($new_table_id > 0) {
192  $datatype_id = (int) $a_rec['datatype_id'];
193  $datatype = $a_rec['datatype_title'] ?? null;
194  $datatypes = ilDclDatatype::getAllDatatype();
195  if ($datatype !== null && ilDclFieldTypePlugin::isPluginDatatype($datatype)) {
196  $datatype_id = null;
197  foreach ($datatypes as $dt) {
198  if ($dt->getTitle() === $datatype) {
199  $datatype_id = $dt->getId();
200  }
201  }
202  }
203  if (in_array($datatype_id, array_keys($datatypes))) {
204  $field = new ilDclBaseFieldModel();
205  $field->setTableId($new_table_id);
206  $field->setDatatypeId($datatype_id);
207  $field->setTitle($a_rec['title']);
208  $field->setDescription($a_rec['description']);
209  $field->setUnique($a_rec['is_unique'] === '1');
210  $field->doCreate();
211  $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['id'], $field->getId());
212  }
213  }
214  break;
215  case 'il_dcl_tfield_set':
216  $new_table_id = (int) $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
217  $new_field_id = is_numeric($a_rec['field']) ? $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field']) : $a_rec['field'];
218  if ($new_table_id > 0 && (is_string($new_field_id) || $new_field_id > 0)) {
220  $new_table_id,
221  $new_field_id
222  );
223  $setting->setFieldOrder((int) $a_rec['field_order']);
224  $setting->setExportable($a_rec['exportable'] === '1');
225  $setting->store();
226  }
227  break;
228  case 'il_dcl_tview_set':
229  $new_tableview_id = $a_mapping->getMapping(
230  'Modules/DataCollection',
231  'il_dcl_tableview',
232  $a_rec['tableview_id']
233  );
234  $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field']);
235  if ($new_tableview_id) {
236  $setting = new ilDclTableViewFieldSetting();
237  $setting->setTableviewId((int) $new_tableview_id);
238  $setting->setVisible($a_rec['visible'] === '1');
239  $setting->setField($new_field_id ?: $a_rec['field']);
240  $setting->setInFilter($a_rec['in_filter'] === '1');
241  $setting->setFilterValue($a_rec['filter_value']);
242  $setting->setFilterChangeable($a_rec['filter_changeable'] === '1');
243  $setting->setRequiredCreate($a_rec['required_create'] === '1');
244  $setting->setLockedCreate($a_rec['locked_create'] === '1');
245  $setting->setVisibleCreate($a_rec['visible_create'] === '1');
246  $setting->setVisibleEdit($a_rec['visible_edit'] === '1');
247  $setting->setRequiredEdit($a_rec['required_edit'] === '1');
248  $setting->setLockedEdit($a_rec['locked_edit'] === '1');
249  $setting->setDefaultValue($a_rec['default_value']);
250  $setting->create();
251  $a_mapping->addMapping(
252  'Modules/DataCollection',
253  'il_dcl_tview_set',
254  $a_rec['id'],
255  (string) $setting->getId()
256  );
257  }
258  break;
259  case 'il_dcl_record':
260  $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
261  if ($new_table_id) {
262  $record = new ilDclBaseRecordModel();
263  $record->setTableId((int) $new_table_id);
264  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
265  $record->setCreateDate($datetime);
266  $record->setLastUpdate($datetime);
267  $record->setOwner($this->user->getId());
268  $record->setLastEditBy($this->user->getId());
269  $record->doCreate();
270  $a_mapping->addMapping(
271  'Modules/DataCollection',
272  'il_dcl_record',
273  $a_rec['id'],
274  (string) $record->getId()
275  );
276  }
277  break;
278  case 'il_dcl_view':
279  $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
280  if ($new_table_id) {
281  //if import contains il_dcl_view, it must origin from an earlier ILIAS Version and therefore contains no tableviews
282  //->create standard view
283  $tableview = ilDclTableView::createOrGetStandardView((int) $new_table_id);
284  if ($a_rec['type'] == 0 && $a_rec['formtype'] == 0) { //set page_object to tableview
285  // This mapping is needed for the import handled by Services/COPage
286  $a_mapping->addMapping(
287  'Services/COPage',
288  'pg',
289  'dclf:' . $a_rec['id'],
290  'dclf:' . $tableview->getId()
291  );
292  $a_mapping->addMapping(
293  'Modules/DataCollection',
294  'il_dcl_view',
295  $a_rec['id'],
296  (string) $tableview->getId()
297  );
298  } else {
299  $a_mapping->addMapping(
300  'Modules/DataCollection',
301  'il_dcl_view',
302  $a_rec['id'],
303  json_encode(['type' => $a_rec['type'],
304  'table_id' => $new_table_id,
305  'tableview_id' => $tableview->getId()
306  ])
307  );
308  }
309  }
310  break;
311  case 'il_dcl_viewdefinition':
312  $map = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_view', $a_rec['view_id']);
313  $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field']);
314  $field = ($new_field_id) ?: $a_rec['field'];
315  switch ($map['type']) {
316  case 1: //visible
317  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
318  $viewfield_setting->setVisible($a_rec['is_set']);
319  $viewfield_setting->store();
320  break;
321  case 3: //in_filter
322  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
323  $viewfield_setting->setInFilter($a_rec['is_set']);
324  $viewfield_setting->store();
325  break;
326  case 4: //exportable
327  $tablefield_setting = ilDclTableFieldSetting::getInstance($map['table_id'], $field);
328  $tablefield_setting->setExportable($a_rec['is_set']);
329  $tablefield_setting->setFieldOrder($a_rec['field_order']);
330  $tablefield_setting->store();
331  break;
332  }
333  break;
334  case 'il_dcl_sel_opts':
335  $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field_id']);
336  if ($new_field_id) {
337  $opt = new ilDclSelectionOption();
338  $opt->setFieldId((int) $new_field_id);
339  $opt->setOptId((int) $a_rec['opt_id']);
340  $opt->setSorting((int) $a_rec['sorting']);
341  $opt->setValue($a_rec['value']);
342  $opt->store();
343  }
344  break;
345  case 'il_dcl_field_prop':
346  $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field_id']);
347  if ($new_field_id) {
348  $prop = new ilDclFieldProperty();
349  $prop->setFieldId((int) $new_field_id);
350 
351  // OLD IMPORT! Backwards-compatibility
352  $name = $a_rec['name'];
353  if (!isset($name) && isset($a_rec['datatype_prop_id'])) {
354  $properties = [
355  1 => 'length',
356  2 => 'regex',
357  3 => 'table_id',
358  4 => 'url',
359  5 => 'text_area',
360  6 => 'reference_link',
361  7 => 'width',
362  8 => 'height',
363  9 => 'learning_progress',
364  10 => 'ILIAS_reference_link',
365  11 => 'multiple_selection',
366  12 => 'expression',
367  13 => 'display_action_menu',
368  14 => 'link_detail_page',
369  15 => 'link_detail_page',
370  ];
371 
372  $name = $properties[$a_rec['datatype_prop_id']];
373  }
374 
375  $prop->setName($name);
376  $prop->setValue($a_rec['value']);
377  $prop->save();
378  $a_mapping->addMapping(
379  'Modules/DataCollection',
380  'il_dcl_field_prop',
381  $a_rec['id'],
382  (string) $prop->getId()
383  );
384  $this->import_temp_refs_props[$prop->getId()] = $a_rec['value'];
385  }
386  break;
387  case 'il_dcl_record_field':
388  $record_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record', $a_rec['record_id']);
389  $field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field_id']);
390  if ($record_id && $field_id) {
391  $record = ilDclCache::getRecordCache((int) $record_id);
392  $field = ilDclCache::getFieldCache((int) $field_id);
393  $record_field = new ilDclBaseRecordFieldModel($record, $field);
394  $a_mapping->addMapping(
395  'Modules/DataCollection',
396  'il_dcl_record_field',
397  $a_rec['id'],
398  (string) $record_field->getId()
399  );
400  $this->import_record_field_cache[$record_field->getId()] = $record_field;
401  }
402  break;
403  case 'il_dcl_stloc1_value':
404  case 'il_dcl_stloc2_value':
405  case 'il_dcl_stloc3_value':
406  $new_record_field_id = $a_mapping->getMapping(
407  'Modules/DataCollection',
408  'il_dcl_record_field',
409  $a_rec['record_field_id']
410  );
411  if ($new_record_field_id) {
413  $record_field = $this->import_record_field_cache[$new_record_field_id];
414  if (is_object($record_field)) {
415  // Need to rewrite internal references and lookup new objects if MOB or File
416  // For some fieldtypes it's better to reset the value, e.g. ILIAS_REF
417  switch ($record_field->getField()->getDatatypeId()) {
419  // Check if we got a mapping from old object
420  $new_mob_id = $a_mapping->getMapping('Services/MediaObjects', 'mob', $a_rec['value']);
421  $value = ($new_mob_id) ? (int) $new_mob_id : null;
422  $this->import_temp_new_mob_ids[] = $new_mob_id;
423  break;
425  $new_file_id = $a_mapping->getMapping('Modules/File', 'file', $a_rec['value']);
426  $value = ($new_file_id) ? (int) $new_file_id : null;
427  break;
430  $value = $a_rec['value'];
431  $decode = json_decode($a_rec['value']);
432  if (is_array($decode)) {
433  foreach ($decode as $id) {
434  $this->import_temp_refs[$new_record_field_id][] = $id;
435  }
436  } else {
437  $this->import_temp_refs[$new_record_field_id] = $value;
438  }
439  break;
441  $value = null;
442  break;
444  $value = $a_rec['value'];
445  if ($value == '0000-00-00 00:00:00') {
446  $value = null;
447  }
448  break;
450  if (version_compare($a_schema_version, "8.13") < 0) {
451  $a_rec['value'] = str_replace('&lt;br /&gt;', '', $a_rec['value']);
452  }
453  // no break
454  default:
455  $value = $a_rec['value'];
456  if ($a_entity == 'il_dcl_stloc3_value' && empty($value)) {
457  $value = null;
458  }
459  }
460  $record_field->setValue($value, true);
461  $record_field->doUpdate();
462  }
463  }
464  break;
465  case 'il_dcl_stloc1_default':
466  case 'il_dcl_stloc2_default':
467  case 'il_dcl_stloc3_default':
468 
469  $tview_set_id = $a_mapping->getMapping(
470  'Modules/DataCollection',
471  'il_dcl_tview_set',
472  $a_rec['tview_set_id']
473  );
474 
475  if ($tview_set_id) {
476  $value = $a_rec['value'];
477  if ($value) {
478  $stloc_default = (new ilDclDefaultValueFactory())->createByTableName($a_entity);
480  $value = (int) $value;
481  }
482  $stloc_default->setValue($value);
483  $stloc_default->setTviewSetId((int) $tview_set_id);
484  $stloc_default->create();
485  }
486  }
487  break;
488  }
489  }
490 
491  protected function escapeArray(array $array): array
492  {
493  $new = [];
494  foreach ($array as $key => $value) {
495  $newkey = $key;
496  if (is_string($key)) {
497  $newkey = htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8');
498  }
499  $newvalue = $value;
500  if (is_string($value)) {
501  $newvalue = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8');
502  }
503  if (is_array($value)) {
504  $newvalue = $this->escapeArray($value);
505  }
506  $new[$newkey] = $newvalue;
507  }
508  return $new;
509  }
510 
515  public function beforeFinishImport(ilImportMapping $a_mapping): void
516  {
517  foreach ($this->import_dc_object->getTables() as $table) {
518  if (is_numeric($table->getDefaultSortField())) {
519  $table->setDefaultSortField(
520  $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $table->getDefaultSortField())
521  );
522  $table->doUpdate();
523  }
524  }
525  foreach ($this->import_temp_new_mob_ids as $new_mob_id) {
526  if ($new_mob_id) {
527  ilObjMediaObject::_saveUsage((int) $new_mob_id, "dcl:html", $a_mapping->getTargetId());
528  }
529  }
530  foreach ($this->import_temp_refs as $record_field_id => $old_record_id) {
531  if (is_array($old_record_id)) {
532  $new_record_id = [];
533  foreach ($old_record_id as $id) {
534  $new_record_id[] = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record', $id);
535  }
536  $value = $new_record_id;
537  } else {
538  $value = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record', $old_record_id);
539  }
541  $record_field = $this->import_record_field_cache[$record_field_id];
542  $record_field->setValue($value, true);
543  $record_field->doUpdate();
544  }
545  foreach ($this->import_temp_refs_props as $field_prop_id => $prop_value) {
546  $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $prop_value);
547  $value = ($new_field_id) ? (int) $new_field_id : $prop_value;
548 
549  $field_prop = new ilDclFieldProperty($field_prop_id);
550  $field_prop->setValue($value);
551  $field_prop->update();
552  }
553  }
554 
558  protected function getTypes(string $a_entity, string $a_version): array
559  {
560  switch ($a_entity) {
561  case 'dcl':
562  return [
563  "id" => "integer",
564  "title" => "text",
565  "description" => "text",
566  'is_online' => 'integer',
567  'rating' => 'integer',
568  'public_notes' => 'integer',
569  'approval' => 'integer',
570  'notification' => 'integer',
571  ];
572  case 'il_dcl_table':
573  return [
574  'id' => 'integer',
575  'obj_id' => 'integer',
576  'title' => 'text',
577  'add_perm' => 'integer',
578  'edit_perm' => 'integer',
579  'delete_perm' => 'integer',
580  'edit_by_owner' => 'integer',
581  'limited' => 'integer',
582  'limit_start' => 'text',
583  'limit_end' => 'text',
584  'is_visible' => 'integer',
585  'export_enabled' => 'integer',
586  'import_enabled' => 'integer',
587  'default_sort_field_id' => 'text',
588  'default_sort_field_order' => 'text',
589  'description' => 'text',
590  'public_comments' => 'integer',
591  'view_own_records_perm' => 'integer',
592  'delete_by_owner' => 'integer',
593  'save_confirmation' => 'integer',
594  'table_order' => 'integer',
595  ];
596  case 'il_dcl_tableview':
597  return [
598  'id' => 'integer',
599  'table_id' => 'integer',
600  'title' => 'text',
601  'roles' => 'text',
602  'description' => 'text',
603  'tableview_order' => 'integer',
604  ];
605  case 'il_dcl_field':
606  return [
607  'id' => 'integer',
608  'table_id' => 'integer',
609  'title' => 'text',
610  'description' => 'text',
611  'datatype_id' => 'integer',
612  'datatype_title' => 'text',
613  'is_unique' => 'integer',
614  ];
615  case 'il_dcl_tview_set':
616  return [
617  'id' => 'integer',
618  'tableview_id' => 'integer',
619  'field' => 'text',
620  'visible' => 'integer',
621  'in_filter' => 'integer',
622  'filter_value' => 'text',
623  'filter_changeable' => 'integer',
624  'required_create' => 'integer',
625  'required_edit' => 'integer',
626  'locked_create' => 'integer',
627  'locked_edit' => 'integer',
628  'visible_create' => 'integer',
629  'visible_edit' => 'integer',
630  'default_value' => 'text',
631  ];
632  case 'il_dcl_tfield_set':
633  return [
634  'id' => 'integer',
635  'table_id' => 'integer',
636  'field' => 'text',
637  'field_order' => 'integer',
638  'exportable' => 'integer',
639  ];
640  case 'il_dcl_field_prop':
641  return [
642  'id' => 'integer',
643  'field_id' => 'integer',
644  'name' => 'text',
645  'value' => 'integer',
646  ];
647  case 'il_dcl_sel_opts':
648  return [
649  'id' => 'integer',
650  'field_id' => 'integer',
651  'opt_id' => 'integer',
652  'sorting' => 'integer',
653  'value' => 'text',
654  ];
655  case 'il_dcl_record':
656  return [
657  'id' => 'integer',
658  'table_id' => 'integer',
659  ];
660  case 'il_dcl_record_field':
661  return [
662  'id' => 'integer',
663  'record_id' => 'integer',
664  'field_id' => 'integer',
665  ];
666  case 'il_dcl_stloc1_value':
667  case 'il_dcl_stloc2_value':
668  case 'il_dcl_stloc3_value':
669  return [
670  'id' => 'integer',
671  'record_field_id' => 'integer',
672  'value' => 'text',
673  ];
674  case 'il_dcl_stloc1_default':
675  case 'il_dcl_stloc2_default':
676  case 'il_dcl_stloc3_default':
677  return [
678  'id' => 'integer',
679  'tview_set_id' => 'integer',
680  'value' => 'text',
681  ];
682  default:
683  return [];
684  }
685  }
686 
695  protected function getDependencies(
696  string $a_entity,
697  string $a_version,
698  ?array $a_rec = null,
699  ?array $a_ids = null
700  ): array {
701  if (!$a_rec && !$a_ids) {
702  return [];
703  }
704  switch ($a_entity) {
705  case 'dcl':
706  $set = $this->db->query('SELECT * FROM il_dcl_table WHERE obj_id = ' . $this->db->quote(
707  $a_rec['id'],
708  'integer'
709  ) . ' ORDER BY id');
710  $ids = $this->buildCache('il_dcl_table', $set);
711 
712  return [
713  'il_dcl_table' => ['ids' => $ids],
714  ];
715  case 'il_dcl_table':
716  $set = $this->db->query('SELECT * FROM il_dcl_record WHERE table_id = ' . $this->db->quote(
717  $a_rec['id'],
718  'integer'
719  ));
720  $ids_records = $this->buildCache('il_dcl_record', $set);
721  $set = $this->db->query('SELECT il_dcl_field.*, il_dcl_datatype.title as datatype_title FROM il_dcl_field INNER JOIN il_dcl_datatype ON il_dcl_field.datatype_id = il_dcl_datatype.id WHERE table_id = ' . $this->db->quote(
722  $a_rec['id'],
723  'integer'
724  ));
725  $ids_fields = $this->buildCache('il_dcl_field', $set);
726  $set = $this->db->query('SELECT * FROM il_dcl_tableview WHERE table_id = ' . $this->db->quote(
727  $a_rec['id'],
728  'integer'
729  ));
730  $ids_tableviews = $this->buildCache('il_dcl_tableview', $set);
731  $set = $this->db->query('SELECT * FROM il_dcl_tfield_set WHERE table_id = ' . $this->db->quote(
732  $a_rec['id'],
733  'integer'
734  ));
735  $ids_tablefield_settings = $this->buildCache('il_dcl_tfield_set', $set);
736 
737  return [
738  'il_dcl_field' => ['ids' => $ids_fields],
739  'il_dcl_record' => ['ids' => $ids_records],
740  'il_dcl_tableview' => ['ids' => $ids_tableviews],
741  'il_dcl_tfield_set' => ['ids' => $ids_tablefield_settings],
742  ];
743  case 'il_dcl_field':
744  $set = $this->db->query('SELECT * FROM il_dcl_field_prop WHERE field_id = ' . $this->db->quote(
745  $a_rec['id'],
746  'integer'
747  ));
748  $prop_ids = $this->buildCache('il_dcl_field_prop', $set);
749 
750  $set = $this->db->query('SELECT * FROM il_dcl_sel_opts WHERE field_id = ' . $this->db->quote(
751  $a_rec['id'],
752  'integer'
753  ));
754  $opt_ids = $this->buildCache('il_dcl_sel_opts', $set);
755 
756  return [
757  'il_dcl_field_prop' => ['ids' => $prop_ids],
758  'il_dcl_sel_opts' => ['ids' => $opt_ids],
759  ];
760  case 'il_dcl_record':
761  $sql = 'SELECT rf.*, d.storage_location FROM il_dcl_record_field AS rf' . ' INNER JOIN il_dcl_field AS f ON (f.id = rf.field_id)'
762  . ' INNER JOIN il_dcl_datatype AS d ON (f.datatype_id = d.id) ' . ' WHERE rf.record_id = '
763  . $this->db->quote($a_rec['id'], 'integer');
764  $set = $this->db->query($sql);
765  $ids = $this->buildCache('il_dcl_record_field', $set);
766 
767  $set = $this->db->query($sql);
768  while ($rec = $this->db->fetchObject($set)) {
769  $this->record_field_ids_2_storage[$rec->id] = ilDclCache::getFieldCache($rec->field_id)->getStorageLocation();
770  }
771  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
772  $sql
773  = 'SELECT rf.id AS record_field_id, st1.value AS value1, st2.value AS value2, st3.value AS value3 FROM il_dcl_record_field AS rf '
774  . 'LEFT JOIN il_dcl_stloc1_value AS st1 ON (st1.record_field_id = rf.id) '
775  . 'LEFT JOIN il_dcl_stloc2_value AS st2 ON (st2.record_field_id = rf.id) '
776  . 'LEFT JOIN il_dcl_stloc3_value AS st3 ON (st3.record_field_id = rf.id) ' . 'WHERE rf.record_id = '
777  . $this->db->quote($a_rec['id'], 'integer');
778  $set = $this->db->query($sql);
779 
780  while ($rec = $this->db->fetchObject($set)) {
781  $stloc = $this->record_field_ids_2_storage[$rec->record_field_id];
782  $value = null;
783  if ($stloc != 0) {
784  $value = "value$stloc";
785  $value = $rec->{$value};
786  }
787  // Save reocrd field id. Internal ID is not used currently
788  $this->caches["il_dcl_stloc{$stloc}_value"][$rec->record_field_id] = [
789  'record_field_id' => $rec->record_field_id,
790  'value' => $value,
791  ];
792  }
793 
794  return [
795  'il_dcl_record_field' => ['ids' => $ids],
796  ];
797  case 'il_dcl_tableview':
798  $set = $this->db->query('SELECT * FROM il_dcl_tview_set WHERE tableview_id = ' . $this->db->quote(
799  $a_rec['id'],
800  'integer'
801  ));
802  $ids = $this->buildCache('il_dcl_tview_set', $set);
803 
804  return [
805  'il_dcl_tview_set' => ['ids' => $ids],
806  ];
807  case 'il_dcl_tview_set':
808 
809  if (!(int) $a_rec['field'] > 0) {
810  break;
811  }
812  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
813  $sql
814  = '
815  SELECT tview_set.id AS tview_set_id, st1.value AS value1, st2.value AS value2, st3.value AS value3,
816  st1.id AS id1, st2.id AS id2, st3.id AS id3
817  FROM il_dcl_tview_set AS tview_set
818  LEFT JOIN il_dcl_stloc1_default AS st1 ON (st1.tview_set_id = tview_set.id)
819  LEFT JOIN il_dcl_stloc2_default AS st2 ON (st2.tview_set_id = tview_set.id)
820  LEFT JOIN il_dcl_stloc3_default AS st3 ON (st3.tview_set_id = tview_set.id)
821  WHERE tview_set.id = ' . $this->db->quote($a_rec['id'], 'integer');
822  $set = $this->db->query($sql);
823 
824  while ($rec = $this->db->fetchObject($set)) {
825  $stloc = ilDclCache::getFieldCache((int) $a_rec['field'])->getStorageLocation();
826  if ($stloc != 0) {
827  $value_str = "value$stloc";
828  $value = $rec->{$value_str};
829  $id_str = "id$stloc";
830  $id = $rec->{$id_str};
831  $tview_set_id = $rec->tview_set_id;
832 
833  // Save reocrd field id. Internal ID is not used currently
834  $this->caches["il_dcl_stloc" . "$stloc" . "_default"][$rec->tview_set_id] = [
835  'id' => $id,
836  'tview_set_id' => $rec->tview_set_id,
837  'value' => $value,
838  ];
839 
840  return [
841  "il_dcl_stloc{$stloc}_default" => ['ids' => [$tview_set_id]],
842  ];
843  }
844  }
845  break;
846  case 'il_dcl_record_field':
847  $record_field_id = $a_rec['id'];
848  $storage_loc = $this->record_field_ids_2_storage[$record_field_id];
849 
850  return [
851  "il_dcl_stloc{$storage_loc}_value" => ['ids' => [$record_field_id]],
852  ];
853  }
854 
855  return [];
856  }
857 
862  public function readData(string $a_entity, string $a_version, array $a_ids): void
863  {
864  $this->data = [];
865  $this->_readData($a_entity, $a_ids);
866  }
867 
873  protected function _readData(string $a_entity, array $a_ids): void
874  {
875  switch ($a_entity) {
876  case 'dcl':
877  foreach ($a_ids as $dcl_id) {
878  if (ilObject::_lookupType((int) $dcl_id) === 'dcl') {
879  $obj = new ilObjDataCollection((int) $dcl_id, false);
880  $data = [
881  'id' => $dcl_id,
882  'title' => $obj->getTitle(),
883  'description' => $obj->getDescription(),
884  'is_online' => $obj->getOnline() ? '1' : '0',
885  'rating' => $obj->getRating() ? '1' : '0',
886  'public_notes' => $obj->getPublicNotes() ? '1' : '0',
887  'approval' => $obj->getApproval() ? '1' : '0',
888  'notification' => $obj->getNotification() ? '1' : '0',
889  ];
890  $this->caches['dcl'][$dcl_id] = $data;
891  $this->data[] = $data;
892  }
893  }
894  break;
895  default:
896  $data = $this->getCache($a_entity);
897  foreach ($a_ids as $id) {
898  $this->data[] = $data[$id];
899  }
900  }
901  }
902 
903  protected function buildCache(string $a_entity, ilDBStatement $set): array
904  {
905  $fields = array_keys($this->getTypes($a_entity, ''));
906  $ids = [];
907  while ($rec = $this->db->fetchObject($set)) {
908  $data = [];
909  foreach ($fields as $field) {
910  $data[$field] = $rec->{$field};
911  }
912  $id = $rec->id;
913  $this->caches[$a_entity][$id] = $data;
914  $ids[] = $id;
915  }
916 
917  return $ids;
918  }
919 }
getXmlNamespace(string $a_entity, string $a_schema_version)
static createOrGetStandardView(int $table_id)
getTypes(string $a_entity, string $a_version)
Map XML attributes of entities to datatypes (text, integer...)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildCache(string $a_entity, ilDBStatement $set)
static getFieldCache(int $field_id=0)
$datetime
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
static isPluginDatatype(string $datatype)
array $import_record_field_cache
Caches ilDclBaseRecordFieldModel objects.
const IL_CAL_UNIX
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
global $DIC
Definition: feed.php:28
static getInstance(int $tableview_id, int $field_id)
const ALLOWED_TAGS_IN_TITLE_AND_DESCRIPTION
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
readData(string $a_entity, string $a_version, array $a_ids)
Read data from Cache for a given entity and ID(s)
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
getCache(string $a_entity)
Get cached data from a given entity.
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
string $key
Consumer key/client ID value.
Definition: System.php:193
array ilObjDataCollection $import_dc_object
_readData(string $a_entity, array $a_ids)
Build data array, data is read from cache except dcl object itself.
static getInstance(int $table_id, string $field)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static getRecordCache(?int $record_id)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Return dependencies form entities to other entities (in our case these are all the DB relations) ...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _lookupType(int $id, bool $reference=false)