Example #1
0
 function generateParadata()
 {
     /* set arrays */
     if (trim($this->getProperty(DATA_OUTPUT_MODES)) != "") {
         $this->setProperty(DATA_OUTPUT_MODES, explode("~", $this->getProperty(DATA_OUTPUT_MODES)));
     } else {
         $this->setProperty(DATA_OUTPUT_MODES, array());
     }
     if (trim($this->getProperty(DATA_OUTPUT_LANGUAGES)) != "") {
         $this->setProperty(DATA_OUTPUT_LANGUAGES, explode("~", $this->getProperty(DATA_OUTPUT_LANGUAGES)));
     } else {
         $this->setProperty(DATA_OUTPUT_LANGUAGES, array());
     }
     if (trim($this->getProperty(DATA_OUTPUT_VERSIONS)) != "") {
         $this->setProperty(DATA_OUTPUT_VERSIONS, explode("~", $this->getProperty(DATA_OUTPUT_VERSIONS)));
     } else {
         $this->setProperty(DATA_OUTPUT_VERSIONS, array());
     }
     $this->languages = $this->getProperty(DATA_OUTPUT_LANGUAGES);
     $this->modes = $this->getProperty(DATA_OUTPUT_MODES);
     $this->versions = $this->getProperty(DATA_OUTPUT_VERSIONS);
     /* set file names */
     $this->setProperty(DATA_OUTPUT_FILENAME_CSV, $this->getProperty(DATA_OUTPUT_FILENAME) . "_paradata.csv");
     $this->separator = ",";
     $_SESSION['PARAMETER_RETRIEVAL'] = PARAMETER_SURVEY_RETRIEVAL;
     set_time_limit(0);
     // check for filter
     $filter = array();
     $extra = '';
     if ($this->getProperty(DATA_OUTPUT_VARLIST) != "") {
         $filter = explode("~", $this->getProperty(DATA_OUTPUT_VARLIST));
         $extra = " AND (variable='" . implode("' OR variable='", $filter) . "')";
         //echo $extra;
         //exit;
     }
     if ($this->getProperty(DATA_OUTPUT_FROM) != "") {
         $extra .= " and ts > '" . $this->getProperty(DATA_OUTPUT_FROM) . "'";
     }
     if ($this->getProperty(DATA_OUTPUT_TO) != "") {
         $extra .= " and ts < '" . $this->getProperty(DATA_OUTPUT_TO) . "'";
     }
     $decrypt = "paradata as paradata_dec";
     if ($this->survey->getDataEncryptionKey() != "") {
         $decrypt = "aes_decrypt(paradata, '" . $this->survey->getDataEncryptionKey() . "') as paradata_dec";
     }
     $data = '';
     $select = "select primkey, displayed, language, mode, {$decrypt}, ts from " . $this->getProperty(DATA_OUTPUT_MAINDATATABLE) . "_paradata where suid=" . $this->suid . " and length(primkey) >= " . $this->minprimkeylength . " and length(primkey) < " . $this->maxprimkeylength . $extra . " order by primkey asc, pid asc";
     //echo $select;
     //exit;
     $res = $this->db->selectQuery($select);
     if ($this->db->getNumberOfRows($res) > 0) {
         while ($row = $this->db->getRow($res)) {
             //print_r($row);
             //if (is_numeric($row["primkey"])) {
             /* no match on language, mode and version, then treat as never gotten */
             if (!(sizeof($this->languages) == 0 || sizeof($this->languages) > 0 && inArray($row["language"], $this->languages))) {
                 //
                 continue;
             } else {
                 if (!(sizeof($this->modes) == 0 || sizeof($this->modes) > 0 && inArray($row["mode"], $this->modes))) {
                     //
                     continue;
                 } else {
                     if (sizeof($filter) > 0) {
                         $include = true;
                         $displayed = explode("~", $row["displayed"]);
                         foreach ($displayed as $d) {
                             if (!inArray(getBasicName($d), $filter)) {
                                 $include = false;
                             }
                         }
                         if ($include == false) {
                             continue;
                         }
                     } else {
                         $include = true;
                         $displayed = explode("~", $row["displayed"]);
                         foreach ($displayed as $d) {
                             $vd = $this->getVariableDescriptive(getBasicName($d));
                             if ($vd->getVsid() != "") {
                                 // if info not found, then ignore since we don't know how to handle it
                                 // hidden variable
                                 if ($this->getProperty(DATA_OUTPUT_HIDDEN) == HIDDEN_YES && $vd->isHidden()) {
                                     $include = false;
                                 }
                             }
                         }
                         if ($include == false) {
                             continue;
                         }
                     }
                 }
             }
             $line = '';
             if ($this->getProperty(DATA_OUTPUT_PRIMARY_KEY_IN_DATA) != PRIMARYKEY_NO) {
                 if ($this->getProperty(DATA_OUTPUT_PRIMARY_KEY_ENCRYPTION) != "") {
                     $line .= getValueForCsv(encryptC($row["primkey"], $this->getProperty(DATA_OUTPUT_PRIMARY_KEY_ENCRYPTION)));
                 } else {
                     $line .= getValueForCsv($row["primkey"]);
                 }
             }
             $line .= getValueForCsv($row["displayed"]);
             $line .= getValueForCsv($row["paradata_dec"]);
             $line .= getValueForCsv($row["language"]);
             $line .= getValueForCsv($row["mode"]);
             $line .= getValueForCsv($row["ts"]);
             if (trim($line) != "") {
                 $data .= trim($line) . "\n";
             }
             //}
         }
     }
     $this->csvhandle = null;
     $outdir = sys_get_temp_dir();
     if (!endsWith($outdir, DIRECTORY_SEPARATOR)) {
         $outdir .= DIRECTORY_SEPARATOR;
     }
     $this->csvhandle = fopen($outdir . $this->getProperty(DATA_OUTPUT_FILENAME_CSV), "w");
     if (!$this->csvhandle) {
         /* show error */
         return;
     }
     $this->downloadlocation = $outdir . $this->getProperty(DATA_OUTPUT_FILENAME_CSV);
     if ($this->getProperty(DATA_OUTPUT_PRIMARY_KEY_IN_DATA) != PRIMARYKEY_NO) {
         $header = 'primkey' . $this->separator . 'variable(s)' . $this->separator . 'paradata' . $this->separator . 'language' . $this->separator . 'mode' . $this->separator . 'date/time';
     } else {
         $header = 'variable(s)' . $this->separator . 'paradata' . $this->separator . 'language' . $this->separator . 'mode' . $this->separator . 'date/time';
     }
     $data = str_replace("\r", "", $data);
     if ($data == "") {
         $data = "\n(0) Records Found!\n";
     }
     // write file
     fwrite($this->csvhandle, "{$header}\n{$data}");
     /* finish */
     $this->finishCSVFile();
     $_SESSION['PARAMETER_RETRIEVAL'] = PARAMETER_ADMIN_RETRIEVAL;
 }
Example #2
0
function createCSV($export, $filename)
{
    ob_end_clean();
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=" . $filename . ".csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    $header = "";
    $fields = mysqli_fetch_fields($export);
    foreach ($fields as $val) {
        $header .= $val->name . ",";
    }
    $data = '';
    while ($row = mysqli_fetch_assoc($export)) {
        $line = '';
        foreach ($row as $value) {
            $line .= getValueForCsv($value, ',');
        }
        $data .= trim($line) . "\n";
    }
    $data = str_replace("\r", "", $data);
    if ($data == "") {
        $data = "\n(0) Records Found!\n";
    }
    print "{$header}\n{$data}";
    exit;
}