Use DataImport class.

1.10.x
Julio Montoya 10 years ago
parent f46b72ddd8
commit 9374e43095
  1. 3
      composer.json
  2. 38
      main/course_progress/thematic_controller.php
  3. 84
      main/inc/lib/import.lib.php

@ -67,7 +67,8 @@
"bower-asset/modernizr": "2.8.*",
"bower-asset/jqueryui-timepicker-addon": "1.5.*",
"ramsey/array_column": "~1.1",
"patchwork/utf8": "~1.2"
"patchwork/utf8": "~1.2",
"ddeboer/data-import": "@stable"
},
"require-dev": {
"behat/behat": "2.5.*@stable",

@ -113,7 +113,7 @@ class ThematicController
case 'thematic_import_select':
break;
case 'thematic_import':
$csv_import_array = Import::csv_to_array($_FILES['file']['tmp_name'], 'horizontal');
$csv_import_array = Import::csv_to_array($_FILES['file']['tmp_name']);
if (isset($_POST['replace']) && $_POST['replace']) {
// Remove current thematic.
@ -126,22 +126,35 @@ class ThematicController
// Import the progress.
$current_thematic = null;
foreach ($csv_import_array as $data) {
foreach ($data as $key => $item) {
if ($key == 'title') {
$thematic->set_thematic_attributes(null, $item[0], $item[1], api_get_session_id());
foreach ($csv_import_array as $item) {
$key = $item['type'];
switch ($key) {
case 'title':
$thematic->set_thematic_attributes(null, $item['data1'], $item['data2'], api_get_session_id());
$current_thematic = $thematic->thematic_save();
$description_type = 0;
}
if ($key == 'plan') {
$thematic->set_thematic_plan_attributes($current_thematic, $item[0], $item[1], $description_type);
break;
case 'plan':
$thematic->set_thematic_plan_attributes(
$current_thematic,
$item['data1'],
$item['data2'],
$description_type
);
$thematic->thematic_plan_save();
$description_type++;
}
if ($key == 'progress') {
$thematic->set_thematic_advance_attributes(null, $current_thematic, 0, $item[2], $item[0], $item[1]);
break;
case 'progress':
$thematic->set_thematic_advance_attributes(
null,
$current_thematic,
0,
$item['data3'],
$item['data1'],
$item['data2']
);
$thematic->thematic_advance_save();
}
break;
}
}
$action = 'thematic_details';
@ -149,6 +162,7 @@ class ThematicController
case 'thematic_export':
$list = $thematic->get_thematic_list();
$csv = array();
$csv[] = array('type', 'data1', 'data2', 'data3');
foreach ($list as $theme) {
$csv[] = array('title', $theme['title'], $theme['content']);
$data = $thematic->get_thematic_plan_data($theme['id']);

@ -1,6 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\Writer\ArrayWriter;
/**
* Class Import
* This class provides some functions which can be used when importing data from
@ -15,9 +19,16 @@ class Import
* @param string $path
* @return \CsvReader
*/
static function csv_reader($path)
static function csv_reader($path, $setFirstRowAsHeader = true)
{
return new CsvReader($path);
$file = new \SplFileObject($path);
$csvReader = new CsvReader($file, ';');
if ($setFirstRowAsHeader) {
$csvReader->setHeaderRowNumber(0);
}
return $csvReader;
}
/**
@ -40,68 +51,15 @@ class Import
*
* @deprecated use cvs_reader instead
*/
static function csv_to_array($filename, $csv_order = 'vertical') {
$result = array();
// Encoding detection.
$handle = fopen($filename, 'r');
if ($handle === false) {
return $result;
}
$buffer = array();
$i = 0;
while (!feof($handle) && $i < 200) {
// We assume that 200 lines are enough for encoding detection.
$buffer[] = fgets($handle);
$i++;
}
fclose($handle);
$buffer = implode("\n", $buffer);
$from_encoding = api_detect_encoding($buffer);
unset($buffer);
// Reading the file, parsing and importing csv data.
$handle = fopen($filename, 'r');
if ($handle === false) {
return $result;
}
if ($csv_order == 'vertical') {
$keys = api_fgetcsv($handle, null, ';');
foreach ($keys as $key => &$key_value) {
$key_value = api_to_system_encoding($key_value, $from_encoding);
}
}
static function csv_to_array($filename)
{
$csvReader = self::csv_reader($filename);
while (($row_tmp = api_fgetcsv($handle, null, ';')) !== false) {
$row = array();
// Avoid empty lines in csv
if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
if (!is_null($row_tmp[0])) {
if ($csv_order == 'vertical') {
foreach ($row_tmp as $index => $value) {
$row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
}
} else {
$first = null;
$count = 1;
foreach ($row_tmp as $index => $value) {
if ($count == 1) {
$first = $value;
} else {
$row[$first][] = api_to_system_encoding($value, $from_encoding);
}
$count++;
}
}
$result[] = $row;
}
}
}
$workflow = new Workflow($csvReader);
$resultArray = [];
$writer = new ArrayWriter($resultArray);
$result = $workflow->addWriter($writer)->process();
fclose($handle);
return $result;
return $resultArray;
}
}

Loading…
Cancel
Save