<?php
//requires $sid to exist as parameter
//the template has $sid=3
// the template has to be changed to fit the particular teacher:
// -adopt the Title (replace $name in the title field with the actual name of the teacher
//adpated dumpsurvey.php from phpsurveyor . original comment header can be found there.
// DUMP THE RELATED DATA FOR A SINGLE SURVEY INTO A SQL FILE FOR IMPORTING LATER ON OR ON ANOTHER SURVEY SETUP
// DUMP ALL DATA WITH RELATED SID FROM THE FOLLOWING TABLES
// 1. surveys
// 2. groups
// 3. questions
// 4. answers
require_once("../admin/config.php");
$sid=SURVEY_TEMPLATE_SID;
if (!isset($sid)) {$sid=returnglobal('sid');}
$dumphead = "# SURVEYOR SURVEY DUMP\n"
. "#\n# This is a dumped survey from the PHPSurveyor Script\n"
. "# http://phpsurveyor.sourceforge.net/\n";
function BuildOutput($Query)
{
global $dbprefix;
$QueryResult = mysql_query($Query) or die ("ERROR: $QueryResult<br />".mysql_error());
preg_match('/FROM (\w+)( |,)/i', $Query, $MatchResults);
$TableName = $MatchResults[1];
if ($dbprefix)
{
$TableName = substr($TableName, strlen($dbprefix), strlen($TableName));
}
$Output = "\n# NEW TABLE\n# " . strtoupper($TableName) . " TABLE\n#\n";
while ($Row = mysql_fetch_assoc($QueryResult))
{
$ColumnNames = "";
$ColumnValues = "";
foreach ($Row as $Key=>$Value)
{
$ColumnNames .= "`" . $Key . "`, "; //Add all the column names together
if (phpversion() >= "4.3.0")
{
$ColumnValues .= "'" . mysql_real_escape_string(str_replace("\r\n", "\n", $Value)) . "', ";
}
else
{
$ColumnValues .= "'" . mysql_escape_string(str_replace("\r\n", "\n", $Value)) . "', ";
}
}
$ColumnNames = substr($ColumnNames, 0, -2); //strip off last comma space
$ColumnValues = substr($ColumnValues, 0, -2); //strip off last comma space
$Output .= "INSERT INTO $TableName ($ColumnNames) VALUES ($ColumnValues)\n";
}
return $Output;
}
//1: Surveys table
$squery = "SELECT * FROM {$dbprefix}surveys WHERE sid=$sid";
$sdump = BuildOutput($squery);
//2: Groups Table
$gquery = "SELECT * FROM {$dbprefix}groups WHERE sid=$sid";
$gdump = BuildOutput($gquery);
//3: Questions Table
$qquery = "SELECT * FROM {$dbprefix}questions WHERE sid=$sid";
$qdump = BuildOutput($qquery);
//4: Answers table
$aquery = "SELECT {$dbprefix}answers.* FROM {$dbprefix}answers, {$dbprefix}questions WHERE {$dbprefix}answers.qid={$dbprefix}questions.qid AND {$dbprefix}questions.sid=$sid";
$adump = BuildOutput($aquery);
//5: Conditions table
$cquery = "SELECT {$dbprefix}conditions.* FROM {$dbprefix}conditions, {$dbprefix}questions WHERE {$dbprefix}conditions.qid={$dbprefix}questions.qid AND {$dbprefix}questions.sid=$sid";
$cdump = BuildOutput($cquery);
//6: Label Sets
$lsquery = "SELECT DISTINCT {$dbprefix}labelsets.lid, label_name FROM {$dbprefix}labelsets, {$dbprefix}questions WHERE {$dbprefix}labelsets.lid={$dbprefix}questions.lid AND type='F' AND sid=$sid";
$lsdump = BuildOutput($lsquery);
//7: Labels
$lquery = "SELECT DISTINCT {$dbprefix}labels.lid, {$dbprefix}labels.code, {$dbprefix}labels.title, {$dbprefix}labels.sortorder FROM {$dbprefix}labels, {$dbprefix}questions WHERE {$dbprefix}labels.lid={$dbprefix}questions.lid AND type='F' AND sid=$sid";
$ldump = BuildOutput($lquery);
$filestring="#<pre>\n"
.$dumphead. $sdump. $gdump. $qdump. $adump. $cdump .$lsdump. $ldump
."#</pre>\n";
$the_full_file_path = $tempdir . "/currentsurveyimport.sql";
function writeTempSQLfile(&$datastring){
global $tempdir;
global $the_full_file_path;
if (!$handle = fopen($the_full_file_path, 'w+')) {
print "Cannot open file ($the_full_file_path)";
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle, $datastring)) {
print "Cannot write to file ($the_full_file_path)";
exit;
}
//print "Success, wrote ($datastring) to file ($the_full_file_path)";
fclose($handle);
}
?>