mirror of
https://github.com/ACSPRI/queXS
synced 2024-04-02 12:12:16 +00:00
Merging the Limesurvey 1.91+ branch of queXS in to the trunk
This commit is contained in:
@@ -1,172 +1,131 @@
|
||||
<?php
|
||||
/*
|
||||
* LimeSurvey
|
||||
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
|
||||
* All rights reserved.
|
||||
* License: GNU/GPL License v2 or later, see LICENSE.php
|
||||
* LimeSurvey is free software. This version may have been modified pursuant
|
||||
* to the GNU General Public License, and as distributed it includes or
|
||||
* is derivative of works licensed under the GNU General Public License or
|
||||
* other free or open source software licenses.
|
||||
* See COPYRIGHT.php for copyright notices and details.
|
||||
*
|
||||
* $Id: dumpquestion.php 6853 2009-05-15 12:51:09Z c_schmitz $
|
||||
*/
|
||||
* LimeSurvey
|
||||
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
|
||||
* All rights reserved.
|
||||
* License: GNU/GPL License v2 or later, see LICENSE.php
|
||||
* LimeSurvey is free software. This version may have been modified pursuant
|
||||
* to the GNU General Public License, and as distributed it includes or
|
||||
* is derivative of works licensed under the GNU General Public License or
|
||||
* other free or open source software licenses.
|
||||
* See COPYRIGHT.php for copyright notices and details.
|
||||
*
|
||||
* $Id: dumpquestion.php 10925 2011-09-02 14:12:02Z c_schmitz $
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// 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(!bHasSurveyPermission($surveyid,'surveycontent','export')) safe_die("You are not allowed to export questions.");
|
||||
|
||||
|
||||
$qid = returnglobal('qid');
|
||||
|
||||
include_once("login_check.php");
|
||||
|
||||
//echo $htmlheader;
|
||||
if (!$qid)
|
||||
{
|
||||
echo $htmlheader;
|
||||
echo "<br />\n";
|
||||
echo "<table width='350' align='center' style='border: 1px solid #555555' cellpadding='1' cellspacing='0'>\n";
|
||||
echo "\t<tr bgcolor='#555555'><td colspan='2' height='4'><font size='1' face='verdana' color='white'><strong>".$clang->gT("Export Question")."</strong></td></tr>\n";
|
||||
echo "\t<tr bgcolor='#CCCCCC'><td align='center'>\n";
|
||||
echo "<br /><strong><font color='red'>".$clang->gT("Error")."</font></strong><br />\n".$clang->gT("No QID has been provided. Cannot dump question.")."<br />\n";
|
||||
echo "<br /><input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname', '_self')\">\n";
|
||||
echo "\t</td></tr>\n";
|
||||
echo "</table>\n";
|
||||
echo "</body></html>\n";
|
||||
exit;
|
||||
safe_die("No QID has been provided. Cannot dump question.");
|
||||
}
|
||||
|
||||
$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";
|
||||
$fn = "limesurvey_question_$qid.lsq";
|
||||
$xml = getXMLWriter();
|
||||
|
||||
function BuildOutput($Query)
|
||||
if($action=='exportstructureLsrcCsvQuestion')
|
||||
{
|
||||
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())
|
||||
{
|
||||
include_once($homedir.'/remotecontrol/lsrc.config.php');
|
||||
//Select title as Filename and save
|
||||
$questionTitleSql = "SELECT title
|
||||
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: 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
|
||||
|
||||
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;
|
||||
$xml->openURI('php://output');
|
||||
}
|
||||
|
||||
|
||||
//1: Questions Table
|
||||
$qquery = "SELECT *
|
||||
$xml->setIndent(true);
|
||||
$xml->startDocument('1.0', 'UTF-8');
|
||||
$xml->startElement('document');
|
||||
$xml->writeElement('LimeSurveyDocType','Question');
|
||||
$xml->writeElement('DBVersion',$dbversionnumber);
|
||||
$xml->startElement('languages');
|
||||
$lquery = "SELECT language
|
||||
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 = "";
|
||||
WHERE qid=$qid or parent_qid=$qid group by language";
|
||||
$lresult=db_execute_assoc($lquery);
|
||||
while ($row=$lresult->FetchRow())
|
||||
{
|
||||
$xml->writeElement('language',$row['language']);
|
||||
}
|
||||
//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";
|
||||
|
||||
|
||||
header("Content-Type: application/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;
|
||||
$xml->endElement();
|
||||
getXMLStructure($xml,$qid);
|
||||
$xml->endElement(); // close columns
|
||||
$xml->endDocument();
|
||||
exit;
|
||||
?>
|
||||
|
||||
function getXMLStructure($xml,$qid)
|
||||
{
|
||||
global $dbprefix, $connect;
|
||||
// Questions table
|
||||
$qquery = "SELECT *
|
||||
FROM {$dbprefix}questions
|
||||
WHERE qid=$qid and parent_qid=0 order by language, scale_id, question_order";
|
||||
BuildXMLFromQuery($xml,$qquery);
|
||||
|
||||
// Questions table - Subquestions
|
||||
$qquery = "SELECT *
|
||||
FROM {$dbprefix}questions
|
||||
WHERE parent_qid=$qid order by language, scale_id, question_order";
|
||||
BuildXMLFromQuery($xml,$qquery,'subquestions');
|
||||
|
||||
|
||||
// Answers table
|
||||
$aquery = "SELECT *
|
||||
FROM {$dbprefix}answers
|
||||
WHERE qid = $qid order by language, scale_id, sortorder";
|
||||
BuildXMLFromQuery($xml,$aquery);
|
||||
|
||||
|
||||
|
||||
// Question attributes
|
||||
$surveyid=$connect->GetOne("select sid from {$dbprefix}questions where qid={$qid}");
|
||||
$sBaseLanguage=GetBaseLanguageFromSurveyID($surveyid);
|
||||
if ($connect->databaseType == 'odbc_mssql' || $connect->databaseType == 'odbtp' || $connect->databaseType == 'mssql_n' || $connect->databaseType =='mssqlnative')
|
||||
{
|
||||
$query="SELECT qa.qid, qa.attribute, cast(qa.value as varchar(4000)) as value
|
||||
FROM {$dbprefix}question_attributes qa JOIN {$dbprefix}questions q ON q.qid = qa.qid AND q.sid={$surveyid} and q.qid={$qid}
|
||||
where q.language='{$sBaseLanguage}' group by qa.qid, qa.attribute, cast(qa.value as varchar(4000))";
|
||||
}
|
||||
else {
|
||||
$query="SELECT qa.qid, qa.attribute, qa.value
|
||||
FROM {$dbprefix}question_attributes qa JOIN {$dbprefix}questions q ON q.qid = qa.qid AND q.sid={$surveyid} and q.qid={$qid}
|
||||
where q.language='{$sBaseLanguage}' group by qa.qid, qa.attribute, qa.value";
|
||||
}
|
||||
BuildXMLFromQuery($xml,$query);
|
||||
|
||||
// Default values
|
||||
$query = "SELECT *
|
||||
FROM {$dbprefix}defaultvalues
|
||||
WHERE qid=$qid order by language, scale_id";
|
||||
BuildXMLFromQuery($xml,$query);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user