Ejemplo n.º 1
0
 public function exportspss()
 {
     global $length_vallabel;
     $iSurveyID = sanitize_int(Yii::app()->request->getParam('sid'));
     $clang = $this->getController()->lang;
     //for scale 1=nominal, 2=ordinal, 3=scale
     //		$typeMap = $this->_getTypeMap();
     $filterstate = incompleteAnsFilterState();
     $spssver = returnGlobal('spssver');
     if (is_null($spssver)) {
         if (!Yii::app()->session['spssversion']) {
             Yii::app()->session['spssversion'] = 2;
             //Set default to 2, version 16 or up
         }
         $spssver = Yii::app()->session['spssversion'];
     } else {
         Yii::app()->session['spssversion'] = $spssver;
     }
     $length_varlabel = '231';
     // Set the max text length of Variable Labels
     $length_vallabel = '120';
     // Set the max text length of Value Labels
     switch ($spssver) {
         case 1:
             //<16
             $iLength = '255';
             // Set the max text length of the Value
             break;
         case 2:
             //>=16
             $iLength = '16384';
             // Set the max text length of the Value
             break;
         default:
             $iLength = '16384';
             // Set the max text length of the Value
     }
     $headerComment = '*$Rev: 121017 $' . " {$filterstate} {$spssver}.\n";
     if (isset($_POST['dldata'])) {
         $subaction = "dldata";
     }
     if (isset($_POST['dlstructure'])) {
         $subaction = "dlstructure";
     }
     if (!isset($subaction)) {
         $selecthide = "";
         $selectshow = "";
         $selectinc = "";
         switch ($filterstate) {
             case "incomplete":
                 $selectinc = "selected='selected'";
                 break;
             case "complete":
                 $selecthide = "selected='selected'";
                 break;
             default:
                 $selectshow = "selected='selected'";
         }
         $data['selectinc'] = $selectinc;
         $data['selecthide'] = $selecthide;
         $data['selectshow'] = $selectshow;
         $data['spssver'] = $spssver;
         $data['surveyid'] = $iSurveyID;
         $data['display']['menu_bars']['browse'] = $clang->gT('Export results');
         $this->_renderWrappedTemplate('export', 'spss_view', $data);
         return;
     }
     // Get Base language:
     $language = Survey::model()->findByPk($iSurveyID)->language;
     $clang = new limesurvey_lang($language);
     Yii::app()->loadHelper("admin/exportresults");
     if ($subaction == 'dldata') {
         header("Content-Disposition: attachment; filename=survey_" . $iSurveyID . "_SPSS_data_file.dat");
         header("Content-type: text/comma-separated-values; charset=UTF-8");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Pragma: public");
         if ($spssver == 2) {
             echo "";
         }
         SPSSExportData($iSurveyID, $iLength);
         exit;
     }
     if ($subaction == 'dlstructure') {
         header("Content-Disposition: attachment; filename=survey_" . $iSurveyID . "_SPSS_syntax_file.sps");
         header("Content-type: application/download; charset=UTF-8");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Pragma: public");
         // Build array that has to be returned
         $fields = SPSSFieldMap($iSurveyID);
         //Now get the query string with all fields to export
         $query = SPSSGetQuery($iSurveyID);
         $result = Yii::app()->db->createCommand($query)->query();
         $num_fields = 0;
         //Now we check if we need to adjust the size of the field or the type of the field
         foreach ($result as $row) {
             if ($num_fields == 0) {
                 $num_fields = count($row);
             }
             $row = array_values($row);
             $fieldno = 0;
             while ($fieldno < $num_fields) {
                 //Performance improvement, don't recheck fields that have valuelabels
                 if (!isset($fields[$fieldno]['answers'])) {
                     $strTmp = mb_substr(stripTagsFull($row[$fieldno]), 0, $iLength);
                     $len = mb_strlen($strTmp);
                     if ($len > $fields[$fieldno]['size']) {
                         $fields[$fieldno]['size'] = $len;
                     }
                     if (trim($strTmp) != '') {
                         if ($fields[$fieldno]['SPSStype'] == 'F' && (isNumericExtended($strTmp) === FALSE || $fields[$fieldno]['size'] > 16)) {
                             $fields[$fieldno]['SPSStype'] = 'A';
                         }
                     }
                 }
                 $fieldno++;
             }
         }
         $result->close();
         /**
          * End of DATA print out
          *
          * Now $fields contains accurate length data, and the DATA LIST can be rendered -- then the contents of the temp file can
          * be sent to the client.
          */
         if ($spssver == 2) {
             echo "";
         }
         echo $headerComment;
         if ($spssver == 2) {
             echo "SET UNICODE=ON.\n";
         }
         echo "SHOW LOCALE.\n";
         echo "PRESERVE LOCALE.\n";
         echo "SET LOCALE='en_UK'.\n";
         echo "GET DATA\n" . " /TYPE=TXT\n" . " /FILE='survey_" . $iSurveyID . "_SPSS_data_file.dat'\n" . " /DELCASE=LINE\n" . " /DELIMITERS=\",\"\n" . " /QUALIFIER=\"'\"\n" . " /ARRANGEMENT=DELIMITED\n" . " /FIRSTCASE=1\n" . " /IMPORTCASE=ALL\n" . " /VARIABLES=";
         foreach ($fields as $field) {
             if ($field['SPSStype'] == 'DATETIME23.2') {
                 $field['size'] = '';
             }
             if ($field['SPSStype'] == 'F' && ($field['LStype'] == 'N' || $field['LStype'] == 'K')) {
                 $field['size'] .= '.' . ($field['size'] - 1);
             }
             if (!$field['hide']) {
                 echo "\n {$field['id']} {$field['SPSStype']}{$field['size']}";
             }
         }
         echo ".\nCACHE.\n" . "EXECUTE.\n";
         //Create the variable labels:
         echo "*Define Variable Properties.\n";
         foreach ($fields as $field) {
             if (!$field['hide']) {
                 $label_parts = strSplitUnicode(str_replace('"', '""', stripTagsFull($field['VariableLabel'])), $length_varlabel - strlen($field['id']));
                 //if replaced quotes are splitted by, we need to mve the first quote to the next row
                 foreach ($label_parts as $idx => $label_part) {
                     if ($idx != count($label_parts) && substr($label_part, -1) == '"' && substr($label_part, -2) != '"') {
                         $label_parts[$idx] = rtrim($label_part, '"');
                         $label_parts[$idx + 1] = '"' . $label_parts[$idx + 1];
                     }
                 }
                 echo "VARIABLE LABELS " . $field['id'] . " \"" . implode("\"+\n\"", $label_parts) . "\".\n";
             }
         }
         // Create our Value Labels!
         echo "*Define Value labels.\n";
         foreach ($fields as $field) {
             if (isset($field['answers'])) {
                 $answers = $field['answers'];
                 //print out the value labels!
                 echo "VALUE LABELS  {$field['id']}\n";
                 $i = 0;
                 foreach ($answers as $answer) {
                     $i++;
                     if ($field['SPSStype'] == "F" && isNumericExtended($answer['code'])) {
                         $str = "{$answer['code']}";
                     } else {
                         $str = "\"{$answer['code']}\"";
                     }
                     if ($i < count($answers)) {
                         echo " {$str} \"{$answer['value']}\"\n";
                     } else {
                         echo " {$str} \"{$answer['value']}\".\n";
                     }
                 }
             }
         }
         foreach ($fields as $field) {
             if ($field['scale'] !== '') {
                 switch ($field['scale']) {
                     case 2:
                         echo "VARIABLE LEVEL {$field['id']}(ORDINAL).\n";
                         break;
                     case 3:
                         echo "VARIABLE LEVEL {$field['id']}(SCALE).\n";
                 }
             }
         }
         //Rename the Variables (in case somethings goes wrong, we still have the OLD values
         foreach ($fields as $field) {
             if (isset($field['sql_name']) && $field['hide'] === 0) {
                 $ftitle = $field['title'];
                 if (!preg_match("/^([a-z]|[A-Z])+.*\$/", $ftitle)) {
                     $ftitle = "q_" . $ftitle;
                 }
                 $ftitle = str_replace(array(" ", "-", ":", ";", "!", "/", "\\", "'"), array("_", "_hyph_", "_dd_", "_dc_", "_excl_", "_fs_", "_bs_", '_qu_'), $ftitle);
                 if ($ftitle != $field['title']) {
                     echo "* Variable name was incorrect and was changed from {$field['title']} to {$ftitle} .\n";
                 }
                 echo "RENAME VARIABLE ( " . $field['id'] . ' = ' . $ftitle . " ).\n";
             }
         }
         echo "RESTORE LOCALE.\n";
         exit;
     }
 }
Ejemplo n.º 2
0
 public function keys($val)
 {
     $this->session->keys(array('value' => strSplitUnicode($val)));
     return $this;
 }