ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilDclContentImporter Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ 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

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning

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

Constructor & Destructor Documentation

◆ __construct()

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

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

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

56  {
57  global $DIC;
58 
59  $this->ref_id = $ref_id;
60  $this->table_id = $table_id;
61 
62  $this->lng = $DIC->language();
63  $this->user = $DIC->user();
64 
65  $this->dcl = new ilObjDataCollection($ref_id);
66  $this->tables = ($table_id) ? [$this->dcl->getTableById($table_id)] : $this->dcl->getTables();
67  }
int $ref_id
Ref-ID of DataCollection.
global $DIC
Definition: feed.php:28
int $table_id
Table-Id for export.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
+ Here is the call graph for this function:

Member Function Documentation

◆ checkImportType()

ilDclContentImporter::checkImportType ( ilDclBaseFieldModel  $field)
protected

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

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

Referenced by getImportFieldsFromTitles().

174  : bool
175  {
176  if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
177  return true;
178  } else {
179  $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
180 
181  return false;
182  }
183  }
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 189 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().

189  : array
190  {
191  $fields = $table->getRecordFields();
192  $import_fields = array();
193  foreach ($fields as $field) {
194  if ($this->checkImportType($field)) {
195  // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
196  $field->checkTitlesForImport($titles, $import_fields);
197  }
198  }
199 
200  foreach ($titles as $key => $value) {
203  foreach ($importable_titles as $identifier => $values) {
204  if (in_array($value, $values)) {
205  $std_field = new ilDclStandardField();
206  $std_field->setId(substr($identifier, 4));
207  $import_fields[$key] = $std_field;
208  continue 2;
209  }
210  }
211  if (in_array($value, $not_importable_titles)) {
212  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
213  } else {
214  if (!isset($import_fields[$key])) {
215  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
216  }
217  }
218  }
219 
220  return $import_fields;
221  }
getRecordFields()
Returns all fields of this table which are NOT standard fields.
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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 74 of file class.ilDclContentImporter.php.

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

Referenced by ilDclRecordListGUI\importRecords().

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

◆ $lng

ilLanguage ilDclContentImporter::$lng
protected

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

◆ $max_imports

int ilDclContentImporter::$max_imports = 100
protected

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

◆ $ref_id

int ilDclContentImporter::$ref_id
protected

Ref-ID of DataCollection.

Definition at line 40 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 44 of file class.ilDclContentImporter.php.

Referenced by __construct().

◆ $tables

array ilDclContentImporter::$tables
protected

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

◆ $user

ilObjUser ilDclContentImporter::$user
protected

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

◆ $warnings

array ilDclContentImporter::$warnings
protected

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

Referenced by import().

◆ EXPORT_EXCEL

const ilDclContentImporter::EXPORT_EXCEL = 'xlsx'

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


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