<?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);
//echo $colcount;
$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";
	//echo $chkqry;
	$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'];
		//echo 'type='.$qntype[$k];
	}

	else 
	{
		$chkqry1 = "SELECT title, qid,type FROM avenirsurvey.$qntable WHERE sid=$sid AND gid=$gid and type in ('M','F') and parent_qid=0 ";
		//echo $chkqry1;
		$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'];
				//echo 'pqid='.$pqid;
			
				$pos = stripos($qid, $pqid);
				//echo 'pos='.$pos;
				 
				 if(stripos($qid, $pqid) !== false){
				 	//echo 'q='.$q;
					
					$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') order by question_order";
				$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];
				
	
		
		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));
	}	 
	
	
			
}
elseif($attr == "export2")
{
	header("Content-Type: text/csv");
	header("Content-Disposition: attachment; filename=ScreeningExport.csv");
	
	$row2 = 1;
	
	function outputCSV2($data)
	{
	
		$output = fopen("php://output", "r");
	
		foreach ($data as $row1)
			fputcsv($output, $row1);
				
			fclose($output);
	}
	
	$qry2 = "SHOW COLUMNS from custom_msm_screening where field in ('mem_scrn_id','mem_scrn_survey','mem_scrn_part_id','mem_scrn_q1','mem_scrn_q1a','mem_scrn_q2','mem_scrn_q3','mem_scrn_q4','mem_scrn_dob','mem_scrn_q5','mem_scrn_q6','mem_scrn_q7','mem_scrn_q8','mem_scrn_q9','mem_scrn_q10','mem_scrn_q11','mem_scrn_q12','mem_scrn_q13','status','create_time','create_user','update_time','update_user')";
	$common->sql->query($qry2, SQL_ALL, SQL_ASSOC);
	$colnames = $common->sql->record;
	$colcount = count($colnames);
	
	$headers = array();
	$headers1 = array();
	$row2++;
	
	for($k=0;$k<$colcount;$k++)
	{
		$headers[$k] = $colnames[$k]['Field'];
		$headers1[$k] = $colnames[$k]['Field'];
		$dob = $headers[17];
	}
	
	for($j=9;$j<=17;$j++)
	{
		$headers[$j] = $headers1[$j-1];
		
	}
	$headers[8] = $dob;
	
	outputCSV2(array($headers));
	
	$table = "custom_msm_screening";
	
	$qry = "SELECT mem_scrn_id,mem_scrn_survey,mem_scrn_part_id,mem_scrn_q1,mem_scrn_q1a,mem_scrn_q2,mem_scrn_q3,mem_scrn_q4,mem_scrn_dob,mem_scrn_q5,mem_scrn_q6,
			mem_scrn_q7,mem_scrn_q8,mem_scrn_q9,mem_scrn_q10,mem_scrn_q11,mem_scrn_q12,mem_scrn_q13,status,create_time,create_user,
			update_time,update_user FROM avenircms.custom_msm_screening";
	
	$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];
		}
	
		outputCSV2(array($cols));
	}
	
}
?>