ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilDclContentImporter Class Reference
+ Collaboration diagram for ilDclContentImporter:

Public Member Functions

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

Data Fields

const EXPORT_EXCEL = 'xlsx'
 
const array SUPPORTED_IMPORT_DATATYPES
 

Protected Member Functions

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

Protected Attributes

int $max_imports = 100
 
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 59 of file class.ilDclContentImporter.php.

60 {
61 global $DIC;
62
63 $this->ref_id = $ref_id;
64 $this->table_id = $table_id;
65
66 $this->lng = $DIC->language();
67 $this->user = $DIC->user();
68
69 $this->dcl = new ilObjDataCollection($ref_id);
70 $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
71 }
int $ref_id
Ref-ID of DataCollection.
int $table_id
Table-Id for export.
global $DIC
Definition: shib_login.php:26

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

+ Here is the call graph for this function:

Member Function Documentation

◆ checkImportType()

ilDclContentImporter::checkImportType ( ilDclBaseFieldModel  $field)
protected

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

200 : bool
201 {
202 if (in_array($field->getDatatypeId(), $this::SUPPORTED_IMPORT_DATATYPES)) {
203 return true;
204 } else {
205 $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
206
207 return false;
208 }
209 }

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

Referenced by getImportFieldsFromTitles().

+ 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 215 of file class.ilDclContentImporter.php.

215 : array
216 {
217 $fields = $table->getRecordFields();
218 $import_fields = [];
219 foreach ($fields as $field) {
220 if ($this->checkImportType($field)) {
221 // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
222 $field->checkTitlesForImport($titles, $import_fields);
223 }
224 }
225
226 foreach ($titles as $key => $value) {
229 foreach ($importable_titles as $identifier => $values) {
230 if (in_array($value, $values)) {
231 $std_field = new ilDclStandardField();
232 $std_field->setId(substr($identifier, 4));
233 $import_fields[$key] = $std_field;
234 continue 2;
235 }
236 }
237 if (in_array($value, $not_importable_titles)) {
238 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
239 } else {
240 if (!isset($import_fields[$key])) {
241 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
242 }
243 }
244 }
245
246 return $import_fields;
247 }
checkImportType(ilDclBaseFieldModel $field)
getRecordFields()
Returns all fields of this table which are NOT standard fields.

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

Referenced by import().

+ 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
ilDateTimeException

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

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

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

Referenced by ilDclRecordListGUI\importRecords().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendImportTemplate()

ilDclContentImporter::sendImportTemplate ( )

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

179 : void
180 {
181 $adapter = new ilExcel();
182 $exporter = new ilDclContentExporter($this->ref_id, $this->table_id);
183
184 foreach ($this->tables as $table) {
185 $adapter->addSheet($table->getTitle());
186 $row = 1;
187 $col = 0;
188 foreach ($table->getRecordFields() as $field) {
189 if (in_array($field->getDatatypeId(), $this::SUPPORTED_IMPORT_DATATYPES, true)) {
190 $field->fillHeaderExcel($adapter, $row, $col);
191 }
192 }
193 }
194
195 $basename = $this->tables[0]->getTitle() . '_import_template';
196 $filename = $exporter->sanitizeFilename($basename);
197 $adapter->sendToClient($filename);
198 }
$filename
Definition: buildRTE.php:78

References ILIAS\TestQuestionPool\ExportImport\Foundation\Export\$exporter, and $filename.

Referenced by ilDclRecordListGUI\downloadImportTemplate().

+ Here is the caller graph for this function:

Field Documentation

◆ $dcl

ilObjDataCollection ilDclContentImporter::$dcl
protected

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

◆ $lng

ilLanguage ilDclContentImporter::$lng
protected

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

◆ $max_imports

int ilDclContentImporter::$max_imports = 100
protected

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

◆ $ref_id

int ilDclContentImporter::$ref_id
protected

Ref-ID of DataCollection.

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

Referenced by __construct().

◆ $table_id

int ilDclContentImporter::$table_id
protected

Table-Id for export.

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

Referenced by __construct().

◆ $tables

array ilDclContentImporter::$tables
protected

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

◆ $user

ilObjUser ilDclContentImporter::$user
protected

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

◆ $warnings

array ilDclContentImporter::$warnings
protected

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

Referenced by import().

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xlsx'

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

◆ SUPPORTED_IMPORT_DATATYPES


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