ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilDclContentImporter Class Reference
+ Collaboration diagram for ilDclContentImporter:

Public Member Functions

 __construct (int $ref_id, ?int $table_id=null)
 
 import (string $file, bool $simulate=false)
 

Data Fields

const EXPORT_EXCEL = 'xlsx'
 

Protected Member Functions

 checkImportType (ilDclBaseFieldModel $field)
 
 getImportFieldsFromTitles (ilDclTable $table, array $titles)
 

Protected Attributes

int $max_imports = 100
 
array $supported_import_datatypes
 
array $warnings
 
int $ref_id
 Ref-ID of DataCollection. More...
 
int $table_id
 Table-Id for export. More...
 
ilObjDataCollection $dcl
 
array $tables
 
ilLanguage $lng
 
ilObjUser $user
 

Detailed Description

Definition at line 21 of file class.ilDclContentImporter.php.

Constructor & Destructor Documentation

◆ __construct()

ilDclContentImporter::__construct ( int  $ref_id,
?int  $table_id = null 
)

Definition at line 56 of file class.ilDclContentImporter.php.

References $DIC, $ref_id, $table_id, ILIAS\Repository\lng(), and ILIAS\Repository\user().

57  {
58  global $DIC;
59 
60  $this->ref_id = $ref_id;
61  $this->table_id = $table_id;
62 
63  $this->lng = $DIC->language();
64  $this->user = $DIC->user();
65 
66  $this->dcl = new ilObjDataCollection($ref_id);
67  $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
68  }
int $ref_id
Ref-ID of DataCollection.
global $DIC
Definition: feed.php:28
int $table_id
Table-Id for export.
+ Here is the call graph for this function:

Member Function Documentation

◆ checkImportType()

ilDclContentImporter::checkImportType ( ilDclBaseFieldModel  $field)
protected

Definition at line 177 of file class.ilDclContentImporter.php.

References $supported_import_datatypes, ilDclBaseFieldModel\getDatatypeId(), ilDclBaseFieldModel\getTitle(), and ILIAS\Repository\lng().

Referenced by getImportFieldsFromTitles().

177  : bool
178  {
179  if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
180  return true;
181  } else {
182  $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
183 
184  return false;
185  }
186  }
getDatatypeId()
Get datatype_id.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getImportFieldsFromTitles()

ilDclContentImporter::getImportFieldsFromTitles ( ilDclTable  $table,
array  $titles 
)
protected
Parameters
string[]$titles
Returns
ilDclBaseFieldModel[]

Definition at line 192 of file class.ilDclContentImporter.php.

References ILIAS\LTI\ToolProvider\$key, ilDclStandardField\_getImportableStandardFieldTitle(), ilDclStandardField\_getNonImportableStandardFieldTitles(), checkImportType(), ilDataCollectionImporter\getExcelCharForInteger(), ilDclTable\getRecordFields(), and ILIAS\Repository\lng().

Referenced by import().

192  : array
193  {
194  $fields = $table->getRecordFields();
195  $import_fields = [];
196  foreach ($fields as $field) {
197  if ($this->checkImportType($field)) {
198  // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
199  $field->checkTitlesForImport($titles, $import_fields);
200  }
201  }
202 
203  foreach ($titles as $key => $value) {
206  foreach ($importable_titles as $identifier => $values) {
207  if (in_array($value, $values)) {
208  $std_field = new ilDclStandardField();
209  $std_field->setId(substr($identifier, 4));
210  $import_fields[$key] = $std_field;
211  continue 2;
212  }
213  }
214  if (in_array($value, $not_importable_titles)) {
215  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
216  } else {
217  if (!isset($import_fields[$key])) {
218  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
219  }
220  }
221  }
222 
223  return $import_fields;
224  }
getRecordFields()
Returns all fields of this table which are NOT standard fields.
string $key
Consumer key/client ID value.
Definition: System.php:193
checkImportType(ilDclBaseFieldModel $field)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ import()

ilDclContentImporter::import ( string  $file,
bool  $simulate = false 
)
Exceptions
ilException

Definition at line 75 of file class.ilDclContentImporter.php.

References Vendor\Package\$e, $warnings, ilDataCollectionImporter\getExcelCharForInteger(), getImportFieldsFromTitles(), IL_CAL_UNIX, ILIAS\Repository\lng(), and ILIAS\Repository\user().

Referenced by ilDclRecordListGUI\importRecords().

75  : array
76  {
77  $this->warnings = [];
78  $excel = new ilExcel();
79  try {
80  $excel->loadFromFile($file);
81  } catch (Exception) {
82  $this->warnings[] = $this->lng->txt("dcl_file_not_readable");
83  }
84 
85  $sheet_count = $excel->getSheetCount();
86  $excel->setActiveSheet(0);
87 
88  if ($sheet_count != count($this->tables)) {
89  $this->warnings[] = $this->lng->txt('dcl_file_not_readable');
90  }
91 
92  if (count($this->warnings)) {
93  return ['line' => 0, 'warnings' => $this->warnings];
94  }
95 
96  $i = 0;
97 
98  for ($sheet = 0; $sheet < $sheet_count; $sheet++) {
99  $excel->setActiveSheet($sheet);
100  $table = $this->tables[$sheet];
101 
102  // only 31 character-long table-titles are allowed
103  $sheet_title = substr($table->getTitle(), 0, 31);
104  if ($excel->getSheetTitle() != $sheet_title) {
105  $this->warnings[] = $this->lng->txt('dcl_table_title_not_matching');
106  continue;
107  }
108 
109  $field_names = [];
110  $sheet_data = $excel->getSheetAsArray();
111 
112  foreach ($sheet_data[0] as $column) {
113  $field_names[] = $column;
114  }
115  $fields = $this->getImportFieldsFromTitles($table, $field_names);
116 
117  $records_failed = 0;
118  for ($i = 2; $i <= count($sheet_data); $i++) {
119  $record = new ilDclBaseRecordModel();
120  $record->setOwner($this->user->getId());
121  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
122  $record->setCreateDate($date_obj);
123  $record->setLastUpdate($date_obj);
124  $record->setLastEditBy($this->user->getId());
125  $record->setTableId($table->getId());
126  if (!$simulate) {
127  $record->doCreate();
128  }
129  $fields_failed = 0;
130  foreach ($fields as $col => $field) {
131  try {
132  if ($field->isStandardField()) {
133  $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
134  } else {
135  $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
136 
137  if (is_array($value) && isset($value['warning'])) {
138  $this->warnings[] = $value['warning'];
139  $value = '';
140  }
141 
142  $field->checkValidity($value, $record->getId());
143  if (!$simulate) {
144  $record->setRecordFieldValue($field->getId(), $value);
145  }
146  }
147  } catch (ilDclInputException $e) {
148  $fields_failed++;
149  $this->warnings[] = "(" . $i . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $e;
150  }
151  }
152 
153  if ($fields_failed < count($fields)) {
154  $record_imported = true;
155  } else {
156  $records_failed++;
157  $record_imported = false;
158  }
159 
160  if (!$simulate) {
161  if (!$record_imported) { // if no fields have been filled, delete the record again
162  $record->doDelete(true); // omit notification
163  } else {
164  $record->doUpdate();
165  }
166  }
167  if (($i - 1) - $records_failed > $this->max_imports) {
168  $this->warnings[] = $this->lng->txt("dcl_max_import") . (count($sheet_data) - 1) . " > " . $this->max_imports;
169  break;
170  }
171  }
172  }
173 
174  return ['line' => (max($i - 2, 0)), 'warnings' => $this->warnings];
175  }
const IL_CAL_UNIX
getImportFieldsFromTitles(ilDclTable $table, array $titles)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $dcl

ilObjDataCollection ilDclContentImporter::$dcl
protected

Definition at line 47 of file class.ilDclContentImporter.php.

◆ $lng

ilLanguage ilDclContentImporter::$lng
protected

Definition at line 53 of file class.ilDclContentImporter.php.

◆ $max_imports

int ilDclContentImporter::$max_imports = 100
protected

Definition at line 26 of file class.ilDclContentImporter.php.

◆ $ref_id

int ilDclContentImporter::$ref_id
protected

Ref-ID of DataCollection.

Definition at line 41 of file class.ilDclContentImporter.php.

Referenced by __construct().

◆ $supported_import_datatypes

◆ $table_id

int ilDclContentImporter::$table_id
protected

Table-Id for export.

Definition at line 45 of file class.ilDclContentImporter.php.

Referenced by __construct().

◆ $tables

array ilDclContentImporter::$tables
protected

Definition at line 51 of file class.ilDclContentImporter.php.

◆ $user

ilObjUser ilDclContentImporter::$user
protected

Definition at line 54 of file class.ilDclContentImporter.php.

◆ $warnings

array ilDclContentImporter::$warnings
protected

Definition at line 37 of file class.ilDclContentImporter.php.

Referenced by import().

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xlsx'

Definition at line 25 of file class.ilDclContentImporter.php.


The documentation for this class was generated from the following file: