<?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"]:'';
	
	$events_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"])?$HTTP_POST_VARS["survey_report_from"]:'';
	$tx_report_to=isset($HTTP_POST_VARS["survey_report_to"])?$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_EVENTS_SURVEYS . " where events_id='" . $events_id . "' and survey_type='" . $survey_type . "'");

?>
<!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" onLoad="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
		<!-- body_text //-->
		<td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php if ($action=='') { ?>
			<tr>
				<td class="pageHeading"><?php echo HEADING_TITLE ; ?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
			<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
			<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
			<script language="javascript"><!--
				var dateFrom = new ctlSpiffyCalendarBox("dateFrom", "survey_reports", "survey_report_from","btnDate1","",scBTNMODE_CUSTOMBLUE);
				var dateTo = new ctlSpiffyCalendarBox("dateTo", "survey_reports", "survey_report_to","btnDate2","",scBTNMODE_CUSTOMBLUE);
			//--></script>
			<?php // get the survey names
				$survey_query=tep_db_query("SELECT survey_id,survey_name from " . TABLE_EVENTS_SURVEYS . " order by survey_name");
				$surveys=array();
				while($survey_result=tep_db_fetch_array($survey_query)){
					$surveys[]=array('id'=>$survey_result["survey_id"],'text'=>$survey_result["survey_name"]);
				}
				// get the report types
				$report_types=array();
				for ($icnt=1;$icnt<=4;$icnt++) $report_types[]=array('id'=>$icnt,'text'=>constant('REPORT_TYPE_' . $icnt));
				$report_to=array(array('id'=>1,'text'=>REPORT_TO_SCREEN),array('id'=>2,'text'=>REPORT_TO_FILE));
				echo tep_draw_form("survey_reports",FILENAME_EVENTS_REPORTS_SURVEYS_CROSSTAB);
				echo tep_draw_hidden_field("post_action","report");
			?>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="searchArea">
					<tr>
						<td><?php echo SURVEY_NAME;?></td>
						<td><?php echo tep_draw_separator('pixel_trans.gif','10','1') . tep_draw_pull_down_menu('survey_id',$surveys,$tx_id);?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
					<tr>
						<td><?php echo SURVEY_REPORT_TYPE;?></td>
						<td><?php echo tep_draw_separator('pixel_trans.gif','10','1') . tep_draw_pull_down_menu('survey_report_type',$report_types,$tx_report_type);?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
					<tr>
						<td><?php echo SURVEY_DATE_FROM;?><br><small>(YYYY-MM-DD)</small></td>
						<td><?php echo tep_draw_separator('pixel_trans.gif','10','1');?><script language="javascript">dateFrom.writeControl(); dateFrom.dateFormat="yyyy-MM-dd";</script></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
					<tr>
						<td><?php echo SURVEY_DATE_TO;?><br><small>(YYYY-MM-DD)</small></td>
						<td><?php echo tep_draw_separator('pixel_trans.gif','10','1');?><script language="javascript">dateTo.writeControl(); dateTo.dateFormat="yyyy-MM-dd";</script></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
					<tr>
						<td><?php echo SURVEY_REPORT_TO;?></td>
						<td><?php echo tep_draw_separator('pixel_trans.gif','10','1') . tep_draw_pull_down_menu('survey_output_to',$report_to,$tx_output_to);?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
					<tr>
						<td align="right" colspan="2">
						<?php 
							echo tep_image_submit('button_submit.gif',IMAGE_SUBMIT) . '&nbsp;';
							echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_SUMMARY) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>';
						?>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			<?php } else if ($action=='show') { //output headers ?>
				<tr>
					<td class="pageHeading" align="center"><?php echo sprintf(HEADING_TITLE_RESULTS,constant("REPORT_TYPE_" . $tx_report_type),"test") ; ?></td>
				</tr>
				<tr>
					<td class="main" align="center">
					<?php 	if ($tx_report_from!="") echo FROM_DATE . format_date($tx_report_from) . '&nbsp;'; 
							if ($tx_report_to!="") echo TO_DATE . format_date($tx_report_to);
					?>
					</td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
				</tr>
				<?php if ($tx_report_type==1) {  // summary report
						// get the questions for selected survey
						$question_query=tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS_QUESTION . " question where survey_id='" . $tx_id . "'");
						if (tep_db_num_rows($question_query)>0){
				?>
					<tr>
						<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%">
							<tr>
								<td>
								<table border="0" cellpadding="5" cellspacing="0" class="formArea" 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=$choice_total_count/$total_count*100;
													?>
														<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
												 	$row++;
											 } // question_type if
										} //question while
									?>
								</table>
								</td>
							</tr>
							<tr>
								<td>
								<table border="0" cellpadding="5" cellspacing="0" class="formArea" width="100%">
									<tr>
										<td class="main" align="center">
										<?php 	echo '<a href="' . tep_href_link(FILENAME_EVENTS_REPORTS_SURVEYS) . '">' . tep_image_button("button_back",IMAGE_BACK) . '</a>&nbsp;';
												echo tep_image_button("button_admin_print",IMAGE_PRINT);
										?>
										</td>
									</tr>
								</table>
								</td>
							</tr>
						</table>
						<td>
					</tr>
						<?php } else {  ?>
							<tr>
								<td class="tableHeading" align="center"><?php echo SURVEY_NO_QUESTIONS;?></td>
							</tr>
							<tr>
								<td class="tableHeading" align="center"><?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_REPORTS_SURVEYS) . '">' . tep_image_button("button_cancel",IMAGE_CANCEL) . '</a>';?></td>
							</tr>
			<?php		} //report type 1 cnt
					} else if ($tx_report_type==2) { // detail report 
					?>
					
			<?php 
				} // report type
			} //action if?>
		</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'); ?>