Skip to content

Commit

Permalink
dev: Fixes to export functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mennodekker committed Jul 2, 2013
1 parent da0369c commit da0af0c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 90 deletions.
5 changes: 4 additions & 1 deletion application/helpers/admin/export/CsvWriter.php
Expand Up @@ -59,12 +59,15 @@ protected function outputRecord($headers, $values, FormattingOptions $oOptions)
$this->output .= $sRecord;
fwrite($this->file, $this->output);
$this->output='';
}
}
}

public function close()
{
// Output white line at the end, better for R import
echo "\n";
if (!is_null($this->file)) {
fwrite($this->file, "\n");
fclose($this->file);
}
}
Expand Down
6 changes: 4 additions & 2 deletions application/helpers/admin/export/HtmlWriter.php
Expand Up @@ -90,8 +90,10 @@ protected function outputRecord($headers, $values, FormattingOptions $oOptions)
));
foreach ($questions as $question)
{
$this->renderQuestion($question, $values[$question['index']], $headers[$question['index']]);

if (isset($values[$question['index']]) && isset($headers[$question['index']]))
{
$this->renderQuestion($question, $values[$question['index']], $headers[$question['index']]);
}
}
$this->closeTag();
}
Expand Down
10 changes: 0 additions & 10 deletions application/helpers/admin/export/SurveyDao.php
Expand Up @@ -116,14 +116,4 @@ public function loadSurveyResults(SurveyObj $survey, $iMinimum, $iMaximum, $sFil
$iOffset = $iMinimum - 1;
$survey->responses=$oRecordSet->select($aSelectFields)->order('{{survey_' . $survey->id . '}}.id')->limit($iMaximum - $iOffset, $iOffset)->query();
}

/**
* Close the resultset
*/
public function close()
{
if ($survey->responses instanceof CDbDataReader) {
$survey->responses->close();
}
}
}
48 changes: 0 additions & 48 deletions application/helpers/admin/export/Translator.php
Expand Up @@ -4,59 +4,11 @@ class Translator
//An associative array: key = language code, value = translation library
private $translations = array();

//The following array stores field names that require pulling a value from the
//internationalization layer. <fieldname> => <internationalization key>
private $headerTranslationKeys = array(
'id' => 'id',
'lastname' => 'Last name',
'firstname' => 'First name',
'email' => 'Email address',
'token' => 'Token',
'datestamp' => 'Date last action',
'startdate' => 'Date started',
'submitdate' => 'Completed',
'ipaddr' => 'IP address',
'refurl' => 'Referring URL',
'lastpage' => 'Last page',
'startlanguage' => 'Start language'
);

public function translate($key, $sLanguageCode)
{
return $this->getTranslationLibrary($sLanguageCode)->gT($key);
}

/**
* Accepts a fieldName from a survey fieldMap and returns the translated value
* for the fieldName in the survey's base language (if one exists).
* If no translation exists for the provided column/fieldName then
* false is returned.
*
* To add any columns/fieldNames to be processed by this function, simply add the
* column/fieldName to the $headerTranslationKeys associative array.
*
* This provides a mechanism for determining of a column in a survey's data table
* needs to be translated through the translation mechanism, or if its an actual
* survey data column.
*
* @param string $column
* @param string $sLanguageCode
* @return string
*/
public function translateHeading($column, $sLanguageCode)
{
$key = $this->getHeaderTranslationKey($column);
//echo "Column: $column, Key: $key".PHP_EOL;
if ($key)
{
return $this->translate($key, $sLanguageCode);
}
else
{
return false;
}
}

protected function getTranslationLibrary($sLanguageCode)
{
$library = null;
Expand Down
40 changes: 12 additions & 28 deletions application/helpers/admin/export/Writer.php
Expand Up @@ -17,12 +17,6 @@ protected function translate($key, $sLanguageCode)
return $this->translator->translate($key, $sLanguageCode);
}

protected function translateHeading($column, $sLanguageCode)
{
if (substr($column,0,10)=='attribute_') return $column;
return $this->translator->translateHeading($column, $sLanguageCode);
}

/**
* An initialization method that implementing classes can override to gain access
* to any information about the survey, language, or formatting options they
Expand Down Expand Up @@ -405,29 +399,19 @@ final public function write(SurveyObj $survey, $sLanguageCode, FormattingOptions

foreach ($oOptions->selectedColumns as $column)
{
//Output the header.
$value = $this->translateHeading($column, $sLanguageCode);
if($value===false)
//Survey question field, $column value is a field name from the getFieldMap function.
switch ($oOptions->headingFormat)
{
//This branch may be reached erroneously if columns are added to the LimeSurvey product
//but are not updated in the Writer->headerTranslationKeys array. We should trap for this
//condition and do a safeDie.
//FIXME fix the above condition

//Survey question field, $column value is a field name from the getFieldMap function.
switch ($oOptions->headingFormat)
{
case 'abbreviated':
$value = $this->getAbbreviatedHeading($survey, $column);
break;
case 'full':
$value = $this->getFullHeading($survey, $oOptions, $column);
break;
default:
case 'code':
$value = $this->getCodeHeading($survey, $oOptions, $column);
break;
}
case 'abbreviated':
$value = $this->getAbbreviatedHeading($survey, $column);
break;
case 'full':
$value = $this->getFullHeading($survey, $oOptions, $column);
break;
default:
case 'code':
$value = viewHelper::getFieldCode($survey->fieldMap[$column]);
break;
}
if ($oOptions->headerSpacesToUnderscores)
{
Expand Down
6 changes: 5 additions & 1 deletion application/helpers/admin/exportresults_helper.php
Expand Up @@ -133,7 +133,11 @@ function exportSurvey($iSurveyId, $sLanguageCode, $sExportPlugin, FormattingOpti

$writer->write($survey, $sLanguageCode, $oOptions,true);
$result = $writer->close();
$surveyDao->close();

// Close resultset if needed
if ($survey->responses instanceof CDbDataReader) {
$survey->responses->close();
}

if ($oOptions->output=='file')
{
Expand Down

0 comments on commit da0af0c

Please sign in to comment.