<?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');
  
	// get the type of action
	$action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';
	
	if ($action!=""){
		$service_id=isset($HTTP_POST_VARS["select_id"])?(int)$HTTP_POST_VARS["select_id"]: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_from=isset($HTTP_POST_VARS["survey_report_from"])?tep_convert_date_raw($HTTP_POST_VARS["survey_report_from"]):'';
		$tx_report_to=isset($HTTP_POST_VARS["survey_report_to"])?tep_convert_date_raw($HTTP_POST_VARS["survey_report_to"]):'';
		 
		// 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='" . $survey_type . "'");

		$question_found=false;
		$found_results=SURVEY_NO_DETAILS;
		if (tep_db_num_rows($survey_detail_query)>0) {
			$survey_detail_result=tep_db_fetch_array($survey_detail_query);
			$survey_flag_query=tep_db_query("SELECT survey_flag_id from " . TABLE_SERVICES_SURVEYS_FLAG . " suf where suf.survey_id='" . $survey_detail_result["survey_id"] . "' " . $add_option);
			
			if (tep_db_num_rows($survey_flag_query)>0)  {
				$survey_found=true;
				$found_results="SURVEY";
				// 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;
				} else {
					$found_results=SURVEY_NO_QUESTIONS;
				}
			}
		}
		
	} 
	if ($action=="excel"){
			// create csv file
			$file_path=DIR_FS_CATALOG . "images/survey_reports_detail.csv";
			$handle=fopen($file_path,"w");
			if ($handle){
				$result=output_detail();
				fwrite($handle,$result,strlen($result));
				fclose($handle);
			}
			tep_redirect(DIR_WS_CATALOG . "images/survey_reports_detail.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") {
?>
				<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>
				<tr>
					<td>
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<?php 	// get the survey_flag_dates for the given survey_id also checking dates
						
						// get all the questions to be found for this survey

						if ($found_results=="SURVEY") { // if both survey_flag and questions found
							$row=1;
							while($survey_flag=tep_db_fetch_array($survey_flag_query)){
				?>
								<tr>
									<td>
									<table border="0" cellpadding="4" cellspacing="0" class="searchArea" width="100%">
										<tr>
											<td class="tableHeading" align="center"><?php echo sprintf(SURVEY_ID,$row) ; ?></td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td>
									<table border="0" cellpadding="2" cellspacing="0" width="100%">
									<?php //display the questions
										$row2=1;
										$cur_row=0;
										tep_db_data_seek($question_query,0);
										while($question=tep_db_fetch_array($question_query)){
											// display question detail
											if ($cur_row%2==0) $row_class="attributes-even";
											else $row_class="attributes-odd";
									?>
											<tr class="<?php echo $row_class;?>">
												<td><?php echo tep_draw_separator('pixel_trans.gif',1,5);?></td>
											</tr>
											<tr class="<?php echo $row_class;?>">
												<?php 	if ($question['question_type']==6) { 
															echo '<td class="main" align="center"><b>' . $question['question_text'] . '</b></td>';
														} else {
															echo '<td class="main"><b>' . $row2 . '.&nbsp;' . $question["question_text"]; 
															if ($question["question_required"]==1) echo '&nbsp;<i>' . TEXT_REQUIRED . '</i></td>';
														}
												?>
											</tr>
												<?php	// determine type and display question answer details
													$answer_text="";
													if ($question["question_type"]<=3) {// if type is single radio or pull down choice or multiple choice
														$answer_query=tep_db_query("SELECT suc.choice_text from " . TABLE_SERVICES_SURVEYS_ANSWER . " sua, " . TABLE_SERVICES_SURVEYS_CHOICE . " suc where sua.survey_flag_id='" . $survey_flag["survey_flag_id"] . "' " .
																					"and sua.question_id='" . $question["question_id"] . "' and sua.choice_id=suc.choice_id order by suc.choice_id");
														if (tep_db_num_rows($answer_query)>0){
															while($answer=tep_db_fetch_array($answer_query)){
																if ($answer_text!="") $answer_text.="<br>";
																$answer_text.=$answer["choice_text"];
															}
														} else {
															$answer_text=QUESTION_NO_ANSWER;
														}
													} else if ($question["question_type"]==4 || $question["question_type"]==5) {// if type is text answer
														$answer_query=tep_db_query("SELECT answer_text from " . TABLE_SERVICES_SURVEYS_TEXT . " where survey_flag_id='" . $survey_flag["survey_flag_id"] . "' and question_id='" . $question["question_id"] . "'");
														if (tep_db_num_rows($answer_query)>0){
															$answer=tep_db_fetch_array($answer_query);
															$answer_text=$answer["answer_text"];
														} else {
															$answer_text=QUESTION_NO_ANSWER;
														}
													}
													if ($question['question_type']!=6){
												?>
													<tr class="<?php echo $row_class;?>">
														<td>
														<table border="0" cellpadding="2" cellspacing="0" width="100%">
															<tr>
																<td width="20">&nbsp;</td>
																<td align="left" class="main"><?php echo $answer_text;?></td>
															</tr>
														</table>
														</td>
													</tr>
												<?php 	$row2++; 
													  	} // question type if
														$cur_row++;
													  } //question while 
												?>
									</table>
									</td>
								</tr>
							<?php 
								 	$row++;
								} //survey while
							?>
						</table>
						</td>
					</tr>
				<?php } else {  ?>
				<tr>
					<td>
					<table border="0" cellpadding="5" cellspacing="0" width="100%">
						<tr>
							<td class="tableHeading" align="center"><?php echo $found_results;?></td>
						</tr>
					</table>
					</td>
				</tr>
				<?php } //found results	?>
				</table>
				<td>
			</tr>
		<?php } // action?>
		</table>
		</td>
<!-- body_text_eof //-->
	 </tr>
</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_detail(){
	 	global $add_option,$question_query,$found_results,$survey_flag_query;	
		
		$row=1;
		if ($found_results!='SURVEY'){
			$result.=sprintf("\"%s\",,,\n",$found_results);
			return $result;
		}
		while($survey_flag=tep_db_fetch_array($survey_flag_query)){
			$result.=sprintf("\"%s\"\n",SURVEY_ID . $row);
			$row2=1;
			$cur_row=0;
			tep_db_data_seek($question_query,0);
			while($question=tep_db_fetch_array($question_query)){
				// display question detail
				if ($question['question_type']==6) { 
					$result.=sprintf("\"%s\"\n",$question['question_text']);
				} else {
					$result.=sprintf("\"%d. %s\"\n",$row2,$question["question_text"] . ($question["question_required"]==1?TEXT_REQUIRED:''));
				}
				$answer_text="";
				if ($question["question_type"]<=3) {// if type is single radio or pull down choice or multiple choice
					$answer_query=tep_db_query("SELECT suc.choice_text from " . TABLE_SERVICES_SURVEYS_ANSWER . " sua, " . TABLE_SERVICES_SURVEYS_CHOICE . " suc where sua.survey_flag_id='" . $survey_flag["survey_flag_id"] . "' " .
												"and sua.question_id='" . $question["question_id"] . "' and sua.choice_id=suc.choice_id order by suc.choice_id");
					if (tep_db_num_rows($answer_query)>0){
						while($answer=tep_db_fetch_array($answer_query)){
							if ($answer_text!="") $answer_text.=", ";
							$answer_text.=$answer["choice_text"];
						}
					} else {
						$answer_text=QUESTION_NO_ANSWER;
					}
				} else if ($question["question_type"]==4 || $question["question_type"]==5) {// if type is text answer
					$answer_query=tep_db_query("SELECT answer_text from " . TABLE_SERVICES_SURVEYS_TEXT . " where survey_flag_id='" . $survey_flag["survey_flag_id"] . "' and question_id='" . $question["question_id"] . "'");
					if (tep_db_num_rows($answer_query)>0){
						$answer=tep_db_fetch_array($answer_query);
						$answer_text=$answer["answer_text"];
					} else {
						$answer_text=QUESTION_NO_ANSWER;
					}
				}
				if ($question['question_type']!=6) {
					$result.=sprintf("\"%s\"\n",$answer_text);
					$row2++; 
				} // question type if
			$cur_row++;
			} //question while
			$row++;
		} //survey while
		return $result;
	 }
?>