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,21 +1,21 @@
|
||||
<?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: importoldresponses.php 7058 2009-06-10 14:42:27Z 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: importoldresponses.php 10925 2011-09-02 14:12:02Z c_schmitz $
|
||||
*/
|
||||
|
||||
|
||||
//import responses from an old_ survey table into an active survey
|
||||
include_once("login_check.php");
|
||||
include_once("login_check.php");
|
||||
|
||||
if (!isset($oldtable)) {$oldtable=returnglobal('oldtable');}
|
||||
if (!isset($surveyid)) {$surveyid=returnglobal('sid');}
|
||||
@@ -23,32 +23,32 @@ if (!isset($surveyid)) {$surveyid=returnglobal('sid');}
|
||||
|
||||
if (!$subaction == "import")
|
||||
{
|
||||
// show UI for choosing old table
|
||||
// show UI for choosing old table
|
||||
|
||||
$query = db_select_tables_like("{$dbprefix}old\_survey\_%");
|
||||
$result = db_execute_num($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
$optionElements = '';
|
||||
$queryCheckColumnsActive = "SELECT * FROM {$dbprefix}survey_{$surveyid} ";
|
||||
$resultActive = db_execute_num($queryCheckColumnsActive) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
$countActive = $resultActive->FieldCount();
|
||||
|
||||
while ($row = $result->FetchRow())
|
||||
{
|
||||
$queryCheckColumnsOld = "SELECT * FROM {$row[0]} ";
|
||||
|
||||
$resultOld = db_execute_num($queryCheckColumnsOld) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
|
||||
if($countActive== $resultOld->FieldCount())
|
||||
{
|
||||
$optionElements .= "\t\t\t<option value='{$row[0]}'>{$row[0]}</option>\n";
|
||||
}
|
||||
}
|
||||
$query = db_select_tables_like("{$dbprefix}old\_survey\_%");
|
||||
$result = db_execute_num($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
$optionElements = '';
|
||||
$queryCheckColumnsActive = "SELECT * FROM {$dbprefix}survey_{$surveyid} ";
|
||||
$resultActive = db_execute_num($queryCheckColumnsActive) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
$countActive = $resultActive->FieldCount();
|
||||
|
||||
while ($row = $result->FetchRow())
|
||||
{
|
||||
$queryCheckColumnsOld = "SELECT * FROM {$row[0]} ";
|
||||
|
||||
$resultOld = db_execute_num($queryCheckColumnsOld) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
|
||||
if($countActive== $resultOld->FieldCount())
|
||||
{
|
||||
$optionElements .= "\t\t\t<option value='{$row[0]}'>{$row[0]}</option>\n";
|
||||
}
|
||||
}
|
||||
|
||||
//Get the menubar
|
||||
$importoldresponsesoutput = browsemenubar($clang->gT("Quick Statistics"));
|
||||
$importoldresponsesoutput .= "<br />
|
||||
<div class='header'>
|
||||
".$clang->gT("Import responses from an deactivated survey table")."
|
||||
$importoldresponsesoutput = browsemenubar($clang->gT("Quick statistics"));
|
||||
$importoldresponsesoutput .= "
|
||||
<div class='header ui-widget-header'>
|
||||
".$clang->gT("Import responses from a deactivated survey table")."
|
||||
</div>
|
||||
<form id='personalsettings' method='post'>
|
||||
<ul>
|
||||
@@ -60,210 +60,86 @@ if (!$subaction == "import")
|
||||
".$clang->gT("Source table:")."
|
||||
</label>
|
||||
<select name='oldtable' >
|
||||
{$optionElements}
|
||||
{$optionElements}
|
||||
</select>
|
||||
</li>
|
||||
<li>
|
||||
<label for='importtimings'>
|
||||
".$clang->gT("Import also timings (if exist):")."
|
||||
</label>
|
||||
<select name='importtimings' >
|
||||
<option value='Y' selected='selected'>".$clang->gT("Yes")."</option>
|
||||
<option value='N'>".$clang->gT("No")."</option>
|
||||
</select>
|
||||
</li>
|
||||
</ul>
|
||||
<input type='submit' value='".$clang->gT("Import Responses")."' onclick='return confirm(\"".$clang->gT("Are you sure?","js").")'>
|
||||
<p><input type='submit' value='".$clang->gT("Import Responses")."' onclick='return confirm(\"".$clang->gT("Are you sure?","js").")'>
|
||||
<input type='hidden' name='subaction' value='import'><br /><br />
|
||||
<div class='warningtitle'>".$clang->gT("Warning: You can import all old responses with the same amount of columns as in your active survey. YOU have to make sure, that this responses corresponds to the questions in your active survey.")."</div>
|
||||
<div class='messagebox ui-corner-all'><div class='warningheader'>".$clang->gT("Warning").'</div>'.$clang->gT("You can import all old responses with the same amount of columns as in your active survey. YOU have to make sure, that this responses corresponds to the questions in your active survey.")."</div>
|
||||
</form>
|
||||
</div>
|
||||
<br />";
|
||||
}
|
||||
elseif (isset($surveyid) && $surveyid && isset($oldtable))
|
||||
{
|
||||
/*
|
||||
* TODO:
|
||||
* - mysql fit machen
|
||||
* -- quotes für mysql beachten --> `
|
||||
* - warnmeldung mehrsprachig
|
||||
* - testen
|
||||
*/
|
||||
// if($databasetype=="postgres")
|
||||
// {
|
||||
$activetable = "{$dbprefix}survey_$surveyid";
|
||||
|
||||
//Fields we don't want to import
|
||||
$dontimportfields = array(
|
||||
'id' //,'otherfield'
|
||||
);
|
||||
|
||||
// fields we can import
|
||||
$importablefields = array();
|
||||
if($databasetype=="postgres")
|
||||
{
|
||||
$query = "SELECT column_name as field FROM information_schema.columns WHERE table_name = '{$activetable}' ";
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = "SHOW COLUMNS FROM {$activetable}";
|
||||
}
|
||||
|
||||
$result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
$activetable = "{$dbprefix}survey_$surveyid";
|
||||
//Fields we don't want to import
|
||||
$dontimportfields = array(
|
||||
//,'otherfield'
|
||||
);
|
||||
|
||||
while ($row = $result->FetchRow())
|
||||
{
|
||||
if($databasetype=="postgres")
|
||||
{
|
||||
if (!in_array($row['field'],$dontimportfields))
|
||||
{
|
||||
$importablefields[] = $row['field'];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!in_array($row['Field'],$dontimportfields))
|
||||
{
|
||||
$importablefields[] = $row['Field'];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($importablefields as $field => $value)
|
||||
{
|
||||
$fields2insert[]=($databasetype=="postgres") ? "\"".$value."\"" : "`".$value."`";
|
||||
// if($databasetype=="postgres") { $fields2insert[] = "\"".$value."\""; }
|
||||
}
|
||||
|
||||
//fields we can supply //fields in the old database
|
||||
$availablefields = array();
|
||||
|
||||
if($databasetype=="postgres")
|
||||
{
|
||||
$query = "SELECT column_name as field FROM information_schema.columns WHERE table_name = '{$oldtable}' ";
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = "SHOW COLUMNS FROM {$oldtable}";
|
||||
}
|
||||
$result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
|
||||
while ($row = $result->FetchRow())
|
||||
{
|
||||
if($databasetype=="postgres")
|
||||
{
|
||||
if (!in_array($row['field'],$dontimportfields))
|
||||
{
|
||||
$availablefields[] = $row['field'];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!in_array($row['Field'],$dontimportfields))
|
||||
{
|
||||
$availablefields[] = $row['Field'];
|
||||
}
|
||||
}
|
||||
$aFieldsOldTable=array_values($connect->MetaColumnNames($oldtable, true));
|
||||
$aFieldsNewTable=array_values($connect->MetaColumnNames($activetable, true));
|
||||
|
||||
// Only import fields where the fieldnames are matching
|
||||
$aValidFields=array_intersect($aFieldsOldTable,$aFieldsNewTable);
|
||||
|
||||
}
|
||||
|
||||
foreach ($availablefields as $field => $value)
|
||||
{
|
||||
if($databasetype=="postgres")
|
||||
{
|
||||
$fields2import[] = "\"".$value."\"";
|
||||
}
|
||||
else
|
||||
{
|
||||
$fields2import[] = '`'.$value.'`';
|
||||
}
|
||||
}
|
||||
|
||||
$queryOldValues = "SELECT ".implode(", ",$fields2import)." "
|
||||
. "FROM {$oldtable} ";
|
||||
$resultOldValues = db_execute_assoc($queryOldValues) or safe_die("Error:<br />$queryOldValues<br />".$connect->ErrorMsg());
|
||||
|
||||
while ($row = $resultOldValues->FetchRow())
|
||||
{
|
||||
$values2import = array();
|
||||
foreach($row as $fieldName => $fieldValue)
|
||||
{
|
||||
if( $fieldValue=="")
|
||||
{
|
||||
$values2import[] = "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!is_numeric($fieldValue))
|
||||
$values2import[] = "'".db_quote($fieldValue)."'";
|
||||
else
|
||||
$values2import[] = "".$fieldValue."";
|
||||
}
|
||||
}
|
||||
|
||||
$insertOldValues = "INSERT INTO {$activetable} ( ".implode(", ",$fields2insert).") "
|
||||
. "VALUES( ".implode(", ",$values2import)."); ";
|
||||
$result = $connect->Execute($insertOldValues) or safe_die("Error:<br />$insertOldValues<br />".$connect->ErrorMsg());
|
||||
}
|
||||
// Only import fields not being in the $dontimportfields array
|
||||
$aValidFields=array_diff($aValidFields,$dontimportfields);
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // options (UI not implemented)
|
||||
//
|
||||
// $dontimportfields = array(
|
||||
// 'id' //,'otherfield'
|
||||
// );
|
||||
// $presetfields = array( // quote all strings so we can allow NULL
|
||||
// //'4X13X951'=>"'Y'"
|
||||
// //'id' => "NULL"
|
||||
// );
|
||||
// $importidrange = false; //array('first'=>3,'last'=>10);
|
||||
//
|
||||
// $activetable = "{$dbprefix}survey_$surveyid";
|
||||
//
|
||||
// // fields we can import
|
||||
// $importablefields = array();
|
||||
// $query = "SHOW COLUMNS FROM {$activetable}";
|
||||
// $result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
// while ($row = $result->FetchRow())
|
||||
// {
|
||||
// if (!in_array($row['Field'],$dontimportfields))
|
||||
// {
|
||||
// $importablefields[] = $row['Field'];
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // fields we can supply
|
||||
// $availablefields = array();
|
||||
// $query = "SHOW COLUMNS FROM {$oldtable}";
|
||||
// $result = db_execute_assoc($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
// while ($row = $result->FetchRow())
|
||||
// {
|
||||
// $availablefields[] = $row['Field'];
|
||||
// }
|
||||
// foreach ($presetfields as $field => $value)
|
||||
// {
|
||||
// if (!in_array($field,$availablefields))
|
||||
// {
|
||||
// $availablefields[] = $field;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// $fieldstoimport = array_intersect($importablefields,$availablefields);
|
||||
//
|
||||
// // data sources for each field (field of oldtable or preset value)
|
||||
// $sourcefields = array();
|
||||
// foreach ($fieldstoimport as $field)
|
||||
// {
|
||||
// $sourcefields[] = array_key_exists($field,$presetfields)?
|
||||
// $presetfields[$field]
|
||||
// : ($oldtable.'.`'.$field.'`');
|
||||
// $fieldstoimport2[] = '`'.$field.'`';
|
||||
// }
|
||||
//
|
||||
// $query = "INSERT INTO {$activetable} (\n\t".join("\t, ",$fieldstoimport2)."\n) "
|
||||
// ."SELECT\n\t".join("\t,",$sourcefields)."\n"
|
||||
// ."FROM {$oldtable}";
|
||||
// if (is_array($importidrange))
|
||||
// {
|
||||
// $query .= " WHERE {$oldtable}.id >= {$importidrange['first']} "
|
||||
// ." AND {$oldtable}.id <= {$importidrange['last']}";
|
||||
// }
|
||||
//
|
||||
// $result = $connect->Execute($query) or safe_die("Error:<br />$query<br />".$connect->ErrorMsg());
|
||||
// }
|
||||
header("Location: $scriptname?action=browse&sid=$surveyid");
|
||||
|
||||
$queryOldValues = "SELECT ".implode(", ",array_map("db_quote_id",$aValidFields))." FROM {$oldtable} ";
|
||||
$resultOldValues = db_execute_assoc($queryOldValues) or safe_die("Error:<br />$queryOldValues<br />".$connect->ErrorMsg());
|
||||
$iRecordCount=$resultOldValues->RecordCount();
|
||||
$aSRIDConversions=array();
|
||||
while ($row = $resultOldValues->FetchRow())
|
||||
{
|
||||
$iOldID=$row['id'];
|
||||
unset($row['id']);
|
||||
|
||||
$sInsertSQL="INSERT into {$activetable} (".implode(",",array_map("db_quote_id",array_keys($row))).") VALUES (".implode(",",array_map("db_quoteall",array_values($row))).")";
|
||||
$result = $connect->Execute($sInsertSQL) or safe_die("Error:<br />$sInsertSQL<br />".$connect->ErrorMsg());
|
||||
$aSRIDConversions[$iOldID]=$connect->Insert_Id($activetable,"id");
|
||||
}
|
||||
|
||||
$_SESSION['flashmessage'] = sprintf($clang->gT("%s old response(s) were successfully imported."),$iRecordCount);
|
||||
|
||||
$sOldTimingsTable=substr($oldtable,0,strrpos($oldtable,'_')).'_timings'.substr($oldtable,strrpos($oldtable,'_'));
|
||||
$sNewTimingsTable=db_table_name_nq("survey_{$surveyid}_timings");
|
||||
if (tableExists(sStripDBPrefix($sOldTimingsTable)) && tableExists(sStripDBPrefix($sNewTimingsTable)) && returnglobal('importtimings')=='Y')
|
||||
{
|
||||
// Import timings
|
||||
$aFieldsOldTimingTable=array_values($connect->MetaColumnNames($sOldTimingsTable, true));
|
||||
$aFieldsNewTimingTable=array_values($connect->MetaColumnNames($sNewTimingsTable, true));
|
||||
$aValidTimingFields=array_intersect($aFieldsOldTimingTable,$aFieldsNewTimingTable);
|
||||
|
||||
$queryOldValues = "SELECT ".implode(", ",$aValidTimingFields)." FROM {$sOldTimingsTable} ";
|
||||
$resultOldValues = db_execute_assoc($queryOldValues) or safe_die("Error:<br />$queryOldValues<br />".$connect->ErrorMsg());
|
||||
$iRecordCountT=$resultOldValues->RecordCount();
|
||||
$aSRIDConversions=array();
|
||||
while ($row = $resultOldValues->FetchRow())
|
||||
{
|
||||
if (isset($aSRIDConversions[$row['id']]))
|
||||
{
|
||||
$row['id']=$aSRIDConversions[$row['id']];
|
||||
}
|
||||
$sInsertSQL="INSERT into {$sNewTimingsTable} (".implode(",",array_map("db_quote_id",array_keys($row))).") VALUES (".implode(",",array_map("db_quoteall",array_values($row))).")";
|
||||
$result = $connect->Execute($sInsertSQL) or safe_die("Error:<br />$sInsertSQL<br />".$connect->ErrorMsg());
|
||||
}
|
||||
$_SESSION['flashmessage'] = sprintf($clang->gT("%s old response(s) and according timings were successfully imported."),$iRecordCount,$iRecordCountT);
|
||||
}
|
||||
$importoldresponsesoutput = browsemenubar($clang->gT("Quick statistics"));
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user