Skip to content

Commit

Permalink
Fixed issue #16939: exporting list question with 'other' to SPSS does…
Browse files Browse the repository at this point in the history
… not read all data (#1791)
  • Loading branch information
gabrieljenik committed Mar 11, 2021
1 parent 70ef483 commit abebe9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
12 changes: 12 additions & 0 deletions application/controllers/admin/export.php
Expand Up @@ -554,6 +554,18 @@ public function exportspss()
}
}

// Add instructions to change variable type and recode 'Other' option.
// This is needed when all answer option codes are numeric but the question has 'Other' enabled,
// because the variable is initialy set as alphanumeric in order to hold the '-oth-' value. See issue #16939
foreach ($fields as $field) {
if (isset($field['needsAlterType'])) {
echo "RECODE {$field['id']} (\"-oth-\" = \"666666\").\n";
echo "EXECUTE.\n";
echo "ADD VALUE LABELS {$field['id']} 666666 \"other\".\n";
echo "ALTER TYPE {$field['id']} (F6.0).\n";
}
}

foreach ($fields as $field) {
if ($field['scale'] !== '') {
switch ($field['scale']) {
Expand Down
25 changes: 21 additions & 4 deletions application/helpers/export_helper.php
Expand Up @@ -17,12 +17,15 @@
* Strips html tags and replaces new lines
*
* @param $string
* @param $removeOther if 'true', removes '-oth-' from the string.
* @return string
*/
function stripTagsFull($string)
function stripTagsFull($string, $removeOther = true)
{
$string = flattenText($string, false, true); // stripo whole + html_entities
$string = str_replace('-oth', '', $string);// Why ?
if ($removeOther) {
$string = str_replace('-oth-', '', $string);
}
//The backslashes must be escaped twice, once for php, and again for the regexp
$string = str_replace("'|\\\\'", "'", $string);
return $string;
Expand Down Expand Up @@ -252,7 +255,7 @@ function SPSSExportData($iSurveyID, $iLength, $na = '', $sEmptyAnswerValue = '',
break; // Break inside if : comment and other are string to be filtered
} // else do default action
default:
$strTmp = mb_substr(stripTagsFull($row[$fieldno]), 0, $iLength);
$strTmp = mb_substr(stripTagsFull($row[$fieldno], false), 0, $iLength);
if (trim($strTmp) != '') {
echo quoteSPSS($strTmp, $q, $field);
} elseif ($row[$fieldno] === '') {
Expand Down Expand Up @@ -314,7 +317,7 @@ function SPSSGetValues($field = array(), $qidattributes = null, $language)
foreach ($result as $row) {
$answers[] = array(
'code' => $row['code'],
'value' => mb_substr(stripTagsFull($row["answer"]), 0, $length_vallabel),
'value' => mb_substr(stripTagsFull($row["answer"], false), 0, $length_vallabel),
);
}
}
Expand Down Expand Up @@ -408,8 +411,18 @@ function SPSSGetValues($field = array(), $qidattributes = null, $language)
$spsstype = 'A';
}
}
// For questions types with answer options, if all answer codes are numeric but "Other" option is enabled,
// field should be exported as SPSS type 'A', size 6. See issue #16939
if (strpos("!LORFH1", $field['LStype']) !== false && $spsstype == 'F') {
$oQuestion = Question::model()->findByPk($field["qid"]);
if ($oQuestion->other == 'Y') {
$spsstype = 'A';
$size = 6;
}
}
$answers['SPSStype'] = $spsstype;
$answers['size'] = $size;
$answers['needsAlterType'] = true;
return $answers;
} else {
/* Not managed (currently): url, IP, … */
Expand Down Expand Up @@ -649,6 +662,10 @@ function SPSSFieldMap($iSurveyID, $prefix = 'V', $sLanguage = '')
$tempArray['SPSStype'] = $answers['SPSStype'];
unset($answers['SPSStype']);
}
if (isset($answers['needsAlterType'])) {
$tempArray['needsAlterType'] = $answers['needsAlterType'];
unset($answers['needsAlterType']);
}
if (!empty($answers)) {
$tempArray['answers'] = $answers;
}
Expand Down

0 comments on commit abebe9b

Please sign in to comment.