<?php /* ------------------------------------------------------------------------ Copyright (C) 2015 Albert Weerman This library/program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------ */ if (function_exists('stata_write')) { echo 'hoooooray'; $data = array(1 => array('primkey' => '5631989408cbd', 'language' => '1', 'browserinfo' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36', 'startts' => '2015-10-28 20:55:00', 'endts' => '2015-10-28 20:55:09', 'q_intro' => '1', 'q1_1_' => 8.98846567431158E+307, 'q1_2_' => 8.98846567431158E+307, 'PA1_1_' => 8.98846567431158E+307, 'PA1_2_' => 8.98846567431158E+307, 'PA2' => '.', 'bastest' => '.', 'a1' => '.', 'b1' => '.', 'teststring' => 'bas test', 'a12' => '.', 'married' => 1, 'qi' => 4, 'q2_3_' => 8.98846567431158E+307, 'q2_4_' => 8.98846567431158E+307, 'q2_5_' => 8.98846567431158E+307), 2 => array('primkey' => '5631989fa8c5b', 'language' => '1', 'browserinfo' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36', 'startts' => '2015-10-28 20:55:11', 'endts' => '2015-10-28 20:55:21', 'q_intro' => '1', 'q1_1_' => 8.98846567431158E+307, 'q1_2_' => 8.98846567431158E+307, 'PA1_1_' => 8.98846567431158E+307, 'PA1_2_' => 8.98846567431158E+307, 'PA2' => '.', 'bastest' => '.', 'a1' => '.', 'b1' => '.', 'teststring' => 'test 2', 'a12' => '.', 'married' => 12, 'qi' => 4, 'q2_3_' => 8.98846567431158E+307, 'q2_4_' => 8.98846567431158E+307, 'q2_5_' => 8.98846567431158E+307)); $variables = array('primkey' => array('vlabels' => '', 'dlabels' => 'test string', 'vfmt' => '%240s', 'valueType' => '240'), 'language' => array('vlabels' => '', 'dlabels' => 'survey language', 'vfmt' => '%240s', 'valueType' => '240'), 'browserinfo' => array('vlabels' => '', 'dlabels' => 'browser information', 'vfmt' => '%240s', 'valueType' => '240'), 'startts' => array('vlabels' => '', 'dlabels' => 'survey start timestamp', 'vfmt' => '%240s', 'valueType' => '240'), 'endts' => array('vlabels' => '', 'dlabels' => 'survey end timestamp', 'vfmt' => '%240s', 'valueType' => '240'), 'q_intro' => array('vlabels' => '', 'dlabels' => 'description intro', 'vfmt' => '%1s', 'valueType' => '1'), 'q1_1_' => array('vlabels' => '', 'dlabels' => 'description1', 'vfmt' => '%9.0g', 'valueType' => '255'), 'q1_2_' => array('vlabels' => '', 'dlabels' => 'description1', 'vfmt' => '%9.0g', 'valueType' => '255'), 'PA1_1_' => array('vlabels' => '', 'dlabels' => 'description1', 'vfmt' => '%9.0g', 'valueType' => '255'), 'PA1_2_' => array('vlabels' => '', 'dlabels' => 'description1', 'vfmt' => '%9.0g', 'valueType' => '255'), 'PA2' => array('vlabels' => '', 'dlabels' => 'dasd', 'vfmt' => '%1s', 'valueType' => '1'), 'bastest' => array('vlabels' => '', 'dlabels' => '', 'vfmt' => '%240s', 'valueType' => '240'), 'a1' => array('vlabels' => '', 'dlabels' => '', 'vfmt' => '%1s', 'valueType' => '1'), 'b1' => array('vlabels' => '', 'dlabels' => 'description yes', 'vfmt' => '%1s', 'valueType' => '1'), 'teststring' => array('vlabels' => '', 'dlabels' => 'test string question', 'vfmt' => '%240s', 'valueType' => '240'), 'a12' => array('vlabels' => '', 'dlabels' => '', 'vfmt' => '%1s', 'valueType' => '1'), 'married' => array('vlabels' => '', 'dlabels' => 'R married', 'vfmt' => '%9.0g', 'valueType' => '255'), 'qi' => array('vlabels' => '', 'dlabels' => 'temp var', 'vfmt' => '%9.0g', 'valueType' => '255'), 'q2_3_' => array('vlabels' => 'TYesNo', 'dlabels' => 'description2', 'vfmt' => '%9.0g', 'valueType' => '255'), 'q2_4_' => array('vlabels' => 'TYesNo', 'dlabels' => 'description2', 'vfmt' => '%9.0g', 'valueType' => '255'), 'q2_5_' => array('vlabels' => 'TYesNo', 'dlabels' => 'description2', 'vfmt' => '%9.0g', 'valueType' => '255')); $labels = array('TYesNo' => array(1 => '1 Yes', 2 => '2 Ja', 12 => '12 test')); stata_write('/tmp/aa_' . date('ymdhis') . '.dta', array('data' => $data), $variables, array('labels' => $labels)); } else { echo ' stata_write not found'; }
<?php /* Reading */ /* $res = stata_open("/var/www/html/filename.dta"); echo "Stata observations: " . stata_observations($res); echo "Stata variables: " . stata_nvariables($res); print_r(stata_variables($res)); $df = stata_data($res); echo $df['data'][0]['variablename'] $labels = stata_labels($res)['labels']; stata_close($res); */ /* Writing */ echo 'write'; if (function_exists(stata_write)) { stata_write("/tmp/filename.dta", array("data" => array(1 => array("prim_key" => "232342342", "testswitch" => 32.3234, "mode" => 32741), 2 => array("prim_key" => "33333333333333333", "testswitch" => pow(2.0, 1023), "mode" => 2147483621))), array("prim_key" => array("vlabels" => "", "dlabels" => "PRIM KEY", "vfmt" => "%17s", "valueType" => 20), "testswitch" => array("vlabels" => "", "dlabels" => "TEST SWITCH", "vfmt" => "%9.0g", "valueType" => 255), "mode" => array("vlabels" => "gfk2_live_vl5", "dlabels" => "INTERVIEW MODE", "vfmt" => "%9.0g", "valueType" => 253)), array("labels" => array("gfk2_live_vl5" => array(44 => "44 Face", 55 => "55 Call center")))); echo 'done'; } else { echo 'aaa'; }
function exportStata() { if (function_exists('stata_write')) { $metadata['observations'] = $this->survey->db->count(); $metadata['variables'] = sizeof($this->survey->getAllDataQuestions()); // echo '<br/><b>metadata</b> '; //METADATA NOT USED // print_r($metadata); //getVariables first $variablesraw = array(); $variables = array(); foreach ($this->survey->getAllDataQuestions() as $name => $question) { $vlables = ''; if ($question->getQuestionType()->getAnswerType() == QUESTION_TYPE_ENUMERATED) { $vlables = $question->getQuestionType()->getName(); } $variablesraw[$name] = ''; $variables[$name] = array("vlabels" => $vlables, "dlabels" => $question->getDescription(), "vfmt" => $question->getQuestionType()->getVFMT(), "valueType" => $question->getQuestionType()->getValueType()); } // echo '<br/><b>variables</b> '; // print_r($variables); $labels = array(); foreach ($this->survey->getTypes() as $type) { if ($type->getAnswerType() == QUESTION_TYPE_ENUMERATED) { $enumerated = $type->getEnumeratedOptions(); //enumerated stored in details $typeArray = array(); foreach ($enumerated as $key => $enum) { $typeArray[$key] = $key . ' ' . $enum[1]; } $labels[$type->getName()] = $typeArray; } } //echo '<br/><b>labels</b> '; //print_r($labels); //$collection = $this->survey->db->getCollection(); //$cursor = $collection->find(); $cursor = $this->survey->db->getContent(); $i = 1; //standard: array with missings $baseArray = array(); foreach ($variablesraw as $name => $question) { if ($this->survey->getQuestion(getTextWithoutBrackets($name))->getQuestionType()->getValueType() < 250) { $baseArray[$name] = (string) '.'; } else { $baseArray[$name] = (double) $this->getValDouble('.'); } } foreach ($cursor as $doc) { $baseArr = $baseArray; array_shift($doc); $doc = $this->survey->db->toArray($doc); //convert to array for STATA //remove variables that are not in the list! foreach ($doc as $varname => $value) { // echo $varname . ':' . $value . '<br/>'; if (!isset($variables[$varname])) { //$this->survey->getQuestion($varname) == null){ //remove from array: not present in question list // unset($doc[$varname]); // echo $varname . ' is not in array<br/>'; } else { if (is_array($value)) { //array: SET OF QUESTION //unset($doc[$varname]); foreach ($value as $key => $val) { // $doc[$varname . '[' . $key . ']'] = (double) $this->getValDouble($val); //floatval($val); $baseArr[$varname . '[' . $key . ']'] = (double) $this->getValDouble($val); //floatval($val); } } else { // echo 'update' . $varname; if ($this->survey->getQuestion(getTextWithoutBrackets($varname))->getQuestionType()->getValueType() < 250) { // echo 'string'; // $doc[$varname] = (string) $value; $baseArr[$varname] = (string) $value; } else { // echo $varname . 'float : ' . $value . '<br/>'; // $doc[$varname] = (double) $this->getValDouble($value); //floatval($val); $baseArr[$varname] = (double) $this->getValDouble($value); //floatval($val); } } } } $data[$i++] = $baseArr; //make the table square.. mongo only stores fields that have been on the route // $data[$i++] = array_replace($baseArr, $doc); //make the table square.. mongo only stores fields that have been on the route // $data[$i++] = $doc; } // echo '<br/><b>data</b>'; // print_r($data); /* $stataExport = array(); $stataExport['data'] = $data; $stataExport['variables'] = $variables; $stataExport['labels'] = $labels; $stataExport['metadata'] = $metadata; */ $filename = 'xi_' . date('ymdhis') . '.dta'; $tempName = tempnam(sys_get_temp_dir(), '') . '_' . $filename; try { stata_write($tempName, array('data' => $data), $variables, array('labels' => $labels)); ob_clean(); header('Content-Type: application/stata; charset=utf-8'); header('Content-Disposition: attachment; filename=' . $filename); echo file_get_contents($tempName); unlink($tempName); exit; } catch (Exception $e) { echo $e->getMessage(); } if (file_exists($tempName)) { unlink($tempName); } } else { echo 'Stata module not loaded.'; } }