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

session_start();

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

require_once('login.inc.php');

switch ($_REQUEST['action']) {

	case "EditData" :

		include('includes/header.php');

		$qid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
		$loaded = loadFile($qid);
		if ($loaded == "Array") { $loaded = ""; }
		
		$_SESSION['statusmsg'] = null;

		$outputform = "
			<form action=\"respondentreport.php\" method=\"post\" class=\"clearbox\">
			<div id=\"dataeditform\" width=\"99%;\">
				<textarea cols=\"80\" rows=\"25\" wrap=\"off\" name=\"rawdata\">$loaded</textarea>
				<input type=\"hidden\" name=\"action\" value=\"SaveData\" />
				<p style=\"text-align:center;\"><input type=\"submit\" value=\"Save changes to data\" /></p>
			</div>
			</form>
		";

	  break;

	case "SaveData" :

		$qid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";

		$saved = saveFile($qid,$_POST['rawdata']);

		if ($saved) {
			$_SESSION['statusmsg'] = "The data file was saved.";
		} else {
			$_SESSION['statusmsg'] = "Sorry, the data file could not be saved.";
		}
		header("Location: respondentreport.php?action=EditData");
	  
	  break;

	case "DownloadData" :
	
		$rid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
		$loaded = loadFile($rid);

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

		$qtitle = $loadedq['meta']['title'];
		
		$fields = makeFieldRow($_SESSION['admin_sid']);
		$questions = makeQuestionRow($_SESSION['admin_sid']);
		
		$dodgy = array(" ","'",",",".");
		
		$now = date("d-M");
		$nicefilename = substr(strtolower(str_replace($dodgy,"_",$qtitle)),0,10) . "_" . $now . "_data.xls";

		header("Content-Type: text/plain");
		// a load of extra headers here so IE doesn't choke on the file...
		header("Pragma: public");
		header("Expires: 0"); // set expiration time
		header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
		// define filename for download as export.txt
		header("Content-Disposition: attachment; filename=\"".$nicefilename."\"");
		
		echo ($fields) . ($questions). ($loaded);
		exit();

		break;

	default : // show per-respondent report

	  include('includes/header.php');
	  
	  if ($_REQUEST['sid']) {
	  		$_SESSION['admin_sid'] = $_REQUEST['sid'];
	  }

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

		if ($_SESSION['admin_sid']) { // show form
		
			$rid = $_GET['respondentid'];
		
			$datafile = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
			$rawdata = loadFile($datafile);
			$rawdata_array = explode("\n",$rawdata);
			for ($r=0;$r<count($rawdata_array);$r++) {
				if (strlen($rawdata_array[$r])>0) {
					$newdata = explode("\t",$rawdata_array[$r]);
								
					$newdata[1] = ($newdata[1] == "Anonymous") ? $newdata[1] . "_" . $r : $newdata[1]; // need to give it an extra 'tag' to help differentiate
					
					if ($newdata[1] == $_REQUEST['respondentid']) {
						$respondentoptions .= "<option value=\"$newdata[1]\" selected=\"selected\">$newdata[1]</option>\n";
					} else {
						$respondentoptions .= "<option value=\"$newdata[1]\">$newdata[1]</option>\n";			
					}
					
					if ($newdata[1] == $rid) { // matched username
					    $data_array = $newdata;	// so $data_array now contains array of responses for the selected respondent
						$respondenttable = makeRespondentResultsTable($data_array,$_SESSION['admin_sid'],"TRUE","View results");
						$respflag = "TRUE";
					}
				}
			}
		
			$replink = "<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='{$reportlink}'>{$reportlink}</a></p>";
			$respondentmenu = "
			<p><form action=\"respondentreport.php\" method=\"get\" id='results' style='text-align:center;'><span style='font-size: 1.3em;'>View responses for user ID: </span> <select name=\"respondentid\" onchange=\"this.form.submit()\"><option value=\"\">-- select a respondent --</option>\n{$respondentoptions}</select>
			<input type=\"submit\" value=\"Go\" /></form><br /><br /></p>\n\n";
			
			$outputform .= "
				<style type='text/css'>
					span.questionnumber {
					  display: none;
					}
				</style>
			";
			
			$outputform .= $replink . $respondentmenu . $respondenttable;
			if ($respflag == "TRUE") {  // repeat the menu at the foot of the page for easy switching
				$outputform .= $respondentmenu; 
			}	
		} else {
			echo ("<h1 class='clearbox'>Sorry</h1><p class='clearbox'>No questionnaire data is available to display.</p>");
		}
		
	break;
}

echo ($outputform);

include ('includes/footer.php');

?>
Return current item: AskPeople-Free PHP survey application