ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDclContentImporter.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
13{
14
15 //const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
16
17 const EXPORT_EXCEL = 'xlsx';
21 protected $max_imports = 100;
26 = array(
33 ilDclDataType::INPUTFORMAT_TEXT_SELECTION,
35 );
36 protected $warnings;
40 protected $ref_id;
44 protected $table_id;
48 protected $dcl;
52 protected $tables;
56 protected $lng;
57
58
59 public function __construct($ref_id, $table_id = null)
60 {
61 global $DIC;
62 $lng = $DIC['lng'];
63
64 $this->ref_id = $ref_id;
65 $this->table_id = $table_id;
66
67 $this->lng = $lng;
68
69 $this->dcl = new ilObjDataCollection($ref_id);
70 $this->tables = ($table_id) ? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
71 }
72
73
74 public function import($file, $simulate = false)
75 {
76 global $DIC;
77 $ilUser = $DIC['ilUser'];
78
79 $this->warnings = array();
80 try {
81 $excel = new ilExcel();
82 $excel->loadFromFile($file);
83 } catch (Exception $e) {
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 array('line' => 0, 'warnings' => $this->warnings);
96 }
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 = array();
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($ilUser->getId());
121 $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
122 $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
123 $record->setTableId($table->getId());
124 if (!$simulate) {
125 $record->doCreate();
126 }
127 $fields_failed = 0;
128 foreach ($fields as $col => $field) {
129 try {
130 if ($field->isStandardField()) {
131 $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
132 } else {
133 $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
134
135 if (is_array($value) && isset($value['warning'])) {
136 $this->warnings[] = $value['warning'];
137 $value = '';
138 }
139
140 $field->checkValidity($value, $record->getId());
141 if (!$simulate) {
142 $record->setRecordFieldValue($field->getId(), $value);
143 }
144 }
145 } catch (ilDclInputException $e) {
146 $fields_failed++;
147 $this->warnings[] = "(" . $i . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $e;
148 }
149 }
150
151 if ($fields_failed < count($fields)) {
152 $record_imported = true;
153 } else {
154 $records_failed++;
155 $record_imported = false;
156 }
157
158 if (!$simulate) {
159 if (!$record_imported) { // if no fields have been filled, delete the record again
160 $record->doDelete(true); // omit notification
161 } else {
162 $record->doUpdate();
163 }
164 }
165 if (($i - 1) - $records_failed > $this->max_imports) {
166 $this->warnings[] = $this->lng->txt("dcl_max_import") . (count($sheet_data) - 1) . " > " . $this->max_imports;
167 break;
168 }
169 }
170 }
171
172 return array('line' => ($i - 2 < 0 ? 0 : $i - 2), 'warnings' => $this->warnings);
173 }
174
175
181 protected function checkImportType($field)
182 {
183 if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
184 return true;
185 } else {
186 $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
187
188 return false;
189 }
190 }
191
192
199 protected function getImportFieldsFromTitles($table, $titles)
200 {
201 $fields = $table->getRecordFields();
202 $import_fields = array();
203 foreach ($fields as $field) {
204 if ($this->checkImportType($field)) {
205 // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
206 $field->checkTitlesForImport($titles, $import_fields);
207 }
208 }
209
210 foreach ($titles as $key => $value) {
213 foreach ($importable_titles as $identifier => $values) {
214 if (in_array($value, $values)) {
215 $std_field = new ilDclStandardField();
216 $std_field->setId(substr($identifier, 4));
217 $import_fields[$key] = $std_field;
218 continue 2;
219 }
220 }
221 if (in_array($value, $not_importable_titles)) {
222 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
223 } else {
224 if (!isset($import_fields[$key])) {
225 $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key + 1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
226 }
227 }
228 }
229
230 return $import_fields;
231 }
232}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_DATETIME
@classDescription Date and time handling
Class ilDclBaseRecordModel.
Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by r...
getImportFieldsFromTitles($table, $titles)
__construct($ref_id, $table_id=null)
const INPUTFORMAT_DATE_SELECTION
Class ilDclBaseFieldModel.
Class ilDclBaseFieldModel.
Class ilObjDataCollection.
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
if(empty($password)) $table
Definition: pwgen.php:24
global $DIC
Definition: saml.php:7
$values
$ilUser
Definition: imgupload.php:18