<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

	require('includes/application_top.php');
	tep_get_last_access_file();
	require('includes/classes/pdfTable.php');
	require('includes/languages/'. $language .'/reports_events_surveys.php');
	// get the type of action
	$action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';
	$survey_in=isset($HTTP_GET_VARS["survey_in"])?$HTTP_GET_VARS["survey_in"]:0;
	
	if ($survey_in==1){
		$events_id=isset($HTTP_GET_VARS["events_id"])?(int)$HTTP_GET_VARS["events_id"]:0;
		$survey_type=isset($HTTP_GET_VARS["survey_type"])?$HTTP_GET_VARS["survey_type"]:'CD';
		$action="screen";
	}
	if ($action!="" || $action==""){
		if ($survey_in!=1){
			$events_id=isset($HTTP_POST_VARS["select_id"])?(int)$HTTP_POST_VARS["select_id"]:1;

			$survey_type=isset($HTTP_POST_VARS["survey_type"])?$HTTP_POST_VARS["survey_type"]:'CD';
		}
		$only_content = (isset($HTTP_GET_VARS['only_content']) ? $HTTP_GET_VARS['only_content'] : 'N');
		$tx_report_type=isset($HTTP_POST_VARS["survey_report_type"])?(int)$HTTP_POST_VARS["survey_report_type"]:1;
		$tx_report_from=isset($HTTP_POST_VARS["survey_report_from"])?$HTTP_POST_VARS["survey_report_from"]:'';
		$tx_report_to=isset($HTTP_POST_VARS["survey_report_to"])?$HTTP_POST_VARS["survey_report_to"]:'';
		//Fill display headers
		$display_header ="";
		$display_header .= SURVEY_DATE_FROM.'&nbsp;&nbsp;'.$tx_report_from ."\t"; 
		$display_header .= SURVEY_DATE_TO . '&nbsp;&nbsp;'. $tx_report_to ."\t";	
		$event_name_query=tep_db_query("select events_name from ".TABLE_EVENTS_DESCRIPTION." where events_id= '". $events_id ."' "); 
		$event_name=tep_db_fetch_array($event_name_query);
		$display_header .= SURVEY_EVENT.':'.'&nbsp;' .$event_name['events_name']."\t";
		// build the additional options to query
		$add_option="";
		if ($tx_report_from!="") $add_option=" and suf.survey_flag_date>='" . tep_convert_date_raw($tx_report_from) . "' ";
		if ($tx_report_to!="") $add_option.=" and suf.survey_flag_date<='" . tep_convert_date_raw($tx_report_to) . "' ";

		// find the survey id
		$survey_detail_query=tep_db_query("SELECT survey_name,survey_id,survey_type from " . TABLE_EVENTS_SURVEYS . " where events_id='" . $events_id . "' and survey_type='" . $survey_type . "'");
		$survey_found=false;
		$question_found=false;
		if (tep_db_num_rows($survey_detail_query)>0) {
			$survey_detail_result=tep_db_fetch_array($survey_detail_query);
			$survey_found=true;
			//Fill the display header
			if($survey_detail_result['survey_type']=='RV')
			   $display_header .=SURVEY_TYPE .'&nbsp;&nbsp;'. TEXT_RV;
			elseif($survey_detail_result['survey_type']=='WV') 
			    $display_header .=SURVEY_TYPE .'&nbsp;&nbsp;'. TEXT_WV;    
			elseif($survey_detail_result['survey_type']=='FV')
			     $display_header .=SURVEY_TYPE .'&nbsp;&nbsp;'. TEXT_FV;
			else	 
				 $display_header .=SURVEY_TYPE .'&nbsp;&nbsp;'. TEXT_CD;	
				 
			// get the questions for selected survey
			$question_query=tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . $survey_detail_result["survey_id"] . "' and (question_type=6 or question_type<4) order by question_id");
			if (tep_db_num_rows($question_query)){
				$question_found=true;
			}
		}
		
	} 
	$report_filename=sprintf("survey_reports_summary_%s_%s",$login_id,time());
	//echo $report_filename;
	if ($action=="excel"){
				// create csv file
			$file_path=DIR_FS_CATALOG . "images/".$report_filename.".csv";
			$handle=@fopen($file_path,"w");
			if ($handle){
				$result=output_summary();
				fwrite($handle,$result,strlen($result));
				fclose($handle);
			}
			tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".csv");
	}
	if($action=="pdf"){
	    generate_pdf();
		 tep_redirect(DIR_WS_CATALOG . "images/".$report_filename.".pdf");
		 return;
	}tep_delete_temp_files("survey_reports_summary_" . $login_id);
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" >
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php if ($only_content!='Y') require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2" id="holetable">
	<tr>
		<?php if ($only_content!='Y') {?> 
		<?php }?>
		<!-- body_text //-->
		<td width="100%" valign="top" id="formtable" >
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td ><?php 
							require(DIR_WS_LANGUAGES . $language . "/" . FILENAME_EVENTS_REPORTS_SURVEYS);
							require(FILENAME_EVENTS_REPORTS_SURVEYS);

						?></td>
				</tr>
<?php if ($action=="screen" || $action=="") {
		if ($survey_found) {
?>
				<tr>
					<td class="pageHeading" align="center"><?php echo sprintf(HEADING_TITLE_RESULTS,$survey_detail_result['survey_name']) ; ?></td>
				</tr>
				<tr>
					<td class="main" align="center">
					<?php 	
					if ($tx_report_from!="") echo '<b>' . TEXT_FROM_DATE . '</b>'. $tx_report_from . '&nbsp;'; 
							if ($tx_report_to!="") echo '<b>' . TEXT_TO_DATE . '</b>' . $tx_report_to;
					?>
					</td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
				</tr>
				<?php 
					if ($question_found){
				?>
					<tr>
						<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%">
							<tr>
								<td>
								<table border="0" cellpadding="5" cellspacing="0" class="searchArea" width="100%">
									<tr>
										<td class="tableHeading" align="left"><?php echo SUMMARY_CHOICE ; ?></td>
										<td class="tableHeading" align="right" width="100"><?php echo SUMMARY_TOTAL ; ?></td>
										<td class="tableHeading" align="right" width="100"><?php echo SUMMARY_PERCENT ; ?></td>
									</tr>
								</table>
								</td>
							</tr>
							<tr>
								<td class="dataTableRow">
								<table border="0" cellpadding="2" cellspacing="0" width="100%">
									<?php //display the questions
										$row=1;
										while($question=tep_db_fetch_array($question_query)){
											if ( $question["question_type"]==6){ // if question type is heading
									?>
												<tr>
													<td class="tableHeading" align="left"><?php $question["question_text"] ; ?></td>
												</tr>
										<?php 
											 } else if ($question["question_type"]<4) { // if question type is multiple choice
										?>
												<tr>
													<td class="main" align="left">
													<?php 	echo '<b>' . $row . '.&nbsp;' . $question["question_text"] .'</b>'; 
															if ($question["question_required"]==1) echo '&nbsp;<i>' . TEXT_REQUIRED . '</i>';
													?>
													</td>
												</tr>
												<?php	// get the total count of choice for current question
													$total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.question_id='" . $question['question_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.question_id");
													$total_result=tep_db_fetch_array($total_query);
													$total_count=(int)$total_result["total"];
													
													// get the choices for the question
													$choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . $question["question_id"] . "' order by choice_id");
													if (tep_db_num_rows($choice_query)>0) {
												?>
												<tr>
													<td>
													<table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
													<?php 
													$row2=0;

													while($choice=tep_db_fetch_array($choice_query)){
														// get the total for the current choice
														$choice_total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.choice_id='" . $choice['choice_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.choice_id");
														$choice_total_result=tep_db_fetch_array($choice_total_query);
														$choice_total_count=(int)$choice_total_result['total'];
														$percent=0;
														if ($total_count>0)
															$percent=number_format($choice_total_count/$total_count*100,0);
													?>
														<tr class="<?php echo $row2%2==0?"attributes-even":"attributes-odd";?>">
															<td class="main" width="20">&nbsp;</td>
															<td class="main"><?php echo $choice["choice_text"];?></td>
															<td class="main" width="100" align="right"><?php echo $choice_total_count;?></td>
															<td class="main" width="100" align="right"><?php echo $percent;?>%</td>
														</tr>
													<?php $row2++; 
															} //choice while ?>
														<tr>
															<td class="main" width="20">&nbsp;</td>
															<td class="main" align="center"><i><?php echo TOTAL_CHOICE_QUESTIONS;?></i></td>
															<td class="main" width="100" align="right"><b><?php echo $total_count; ?></b></td>
															<td class="main">&nbsp;</td>
														</tr>
													</table>
													</td>
												</tr>
										<?php 
													} //choice if
											 } // question_type if
											 if ($question['question_type']!=6)$row++;
										} //question while
									?>
								</table>
								</td>
							</tr>
						</table>
						<td>
					</tr>
						<?php } else {  ?>
					<tr>
						<td>
						<table border="0" cellpadding="5" cellspacing="0" width="100%">
							<tr>
								<td class="main" align="center"><?php echo SURVEY_NO_QUESTIONS;?></td>
							</tr>
						</table>
						</td>
					</tr>
				<?php	} //report type 1 cnt ?>
		</table>
		</td>
<!-- body_text_eof //-->
	 </tr>
	 <?php }  else { // $survey_found 
	 ?>
		<tr>
			<td>
			<table border="0" cellpadding="5" cellspacing="0" width="100%">
				<tr>
					<td class="main" align="center"><?php echo TEXT_NO_RECORDS_FOUND;?></td>
				</tr>
			</table>
			</td>
		</tr>
	 <?php
	 		} // survey_found
	 	} // action
	 ?>
</table>
</td>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 

	function generate_pdf(){
		global $question_query,$question_found,$survey_found,$display_header,$report_filename;
		
		// set initial params
		$table=new pdfTable("A4","l");
		$table->left_margin=20;
		$table->top_margin=20;
		$table->right_margin=20;
		$table->bottom_margin=20;
		$table->pdfInit();
		
		// Add the styles
		$table->AddStyle("heading","color:#727272;bgcolor:#ffffff;font:Arial;size:18;style:B");
		$table->AddStyle("user","color:#000000;bgcolor:#f1f9fe;font:Arial;size:14;style:B;border-color:#7b9ebd");
		$table->AddStyle("subhead","color:#000000;bgcolor:#7b9ebd;font:Arial;size:10;style:B");
		$table->AddStyle("headrow","color:#000000;bgcolor:#C9C9C9;font:Arial;size:10;style:B");
		$table->AddStyle("row","color:#000000;bgcolor:#F0F1F1;font:Arial;size:10");
		$table->AddStyle("query","color:#000000;bgcolor:#FFFFFF;font:Arial;size:11");
		$table->AddStyle("subrow","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10");
		$table->AddStyle("subrowhead","color:#000000;bgcolor:#FFFFFF;font:Arial;size:10;style:B");

		
		$table->headers["text"]=$display_header;
		$table->headers["style"]="query";
		$table->headers["height"]=15;
		$table->headers["width"]="100%";
		$table->headers["cols"]=4;

		//define the column width
		$widths[0]=$table->width*5/100;
		$widths[2]=$table->width*30/100;
		$widths[3]=$table->width*10/100;
		$widths[4]=$table->width*10/100;

		$widths[1]=$table->width-($widths[0]+$widths[2]+$widths[3]+$widths[4]);
		// create the table header columns
		$cols=array();
		if(!$survey_found){
		  $cols[]=array("text"=>TEXT_NO_RECORDS_FOUND);
		  $table->tableheaders["text"]=$cols;
		  unset($cols);
		}
		if(!$question_found){
		  $cols[]=array("text"=>SURVEY_NO_QUESTIONS);
		  $table->tableheaders["text"]=$cols;
		  unset($cols);
		 } 
		  
		$cols[]=array("text"=>SUMMARY_CHOICE,"width"=>$widths[1],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>SUMMARY_TOTAL,"width"=>$widths[0],"align"=>"L","style"=>"headrow","valign"=>"M");
		$cols[]=array("text"=>SUMMARY_PERCENT,"width"=>$widths[4],"align"=>"L","style"=>"headrow","valign"=>"M");
		$table->tableheaders["text"]=$cols;
		unset($cols);
		if($question_found){
		while($question=tep_db_fetch_array($question_query)){
			if ( $question["question_type"]==6){ // if question type is heading
				$cols[]=array("text"=>$question["question_text"],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
				$found_question=true;
			} else if ($question["question_type"]<4) { // if question type is multiple choice
				$cols[]=array("text"=>$question["question_text"] . ($question["question_required"]==1?TEXT_REQUIRED:''),"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
				$cols[]=array("text"=>'',"width"=>$widths[4],"align"=>"L","style"=>"row","valign"=>"M");
				$table->OutputRowMultiple($cols,15);
				unset($cols);
				// get the total count of choice for current question
				$total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.question_id='" . $question['question_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.question_id");
				$total_result=tep_db_fetch_array($total_query);
				$total_count=(int)$total_result["total"];
				// get the choices for the question
				$choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . $question["question_id"] . "' order by choice_id");
				if (tep_db_num_rows($choice_query)>0) {
					$row2=0;
					while($choice=tep_db_fetch_array($choice_query)){
						// get the total for the current choice
						$choice_total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.choice_id='" . $choice['choice_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.choice_id");
						$choice_total_result=tep_db_fetch_array($choice_total_query);
						$choice_total_count=(int)$choice_total_result['total'];
						$percent=0;
						if ($total_count>0)
							$percent=number_format($choice_total_count/$total_count*100,0);
						$cols[]=array("text"=>$choice["choice_text"],"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
						$cols[]=array("text"=>$choice_total_count.'',"width"=>$widths[0],"align"=>"L","style"=>"row","valign"=>"M");
						$cols[]=array("text"=>$percent.'%',"width"=>$widths[4],"align"=>"L","style"=>"row","valign"=>"M");
						$row2++; 
						$table->OutputRowMultiple($cols,15);
						unset($cols);
					}
					$cols[]=array("text"=>TOTAL_CHOICE_QUESTIONS,"width"=>$widths[1],"align"=>"L","style"=>"row","valign"=>"M");
					$cols[]=array("text"=>$total_count.'',"width"=>$widths[4],"align"=>"L","style"=>"row","valign"=>"M");
					$table->OutputRowMultiple($cols,15);
						unset($cols);
				}
				$found_question=true;
			}
			 if ($question['question_type']!=6)$row++;
		}
 	}else{
			$cols[]=array("text"=>TEXT_NO_RECORDS_FOUND,"width"=>"100%","align"=>"C","style"=>"subrow","valign"=>"M");
			$table->OutputRow($cols,15);
			unset($cols);
			}
	$table->Render($report_filename.".pdf");
}


	 function output_summary(){
	 	global $question_query,$question_found,$survey_found;
		$result="";

		if (!$survey_found){
			$result.=sprintf("\"%s\",,,\n",TEXT_NO_RECORDS_FOUND);
			return $result;
		}
		if (!$question_found){
			$result.=sprintf("\"%s\",,,\n",SURVEY_NO_QUESTIONS);
			return $result;
		}
		$result.=sprintf("\"%s\",\"%s\",\"%s\",\"%s\"\n",SUMMARY_QUESTION,SUMMARY_CHOICE ,SUMMARY_TOTAL ,SUMMARY_PERCENT);
		$row=1;
		$found_question=false;
		while($question=tep_db_fetch_array($question_query)){
			if ( $question["question_type"]==6){ // if question type is heading
				$result.=sprintf("\"%s\",,,\n",$question["question_text"]);
				$found_question=true;
			} else if ($question["question_type"]<4) { // if question type is multiple choice
				$result.=sprintf("\"%ld. %s\",,,\n",$row,$question["question_text"] . ($question["question_required"]==1?TEXT_REQUIRED:''));
				// get the total count of choice for current question
				$total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.question_id='" . $question['question_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.question_id");
				$total_result=tep_db_fetch_array($total_query);
				$total_count=(int)$total_result["total"];
				
				// get the choices for the question
				$choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . $question["question_id"] . "' order by choice_id");
				if (tep_db_num_rows($choice_query)>0) {
					$row2=0;
					while($choice=tep_db_fetch_array($choice_query)){
						// get the total for the current choice
						$choice_total_query=tep_db_query("SELECT count(sua.choice_id) as total from " . TABLE_EVENTS_SURVEYS_FLAG . " suf, " . TABLE_EVENTS_SURVEYS_ANSWER . " sua where sua.choice_id='" . $choice['choice_id'] . "' " . $add_option . " and suf.survey_flag_id=sua.survey_flag_id group by sua.choice_id");
						$choice_total_result=tep_db_fetch_array($choice_total_query);
						$choice_total_count=(int)$choice_total_result['total'];
						$percent=0;
						if ($total_count>0)
							$percent=number_format($choice_total_count/$total_count*100,0);
						
						$result.=sprintf(",\"%s\",%ld,%d%%\n",$choice["choice_text"],$choice_total_count,$percent);
						$row2++; 
					}
					$result.=sprintf(",\"%s\",%ld,\n",TOTAL_CHOICE_QUESTIONS,$total_count);
				}
				$found_question=true;
			}
			 if ($question['question_type']!=6)$row++;
		}
		return $result;
	 }

?>