Example #1
0
 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.';
     }
 }
Example #2
0
 function loadMetadata()
 {
     //load templates and types
     require_once $this->getTemplatesLocation();
     require_once $this->getTypesLocation();
     $lastitem = 0;
     foreach ($this->getSections() as $section) {
         if ($section->questions == null) {
             $section->questions = array();
             //                $metadata = file_get_contents(prependPath($section->getQuestionLocation()));
             //                $metadata = replaceFillsToAdmin($metadata);
             /*//                eval('?>'.$metadata.'<?'); //ai :(*/
             require_once $section->getQuestionLocation();
             $arr = get_defined_vars();
             //get the defined vars from php and loop
             $startitem = 0;
             //echo '<hr>';
             foreach ($arr as $key => $obj) {
                 if ($lastitem <= $startitem) {
                     //only if not already analyzed
                     $array = is_array($obj) && sizeof($obj) > 0 ? getArray($obj, array()) : array();
                     if ($obj instanceof Question) {
                         if ($key == getTextWithoutBrackets($obj->getName())) {
                             //$array = getArray($obj, array()); //get the array index if array
                             $obj->setArray($array);
                             $obj->setName($key);
                             $section->questions[$key] = $obj;
                         }
                         // echo '<b>' . $obj->getName() . '</b>';
                     } else {
                         if ($obj instanceof QuestionGroup) {
                             if ($key == $obj->getName()) {
                                 //                                $array = getArray($obj, array()); //get the array index if array
                                 $obj->setArray($array);
                                 $obj->setName($key);
                                 $section->questionGroups[getTextWithoutBrackets($obj->getName())] = $obj;
                             }
                             //echo $obj->getName();
                         } else {
                             if ($obj instanceof Type) {
                                 $this->types[$obj->getName()] = $obj;
                                 //echo $obj->getName();
                             } else {
                                 if ($obj instanceof Template) {
                                     $this->templates[$obj->getName()] = $obj;
                                     //echo $obj->getName();
                                 }
                             }
                         }
                     }
                     //echo '<br/>';
                 }
                 $startitem++;
             }
             $lastitem = $startitem;
         }
     }
 }