<?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();                                                                                                   // 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;
	tep_delete_temp_files("survey_reports_summary_" . $login_id);
	
	$tx_report_from=isset($HTTP_POST_VARS["survey_report_from"])?tep_convert_date_raw($HTTP_POST_VARS["survey_report_from"]):getServerDate();
	$tx_report_to=isset($HTTP_POST_VARS["survey_report_to"])?tep_convert_date_raw($HTTP_POST_VARS["survey_report_to"]):getServerDate();
	if ($survey_in==1){
		$service_id=isset($HTTP_GET_VARS["service_id"])?(int)$HTTP_GET_VARS["service_id"]:0;
		$survey_type=isset($HTTP_GET_VARS["survey_type"])?$HTTP_GET_VARS["survey_type"]:'RV';
		$action="screen";
	}
	if ($action!=""){
		if ($survey_in!=1){
			$service_id=isset($HTTP_POST_VARS["select_id"])?(int)$HTTP_POST_VARS["select_id"]:0;
			$service_text=isset($HTTP_POST_VARS["select_text"])?(int)$HTTP_POST_VARS["select_text"]:0;

			$survey_type=isset($HTTP_POST_VARS["survey_type"])?$HTTP_POST_VARS["survey_type"]:'RV';
		}
		$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"]:0;
			
		// build the additional options to query
		$add_option="";
		if ($tx_report_from!="") $add_option=" and suf.survey_flag_date>='" . $tx_report_from . "' ";
		if ($tx_report_to!="") $add_option.=" and suf.survey_flag_date<='" . $tx_report_to . "' ";

		// find the survey id 
		$survey_detail_query=tep_db_query("SELECT survey_name,survey_id from " . TABLE_SERVICES_SURVEYS . " where service_id='" . $service_id . "' and survey_type='" .  tep_db_input($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;
			// get the questions for selected survey
			$question_query=tep_db_query("SELECT * from " . TABLE_SERVICES_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;
			}
		
		}
		
	} 
	if ($action=="excel"){
				// create csv file
			$file_path=DIR_FS_CATALOG . "images/survey_reports_summary_" . $login_id . "_" . time() . ".csv";
			$handle=@fopen($file_path,"w");
			if ($handle){
				$result=output_summary();
				fwrite($handle,$result,strlen($result));
				fclose($handle);
			}
			tep_redirect(DIR_WS_CATALOG . "images/survey_reports_summary_" . $login_id . "_" . time() . ".csv");
	}
?>
<!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">
	<tr>
		<?php if ($only_content!='Y') {?> 
		<?php }?>
		<!-- body_text //-->
		<td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td><?php 
							require(DIR_WS_LANGUAGES . $language . "/" . FILENAME_REPORTS_SERVICES_SURVEYS);
							require(FILENAME_REPORTS_SERVICES_SURVEYS);
						?></td>
				</tr>
<?php if ($action=="screen") {
		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>'. format_date($tx_report_from) . '&nbsp;'; 
							if ($tx_report_to!="") echo '<b>' . TEXT_TO_DATE . '</b>' . format_date($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_SERVICES_SURVEYS_FLAG . " suf, " . TABLE_SERVICES_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_SERVICES_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_SERVICES_SURVEYS_FLAG . " suf, " . TABLE_SERVICES_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 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_SERVICES_SURVEYS_FLAG . " suf, " . TABLE_SERVICES_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_SERVICES_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_SERVICES_SURVEYS_FLAG . " suf, " . TABLE_SERVICES_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;
	 }

?>