Location: PHPKode > scripts > AskPeople-Free PHP survey application > AskPeople_2_2_2/statreport.php
<?php

// Cross tab report module v2, Jan 07

session_start();

require_once('includes/functions.inc.php');
require_once('login.inc.php');

// set out chunks to be included in header.php
$headerstuff = "<style type='text/css'>
	body {
		font-family: Arial, Helvetica, Verdana, sans-serif;
	}
	span.questionnumber {
		display: none;
	}
</style>
<style type='text/css' media='print'>
	form, #surveylink, #toptabs {
	  display: none;
	}
	table th, table td {
		border: 1.2pt solid black;
	}
	body {
	  background: none;
	}
</style>";

if ($_REQUEST['breaks'] == "off") { // pagebreaks off in print mode
	$headerstuff .= "
	<style type='text/css' media='print'>
	.base {
	  page-break-after: auto;
	}
	</style>
	
	<style type='text/css' media='screen'>
	.base {
	  border-bottom: 1px white solid;
	  padding-bottom: 10px;
	}
	</style>
	";
	$breaks_off = " checked='checked'";
} else {
	$headerstuff .= "
	<style type='text/css' media='screen'>
	.base {
	  border-bottom: 1px #aaa dashed;
	  padding-bottom: 10px;
	}
	</style>
	";
	$breaks_on = " checked='checked'";
}

  if ($_REQUEST['show'] == "both") {
 	$show_both = " checked='checked'";	
  } elseif ($_REQUEST['show'] == "count") {
 	$show_count = " checked='checked'";	  
  } elseif ($_REQUEST['show'] == "percentage") {
 	$show_percentage = " checked='checked'";	  
  } else { // default to show both
 	$show_both = " checked='checked'";	  
  }

  if ($_REQUEST['calcperc'] == "all") {
 	$calcperc_all = " checked='checked'";	
 	$calcPercentageOnAll = "TRUE";
  } elseif ($_REQUEST['calcperc'] == "nonblank") {
 	$calcperc_nonblank = " checked='checked'";	  
 	$calcPercentageOnAll = "FALSE";
  } else { // default to show based on all
 	$calcperc_all = " checked='checked'";	  
 	$calcPercentageOnAll = "TRUE";
  }

require_once('includes/header.php');


$rid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
//$loadeddata = loadFile($rid);
//$respdata = explode("\r\n",$loadeddata);
$df = fopen($rid,"r");
$respdata = file($rid);
fclose($df);

$qid = "surveys/quest_" . $_SESSION['admin_sid'] . ".xml";
$loadedq = loadQuest2($qid);

// make an array of fieldnames so we can map it onto the datafile format
$fields = makeFieldRow($_SESSION['admin_sid'],"array");
$fields = @explode("\t",$fields);

//print_r($fields);
foreach((array)$respdata as $kr => $r) {
	if (strlen(trim($r))>0) { // not blank
		$thisresp = @explode("\t",$r);
		//print_r($thisresp);
	
		$data[$kr] = @array_combine($fields,$thisresp); // merge the arrays, with $fields as the keys and $thisresp as the values (PHP5 only)
	}
}

//print_r($data);

// clean up data array to make it multidimensional where scales/multis are involved
foreach((array)$data as $d => $r) {
	foreach((array)$r as $qid => $qval) {
		if (eregi("_other$",$qid)) { // an other_specify
			$keys = explode("_",$qid);
			$data2[$d][$keys[1]][$keys[0]] = $qval;
		} elseif (eregi("_",$qid)) { // an element containing a grid/multi answer
			$keys = explode("_",$qid);
			if (count($keys)>2) { // a multi grid
				$data2[$d][$keys[0]][$keys[1]][$keys[2]] = $qval;					
			} else {
				$data2[$d][$keys[0]][$keys[1]] = $qval;		
			}
		} else {
			$data2[$d][$qid] = $qval;
		}
	}
}

//print_r($data2);

// build array from which subgroup columns can be produced
foreach((array)$loadedq['questions'][$_REQUEST['xtab']]['options'] as $xto) {
	$idxto = idWord($xto);
	foreach((array)$data2 as $rid => $response) {
		if (trim($response[$_REQUEST['xtab']]) == trim($xto) || trim($response[$_REQUEST['xtab']][$idxto]) == "TRUE") { // this respondent matches
			$respcount[$idxto] = $respcount[$idxto]+1;
			
			foreach((array)$response as $qid => $value) {
				if ($loadedq['questions'][$qid]['type'] == "single") { // a question type you can calculate stats from
					if (strlen($loadedq['questions'][$qid]['scale'])>0) { // a grid
						foreach((array)$value as $opt => $v) {
							$opt = idWord($opt);
							$v = idWord($v);
							$stats[$idxto][$qid][$opt][$v] = $stats[$idxto][$qid][$opt][$v]+1;	
						}
					} else {
						$value = idWord($value);
						$stats[$idxto][$qid][$value] = $stats[$idxto][$qid][$value]+1;		
					}	
				} elseif ($loadedq['questions'][$qid]['type'] == "dropdown") { // a question type you can calculate stats from
					$value = idWord($value);
					$stats[$idxto][$qid][$value] = $stats[$idxto][$qid][$value]+1;		
				} elseif ($loadedq['questions'][$qid]['type'] == "multi") { // a question type you can calculate stats from
					if (strlen($loadedq['questions'][$qid]['scale'])>0) { // a grid
						foreach((array)$value as $opt => $v) {
							foreach((array)$v as $mk => $mv) {
								if ($mv == TRUE) {
									$stats[$idxto][$qid][$opt][$mk] = $stats[$idxto][$qid][$opt][$mk]+1;
								}
							}
						}
					} else {
						foreach((array)$value as $optid => $v) {
							if ($v == "TRUE") {
								$stats[$idxto][$qid][$optid] = $stats[$idxto][$qid][$optid]+1;		
							}
						}
					}									
				} elseif ($loadedq['questions'][$qid]['type'] == "smallbox" || $loadedq['questions'][$qid]['type'] == "bigbox") { // an open text q
					$stats[$idxto][$qid] .= (strlen($response[$qid])>0) ? "<span class='respondentcomment'>". htmlentities($response[$qid]) . "</span>" : null;
				}
			}
		}
	}
}

// build array from which Total column can be produced
foreach((array)$data2 as $rid => $response) {
	foreach((array)$response as $qid => $value) {
		
		if (is_array($value)) {
			if (strlen(trim(@implode($value)))<1) { //  a blank response
				$blanks[$qid]++; 
			} 
		} else {
			if (strlen(trim($value))<1) { //  a blank response
				$blanks[$qid]++; 
			} 		
		}
		
		if ($loadedq['questions'][$qid]['type'] == "single") { // a question type you can calculate stats from

			if (strlen($loadedq['questions'][$qid]['scale'])>0) { // a grid
				foreach((array)$value as $opt => $v) {
					$opt = idWord($opt);
					$v = idWord($v);
					$stats[Total][$qid][$opt][$v] = $stats[Total][$qid][$opt][$v]+1;	
				}
			} else {
				$value = idWord($value);
				$stats[Total][$qid][$value] = $stats[Total][$qid][$value]+1;		
			}			
		} elseif ($loadedq['questions'][$qid]['type'] == "dropdown") { // a question type you can calculate stats from
				$value = idWord($value);
				$stats[Total][$qid][$value] = $stats[Total][$qid][$value]+1;
		} elseif ($loadedq['questions'][$qid]['type'] == "multi") { // a question type you can calculate stats from

			if (strlen($loadedq['questions'][$qid]['scale'])>0) { // a grid
				foreach((array)$value as $opt => $v) {
					foreach((array)$v as $mk => $mv) {
						if ($mv == TRUE) {
							  $stats[Total][$qid][$opt][$mk] = $stats[Total][$qid][$opt][$mk]+1;	
						}					
					}
				}
			} else {
				foreach((array)$value as $optid => $v) {
					if ($v == "TRUE") {
						$stats[Total][$qid][$optid] = $stats[Total][$qid][$optid]+1;		
					}
				}
			}	

		} elseif ($loadedq['questions'][$qid]['type'] == "smallbox" || $loadedq['questions'][$qid]['type'] == "bigbox") { // an open text q
			$stats[Total][$qid] .= (strlen($response[$qid])>0) ? "<span class='respondentcomment'>". htmlentities($response[$qid]) . "</span>\n" : null;
		}
		
		if (strlen($response[other][$qid])>0) {
			$othervals[$qid][] = htmlentities(stripslashes(trim($response[other][$qid])));
		}
	}
}

$questiontypes = array(
 "single" => "Single answer question or grid (answers per option add up to roughly 100%)",
 "dropdown" => "Single dropdown answer question (answers per option add up to roughly 100%)",
 "multi" => "Multiple answer question or grid (answers per row option may add up to more than 100%)",
 "smallbox" => "Small free-text box",
 "bigbox" => "Large free-text box");

if ($_REQUEST['qid'] == "all") {
  foreach((array)$loadedq['questions'] as $thisqid => $q) {
  	$table .= stripslashes(makeResultTable($q,$thisqid,$calcPercentageOnAll));
  }
} else { // just show specified question
  $thisqid = $_REQUEST['qid'];
  $q = $loadedq['questions'][$thisqid];
  $table .= stripslashes(makeResultTable($q,$thisqid,$calcPercentageOnAll));
}


// build menu of qs
foreach((array)$loadedq['questions'] as $q) {
	if ($q['type'] != "info" && $q['type'] != "pagebreak") {
		$qt = (strlen($q['questiontext'])>0) ? strip_tags(substr($q['questiontext'],0,70))."..." : strip_tags(substr(@implode(" ",$q['options']),0,70));
		if ($_REQUEST['qid'] == $q['questionid']) {
			$qmenu[] = "<option value=\"{$q['questionid']}\" selected='selected'>{$qt}</option>";	
		} else {
			$qmenu[] = "<option value=\"{$q['questionid']}\">{$qt}</option>";
		}
	
		if (($q['type'] == "single" || $q['type'] == 'dropdown' || $q['type'] == 'multi') && count($q['scale'])<1) { 
			if ($_REQUEST['xtab'] == $q['questionid']) {
				$xtabmenu[] = "<option value=\"{$q['questionid']}\" selected=\"selected\">{$qt}</option>";
			} else {
				$xtabmenu[] = "<option value=\"{$q['questionid']}\">{$qt}</option>";
			
			}
		}
	}
}
$menuqs = "<select name='qid'>\n<option value='all'>--All questions--</option>\n"hide@address.com("\n",$qmenu)."\n</select>";
$menuxtab = "<select name='xtab'>\n<option value=''>--Choose a cross-tabbable question--</option>\n"hide@address.com("\n",$xtabmenu)."\n</select>";
$qtitle = $loadedq['meta']['title'];

$directory = (substr(dirname($_SERVER['PHP_SELF']),-1) == "/") ? dirname($_SERVER['PHP_SELF']) : dirname($_SERVER['PHP_SELF']) . "/";
$reportlink = $protocol . $_SERVER['HTTP_HOST'] . $directory . "statreport.php?sid=".$_SESSION['admin_sid'];

?>

<p id='surveylink'><img src="images/icons/web_16x16.gif" width="16" height="16" class='icon' alt="Link to report" border="0" />The link to this report is: <a href='<?echo($reportlink);?>'><?echo($reportlink);?></a></p>

<form id="results" action="statreport.php">
	<div id='options'>
		<p><strong>Show:</strong></p>
		<p><label><input type='radio' name='show' value='count'<?echo($show_count);?> /> Count</label><br />
		<label><input type='radio' name='show' value='percentage'<?echo($show_percentage);?> /> Percentage</label><br />
		<label><input type='radio' name='show' value='both'<?echo($show_both);?> /> Both</label></p>
		
		<p><strong>Print:</strong></p>
		<p><label><input type='radio' name='breaks' value='on'<?echo($breaks_on);?> /> Pagebreaks between questions</label><br />
		<label><input type='radio' name='breaks' value='off'<?echo($breaks_off);?> /> Multiple questions on one page</label></p>

		<p><strong>Calculate percentages:</strong></p>
		<p><label><input type='radio' name='calcperc' value='all'<?echo($calcperc_all);?> /> Counting all responses</label><br />
		<label><input type='radio' name='calcperc' value='nonblank'<?echo($calcperc_nonblank);?> /> Counting non-blank responses only</label></p>
	</div>
	
	<div style='margin-left: 30px;'>
		<p style='font-size: 1.3em;position:relative;'>Question to display:<br /><? echo($menuqs);?></p>
		<p style='font-size: 1.3em; margin-top: 10px;position:relative;'>Analyzed by:<br /><? echo($menuxtab);?></p>
		<p style='text-align:center; margin-top: 10px;'><input type='submit' value='Show results' /></p>
	</div>
	<div style='clear:both;'></div>
</form>

<div class='resultstable'>
  <? echo($table);?>
</div>

<!-- 
<?
  echo ("Stats:\n");
  //print_r($stats);
  echo ("\n\nRespCount (only applies to subgroup crosstabs):\n");
  //print_r($respcount);
  echo ("\n\nData2:\n");
  //print_r ($data2);
  echo ("\n\nOtherVals:\n");
  //print_r ($othervals);
?>
 -->
 
<?
require_once('includes/footer.php');
?>
Return current item: AskPeople-Free PHP survey application