Skip to content

Commit

Permalink
Dev Question and groups are now exported in the new XML format
Browse files Browse the repository at this point in the history
git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey@8598 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
c-schmitz committed Apr 14, 2010
1 parent 924ce35 commit 2a80a7d
Show file tree
Hide file tree
Showing 10 changed files with 1,337 additions and 1,584 deletions.
5 changes: 0 additions & 5 deletions admin/admin.php
Expand Up @@ -182,11 +182,6 @@
if (hasRight($surveyid,'define_questions')) {$_SESSION['FileManagerContext']="edit:survey:$surveyid";include('import_resources_zip.php');}
else { include('access_denied.php');}
}
elseif ($action == 'exportstructurecsv')
{
if(hasRight($surveyid,'export')) {include('export_structure_csv.php');}
else { include('access_denied.php');}
}
elseif ($action == 'exportstructureLsrcCsv')
{
if(hasRight($surveyid,'export')) {include('export_structure_lsrc.php');}
Expand Down
135 changes: 73 additions & 62 deletions admin/dumpgroup.php
Expand Up @@ -20,16 +20,15 @@
// 1. questions
// 2. answers

//Ensure script is not run directly, avoid path disclosure
if (!isset($dbprefix) || isset($_REQUEST['dbprefix'])) {die("Cannot run this script directly");}
include_once("login_check.php");
require_once("export_data_functions.php");
if(!hasRight($surveyid,'export')) safe_die("You are not allowed to export question groups.");

$gid = returnglobal('gid');
$surveyid = returnglobal('sid');

//Ensure script is not run directly, avoid path disclosure
if (!isset($dbprefix) || isset($_REQUEST['dbprefix'])) {die("Cannot run this script directly");}


//echo $htmlheader;
if (!$gid)
{
echo $htmlheader;
Expand All @@ -45,75 +44,87 @@
exit;
}

$fn = "limesurvey_group_$gid.csv";

$dumphead = "# LimeSurvey Group Dump\n"
. "# DBVersion $dbversionnumber\n"
. "# This is a dumped group from the LimeSurvey Script\n"
. "# http://www.limesurvey.org/\n"
. "# Do not change this header!\n";



//1: Groups Table
$gquery = "SELECT *
FROM {$dbprefix}groups
WHERE gid=$gid";
$gdump = BuildCSVFromQuery($gquery);

//2: Questions Table
$qquery = "SELECT *
FROM {$dbprefix}questions
WHERE gid=$gid";
$qdump = BuildCSVFromQuery($qquery);

//3: Answers table
$aquery = "SELECT DISTINCT {$dbprefix}answers.*
FROM {$dbprefix}answers, {$dbprefix}questions
WHERE ({$dbprefix}answers.qid={$dbprefix}questions.qid)
AND ({$dbprefix}questions.gid=$gid)";
$adump = BuildCSVFromQuery($aquery);

//4: Conditions table - THIS CAN ONLY EXPORT CONDITIONS THAT RELATE TO THE SAME GROUP
$cquery = "SELECT DISTINCT {$dbprefix}conditions.*
FROM {$dbprefix}conditions, {$dbprefix}questions, {$dbprefix}questions b
WHERE ({$dbprefix}conditions.cqid={$dbprefix}questions.qid)
AND ({$dbprefix}conditions.qid=b.qid)
AND ({$dbprefix}questions.gid=$gid)
AND (b.gid=$gid)";
$cdump = BuildCSVFromQuery($cquery);

//5: Question Attributes
$query = "SELECT {$dbprefix}question_attributes.*
FROM {$dbprefix}question_attributes, {$dbprefix}questions
WHERE ({$dbprefix}question_attributes.qid={$dbprefix}questions.qid)
AND ({$dbprefix}questions.gid=$gid)";
$qadump = BuildCSVFromQuery($query);
$fn = "limesurvey_group_$gid.lsg";
$xml = new XMLWriter();



if($action=='exportstructureLsrcCsvGroup')
{
include_once($homedir.'/remotecontrol/lsrc.config.php');
$lsrcString = $dumphead. $gdump. $qdump. $adump. $cdump. $qadump;
//Select title as Filename and save
$groupTitleSql = "SELECT group_name
FROM {$dbprefix}groups
WHERE sid=$surveyid AND gid=$gid ";
$groupTitleRs = db_execute_assoc($groupTitleSql);
$groupTitle = $groupTitleRs->FetchRow();
file_put_contents("remotecontrol/".$modDir.substr($groupTitle['group_name'],0,20).".csv",$lsrcString);
FROM {$dbprefix}groups
WHERE sid=$surveyid AND gid=$gid ";
$groupTitle = $connect->GetOne($groupTitleSql);
$xml->openURI('remotecontrol/'.$queDir.substr($groupTitle,0,20).".lsq");
}
else
{
// HTTP/1.0
header("Content-Type: application/download");
header("Content-Type: text/html/force-download");
header("Content-Disposition: attachment; filename=$fn");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: cache");
header("Pragma: cache"); // HTTP/1.0

echo $dumphead, $gdump, $qdump, $adump, $cdump, $qadump;
exit;
$xml->openURI('php://output');
}

?>
$xml->setIndent(true);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('document');
$xml->writeElement('LimeSurveyDocType','Group');
$xml->writeElement('DBVersion',$dbversionnumber);
getXMLStructure($xml,$gid);
$xml->endElement(); // close columns
$xml->endDocument();
exit;


function getXMLStructure($xml,$gid)
{
global $dbprefix;
// Groups
$gquery = "SELECT *
FROM {$dbprefix}groups
WHERE gid=$gid";
BuildXMLFromQuery($xml,$gquery);

// Questions
$qquery = "SELECT *
FROM {$dbprefix}questions
WHERE gid=$gid";
BuildXMLFromQuery($xml,$qquery);

//Answers
$aquery = "SELECT DISTINCT {$dbprefix}answers.*
FROM {$dbprefix}answers, {$dbprefix}questions
WHERE ({$dbprefix}answers.qid={$dbprefix}questions.qid)
AND ({$dbprefix}questions.gid=$gid)";
BuildXMLFromQuery($xml,$aquery);

//Conditions - THIS CAN ONLY EXPORT CONDITIONS THAT RELATE TO THE SAME GROUP
$cquery = "SELECT DISTINCT {$dbprefix}conditions.*
FROM {$dbprefix}conditions, {$dbprefix}questions, {$dbprefix}questions b
WHERE ({$dbprefix}conditions.cqid={$dbprefix}questions.qid)
AND ({$dbprefix}conditions.qid=b.qid)
AND ({$dbprefix}questions.gid=$gid)
AND (b.gid=$gid)";
BuildXMLFromQuery($xml,$cquery);

//Question attributes
$query = "SELECT {$dbprefix}question_attributes.*
FROM {$dbprefix}question_attributes, {$dbprefix}questions
WHERE ({$dbprefix}question_attributes.qid={$dbprefix}questions.qid)
AND ({$dbprefix}questions.gid=$gid)";
BuildXMLFromQuery($xml,$query);

// Default values
$query = "SELECT dv.*
FROM {$dbprefix}defaultvalues dv, {$dbprefix}questions
WHERE ({$dbprefix}defaultvalues.qid={$dbprefix}questions.qid)
AND ({$dbprefix}questions.gid=$gid) order by dv.language, dv.scale_id";
BuildXMLFromQuery($xml,$query);

}
190 changes: 58 additions & 132 deletions admin/dumpquestion.php
Expand Up @@ -17,16 +17,20 @@

// DUMP THE RELATED DATA FOR A SINGLE QUESTION INTO A SQL FILE FOR IMPORTING LATER ON OR
// ON ANOTHER SURVEY SETUP DUMP ALL DATA WITH RELATED QID FROM THE FOLLOWING TABLES
// 1. questions
// 2. answers
// - Questions
// - Answers
// - Question attributes
// - Default values

//Ensure script is not run directly, avoid path disclosure
if (!isset($dbprefix) || isset($_REQUEST['dbprefix'])) {die("Cannot run this script directly");}
include_once("login_check.php");
require_once("export_data_functions.php");
if(!hasRight($surveyid,'export')) safe_die("You are not allowed to export questions.");


$qid = returnglobal('qid');

include_once("login_check.php");

//echo $htmlheader;
if (!$qid)
{
echo $htmlheader;
Expand All @@ -42,145 +46,67 @@
exit;
}

$dumphead = "# LimeSurvey Question Dump\n"
. "# DBVersion $dbversionnumber\n"
. "# This is a dumped question from the LimeSurvey Script\n"
. "# http://www.limesurvey.org/\n"
. "# Do not change this header!\n";

function BuildOutput($Query)
{
global $dbprefix, $connect;
$QueryResult = db_execute_assoc($Query) or safe_die ("ERROR: $QueryResult<br />".$connect->ErrorMsg());
preg_match('/FROM (\w+)( |,)/i', $Query, $MatchResults);
$TableName = $MatchResults[1];;
if ($dbprefix)
{
$TableName = substr($TableName, strlen($dbprefix), strlen($TableName));
}
$Output = "\n#\n# " . strtoupper($TableName) . " TABLE\n#\n";
$HeaderDone = false; $ColumnNames = "";
while ($Row = $QueryResult->FetchRow())
{

if (!$HeaderDone)
{
foreach ($Row as $Key=>$Value)
{
$ColumnNames .= CSVEscape($Key).","; //Add all the column names together
}
$ColumnNames = substr($ColumnNames, 0, -1); //strip off last comma space
$Output .= "$ColumnNames\n";
$HeaderDone=true;
}
$ColumnValues = "";
foreach ($Row as $Key=>$Value)
{
$ColumnValues .= CSVEscape(str_replace("\r\n", "\n", $Value)) . ",";
}
$ColumnValues = substr($ColumnValues, 0, -1); //strip off last comma space
$Output .= "$ColumnValues\n";
}
return $Output;
}


//1: Questions Table
$qquery = "SELECT *
FROM {$dbprefix}questions
WHERE qid=$qid";
$qdump = BuildCSVFromQuery($qquery);

//2: Answers table
$aquery = "SELECT {$dbprefix}answers.*
FROM {$dbprefix}answers
WHERE {$dbprefix}answers.qid = $qid";
$adump = BuildCSVFromQuery($aquery);

//3: Labelsets Table
//$lsquery = "SELECT DISTINCT {$dbprefix}labelsets.* FROM {$dbprefix}labelsets, {$dbprefix}questions WHERE {$dbprefix}labelsets.lid={$dbprefix}questions.lid AND type='F' AND qid=$qid";
$lsquery = "SELECT DISTINCT {$dbprefix}labelsets.*
FROM {$dbprefix}labelsets, {$dbprefix}questions
WHERE {$dbprefix}labelsets.lid={$dbprefix}questions.lid
AND type in ('F', 'W', 'H', 'Z', '1', ':', ';')
AND qid=$qid";
$lsdump = BuildCSVFromQuery($lsquery);

//4: Labels Table
$lquery = "SELECT DISTINCT {$dbprefix}labels.*
FROM {$dbprefix}labels, {$dbprefix}questions
WHERE {$dbprefix}labels.lid={$dbprefix}questions.lid
AND type in ('F', 'W', 'H', 'Z', '1', ':', ';')
AND qid=$qid";
$ldump = BuildCSVFromQuery($lquery);

//4: Labelsets1 Table
//This exists specifically to deal with dual-scale questions (or any future question that may have 2 labelsets)
$lsquery = "SELECT DISTINCT {$dbprefix}labelsets.* FROM {$dbprefix}labelsets, {$dbprefix}questions WHERE {$dbprefix}labelsets.lid={$dbprefix}questions.lid1 AND type in ('1') AND qid=$qid";
$ls1dump = BuildCSVFromQuery($lsquery);
$ls1=explode("\n", trim($ls1dump));

if(count($ls1)>3) {
//If there is actually some data here, then add just the data (not the headers) into
// $ls1dump - which will be outputted directly after $lsdump
$ls1dump=$ls1[4];
$ls1dump .= "\n";
} else {
//If there is no data then make it an empty string.
$ls1dump = "";
}
//4a: Labels1 Table
// See explanation for Labelsets1 Table!! These are the actual labels
$lquery = "SELECT DISTINCT {$dbprefix}labels.* FROM {$dbprefix}labels, {$dbprefix}questions WHERE {$dbprefix}labels.lid={$dbprefix}questions.lid1 AND type in ('1') AND qid=$qid";
$l1dump = BuildCSVFromQuery($lquery);
$ld1=explode("\n", trim($l1dump));

if(count($ld1)>3) {
//If there is actually some data here, then add just the data (not the headers) into
// $l1dump - which will be outputted directly after $ldump
$l1dump=array();
foreach($ld1 as $key=>$ld) {
//Put every line, other than the first three into this string
if($key > 3) {
$l1dump[]=$ld;
}
}
$l1dump = implode("\n", $l1dump);
$l1dump .= "\n";
} else {
//If there is no data then make it an empty string.
$l1dump = "";
}


//5: Question Attributes
$query = "SELECT {$dbprefix}question_attributes.*
FROM {$dbprefix}question_attributes
WHERE {$dbprefix}question_attributes.qid=$qid";
$qadump = BuildCSVFromQuery($query);
$fn = "limesurvey_question_$qid.csv";
$fn = "limesurvey_question_$qid.lsq";
$xml = new XMLWriter();

if($action=='exportstructureLsrcCsvQuestion')
{
include_once($homedir.'/remotecontrol/lsrc.config.php');
$lsrcString = $dumphead. $qdump. $adump. $lsdump. $ls1dump. $ldump. $l1dump. $qadump;
//Select title as Filename and save
$questionTitleSql = "SELECT title
FROM {$dbprefix}questions
WHERE qid=$qid AND sid=$surveyid AND gid=$gid ";
$questionTitleRs = db_execute_assoc($questionTitleSql);
$questionTitle = $questionTitleRs->FetchRow();
file_put_contents("remotecontrol/".$queDir.substr($questionTitle['title'],0,20).".csv",$lsrcString);
FROM {$dbprefix}questions
WHERE qid=$qid AND sid=$surveyid AND gid=$gid ";
$questionTitle = $connect->GetOne($questionTitleSql);
$xml->openURI('remotecontrol/'.$queDir.substr($questionTitle,0,20).".lsq");
}
else
{
header("Content-Type: application/download");
header("Content-Type: text/html/force-download");
header("Content-Disposition: attachment; filename=$fn");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: cache"); // HTTP/1.0
echo $dumphead, $qdump, $adump, $lsdump, $ls1dump, $ldump, $l1dump, $qadump;
exit;

$xml->openURI('php://output');
}
?>

$xml->setIndent(true);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('document');
$xml->writeElement('LimeSurveyDocType','Question');
$xml->writeElement('DBVersion',$dbversionnumber);
getXMLStructure($xml,$qid);
$xml->endElement(); // close columns
$xml->endDocument();
exit;

function getXMLStructure($xml,$qid)
{
global $dbprefix;
// Questions Table
$qquery = "SELECT *
FROM {$dbprefix}questions
WHERE qid=$qid or parent_qid=$qid order by language, scale_id, question_order";
BuildXMLFromQuery($xml,$qquery);

// Answers table
$aquery = "SELECT {$dbprefix}answers.*
FROM {$dbprefix}answers
WHERE {$dbprefix}answers.qid = $qid order by language, scale_id, sortorder";
BuildXMLFromQuery($xml,$aquery);

// Question attributes
$query = "SELECT {$dbprefix}question_attributes.*
FROM {$dbprefix}question_attributes
WHERE {$dbprefix}question_attributes.qid=$qid order by qid";
BuildXMLFromQuery($xml,$query);

// Default values
$query = "SELECT *
FROM {$dbprefix}defaultvalues
WHERE qid=$qid order by language, scale_id";
BuildXMLFromQuery($xml,$query);

}

0 comments on commit 2a80a7d

Please sign in to comment.