<?php
if($attr == "export")
{

$survey_id = '166474';
$surveytable = "lime_survey_".$survey_id;
$qntable = 'lime_questions';

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=SurveyExport.csv");

$row = 1;

function outputCSV($data) {
	
	$output = fopen("php://output", "w+");
	
		foreach ($data as $row)
			fputcsv($output, $row); 
				
	fclose($output);
}



// Get all column headers from the survey table
$qry1 = "SHOW columns FROM avenirsurvey.$surveytable";
$common->sql->query($qry1, SQL_ALL, SQL_ASSOC);
$colnames = $common->sql->record;
$colcount = count($colnames);

$row++;
$headers = array();
$colheader = array();
for($k=0;$k<$colcount;$k++)
{
	
	$headers[$k] = $colnames[$k]['Field'];
	
	
	$fld = $colnames[$k]['Field'];
	$arr = explode('X',$fld);
	$sid = trim($arr[0]);
	$gid = trim($arr[1]);
	$qid = trim($arr[2]);
	
	
	
	// Get all questions from lime table
	
	$chkqry = "SELECT title,type FROM avenirsurvey.$qntable WHERE sid=$sid AND gid=$gid AND qid=$qid";
	$common->sql->query($chkqry, SQL_ALL, SQL_ASSOC);
	$chkrec = $common->sql->record;
	$qid1 = $chkrec[0]['qid'];
	
	if(count($chkrec) > 0){
		
		$title = $chkrec[0]['title'];
		$colheader[$k] = $title;
		$qntype[$k] = $chkrec[0]['type'];
	}

	else 
	{
		$chkqry1 = "SELECT title, qid,type FROM avenirsurvey.$qntable WHERE sid=$sid AND gid=$gid and type in ('M','F') and parent_qid=0 ";
		$common->sql->query($chkqry1, SQL_ALL, SQL_ASSOC);
		$chkrec1 = $common->sql->record;
		$parent_qid = 0;
		$parent_title = '';
		
		$qntype[$k] = $chkrec2[$k]['type'];
		
		if(count($chkrec1) > 0)
		{
			$pqid = 0;
			//Match Parent qid from query to Qid in 
			for($q=0;$q<count($chkrec1);$q++)
			{
				$pqid = (string)$chkrec1[$q]['qid'];
			
				$pos = stripos($qid, $pqid);
				if(stripos($qid, $pqid) !== false){
					
					$parent_qid = $chkrec1[$q]['qid'];
					$parent_title = $chkrec1[$q]['title'];
				}
			}
			
				$chkqry2 = "SELECT title,qid,type FROM avenirsurvey.$qntable WHERE parent_qid=$parent_qid and sid=$sid AND gid=$gid and type in ('M','F') ";
				$common->sql->query($chkqry2, SQL_ALL, SQL_ASSOC);
				$chkrec2 = $common->sql->record;
				for($l=0;$l<count($chkrec2);$l++)
				{
				
					
					$title = $parent_title.'[' .$chkrec2[$l]['title'].']';
					
					$colheader[$k] = $title;
					$qntype[$k] = $chkrec2[$l]['type'];
					$headers[$k] = $colnames[$k]['Field'];
					$k++;
				}
				//Reduce one column
				$k--;
				
			
		}
		else{
			
			$title = $colnames[$k]['Field'];
			$colheader[$k] = $title;
			$qntype[$k] = $chkrec1[$k]['type'];
		}
		
		
	}
	
	

}

unset($colheader[1]);
outputCSV(array($colheader));

$qry = "SELECT * FROM avenirsurvey.$surveytable WHERE submitdate != ''";
$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
$rec = $common->sql->record;
$cols = array();
for($i=0;$i<count($rec);$i++)
{
	for($k=0;$k<$colcount;$k++)
	{
		$fd = $headers[$k];
		
		$cols[$k] = $rec[$i][$fd];
	
		$fld = $headers[$k];
		$arr = explode('X',$fld);
		$sid = trim($arr[0]);
		$gid = trim($arr[1]);
		$qid = trim($arr[2]);
		$titlecol = $cols[$k];
				
		// Commenting the below lines since they now want only answer code to be shown not answers
		/* $ans_tbl = 'lime_answers';
		$qry2 = "SELECT answer FROM avenirsurvey.$ans_tbl WHERE qid=$qid AND code='$titlecol'";
	
		$common->sql->query($qry2, SQL_ALL, SQL_ASSOC);
		$vals = $common->sql->record;
		if(count($vals) > 0)
		{
		$val = $vals[0]['answer'];
		}
		else 
			$val = $titlecol;
		$cols[$k] = $val; */
		
		if(trim($titlecol) == 'Y' && (trim($qntype[$k]) == 'M' || trim($qntype[$k]) == 'F'))
			$cols[$k] = 1;
		elseif($titlecol === NULL && (trim($qntype[$k]) == 'M' || trim($qntype[$k]) == 'F'))
			$cols[$k] = '';
		elseif(trim($titlecol) == '' && (trim($qntype[$k]) == 'M' || trim($qntype[$k]) == 'F'))
			$cols[$k] = 0;
		
		if(strpos($cols[$k],"00:00") !== false)
		{
			if($colheader[$k] == 'DG1')
			{
				$cols[$k] = rtrim($cols[$k]);
				$cols[$k] = substr_replace($cols[$k],"", -8);
			}
		}
	}
	unset($cols[1]);
	outputCSV(array($cols));
}

}
elseif($attr == "export1")
{
	header("Content-Type: text/csv");
	header("Content-Disposition: attachment; filename=FingerprintExport.csv");
	
	$row1 = 1;
	
	function outputCSV1($data) 
	{
	
		$output = fopen("php://output", "r");
				
		foreach ($data as $row1)
			fputcsv($output, $row1);
			
		fclose($output);
	}
	
	$qry1 = "SHOW COLUMNS from participant_finger_print where field in ('participant_id','Parent_id','finger_id','ICCID','RDS1ID','RDS2ID','RDS1Flg','RDS2Flg','ICCFlg','CreatedDate','CreatedTime')";
	$common->sql->query($qry1, SQL_ALL, SQL_ASSOC);
	$colnames = $common->sql->record;
	$colcount = count($colnames);
	
	$headers = array();
	
	$row1++;
	
	for($k=0;$k<$colcount;$k++)
	{
		$headers[$k] = $colnames[$k]['Field'];
	}	
	
	outputCSV1(array($headers));
	
	$table = "participant_finger_print";
	
	$qry = "SELECT participant_id,Parent_id,finger_id,ICCID,RDS1ID,RDS2ID,RDS1Flg,RDS2Flg,ICCFlg,CreatedDate,CreatedTime 
			FROM avenircms.participant_finger_print";
	
	$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
	$rec = $common->sql->record;
	$cols = array();

	for($i=0;$i<count($rec);$i++)
	{
		for($k=0;$k<$colcount;$k++)
		{
			$fd = $headers[$k];
		
			$cols[$k] = $rec[$i][$fd];
		}
		
		outputCSV1(array($cols));
	}	 
	
	
			
}
?>