ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 require_once('./Modules/DataCollection/classes/class.ilObjDataCollectionAccess.php');
6 require_once('./Modules/DataCollection/classes/class.ilObjDataCollectionGUI.php');
7 require_once('./Modules/DataCollection/classes/Content/class.ilDclRecordListGUI.php');
8 require_once('./Modules/DataCollection/classes/Table/class.ilDclTable.php');
9 require_once ('./Services/Export/classes/class.ilExport.php');
10 require_once ('./Services/Excel/classes/class.ilExcel.php');
11 
12 
21 {
22  //const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
23 
24  const EXPORT_EXCEL = 'xls';
25 
29  protected $max_imports = 100;
30 
41  );
42 
43  protected $warnings;
44 
48  protected $ref_id;
49 
53  protected $table_id;
54 
58  protected $dcl;
59 
63  protected $tables;
64 
68  protected $lng;
69 
70  public function __construct($ref_id, $table_id = null) {
71  global $DIC;
72  $lng = $DIC['lng'];
73 
74  $this->ref_id = $ref_id;
75  $this->table_id = $table_id;
76 
77  $this->lng = $lng;
78 
79  $this->dcl = new ilObjDataCollection($ref_id);
80  $this->tables = ($table_id)? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
81  }
82 
83  public function import($file, $simulate = false) {
84  global $DIC;
85  $ilUser = $DIC['ilUser'];
86 
87  $this->warnings = array();
88  try {
89  $excel = new ilExcel();
90  $excel->loadFromFile($file);
91  } catch (Exception $e) {
92  $this->warnings[] = $this->lng->txt("dcl_file_not_readable");
93  }
94 
95  $sheet_count = $excel->getSheetCount();
96  $excel->setActiveSheet(0);
97 
98  if($sheet_count != count($this->tables)) {
99  $this->warnings[] = $this->lng->txt('dcl_file_not_readable');
100  }
101 
102  if (count($this->warnings)) {
103  return array('line'=>0, 'warnings'=>$this->warnings);
104  }
105 
106  for($sheet = 0; $sheet < $sheet_count; $sheet++) {
107  $excel->setActiveSheet($sheet);
108  $table = $this->tables[$sheet];
109 
110  // only 31 character-long table-titles are allowed
111  $sheet_title = substr($table->getTitle(), 0, 31);
112  if($excel->getSheetTitle() != $sheet_title) {
113  $this->warnings[] = $this->lng->txt('dcl_table_title_not_matching');
114  continue;
115  }
116 
117  $field_names = array();
118  $sheet_data = $excel->getSheetAsArray();
119 
120  foreach ($sheet_data[0] as $column) {
121  $field_names[] = $column;
122  }
123  $fields = $this->getImportFieldsFromTitles($table, $field_names);
124 
125  $records_failed = 0;
126  for ($i = 2; $i <= count($sheet_data); $i ++) {
127  $record = new ilDclBaseRecordModel();
128  $record->setOwner($ilUser->getId());
129  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
130  $record->setCreateDate($date_obj->get(IL_CAL_DATETIME));
131  $record->setTableId($table->getId());
132  if (!$simulate) {
133  $record->doCreate();
134  }
135  $fields_failed = 0;
136  foreach ($fields as $col => $field) {
137  try {
138  $value = $record->getRecordFieldValueFromExcel($excel, $i, $col, $field);
139 
140  if (is_array($value) && isset($value['warning'])) {
141  $this->warnings[] = $value['warning'];
142  $value = '';
143  }
144 
145  $field->checkValidity($value, $record->getId());
146  if (!$simulate) {
147  $record->setRecordFieldValue($field->getId(), $value);
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 
177  return array('line'=>($i-2 < 0 ? 0 : $i-2), 'warnings'=>$this->warnings);
178  }
179 
185  protected function checkImportType($field) {
186  if (in_array($field->getDatatypeId(), $this->supported_import_datatypes)) {
187  return true;
188  } else {
189  $this->warnings[] = $field->getTitle() . ": " . $this->lng->txt("dcl_not_supported_in_import");
190 
191  return false;
192  }
193  }
194 
195 
202  protected function getImportFieldsFromTitles($table, $titles) {
203  $fields = $table->getRecordFields();
204  $import_fields = array();
205  foreach ($fields as $field) {
206  if ($this->checkImportType($field)) {
207  // the fields will add themselves to $import_fields if their title is in $titles
208  $field->checkTitlesForImport($titles, $import_fields);
209  }
210  }
211 
212  foreach ($titles as $key => $value) {
214  if (in_array($value, $std_field_titles)) {
215  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key) . ") \"" . $value . "\" " . $this->lng->txt("dcl_std_field_not_importable");
216  } else if (!isset($import_fields[$key])) {
217  $this->warnings[] = "(1, " . ilDataCollectionImporter::getExcelCharForInteger($key+1) . ") \"" . $value . "\" " . $this->lng->txt("dcl_row_not_found");
218  }
219  }
220 
221  return $import_fields;
222  }
223 }
224 
225 ?>
const IL_CAL_DATETIME
__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...
$column
Definition: 39dropdown.php:62
Date and time handling
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
Class ilDclBaseRecordModel.
global $DIC
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
Class ilObjDataCollection.
getImportFieldsFromTitles($table, $titles)