Skip to content

Commit

Permalink
New feature: Choose language when exporting SPSS data
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Oct 7, 2016
1 parent 757b085 commit d63d9a4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
25 changes: 20 additions & 5 deletions application/controllers/admin/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,22 @@ public function exportspss()
$data['surveyid'] = $iSurveyID;
$data['display']['menu_bars']['browse'] = gT('Export results');

$surveyinfo = Survey::model()->findByPk($iSurveyID)->surveyinfo;
$oSurvey = Survey::model()->findByPk($iSurveyID);
$surveyinfo = $oSurvey->surveyinfo;
$data['display']['menu_bars']['browse'] = gT('Browse responses'); // browse is independent of the above
$data["surveyinfo"] = $surveyinfo;
$data['title_bar']['title'] = gT('Browse responses').': '.$surveyinfo['surveyls_title'];
$data['sBaseLanguage'] = $oSurvey->language;

$aLanguages=array();
$aLanguagesCodes=$oSurvey->getAllLanguages();
foreach ($aLanguagesCodes as $sLanguage){
$aLanguages[$sLanguage]=getLanguageNameFromCode($sLanguage,false);
}
$data['aLanguages'] = $aLanguages; // Pass available exports

$data['sidemenu']['state'] = false;

$data['menu']['edition'] = true;
$data['menu']['close'] = true;

Expand All @@ -427,8 +437,13 @@ public function exportspss()
}

// Get Base language:
$language = Survey::model()->findByPk($iSurveyID)->language;
App()->setLanguage($language);
$oSurvey = Survey::model()->findByPk($iSurveyID);
$sLanguage = Yii::app()->request->getParam('exportlang');
$aLanguagesCodes=$oSurvey->getAllLanguages();
if (!in_array($sLanguage,$aLanguagesCodes)){
$sLanguage = $oSurvey->language;
}
App()->setLanguage($sLanguage);

Yii::app()->loadHelper("admin/exportresults");
viewHelper::disableHtmlLogging();
Expand All @@ -446,7 +461,7 @@ public function exportspss()
}

$sNoAnswerValue = (isset($_POST['noanswervalue']) && $_POST['noanswervalue'] != '' )?'\''.$_POST['noanswervalue'].'\'':'';
SPSSExportData($iSurveyID, $iLength, $sNoAnswerValue);
SPSSExportData($iSurveyID, $iLength, $sNoAnswerValue, $sLanguage);

exit;
}
Expand All @@ -459,7 +474,7 @@ public function exportspss()
header("Pragma: public");

// Build array that has to be returned
$fields = SPSSFieldMap($iSurveyID);
$fields = SPSSFieldMap($iSurveyID, 'V', $sLanguage);

//Now get the query string with all fields to export
$query = SPSSGetQuery($iSurveyID, 500, 0); // Sample first 500 responses for adjusting fieldmap
Expand Down
66 changes: 32 additions & 34 deletions application/helpers/export_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ function strSplitUnicode($str, $l = 0) {
* @param sep Quote separator. Use '\'' for SPSS, '"' for R
* @param logical $header If TRUE, adds SQGA code as column headings (used by export to R)
*/
function SPSSExportData ($iSurveyID, $iLength, $na = '', $q='\'', $header=FALSE) {
function SPSSExportData ($iSurveyID, $iLength, $na = '', $q='\'', $header=FALSE, $sLanguage='') {

// Build array that has to be returned
$fields = SPSSFieldMap($iSurveyID);
$fields = SPSSFieldMap($iSurveyID, 'V', $sLanguage);

// Now see if we have parameters for from (offset) & num (limit)
$limit = App()->getRequest()->getParam('limit');
Expand Down Expand Up @@ -326,7 +326,7 @@ function SPSSGetValues ($field = array(), $qidattributes = null, $language ) {
* @param $prefix string prefix for the variable ID
* @return array
*/
function SPSSFieldMap($iSurveyID, $prefix = 'V')
function SPSSFieldMap($iSurveyID, $prefix = 'V', $sLanguage='')
{
$typeMap = array(
'5'=>Array('name'=>'5 Point Choice','size'=>1,'SPSStype'=>'F','Scale'=>3),
Expand Down Expand Up @@ -362,28 +362,26 @@ function SPSSFieldMap($iSurveyID, $prefix = 'V')
'*'=>Array('name'=>'Equation','size'=>1,'SPSStype'=>'A'),
);

$fieldmap = createFieldMap($iSurveyID,'full',false,false,getBaseLanguageFromSurveyID($iSurveyID));
if (empty($sLanguage)){
$sLanguage=getBaseLanguageFromSurveyID($iSurveyID);
}
$fieldmap = createFieldMap($iSurveyID,'full',false,false,$sLanguage);

#See if tokens are being used
$bTokenTableExists = tableExists('tokens_'.$iSurveyID);
// ... and if the survey uses anonymized responses
$sSurveyAnonymized=Survey::model()->findByPk($iSurveyID)->anonymized;

#Lookup the names of the attributes
$query="SELECT sid, anonymized, language FROM {{surveys}} WHERE sid=$iSurveyID";
$aRow=Yii::app()->db->createCommand($query)->queryRow(); //Checked
$surveyprivate=$aRow['anonymized'];
$language=$aRow['language'];

$fieldno=0;

$iFieldNumber=0;
$fields=array();
if ($bTokenTableExists && $surveyprivate == 'N' && Permission::model()->hasSurveyPermission($iSurveyID,'tokens','read')) {
if ($bTokenTableExists && $sSurveyAnonymized == 'N' && Permission::model()->hasSurveyPermission($iSurveyID,'tokens','read')) {
$tokenattributes=getTokenFieldsAndNames($iSurveyID,false);
foreach ($tokenattributes as $attributefield=>$attributedescription)
{
//Drop the token field, since it is in the survey too
if($attributefield!='token') {
$fieldno++;
$fields[] = array('id'=>"$prefix$fieldno",'name'=>mb_substr($attributefield, 0, 8),
$iFieldNumber++;
$fields[] = array('id'=>"{$prefix}{$iFieldNumber}",'name'=>mb_substr($attributefield, 0, 8),
'qid'=>0,'code'=>'','SPSStype'=>'A','LStype'=>'Undef',
'VariableLabel'=>$attributedescription['description'],'sql_name'=>$attributefield,'size'=>'100',
'title'=>$attributefield,'hide'=>0, 'scale'=>'');
Expand Down Expand Up @@ -480,15 +478,15 @@ function SPSSFieldMap($iSurveyID, $prefix = 'V')
}

}
$fieldno++;
$fid = $fieldno - $diff;
$iFieldNumber++;
$fid = $iFieldNumber - $diff;
$lsLong = isset($typeMap[$ftype]["name"])?$typeMap[$ftype]["name"]:$ftype;
$tempArray = array('id'=>"$prefix$fid",'name'=>mb_substr($fieldname, 0, 8),
'qid'=>$qid,'code'=>$code,'SPSStype'=>$fieldtype,'LStype'=>$ftype,"LSlong"=>$lsLong,
'ValueLabels'=>'','VariableLabel'=>$varlabel,"sql_name"=>$fieldname,"size"=>$val_size,
'title'=>$ftitle,'hide'=>$hide,'scale'=>$export_scale, 'scale_id'=>$scale_id);
//Now check if we have to retrieve value labels
$answers = SPSSGetValues($tempArray, $aQuestionAttribs, $language);
$answers = SPSSGetValues($tempArray, $aQuestionAttribs, $sLanguage);
if (is_array($answers)) {
//Ok we have answers
if (isset($answers['size'])) {
Expand Down Expand Up @@ -1274,23 +1272,23 @@ function quexml_export($surveyi, $quexmllan)
$question->appendChild($directive);
}

if (Yii::app()->getConfig('quexmlshowprintablehelp')==true)
{
if (Yii::app()->getConfig('quexmlshowprintablehelp')==true)
{

$RowQ['printable_help']=quexml_get_lengthth($qid,"printable_help","", $quexmllang);
$RowQ['printable_help']=quexml_get_lengthth($qid,"printable_help","", $quexmllang);

if (!empty($RowQ['printable_help']))
{
$directive = $dom->createElement("directive");
$position = $dom->createElement("position","before");
$text = $dom->createElement("text", '['.gT('Only answer the following question if:')." ".QueXMLCleanup($RowQ['printable_help'])."]");
$administration = $dom->createElement("administration","self");
$directive->appendChild($position);
$directive->appendChild($text);
$directive->appendChild($administration);
$question->appendChild($directive);
}
}
if (!empty($RowQ['printable_help']))
{
$directive = $dom->createElement("directive");
$position = $dom->createElement("position","before");
$text = $dom->createElement("text", '['.gT('Only answer the following question if:')." ".QueXMLCleanup($RowQ['printable_help'])."]");
$administration = $dom->createElement("administration","self");
$directive->appendChild($position);
$directive->appendChild($text);
$directive->appendChild($administration);
$question->appendChild($directive);
}
}

$response = $dom->createElement("response");
$sgq = $RowQ['title'];
Expand Down Expand Up @@ -1820,7 +1818,7 @@ function tokensExport($iSurveyID)

if (Yii::app()->request->getPost('tokendeleteexported') && !empty($aExportedTokens))
{
Token::model($iSurveyID)->deleteByPk($aExportedTokens);
Token::model($iSurveyID)->deleteByPk($aExportedTokens);
}
}

Expand Down
13 changes: 13 additions & 0 deletions application/views/admin/export/spss_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
));?>
</div>
</div>
<?php
if (count($aLanguages)>1)
{ ?>
<div class="form-group row">
<label for='exportlang' class='col-sm-2 form-control-label'><?php eT("Language:");?></label>
<div class="col-sm-2">
<?php echo CHtml::dropDownList('exportlang', $sBaseLanguage, $aLanguages, array('class'=>'form-control')); ?>
</div>
</div>
<?php } else { ?>
<?php echo CHtml::hiddenField('exportlang', $sBaseLanguage); ?>

<?php } ?>
<div class="form-group row">
<label for='limit' class='col-sm-2 form-control-label'><?php eT("Limit:");?></label>
<div class="col-sm-1">
Expand Down

0 comments on commit d63d9a4

Please sign in to comment.