/** * Import records from Excel file * * @param $file * @param bool $simulate */ private function importRecords($file, $simulate = false) { global $ilUser, $lng; include_once "./Modules/DataCollection/libs/ExcelReader/excel_reader2.php"; $warnings = array(); try { $excel = new Spreadsheet_Excel_Reader($file); } catch (Exception $e) { $warnings[] = $lng->txt("dcl_file_not_readable"); } if (count($warnings)) { $this->endImport(0, $warnings); return; } $field_names = array(); for ($i = 1; $i <= $excel->colcount(); $i++) { $field_names[$i] = $excel->val(1, $i); } $fields = $this->getImportFieldsFromTitles($field_names, $warnings); for ($i = 2; $i <= $excel->rowcount(); $i++) { $record = new ilDataCollectionRecord(); $record->setTableId($this->table_obj->getId()); $record->setOwner($ilUser->getId()); $date_obj = new ilDateTime(time(), IL_CAL_UNIX); $record->setCreateDate($date_obj->get(IL_CAL_DATETIME)); $record->setTableId($this->table_id); if (!$simulate) { $record->doCreate(); } foreach ($fields as $col => $field) { $value = $excel->val($i, $col); $value = utf8_encode($value); try { if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) { $old = $value; $value = $this->getReferenceFromValue($field, $value); if (!$value) { $warnings[] = "(" . $i . ", " . $this->getExcelCharForInteger($col) . ") " . $lng->txt("dcl_no_such_reference") . " " . $old; } } else { if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) { $value = array('date' => date('Y-m-d', strtotime($value)), 'time' => '00:00:00'); } } $field->checkValidity($value, $record->getId()); if (!$simulate) { $record->setRecordFieldValue($field->getId(), $value); } } catch (ilDataCollectionInputException $e) { $warnings[] = "(" . $i . ", " . $this->getExcelCharForInteger($col) . ") " . $e; } } if (!$simulate) { $record->doUpdate(); } if ($i - 1 > $this->max_imports) { $warnings[] = $lng->txt("dcl_max_import") . ($excel->rowcount() - 1) . " > " . $this->max_imports; break; } } $this->endImport($i - 2, $warnings); }