diff --git a/application/controllers/admin/Export.php b/application/controllers/admin/Export.php index 61e9479b05c..2d00cde98c7 100644 --- a/application/controllers/admin/Export.php +++ b/application/controllers/admin/Export.php @@ -665,7 +665,7 @@ public function vvexport() $surveytable = "{{survey_$iSurveyId}}"; // Control if fieldcode are unique - $fieldnames = Yii::app()->db->schema->getTable($surveytable)->getColumnNames(); + $fieldnames = App()->db->schema->getTable($surveytable)->getColumnNames(); foreach ($fieldnames as $field) { $fielddata = arraySearchByKey($field, $fieldmap, "fieldname", 1); $fieldcode[] = viewHelper::getFieldCode($fielddata, array("LEMcompat" => true)); @@ -686,43 +686,60 @@ public function vvexport() $this->renderWrappedTemplate('export', 'vv_view', $aData); } elseif (isset($iSurveyId) && $iSurveyId) { //Export is happening - $extension = sanitize_paranoid_string(returnGlobal('extension')); - $vvVersion = (int) Yii::app()->request->getPost('vvversion'); - $vvVersion = (in_array($vvVersion, array(1, 2))) ? $vvVersion : 2; // Only 2 version actually, default to 2 - $fn = "vvexport_$iSurveyId." . $extension; + $extension = sanitize_paranoid_string(App()->request->getParam('extension')); + $vvVersion = intval(App()->request->getParam('vvVersion')); + if (!in_array($vvVersion, [1, 2])) { + $vvVersion = 2; + } + $questionSeparator = array('(', ')'); + switch (App()->request->getParam('qseparator')) { + case 'newline': + $questionSeparator = "\n"; + break; + case 'dash': + $questionSeparator = " - "; + break; + default: + // Nothing to do + } - $this->addHeaders($fn, "text/comma-separated-values", 0); + $questionAbbreviated = intval(App()->request->getParam('abbreviatedtextto')); + $fileName = "vvexport_$iSurveyId." . $extension; - $s = "\t"; + $this->addHeaders($fileName, "text/tab-separated-values", 0); $fieldmap = createFieldMap($survey, 'full', false, false, $survey->language); $surveytable = "{{survey_$iSurveyId}}"; - $fieldnames = Yii::app()->db->schema->getTable($surveytable)->getColumnNames(); + $fieldnames = App()->db->schema->getTable($surveytable)->getColumnNames(); + + /* @var output */ + $vvOutput = fopen('php://output', 'w+'); //Create the human friendly first line - $firstline = ""; - $secondline = ""; + $firstline = []; + $secondline = []; foreach ($fieldnames as $field) { $fielddata = arraySearchByKey($field, $fieldmap, "fieldname", 1); - if (count($fielddata) < 1) { - $firstline .= $field; + $firstline[] = $field; } else { - $firstline .= preg_replace('/\s+/', ' ', flattenText($fielddata['question'], false, true, 'UTF-8', true)); + if ($vvVersion >= 2) { + $firstline[] = viewHelper::getFieldText($fielddata, array('separator' => $questionSeparator, 'abbreviated' => $questionAbbreviated,)); + } else { + $firstline[] = preg_replace('/\s+/', ' ', flattenText($fielddata['question'], false, true, 'UTF-8', true)); + } } - $firstline .= $s; if ($vvVersion == 2) { $fieldcode = viewHelper::getFieldCode($fielddata, array("LEMcompat" => true)); $fieldcode = ($fieldcode) ? $fieldcode : $field; // $fieldcode is empty for token if there are no survey participants table } else { $fieldcode = $field; } - $secondline .= $fieldcode . $s; + $secondline[] = $fieldcode; } - - $vvoutput = $firstline . "\n"; - $vvoutput .= $secondline . "\n"; + fputcsv($vvOutput, $firstline, "\t"); + fputcsv($vvOutput, $secondline, "\t"); $query = "SELECT * FROM " . Yii::app()->db->quoteTableName($surveytable); if (incompleteAnsFilterState() == "incomplete") { @@ -732,9 +749,8 @@ public function vvexport() } $result = Yii::app()->db->createCommand($query)->query(); - echo $vvoutput; - foreach ($result as $row) { + $responseLine = []; $oResponse = Response::model($iSurveyId); $oResponse->setAttributes($row, false); $row = $oResponse->decrypt(); @@ -773,20 +789,20 @@ public function vvexport() $value = preg_replace('/^"/', '{quote}', $value); // yay! that nasty soab won't hurt us now! if ($field == "submitdate" && !$value) { - $value = "NULL"; + $value = '{question_not_shown}'; } - $sun[] = $value; + $responseLine[] = $value; } /* it is important here to stream output data, line by line * in order to avoid huge memory consumption when exporting large * quantities of answers */ - echo implode($s, $sun) . "\n"; - - unset($sun); + fputcsv($vvOutput, $responseLine, "\t"); + unset($responseLine); } - Yii::app()->end(); + fclose($vvOutput); + App()->end(); } } diff --git a/application/views/admin/export/vv_view.php b/application/views/admin/export/vv_view.php index a377b466483..6c57cfe4e3c 100644 --- a/application/views/admin/export/vv_view.php +++ b/application/views/admin/export/vv_view.php @@ -20,37 +20,26 @@ -
+
-
@@ -62,21 +51,22 @@
-
+
-
@@ -98,4 +110,4 @@
- \ No newline at end of file +