From 85c8111bde0c02ba2b41af25c2f47a9fad53eb16 Mon Sep 17 00:00:00 2001 From: andrie Date: Tue, 21 Aug 2012 08:55:26 +0100 Subject: [PATCH] Fixed issue 06189: Export to R - csv file uses incorrect quote character Dev modified SPSSExportData() to use either 'single quotes' or "double quotes" as quoting character Dev modified SPSSExportData() to print column headers if export to R Dev modified exportr() to use R code read.csv() rather than read.table() --- application/controllers/admin/export.php | 12 +++--- application/helpers/export_helper.php | 48 ++++++++++++++++-------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/application/controllers/admin/export.php b/application/controllers/admin/export.php index c6b87016082..2df3ae83a77 100644 --- a/application/controllers/admin/export.php +++ b/application/controllers/admin/export.php @@ -624,11 +624,11 @@ public function exportr() //$typeMap = $this->_getTypeMap(); - $length_vallabel = '120'; // Set the max text length of Value Labels +// $length_vallabel = '120'; // Set the max text length of Value Labels $iLength = '25500'; // Set the max text length of Text Data $length_varlabel = '25500'; // Set the max text length of Variable Labels $headerComment = ''; - $tempFile = ''; +// $tempFile = ''; if ( ! isset($iSurveyID) ) { $iSurveyID = returnGlobal('sid'); } $filterstate = incompleteAnsFilterState(); @@ -679,7 +679,7 @@ public function exportr() header("Pragma: public"); $na = ""; //change to empty string instead of two double quotes to fix warnings on NA - SPSSExportData($iSurveyID, $iLength); + SPSSExportData($iSurveyID, $iLength, $na='', $q='"', $header=TRUE); exit; } @@ -692,11 +692,9 @@ public function exportr() header("Pragma: public"); echo $headerComment; - echo "data <- read.table(\"survey_" . $iSurveyID - ."_R_data_file.csv\", sep=\",\", quote = \"'\", " - ."na.strings=c(\"\",\"\\\"\\\"\"), " - ."stringsAsFactors=FALSE)\n\n"; + echo ('data <- read.csv("survey_' . $iSurveyID .'_R_data_file.csv", na.strings=c(",", "\"\""), stringsAsFactors=FALSE)'); + echo ("\n\n"); // Build array that has to be returned $fields = SPSSFieldMap($iSurveyID,"V"); diff --git a/application/helpers/export_helper.php b/application/helpers/export_helper.php index 4528ec42200..4a2d4d5d952 100644 --- a/application/helpers/export_helper.php +++ b/application/helpers/export_helper.php @@ -54,8 +54,10 @@ function isNumericExtended($value) { * @param mixed $iSurveyID The survey ID * @param mixed $iLength Maximum text lenght data, usually 255 for SPSS $num_fields) safeDie("Database inconsistency error"); + + // Add column headers (used by R export) + if($header==TRUE) + { + $i = 1; + foreach ($fields as $field) { + echo $q.strtoupper($field['sql_name']).$q; + if ($i<$num_fields && !$field['hide']) echo ','; + $i++; + } + echo("\n"); + } + foreach ($result as $row) { $row = array_change_key_case($row,CASE_UPPER); @@ -84,7 +99,7 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { list( $year, $month, $day, $hour, $minute, $second ) = preg_split( '([^0-9])', $row[$fieldno] ); if ($year != '' && (int)$year >= 1970) { - echo "'".date('d-m-Y H:i:s', mktime( $hour, $minute, $second, $month, $day, $year ) )."'"; + echo $q.date('d-m-Y H:i:s', mktime( $hour, $minute, $second, $month, $day, $year ) ).$q; } else { echo ($na); @@ -97,9 +112,9 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { { if ($row[$fieldno] == 'Y') // Yes/No Question Type { - echo( "'1'"); + echo( $q. 1 .$q); } else if ($row[$fieldno] == 'N'){ - echo( "'2'"); + echo( $q. 2 .$q); } else { echo($na); } @@ -107,9 +122,9 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { { if ($row[$fieldno] == 'F') { - echo( "'1'"); + echo( $q. 1 .$q); } else if ($row[$fieldno] == 'M'){ - echo( "'2'"); + echo( $q. 2 .$q); } else { echo($na); } @@ -117,11 +132,11 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { { if ($row[$fieldno] == 'Y') { - echo( "'1'"); + echo( $q. 1 .$q); } else if ($row[$fieldno] == 'N'){ - echo( "'2'"); + echo( $q. 2 .$q); } else if ($row[$fieldno] == 'U'){ - echo( "'3'"); + echo( $q. 3 .$q); } else { echo($na); } @@ -129,11 +144,11 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { { if ($row[$fieldno] == 'I') { - echo( "'1'"); + echo( $q. 1 .$q); } else if ($row[$fieldno] == 'S'){ - echo( "'2'"); + echo( $q. 2 .$q); } else if ($row[$fieldno] == 'D'){ - echo( "'3'"); + echo( $q. 3 .$q); } else { echo($na); } @@ -141,22 +156,23 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') { { if ($row[$fieldno] == 'Y') { - echo("'1'"); + echo($q. 1 .$q); } else { - echo("'0'"); + echo($q. 0 .$q); } } elseif (!$field['hide']) { $strTmp=mb_substr(stripTagsFull($row[$fieldno]), 0, $iLength); if (trim($strTmp) != ''){ - $strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp)); + if($q=='\'') $strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp)); + if($q=='"') $strTemp=str_replace(array('"',"\n","\r"),array('---',' ',' '),trim($strTmp)); /* * Temp quick fix for replacing decimal dots with comma's if (isNumericExtended($strTemp)) { $strTemp = str_replace('.',',',$strTemp); } */ - echo "'$strTemp'"; + echo $q. $strTemp .$q ; } else {