Skip to content

Commit

Permalink
Added error checking in case groups, questions, answers or conditions…
Browse files Browse the repository at this point in the history
… don't exist - checks for existence of matching arrays before processing.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@261 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Apr 24, 2003
1 parent a207980 commit 9ab5694
Showing 1 changed file with 149 additions and 114 deletions.
263 changes: 149 additions & 114 deletions admin/importsurvey.php
Expand Up @@ -79,7 +79,14 @@
$bigarray = array_values($bigarray);

//GROUPS
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
if (array_search("# NEW TABLE\n", $bigarray))
{
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
}
else
{
$stoppoint = count($bigarray)-1;
}
for ($i=0; $i<=$stoppoint+2; $i++)
{
if ($i<$stoppoint-1) {$grouparray[] = $bigarray[$i];}
Expand All @@ -88,7 +95,14 @@
$bigarray = array_values($bigarray);

//QUESTIONS
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
if (array_search("# NEW TABLE\n", $bigarray))
{
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
}
else
{
$stoppoint = count($bigarray)-1;
}
for ($i=0; $i<=$stoppoint+2; $i++)
{
if ($i<$stoppoint-1) {$questionarray[] = $bigarray[$i];}
Expand All @@ -97,7 +111,14 @@
$bigarray = array_values($bigarray);

//ANSWERS
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
if (array_search("# NEW TABLE\n", $bigarray))
{
$stoppoint = array_search("# NEW TABLE\n", $bigarray);
}
else
{
$stoppoint = count($bigarray)-1;
}
for ($i=0; $i<=$stoppoint+2; $i++)
{
if ($i<$stoppoint-1) {$answerarray[] = $bigarray[$i];}
Expand All @@ -106,14 +127,16 @@
$bigarray = array_values($bigarray);

//CONDITIONS
$stoppoint = count($bigarray)-1;
for ($i=0; $i<=$stoppoint+2; $i++)
if ($noconditions != "Y")
{
if ($i<$stoppoint-1) {$conditionsarray[] = $bigarray[$i];}
unset($bigarray[$i]);
$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);
Expand All @@ -134,132 +157,144 @@
while ($srow = mysql_fetch_row($sidres)) {$newsid = $srow[0];}

// DO GROUPS, QUESTIONS FOR GROUPS, THEN ANSWERS FOR QUESTIONS IN A NESTED FORMAT!
foreach ($grouparray as $ga)
if ($grouparray)
{
$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
$gidquery = "SELECT gid FROM groups ORDER BY gid DESC LIMIT 1";
$gidres = mysql_query($gidquery);
while ($grow = mysql_fetch_row($gidres)) {$newgid = $grow[0];}
//NOW DO NESTED QUESTIONS FOR THIS GID
foreach ($questionarray as $qa)
foreach ($grouparray as $ga)
{
$sidpos = ", '$sid'";
$start = strpos($qa, "$sidpos")+2+strlen($sid)+5;
$end = strpos($qa, "'", $start)-$start;
if (substr($qa, $start, $end) == $gid)
$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
$gidquery = "SELECT gid FROM groups ORDER BY gid DESC LIMIT 1";
$gidres = mysql_query($gidquery);
while ($grow = mysql_fetch_row($gidres)) {$newgid = $grow[0];}
//NOW DO NESTED QUESTIONS FOR THIS GID
if ($questionarray)
{
$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];}
$newrank=0;
//NOW DO NESTED ANSWERS FOR THIS QID
foreach ($answerarray as $aa)
foreach ($questionarray as $qa)
{
$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))
$sidpos = ", '$sid'";
$start = strpos($qa, "$sidpos")+2+strlen($sid)+5;
$end = strpos($qa, "'", $start)-$start;
if (substr($qa, $start, $end) == $gid)
{
$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")
$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];}
$newrank=0;
//NOW DO NESTED ANSWERS FOR THIS QID
if ($answerarray)
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid.$code, $newsid."X".$newgid."X".$newqid.$code);
if ($type == "P")
foreach ($answerarray as $aa)
{
$fieldnames[]=array($oldsid."X".$oldgid."X".$oldqid.$code."comment", $newsid."X".$newgid."X".$newqid.$code."comment");
$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);
}
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)
if ($conditionsarray) //ONLY DO THIS IF THERE ARE CONDITIONS!
{
$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)
foreach ($conditionsarray as $car)
{
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];}
$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());
}
$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";
Expand Down

0 comments on commit 9ab5694

Please sign in to comment.