ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
5 
6 
15 {
16  //const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
17 
18  const EXPORT_EXCEL = 'xlsx';
19 
23  protected $max_imports = 100;
24 
35  ilDclDataType::INPUTFORMAT_TEXT_SELECTION,
37  );
38 
39  protected $warnings;
40 
44  protected $ref_id;
45 
49  protected $table_id;
50 
54  protected $dcl;
55 
59  protected $tables;
60 
64  protected $lng;
65 
66  public function __construct($ref_id, $table_id = null)
67  {
68  global $DIC;
69  $lng = $DIC['lng'];
70 
71  $this->ref_id = $ref_id;
72  $this->table_id = $table_id;
73 
74  $this->lng = $lng;
75 
76  $this->dcl = new ilObjDataCollection($ref_id);
77  $this->tables = ($table_id)? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
78  }
79 
80  public function import($file, $simulate = false)
81  {
82  global $DIC;
83  $ilUser = $DIC['ilUser'];
84 
85  $this->warnings = array();
86  try {
87  $excel = new ilExcel();
88  $excel->loadFromFile($file);
89  } catch (Exception $e) {
90  $this->warnings[] = $this->lng->txt("dcl_file_not_readable");
91  }
92 
93  $sheet_count = $excel->getSheetCount();
94  $excel->setActiveSheet(0);
95 
96  if ($sheet_count != count($this->tables)) {
97  $this->warnings[] = $this->lng->txt('dcl_file_not_readable');
98  }
99 
100  if (count($this->warnings)) {
101  return array('line'=>0, 'warnings'=>$this->warnings);
102  }
103 
104  for ($sheet = 0; $sheet < $sheet_count; $sheet++) {
105  $excel->setActiveSheet($sheet);
106  $table = $this->tables[$sheet];
107 
108  // only 31 character-long table-titles are allowed
109  $sheet_title = substr($table->getTitle(), 0, 31);
110  if ($excel->getSheetTitle() != $sheet_title) {
111  $this->warnings[] = $this->lng->txt('dcl_table_title_not_matching');
112  continue;
113  }
114 
115  $field_names = array();
116  $sheet_data = $excel->getSheetAsArray();
117 
118  foreach ($sheet_data[0] as $column) {
119  $field_names[] = $column;
120  }
121  $fields = $this->getImportFieldsFromTitles($table, $field_names);
122 
123  $records_failed = 0;
124  for ($i = 2; $i <= count($sheet_data); $i++) {
125  $record = new ilDclBaseRecordModel();
126  $record->setOwner($ilUser->getId());
127  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
128  $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
129  $record->setTableId($table->getId());
130  if (!$simulate) {
131  $record->doCreate();
132  }
133  $fields_failed = 0;
134  foreach ($fields as $col => $field) {
135  try {
136  if ($field->isStandardField()) {
137  $record->setStandardFieldValueFromExcel($excel, $i, $col, $field);
138  } else {
139  $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
140 
141  if (is_array($value) && isset($value['warning'])) {
142  $this->warnings[] = $value['warning'];
143  $value = '';
144  }
145 
146  $field->checkValidity($value, $record->getId());
147  if (!$simulate) {
148  $record->setRecordFieldValue($field->getId(), $value);
149  }
150  }
151  } catch (ilDclInputException $e) {
152  $fields_failed++;
153  $this->warnings[] = "(" . $i . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $e;
154  }
155  }
156 
157  if ($fields_failed < count($fields)) {
158  $record_imported = true;
159  } else {
160  $records_failed++;
161  $record_imported = false;
162  }
163 
164  if (!$simulate) {
165  if (!$record_imported) { // if no fields have been filled, delete the record again
166  $record->doDelete(true); // omit notification
167  } else {
168  $record->doUpdate();
169  }
170  }
171  if (($i - 1) - $records_failed > $this->max_imports) {
172  $this->warnings[] = $this->lng->txt("dcl_max_import") . (count($sheet_data) - 1) . " > " . $this->max_imports;
173  break;
174  }
175  }
176  }
177 
178 
179  return array('line'=>($i-2 < 0 ? 0 : $i-2), 'warnings'=>$this->warnings);
180  }
181 
187  protected function checkImportType($field)
188  {
189  if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
190  return true;
191  } else {
192  $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
193 
194  return false;
195  }
196  }
197 
198 
205  protected function getImportFieldsFromTitles($table, $titles)
206  {
207  $fields = $table->getRecordFields();
208  $import_fields = array();
209  foreach ($fields as $field) {
210  if ($this->checkImportType($field)) {
211  // the fields will add themselves to $import_fields (at the correct position) if their title is in $titles
212  $field->checkTitlesForImport($titles, $import_fields);
213  }
214  }
215 
216  foreach ($titles as $key => $value) {
219  foreach ($importable_titles as $identifier => $values) {
220  if (in_array($value, $values)) {
221  $std_field = new ilDclStandardField();
222  $std_field->setId(substr($identifier, 4));
223  $import_fields[$key] = $std_field;
224  continue 2;
225  }
226  }
227  if (in_array($value, $not_importable_titles)) {
228  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
229  } elseif (!isset($import_fields[$key])) {
230  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key+1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
231  }
232  }
233 
234  return $import_fields;
235  }
236 }
const IL_CAL_DATETIME
global $DIC
Definition: saml.php:7
__construct($ref_id, $table_id=null)
Class ilDclBaseFieldModel.
const IL_CAL_UNIX
Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by r...
const INPUTFORMAT_DATE_SELECTION
$column
Definition: 39dropdown.php:62
Date and time handling
$ilUser
Definition: imgupload.php:18
Class ilDclBaseFieldModel.
Create styles array
The data for the language used.
Class ilDclBaseRecordModel.
$i
Definition: disco.tpl.php:19
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
if(empty($password)) $table
Definition: pwgen.php:24
$key
Definition: croninfo.php:18
Class ilObjDataCollection.
getImportFieldsFromTitles($table, $titles)