>> PHPSurveyor #
#############################################################
# > Author: Jason Cleeland #
# > E-mail: jason@cleeland.org #
# > Mail: Box 99, Trades Hall, 54 Victoria St, #
# > CARLTON SOUTH 3053, AUSTRALIA #
# > Date: 20 February 2003 #
# #
# This set of scripts allows you to develop, publish and #
# perform data-entry on surveys. #
#############################################################
# #
# Copyright (C) 2003 Jason Cleeland #
# #
# This program is free software; you can redistribute #
# it and/or modify it under the terms of the GNU General #
# Public License as published by the Free Software #
# Foundation; either version 2 of the License, or (at your #
# option) any later version. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write to #
# the Free Software Foundation, Inc., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
#############################################################
*/
if (empty($homedir)) {die ("Cannot run this script directly");}
//Move current step
if (!isset($_SESSION['step'])) {$_SESSION['step']=0;}
if (isset($_POST['move']) && $_POST['move'] == " << "._PREV." ") {$_SESSION['step'] = $_POST['thisstep']-1;}
if (isset($_POST['move']) && $_POST['move'] == " "._NEXT." >> ") {$_SESSION['step'] = $_POST['thisstep']+1;}
if (isset($_POST['move']) && $_POST['move'] == " "._LAST." ") {$_SESSION['step'] = $_POST['thisstep']+1;}
//CONVERT POSTED ANSWERS TO SESSION VARIABLES
if (isset($_POST['fieldnames']) && $_POST['fieldnames'])
{
$postedfieldnames=explode("|", $_POST['fieldnames']);
foreach ($postedfieldnames as $pf)
{
if (isset($_POST[$pf])) {$_SESSION[$pf] = auto_unescape($_POST[$pf]);}
if (!isset($_POST[$pf])) {$_SESSION[$pf] = "";}
}
}
//CHECK IF ALL MANDATORY QUESTIONS HAVE BEEN ANSWERED
//CHECK IF ALL CONDITIONAL MANDATORY QUESTIONS THAT APPLY HAVE BEEN ANSWERED
$notanswered=addtoarray_single(checkmandatorys(),checkconditionalmandatorys());
//CHECK PREGS
$notvalidated=checkpregs();
//SUBMIT
if ((isset($_POST['move']) && $_POST['move'] == " "._SUBMIT." ") && (!isset($notanswered) || !$notanswered) && (!isset($notvalidated) && !$notvalidated) && isset($_SESSION['insertarray']))
{
if ($thissurvey['private'] == "Y")
{
$privacy="";
foreach (file("$thistpl/privacy.pstpl") as $op)
{
$privacy .= templatereplace($op);
}
}
//If survey has datestamp turned on, add $localtimedate to sessions
if ($thissurvey['datestamp'] == "Y")
{
if (!in_array("datestamp", $_SESSION['insertarray'])) //Only add this if it doesn't already exist
{
$_SESSION['insertarray'][] = "datestamp";
}
$_SESSION['datestamp'] = $localtimedate;
}
//If survey has ipaddr turned on, add IP Address to sessions
if ($thissurvey['ipaddr'] == "Y")
{
if (!in_array("ipaddr", $_SESSION['insertarray'])) //Only add this if it doesn't already exist
{
$_SESSION['insertarray'][] = "ipaddr";
}
$_SESSION['ipaddr'] = $_SERVER['REMOTE_ADDR'];
}
//DEVELOP SQL TO INSERT RESPONSES
$subquery = createinsertquery();
//COMMIT CHANGES TO DATABASE
if ($thissurvey['active'] != "Y")
{
sendcacheheaders();
doHeader();
foreach(file("$thistpl/startpage.pstpl") as $op)
{
echo templatereplace($op);
}
//Check for assessments
$assessments = doAssessment($surveyid);
if ($assessments)
{
foreach(file("$thistpl/assessment.pstpl") as $op)
{
echo templatereplace($op);
}
}
$completed = "
"._DIDNOTSAVE."
\n\n"
. _NOTACTIVE1."
\n"
. ""._CLEARRESP."
\n"
. "$subquery\n";
}
else
{
if (mysql_query($subquery))
{
//save responses was succesful
//UPDATE COOKIE IF REQUIRED
$savedid=mysql_insert_id();
if ($thissurvey['usecookie'] == "Y" && $tokensexist != 1)
{
$cookiename="PHPSID".returnglobal('sid')."STATUS";
setcookie("$cookiename", "COMPLETE", time() + 31536000); //365 days
}
if (isset($_SESSION['savename']))
{
//Delete the saved survey
$query = "DELETE FROM {$dbprefix}saved\n"
."WHERE sid=$surveyid\n"
."AND identifier = '".$_SESSION['savename']."'";
$result = mysql_query($query);
//Should put an email to administrator here
//if the delete doesn't work.
}
$content='';
//Start to print the final page
foreach(file("$thistpl/startpage.pstpl") as $op)
{
$content .= templatereplace($op);
}
//Check for assessments
$assessments = doAssessment($surveyid);
if ($assessments)
{
foreach(file("$thistpl/assessment.pstpl") as $op)
{
$content .= templatereplace($op);
}
}
//Create text for use in later print section
$completed = "
"
. _THANKS."
\n\n"
. _SURVEYREC."
\n"
. ""
. _CLOSEWIN_PS."
\n";
//Update the token if needed and send a confirmation email
if (isset($_POST['token']) && $_POST['token'])
{
submittokens();
}
//Send notification to survey administrator //Thanks to Jeff Clement http://jclement.ca
if ($thissurvey['sendnotification'] > 0 && $thissurvey['adminemail'])
{
sendsubmitnotification($thissurvey['sendnotification']);
}
if (isset($_SESSION['scid']))
{
//Delete the saved survey
$query = "DELETE FROM {$dbprefix}saved
WHERE scid=".$_SESSION['scid'];
$result=mysql_query($query);
$query = "DELETE FROM {$dbprefix}saved_control
WHERE scid=".$_SESSION['scid'];
$result=mysql_query($query);
//Should put an email to administrator here
//if the delete doesn't work.
}
session_unset();
session_destroy();
sendcacheheaders();
if (!$embedded && isset($thissurvey['autoredirect']) && $thissurvey['autoredirect'] == "Y" && $thissurvey['url'])
{
//Automatically redirect the page to the "url" setting for the survey
session_write_close();
header("Location: {$thissurvey['url']}");
}
doHeader();
echo $content;
}
else
{
//Submit of Responses Failed
$completed=submitfailed();
}
}
foreach(file("$thistpl/completed.pstpl") as $op)
{
echo templatereplace($op);
}
echo "\n
\n";
foreach(file("$thistpl/endpage.pstpl") as $op)
{
echo templatereplace($op);
}
exit;
}
//LAST PHASE
if (isset($_POST['move']) && $_POST['move'] == " "._LAST." " && (!isset($notanswered) && !$notanswered) && (!isset($notvalidated) && !$notvalidated))
{
//READ TEMPLATES, INSERT DATA AND PRESENT PAGE
sendcacheheaders();
doHeader();
foreach(file("$thistpl/startpage.pstpl") as $op)
{
echo templatereplace($op);
}
echo "\n