Skip to content

Commit

Permalink
Further enhancements - vvexport now has "introductory" screen that al…
Browse files Browse the repository at this point in the history
…lows user to choose whether or not to de-activate the survey after exporting, or not to. vvimport now checks for any fields in the import file that don't exist in the active survey, and can ignore them when importing - allowing questions to be removed from a survey, then the responses imported back. vvimport now MUST have the sid as the URL query - sid cannot be selected in page

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@1197 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Sep 18, 2004
1 parent 181e8f2 commit eccb720
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 62 deletions.
146 changes: 107 additions & 39 deletions admin/vvexport.php
Expand Up @@ -37,53 +37,121 @@
require_once("config.php");

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

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++)
if (!$action == "export")
{
$fieldnames[] = mysql_field_name($fldlist, $i);
echo $htmlheader;
echo "<br /><table align='center' class='outlinetable'>
<tr><th colspan='2'>VV Export</th></tr>
<form method='post'>
<tr>
<td align='right'>"._EXPORTSURVEY.":</td>
<td><input type='text' $slstyle size=4 value='$sid' name='sid' readonly></td>
</tr>
<tr>
<td align='right'>
Mode:
</td>
<td>
<select name='method' $slstyle>
<option value='deactivate' selected>Export then de-activate Survey</option>
<option value='none'>Export but leave results and survey active</option>
</select>
</td>
</tr>
<tr>
<td>&nbsp;
</td>
<td>
<input type='submit' value='"._EXPORTRESULTS."' $btstyle>&nbsp;
</td>
</tr>
<input type='hidden' name='action' value='export'>
</form>
<tr><td colspan='2' align='center'>[<a href='$scriptname?sid=4'>"._B_ADMIN_BT."</a>]</td></tr>
</table>";
}
elseif (isset($sid) && $sid)
{
//Export is happening, first lets do the exporting
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";

//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))
{
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)
{
echo $row[$field].$s;
$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 "\n";

//echo "<pre>$firstline</pre>";
//echo "<pre>$secondline</pre>";
//echo "<pre>"; print_r($fieldnames); echo "</pre>";
//echo "<pre>"; print_r($fieldmap); echo "</pre>";

//Now lets finalised according to the "method"
if (!isset($method)) {$method=returnglobal('method');}
switch($method)
{
case "deactivate": //Deactivate the survey
$date = date('YmdHi'); //'Hi' adds 24hours+minutes to name to allow multiple deactiviations in a day
$result = mysql_list_tables($databasename);
while ($row = mysql_fetch_row($result))
{
$tablelist[]=$row[0];
}
if (in_array("{$dbprefix}tokens_{$_GET['sid']}", $tablelist))
{
$toldtable="{$dbprefix}tokens_{$_GET['sid']}";
$tnewtable="{$dbprefix}old_tokens_{$_GET['sid']}_{$date}";
$tdeactivatequery = "RENAME TABLE $toldtable TO $tnewtable";
$tdeactivateresult = mysql_query($tdeactivatequery) or die ("\n\n"._ERROR."Couldn't deactivate tokens table because:<br />".mysql_error()."<br /><br />Survey was not deactivated either.<br /><br /><a href='$scriptname?sid={$_GET['sid']}'>"._GO_ADMIN."</a>");
}
$oldtable="{$dbprefix}survey_{$_GET['sid']}";
$newtable="{$dbprefix}old_{$_GET['sid']}_{$date}";
$deactivatequery = "RENAME TABLE $oldtable TO $newtable";
$deactivateresult = mysql_query($deactivatequery) or die ("\n\n"._ERROR."Couldn't deactivate because:<BR>".mysql_error()."<BR><BR><a href='$scriptname?sid={$_GET['sid']}'>Admin</a>");
break;
case "delete": //Delete the rows
break;
default:

} // switch
}

//echo "<pre>$firstline</pre>";
//echo "<pre>$secondline</pre>";
//echo "<pre>"; print_r($fieldnames); echo "</pre>";
//echo "<pre>"; print_r($fieldmap); echo "</pre>";
?>
90 changes: 67 additions & 23 deletions admin/vvimport.php
Expand Up @@ -44,16 +44,39 @@
{
//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>";

//Make sure that the survey is active
$result = mysql_list_tables($databasename);
while ($row = mysql_fetch_row($result))
{
$tablelist[]=$row[0];
}
if (in_array("survey_$sid", $tablelist))
{
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' value='$sid' readonly></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 "<br /><table class='outlinetable' align='center'>
<tr><th colspan=2>Import a VV survey file</th></tr>
<tr><td colspan='2' align='center'>
<b>Cannot import</b><br /><br />
This survey is not active. You must activate the survey before attempting to import a VVexport file.<br /><br />
[<a href='$scriptname?sid=4'>"._B_ADMIN_BT."</a>]
</td></tr>
</table>";
}


}
else
{
Expand All @@ -80,8 +103,7 @@
$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)
$buffer = fgets($handle, 10240); //To allow for very long lines (up to 10k)
$bigarray[] = $buffer;
}
fclose($handle);
Expand All @@ -99,22 +121,42 @@
$fieldcount--;
}
if ($noid == "noid") {unset($fieldnames[0]);}
// echo "FIELDS: Count - ".count($fieldnames);

$fldlist = mysql_list_fields($databasename, $surveytable);
$columns = mysql_num_fields($fldlist);
for ($i = 0; $i < $columns; $i++)
{
$realfieldnames[] = mysql_field_name($fldlist, $i);
}
if ($noid == "noid") {unset($realfieldnames[0]);}
unset($bigarray[1]); //delete the second line

// echo "[$noid]<br />";
// echo "<tr><td valign='top'><b>Import Fields:<pre>"; print_r($fieldnames); echo "</pre></td>";
// echo "<td valign='top'><b>Actual Fields:<pre>"; print_r($realfieldnames); echo '</pre></td></tr>';

//See if any fields in the import file don't exist in the active survey
$missing = array_diff($fieldnames, $realfieldnames);
if (is_array($missing) && count($missing) > 0)
{
foreach ($missing as $key=>$val)
{
$donotimport[]=$key;
unset($fieldnames[$key]);
}
}

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 (isset($donotimport))
{
foreach ($donotimport as $not)
{
unset($fieldvalues[$not]);
}
}
if ($noid == "noid") {unset($fieldvalues[0]);}
$insert = "INSERT INTO $surveytable\n";
$insert .= "(".implode(", ", $fieldnames).")\n";
Expand All @@ -125,12 +167,14 @@
{
echo "Failed insert: <pre>$insert</pre>".mysql_error();
}
//echo $insert."<br />\n";

}
}

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

echo "<font color='green'>"._SUCCESS."</font>
File upload completed.<br /><br />
<i>Note: Do NOT refresh this page</i><br /><br />";
echo "[<a href='$scriptname?sid=4'>"._B_ADMIN_BT."</a>]";
echo "</td></tr></table>";
}
?>

0 comments on commit eccb720

Please sign in to comment.