Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modified to be able to correctly import conditions
git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@244 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Apr 20, 2003
1 parent faf081e commit ccade49
Showing 1 changed file with 106 additions and 8 deletions.
114 changes: 106 additions & 8 deletions admin/importsurvey.php
Expand Up @@ -97,31 +97,37 @@
$bigarray = array_values($bigarray);

//ANSWERS
$stoppoint = count($bigarray);
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
for ($i=0; $i<=$stoppoint+2; $i++)
{
if ($i<$stoppoint-1) {$answerarray[] = $bigarray[$i];}
unset($bigarray[$i]);
}
$bigarray = array_values($bigarray);

//CONDITIONS
$stoppoint = count($bigarray)-1;
for ($i=0; $i<=$stoppoint+2; $i++)
{
if ($i<$stoppoint-1) {$conditionsarray[] = $bigarray[$i];}
unset($bigarray[$i]);
}


$countsurveys = count($tablearray);
$countgroups = count($grouparray);
$countquestions = count($questionarray);
$countanswers = count($answerarray);

echo "SURVEY SUMMARY:<br />\n";
echo "<ul>\n\t<li>Surveys: $countsurveys</li>\n";
echo "\t<li>Groups: $countgroups</li>\n";
echo "\t<li>Questions: $countquestions</li>\n";
echo "\t<li>Answers: $countanswers</li>\n</ul>\n";
$countconditions = count($conditionsarray);

// CREATE SURVEY
$sid = substr($tablearray[0], strpos($tablearray[0], "('")+2, (strpos($tablearray[0], "',")-(strpos($tablearray[0], "('")+2)));
$insert = str_replace("('$sid'", "(''", $tablearray[0]);
//$insert = substr($insert, 0, -1);
$iresult = mysql_query($insert) or die("Insert of imported survey completely failed<br />\n$insert<br /><br />\n" . mysql_error() . "</body>\n</html>");

$oldsid=$sid;

//GET NEW SID
$sidquery = "SELECT sid FROM surveys ORDER BY sid DESC LIMIT 1";
$sidres = mysql_query($sidquery);
Expand All @@ -132,6 +138,7 @@
{
$gid = substr($ga, strpos($ga, "('")+2, (strpos($ga, "',")-(strpos($ga, "('")+2)));
$ginsert = str_replace("('$gid', '$sid',", "('', '$newsid',", $ga);
$oldgid=$gid;
//$ginsert = substr($ginsert, 0, -1);
$gres = mysql_query($ginsert);
//GET NEW GID
Expand All @@ -147,32 +154,123 @@
if (substr($qa, $start, $end) == $gid)
{
$qid = substr($qa, strpos($qa, "('")+2, (strpos($qa, "',")-(strpos($qa, "('")+2)));
$oldqid=$qid;
$qinsert = str_replace("('$qid', '$sid', '$gid',", "('$newsid', '$newgid',", $qa);
$qinsert = str_replace("(`qid`, ", "(", $qinsert);
//$qinsert = substr(trim($qinsert), 0, -1);
//FIELDNAME ARRAY GENERATION
$typepos = "('$qid', '$sid', '$gid', '";
$type = substr($qa, strpos($qa, $typepos)+strlen($typepos), 1);
$otherpos = "')";
$other = substr($qa, strpos($qa, $otherpos)-6, 1);

//echo "$qinsert<br />\n";
$qres = mysql_query($qinsert) or die ("<b>ERROR:</b> Failed to insert question<br />\n$qinsert<br />\n".mysql_error()."</body>\n</html>");
//GET NEW GID
$qidquery = "SELECT qid FROM questions ORDER BY qid DESC LIMIT 1";
$qidres = mysql_query($qidquery);
while ($qrow = mysql_fetch_row($qidres)) {$newqid = $qrow[0];}
while ($qrow = mysql_fetch_row($qidres)) {$newqid = $qrow[0];}
$newrank=0;
//NOW DO NESTED ANSWERS FOR THIS QID
foreach ($answerarray as $aa)
{
$qidpos = "('";
$astart = strpos($aa, "$qidpos")+2;
$aend = strpos($aa, "'", $astart)-$astart;
$codepos1=strpos($aa, "', '")+4;
$codepos2=strpos($aa, "', '", strpos($aa, "', '")+1);
$codelength=$codepos2-$codepos1;
$code = substr($aa, $codepos1, $codelength);
if (substr($aa, $astart, $aend) == ($qid))
{
$ainsert = str_replace("('$qid", "('$newqid", $aa);
//$ainsert = substr(trim($ainsert), 0, -1);
$ares = mysql_query($ainsert) or die ("<b>ERROR:</b> Failed to insert answer<br />\n$ainsert<br />\n".mysql_error()."</body>\n</html>");
if ($type == "A" || $type == "B" || $type == "C" || $type == "M" || $type == "P")
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid.$code, $newsid."X".$newgid."X".$newqid.$code);
if ($type == "P")
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid.$code."comment", $newsid."X".$newgid."X".$newqid.$code."comment");
}
}
elseif ($type == "R")
{
$newrank++;
}
}
}
if (($type == "A" || $type == "B" || $type == "C" || $type == "M" || $type == "P") && ($other == "Y"))
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid."other", $newsid."X".$newgid."X".$newqid."other");
if ($type == "P")
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid."othercomment", $newsid."X".$newgid."X".$newqid."othercomment");
}
}
if ($type == "R" && $newrank >0)
{
for ($i=1; $i<=$newrank; $i++)
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid.$i, $newsid."X".$newgid."X".$newqid.$i);
}
}
if ($type != "A" && $type != "B" && $type != "C" && $type != "R" && $type != "M" && $type != "P")
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid, $newsid."X".$newgid."X".$newqid);
if ($type == "O")
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid."comment", $newsid."X".$newgid."X".$newqid."comment");
}
}
$substitutions[]=array($oldsid, $oldgid, $oldqid, $newsid, $newgid, $newqid);
}
}
}
//We've built two arrays along the way - one containing the old SID, GID and QIDs - and their NEW equivalents
//and one containing the old 'extended fieldname' and its new equivalent. These are needed to import conditions.

foreach ($conditionsarray as $car)
{
$startpos=strpos($car, "('")+2;
$nextpos=strpos($car, "', '", $startpos);
$oldcid=substr($car, $startpos, $nextpos-$startpos);
$startpos=$nextpos+4;
$nextpos=strpos($car, "', '", $startpos);
$oldqid=substr($car, $startpos, $nextpos-$startpos);
$startpos=$nextpos+4;
$nextpos=strpos($car, "', '", $startpos);
$oldcqid=substr($car, $startpos, $nextpos-$startpos);
$startpos=$nextpos+4;
$nextpos=strpos($car, "', '", $startpos);
$oldcfieldname=substr($car, $startpos, $nextpos-$startpos);
$toreplace="('$oldcid', '$oldqid', '$oldcqid', '$oldcfieldname'";
//echo "$toreplace<br />\n";
foreach ($substitutions as $subs)
{
if ($oldqid==$subs[2]) {$newqid=$subs[5];}
if ($oldcqid==$subs[2]) {$newcqid=$subs[5];}
}
foreach($fieldnames as $fns)
{
if ($oldcfieldname==$fns[0]) {$newcfieldname=$fns[1];}
}
$replacewith="('', '$newqid', '$newcqid', '$newcfieldname'";
//echo "$replacewith<br />\n";
$insert=str_replace($toreplace, $replacewith, $car);
//echo "$insert<br /><br />\n";
$result=mysql_query($insert) or die ("Couldn't insert condition<br />$insert<br />".mysql_error());
}

echo "SURVEY SUMMARY:<br />\n";
echo "<ul>\n\t<li>Surveys: $countsurveys</li>\n";
echo "\t<li>Groups: $countgroups</li>\n";
echo "\t<li>Questions: $countquestions</li>\n";
echo "\t<li>Answers: $countanswers</li>\n";
echo "\t<li>Conditions: $countconditions</li>\n</ul>\n";

echo "<b>Survey Import has been completed. <a href='admin.php?sid=$newsid'>Administration</a></b>";

echo "</body>\n</html>";
unlink($the_full_file_path);
?>

0 comments on commit ccade49

Please sign in to comment.