Skip to content

Commit

Permalink
Fixed issue #17678: VV export didn't shown sub question (#2283)
Browse files Browse the repository at this point in the history
Dev: feature related to manage question text type and abbreviation
Dev: language string (get from export)
Dev: language string
  • Loading branch information
Shnoulle committed May 10, 2022
1 parent a8ef77c commit 2a8c0b6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 52 deletions.
68 changes: 42 additions & 26 deletions application/controllers/admin/Export.php
Expand Up @@ -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));
Expand All @@ -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") {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}

Expand Down
64 changes: 38 additions & 26 deletions application/views/admin/export/vv_view.php
Expand Up @@ -20,37 +20,26 @@
</div>
</div>

<div class="panel-body">
<div class="panel-body form-horizontal">
<div class="form-group">
<label for="surveyid" class="col-sm-2 control-label">
<label for="surveyid" class="col-sm-4 control-label">
<?php eT("Survey ID:"); ?>
</label>
<div class="col-sm-4">
<div class="col-sm-8">
<?php echo CHtml::textField('surveyid', $surveyid, array('size' => 10, 'readonly' => 'readonly', 'class' => 'form-control')); ?>
</div>
</div>

<div class="form-group">
<label for="completionstate" class="col-sm-2 control-label">
<label for="completionstate" class="col-sm-4 control-label">
<?php eT("Export:"); ?>
</label>
<div class="col-sm-8">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input name="completionstate" value="complete" type="radio" id="completionstate-complete" />
<?php eT("Completed responses only"); ?>
</label>

<label class="btn btn-default active">
<input name="completionstate" value="all" type="radio" checked='checked' id="completionstate-all" autofocus="true" />
<?php eT("All responses"); ?>
</label>

<label class="btn btn-default">
<input name="completionstate" value="incomplete" type="radio" id="completionstate-incomplete" />
<?php eT("Incomplete responses only"); ?>
</label>
</div>
<select class="form-control" name="completionstate" id="completionstate">
<option value="complete" id="completionstate-complete"><?php eT("Completed responses only"); ?></option>
<option value="all" id="completionstate-all" selected><?php eT("All responses"); ?></option>
<option value="incomplete" id="completionstate-incomplete"><?php eT("Incomplete responses only"); ?></option>
</select>
</div>
</div>
</div>
Expand All @@ -62,21 +51,22 @@
<?php eT("Format"); ?>
</div>
</div>
<div class="panel-body">
<div class="panel-body form-horizontal">
<div class="form-group">
<label for="extension" class="col-sm-2 control-label" data-toggle="tooltip" data-placement="right" title='<?php eT("For easy opening in MS Excel, change the extension to 'tab' or 'txt'"); ?>'>
<label for="extension" class="col-sm-4 control-label">
<?php eT("File extension:"); ?>
</label>
<div class="col-sm-4">
<div class="col-sm-8">
<?php echo CHtml::textField('extension', 'csv', array('size' => 3, 'class' => 'form-control')); ?>
<p class="help-block"><?php eT("For easy opening in MS Excel, change the extension to 'tab' or 'txt'"); ?></p>
</div>
</div>

<div class="form-group">
<label for="vvversion" class="col-sm-2 control-label" data-toggle="tooltip" data-placement="right" title='<?php eT("If you want to import survey on old installation or if your survey have problem: use old version (automatically selected if some code are duplicated)."); ?>'>
<label for="vvversion" class="col-sm-4 control-label">
<?php eT("VV export version:"); ?>
</label>
<div class="col-sm-4">
<div class="col-sm-8">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default <?php echo ($vvversionselected == 2 ? "active" : ""); ?>">
<input name="vvversion" value="2" type="radio" id="vvversion-last" <?php echo ($vvversionselected == 2 ? "checked='checked'" : ""); ?> />
Expand All @@ -88,8 +78,30 @@
<?php eT("Old VV version"); ?>
</label>
</div>
<p class="help-block"><?php eT("If you want to import the response data in older version or if your survey has integrity problem, please use the old export version (automatically selected if there are duplicate codes)."); ?></p>
</div>
</div>

<div class="form-group">
<label for="qseparator" class="col-sm-4 control-label">
<?php eT("Separator between question and subquestion:"); ?>
</label>
<div class="col-sm-8">
<select class="form-control" name="qseparator" id="qseparator">
<option value="newline"><?php eT("New line (use with care)"); ?></option>
<option value="parenthesis" selected><?php eT("Subquestion wrapped by parentheses"); ?></option>
<option value="dash"><?php printf(gT("Single dash (%s)"), ' - '); ?></option>
</select>
</div>
</div>
<div class="form-group">
<label for="abbreviatedtextto" class="col-sm-4 control-label">
<?php eT("Number of characters:"); ?>
</label>
<div class="col-sm-8">
<?php echo CHtml::numberField('abbreviatedtextto', '', array('min' => "1", 'step' => "1", 'class' => 'form-control')); ?>
<p class="help-block"><?php eT("Leave empty if you want complete question text."); ?></p>
</div>
</div>
</div>
</div>
Expand All @@ -98,4 +110,4 @@
<form>
</div>
</div>
</div>
</div>

0 comments on commit 2a8c0b6

Please sign in to comment.