Skip to content

Commit

Permalink
NEW: Exports a special tab delimited file which can be edited/modifie…
Browse files Browse the repository at this point in the history
…d, then re-imported. First line of file is "human readable" title, second line is "fieldnames" required on re-import to assign data. VVImport ignores first line, and allows you to remove the "id" number, so that PHPSurveyor automatically renumbers. These scripts are currently standalone and further work is expected.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@1196 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Sep 18, 2004
1 parent f994cd5 commit 181e8f2
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
89 changes: 89 additions & 0 deletions admin/vvexport.php
@@ -0,0 +1,89 @@
<?php
/*
#############################################################
# >>> PHP Surveyor #
#############################################################
# > 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. #
#############################################################
*/
//Exports all responses to a survey in special "Verified Voting" format.
require_once("config.php");

if (!isset($sid)) {$sid=returnglobal('sid');}

header("Content-Disposition: attachment; filename=vvexport_$sid.xls");
header("Content-type: application/vnd.ms-excel");
$s="\t";

$fieldmap=createFieldMap($sid, "full");
$surveytable = "{$dbprefix}survey_$sid";

loadPublicLangFile($sid);

$fldlist = mysql_list_fields($databasename, $surveytable);
$columns = mysql_num_fields($fldlist);
for ($i = 0; $i < $columns; $i++)
{
$fieldnames[] = mysql_field_name($fldlist, $i);
}


//Create the human friendly first line
$firstline="";
$secondline="";
foreach ($fieldnames as $field)
{
$fielddata=arraySearchByKey($field, $fieldmap, "fieldname", 1);
//echo "<pre>";print_r($fielddata);echo "</pre>";
if (count($fielddata) < 1) {$firstline.=$field;}
else
{$firstline.=str_replace("\n", " ", str_replace("\t", " ", $fielddata['question']));}
$firstline .= $s;
$secondline .= $field.$s;
}
echo $firstline."\n";
echo $secondline."\n";
$query = "SELECT * FROM $surveytable";
$result = mysql_query($query) or die("Error:<br />$query<br />".mysql_error());

while ($row=mysql_fetch_array($result))
{
foreach ($fieldnames as $field)
{
echo $row[$field].$s;
}
echo "\n";
}

//echo "<pre>$firstline</pre>";
//echo "<pre>$secondline</pre>";
//echo "<pre>"; print_r($fieldnames); echo "</pre>";
//echo "<pre>"; print_r($fieldmap); echo "</pre>";
?>
136 changes: 136 additions & 0 deletions admin/vvimport.php
@@ -0,0 +1,136 @@
<?php
/*
#############################################################
# >>> PHP Surveyor #
#############################################################
# > 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. #
#############################################################
*/

require_once("config.php");

if (!isset($sid)) {$sid=returnglobal('sid');}
if (!isset($action)) {$action=returnglobal('action');}
if (!isset($noid)) {$noid=returnglobal('noid');}

if ($action != "upload")
{
//PRESENT FORM
echo $htmlheader;
echo "<br /><table class='outlinetable' align='center'>
<form enctype='multipart/form-data' method='post'>
<tr><th colspan=2>Import a VV survey file</th></tr>
<tr><td>File:</td><td><input type='file' name='the_file'></td></tr>
<tr><td>Survey ID:</td><td><input type='text' size=2 name='sid'></td></tr>
<tr><td>Exclude id?</td><td><input type='checkbox' name='noid' value='noid' checked></td></tr>
<tr><td>&nbsp;</td><td><input type='submit' value='Upload'></td></tr>
<input type='hidden' name='action' value='upload'>
</form>
</table>";
}
else
{
echo $htmlheader;
echo "<br /><table class='outlinetable' align='center'>
<tr><th>Upload</th></tr>
<tr><td>";
$the_full_file_path = $tempdir . "/" . $_FILES['the_file']['name'];

if (!@move_uploaded_file($_FILES['the_file']['tmp_name'], $the_full_file_path))
{
echo "<b><font color='red'>"._ERROR."</font></b><br />\n";
echo _IS_FAILUPLOAD."<br /><br />\n";
//echo "<input $btstyle type='submit' value='"._GO_ADMIN."' onClick=\"window.open('$scriptname', '_top')\">\n";
echo "</font></td></tr></table>\n";
echo "</body>\n</html>\n";
exit;
}
// IF WE GOT THIS FAR, THEN THE FILE HAS BEEN UPLOADED SUCCESFULLY

echo "<b><font color='green'>"._SUCCESS."</font></b><br />\n";
echo _IS_OKUPLOAD."<br /><br />\n";
echo _IS_READFILE."<br />\n";
$handle = fopen($the_full_file_path, "r");
while (!feof($handle))
{
//$buffer = fgets($handle, 1024); //Length parameter is required for PHP versions < 4.2.0
$buffer = fgets($handle, 10240); //To allow for very long survey welcomes (up to 10k)
$bigarray[] = $buffer;
}
fclose($handle);

$surveytable = "survey_$sid";

unlink($the_full_file_path); //delete the uploaded file
unset($bigarray[0]); //delete the first line

$fieldnames=explode("\t", trim($bigarray[1]));
$fieldcount=count($fieldnames)-1;
if (trim($fieldnames[$fieldcount]) == "") //Get rid of a blank entry at the end
{
unset($fieldnames[$fieldcount]);
$fieldcount--;
}
if ($noid == "noid") {unset($fieldnames[0]);}
// echo "FIELDS: Count - ".count($fieldnames);
unset($bigarray[1]); //delete the second line

// echo "[$noid]<br />";

foreach($bigarray as $row)
{
if (trim($row) != "")
{
$fieldvalues=explode("\t", mysql_escape_string(str_replace("\n", "", $row)), $fieldcount+1);
// if (trim($fieldvalues[count($fieldvalues)-1]) == "") //Get rid of a blank entry at the end
// {
// unset($fieldvalues[count($fieldvalues)-1]);
// }
// echo "INSERT: Count - ".count($fieldvalues);
// echo "<pre>";print_r($fieldvalues);echo "</pre>";
if ($noid == "noid") {unset($fieldvalues[0]);}
$insert = "INSERT INTO $surveytable\n";
$insert .= "(".implode(", ", $fieldnames).")\n";
$insert .= "VALUES\n";
$insert .= "('".implode("', '", $fieldvalues)."')\n";

if (!$result = mysql_query($insert))
{
echo "Failed insert: <pre>$insert</pre>".mysql_error();
}
//echo $insert."<br />\n";
}
}

// echo "<pre>";print_r($bigarray);echo "</pre>";

echo "</td></tr></table>";
}
?>

0 comments on commit 181e8f2

Please sign in to comment.