Add enclosure parameter when exporting array to CSV BT#15280

In order to export CSV with double quotes (")
pull/2858/head
Julio Montoya 7 years ago
parent d485d9a35d
commit 81d89fb452
  1. 8
      main/inc/lib/export.lib.inc.php
  2. 10
      plugin/surveyexportcsv/export.php

@ -33,19 +33,23 @@ class Export
* @param array $data
* @param string $filename
* @param bool $writeOnly Whether to only write on disk or also send for download
* @param string $enclosure
*
* @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode
*/
public static function arrayToCsv($data, $filename = 'export', $writeOnly = false)
public static function arrayToCsv($data, $filename = 'export', $writeOnly = false, $enclosure = '"')
{
if (empty($data)) {
return false;
}
$enclosure = !empty($enclosure) ? $enclosure : '"';
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
$stream = fopen($filePath, 'w');
$writer = new CsvWriter(';', '"', $stream, true);
$writer = new CsvWriter(';', $enclosure, $stream, true);
$writer->prepare();
foreach ($data as $item) {
if (empty($item)) {
$writer->writeItem([]);

@ -31,6 +31,7 @@ $questionsData = array_filter(
return in_array($questionData['type'], ['yesno', 'multiplechoice', 'open']);
}
);
usort(
$questionsData,
function ($qL, $qR) {
@ -58,7 +59,7 @@ foreach ($surveyAnswers as $i => $answer) {
// Generate file
$fileName = md5($surveyId.time());
Export::arrayToCsv($content, $fileName);
Export::arrayToCsv($content, $fileName, false, "'");
/**
* Generate the first row for file.
@ -181,9 +182,7 @@ function otherRow($questions, $user, $courseId)
if ('open' === $question['type']) {
$answer = getOpenAnswer($question['question_id'], $question['survey_id'], $courseId, $user);
$row[] = Security::remove_XSS(
$answer->getOptionId()
);
$row[] = Security::remove_XSS($answer->getOptionId());
} else {
$options = getQuestionOptions(
$user,
@ -194,7 +193,8 @@ function otherRow($questions, $user, $courseId)
/** @var CSurveyQuestionOption $option */
foreach ($options as $option) {
$row[] = $option->getSort();
$value = $option->getSort();
$row[] = '"'.$value.'"';
}
}
}

Loading…
Cancel
Save