232 lines
6.7 KiB
PHP
232 lines
6.7 KiB
PHP
<?php
|
|
|
|
include_once 'definition.php';
|
|
|
|
define("TEMP_FOLDER", "admin/survey/modules/mod_EVOLI/temp", true);
|
|
define("SCRIPT_FOLDER", "admin/survey/modules/mod_EVOLI/R", true);
|
|
define("RESULTS_FOLDER", "admin/survey/modules/mod_EVOLI/results", true);
|
|
|
|
class SurveyEvoli{
|
|
|
|
var $anketa; # id ankete
|
|
var $db_table = '';
|
|
|
|
|
|
function __construct($anketa){
|
|
global $site_url;
|
|
|
|
// Ce imamo anketo, smo v status->ul evealvacija
|
|
if ((int)$anketa > 0){
|
|
$this->anketa = $anketa;
|
|
|
|
# polovimo vrsto tabel (aktivne / neaktivne)
|
|
SurveyInfo :: getInstance()->SurveyInit($this->anketa);
|
|
if (SurveyInfo::getInstance()->getSurveyColumn('db_table') == 1) {
|
|
$this->db_table = '_active';
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Zgeneriramo pdf analizo
|
|
public function executeExport($usr_id){
|
|
global $site_path;
|
|
global $site_url;
|
|
global $lang;
|
|
global $admin_type;
|
|
|
|
// Zgeneriramo zacasne csv datoteke
|
|
$this->prepareCSV($usr_id);
|
|
|
|
// Poklicemo R skripto in zgeneriramo pdf
|
|
if(isset($_GET['lang']) && $_GET['lang'] == 'dan')
|
|
$script = $site_path . SCRIPT_FOLDER . '/Evoli_dan.R';
|
|
elseif(isset($_GET['lang']) && $_GET['lang'] == 'slo')
|
|
$script = $site_path . SCRIPT_FOLDER . '/Evoli_slo.R';
|
|
else
|
|
$script = $site_path . SCRIPT_FOLDER . '/Evoli_ang.R';
|
|
|
|
$out = exec('Rscript '.$script.' 2>&1', $output, $return_var);
|
|
|
|
// Testiranje - izpis errorjev
|
|
/*if($admin_type == 0){
|
|
echo '<div>';
|
|
echo 'Rscript '.$script;
|
|
//echo '<br />'.$out.'<br />';
|
|
var_dump($output);
|
|
echo '</div>';
|
|
}*/
|
|
|
|
// Ime pdf-ja glede na jezik
|
|
if(isset($_GET['lang']) && $_GET['lang'] == 'dan')
|
|
$pdf_name = 'PEQM-DK';
|
|
elseif(isset($_GET['lang']) && $_GET['lang'] == 'slo')
|
|
$pdf_name = 'PEQM-SL';
|
|
else
|
|
$pdf_name = 'PEQM-EN';
|
|
|
|
// Pripravimo file za download
|
|
if(file_exists($site_path . RESULTS_FOLDER . '/'.$pdf_name.'.pdf')){
|
|
|
|
$file = $site_path . RESULTS_FOLDER . '/'.$pdf_name.'.pdf';
|
|
|
|
header('Content-Description: File Transfer');
|
|
//header('Content-Type: application/octet-stream');
|
|
header('Content-Disposition: attachment; filename='.basename(''.$pdf_name.'.pdf'));
|
|
|
|
header("Content-type: text/x-csv; charset=utf-8");
|
|
//header("Content-type: text/csv");
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Pragma: public');
|
|
header('Content-Length: ' . filesize($file));
|
|
|
|
ob_clean();
|
|
flush();
|
|
|
|
readfile($file);
|
|
}
|
|
|
|
// Na koncu pobrisemo zacasne datoteke
|
|
$this->deleteTemp();
|
|
|
|
// Ugasnemo skripto:)
|
|
die();
|
|
}
|
|
|
|
// Pripravimo zacasne datoteke
|
|
private function prepareCSV($usr_id){
|
|
global $site_path;
|
|
|
|
$temp_folder = $site_path . TEMP_FOLDER.'/';
|
|
|
|
|
|
// Poskrbimo za datoteko s podatki
|
|
$SDF = SurveyDataFile::get_instance();
|
|
$SDF->init($this->anketa);
|
|
$SDF->prepareFiles();
|
|
|
|
$_headFileName = $SDF->getHeaderFileName();
|
|
$_dataFileName = $SDF->getDataFileName();
|
|
|
|
if ($_headFileName != null && $_headFileName != '') {
|
|
$_HEADERS = unserialize(file_get_contents($_headFileName));
|
|
}
|
|
else {
|
|
echo 'Error! Empty file name!';
|
|
}
|
|
|
|
// Zaenkrat dopuscamo samo status 6 in brez lurkerjev
|
|
$status_filter = '('.STATUS_FIELD.' ~ /6|5/)&&('.LURKER_FIELD.'==0)';
|
|
//$status_filter = '('.STATUS_FIELD.'==6)&&('.LURKER_FIELD.'==0)';
|
|
|
|
//$start_sequence = $_HEADERS['_settings']['dataSequence'];
|
|
$start_sequence = 2;
|
|
$end_sequence = $_HEADERS['_settings']['metaSequence']-1;
|
|
|
|
$field_delimit = ';';
|
|
|
|
// Filtriramo podatke po statusu in jih zapisemo v temp folder
|
|
if (IS_WINDOWS) {
|
|
$out = shell_exec('awk -F"|" "BEGIN {{OFS=\",\"} {ORS=\"\n\"}} '.$status_filter.'" '.$_dataFileName.' | cut -d "|" -f '.$start_sequence.'-'.$end_sequence.' >> '.$temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
else {
|
|
$out = shell_exec('awk -F"|" \'BEGIN {{OFS=","} {ORS="\n"}} '.$status_filter.'\' '.$_dataFileName.' | cut -d \'|\' -f '.$start_sequence.'-'.$end_sequence.' >> '.$temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
|
|
|
|
// Ustvarimo koncni CSV
|
|
if ($fd = fopen($temp_folder.'/temp_data_'.$this->anketa.'.dat', "r")) {
|
|
|
|
$fd2 = fopen($temp_folder.'/evoli.csv', "w");
|
|
|
|
$convertType = 1; // kateri tip konvertiranja uporabimo
|
|
$convertTypes[1] = array('charSet' => 'windows-1250',
|
|
'delimit' => ';',
|
|
'newLine' => "\n",
|
|
'BOMchar' => "\xEF\xBB\xBF");
|
|
# dodamo boomchar za utf-8
|
|
fwrite($fd2, $convertTypes[$convertType]['BOMchar']);
|
|
|
|
# naredimo header row
|
|
foreach ($_HEADERS AS $spid => $spremenljivka) {
|
|
if (count($spremenljivka['grids']) > 0) {
|
|
foreach ($spremenljivka['grids'] AS $gid => $grid) {
|
|
foreach ($grid['variables'] AS $vid => $variable ){
|
|
if ($spremenljivka['tip'] !== 'sm' && !($variable['variable'] == 'uid' && $variable['naslov'] == 'User ID')){
|
|
$output1 .= strip_tags($variable['variable']).$field_delimit;
|
|
$output2 .= '"'.strip_tags($variable['naslov']).'"'.$field_delimit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fwrite($fd2, $output1."\r\n");
|
|
fwrite($fd2, $output2."\r\n");
|
|
|
|
$sqlD = sisplet_query("SELECT id, recnum FROM srv_user WHERE id='".$usr_id."'");
|
|
$rowD = mysqli_fetch_array($sqlD);
|
|
|
|
while ($line = fgets($fd)) {
|
|
|
|
$temp = array();
|
|
$temp = explode('|', $line);
|
|
|
|
// Izpisemo samo vrstico z ustreznim recnumom
|
|
if($rowD['recnum'] == $temp[5]){
|
|
|
|
$line = '"' . str_replace(array("\r","\n","\"","|"), array("","","",'";"'), $line) . '"';
|
|
|
|
// Spremenimo encoding v windows-1250
|
|
//$line = iconv("UTF-8","Windows-1250//TRANSLIT", $line);
|
|
|
|
fwrite($fd2, $line);
|
|
fwrite($fd2, "\r\n");
|
|
}
|
|
}
|
|
|
|
fclose($fd2);
|
|
}
|
|
fclose($fd);
|
|
|
|
|
|
// Na koncu pobrisemo temp datoteke
|
|
if (file_exists($temp_folder.'/temp_data_'.$this->anketa.'.dat')) {
|
|
unlink($temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
}
|
|
|
|
// Pobrisemo zacasne datoteke
|
|
private function deleteTemp(){
|
|
global $site_path;
|
|
|
|
$temp_folder = $site_path . TEMP_FOLDER.'/';
|
|
|
|
if (file_exists($temp_folder.'/data_'.$this->anketa.'.csv')) {
|
|
unlink($temp_folder.'/data_'.$this->anketa.'.csv');
|
|
}
|
|
|
|
// Pobrisemo zacasno CSV datoteko s podatki - UGASNEMO, KER VCASIH NE DELA:)
|
|
if (file_exists($temp_folder.'/evoli.csv')) {
|
|
unlink($temp_folder.'/evoli.csv');
|
|
}
|
|
|
|
// Pobrisemo pdf grafe ki so bili vstavljeni v porocilo
|
|
$files = glob($site_path . RESULTS_FOLDER . '/part-predmet-slike/*');
|
|
foreach($files as $file){
|
|
if(is_file($file))
|
|
unlink($file);
|
|
}
|
|
|
|
// Pobrisemo še vse ostalo v rezultatih
|
|
$files = glob($site_path . RESULTS_FOLDER . '/*');
|
|
foreach($files as $file){
|
|
if(is_file($file))
|
|
unlink($file);
|
|
}
|
|
}
|
|
|
|
} |