From 9ab5694b3f6d096d81eb5102a08bfc011eaafafb Mon Sep 17 00:00:00 2001 From: Jason Cleeland Date: Thu, 24 Apr 2003 06:45:36 +0000 Subject: [PATCH] Added error checking in case groups, questions, answers or conditions 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 --- admin/importsurvey.php | 263 +++++++++++++++++++++++------------------ 1 file changed, 149 insertions(+), 114 deletions(-) diff --git a/admin/importsurvey.php b/admin/importsurvey.php index 74ef405e758..329d57c1a57 100644 --- a/admin/importsurvey.php +++ b/admin/importsurvey.php @@ -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];} @@ -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];} @@ -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];} @@ -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); @@ -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
\n"; - $qres = mysql_query($qinsert) or die ("ERROR: Failed to insert question
\n$qinsert
\n".mysql_error()."\n"); - //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 ("ERROR: Failed to insert answer
\n$ainsert
\n".mysql_error()."\n"); - 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
\n"; + $qres = mysql_query($qinsert) or die ("ERROR: Failed to insert question
\n$qinsert
\n".mysql_error()."\n"); + //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 ("ERROR: Failed to insert answer
\n$ainsert
\n".mysql_error()."\n"); + 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
\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
\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
\n"; + $insert=str_replace($toreplace, $replacewith, $car); + //echo "$insert

\n"; + $result=mysql_query($insert) or die ("Couldn't insert condition
$insert
".mysql_error()); } - $replacewith="('', '$newqid', '$newcqid', '$newcfieldname'"; - //echo "$replacewith
\n"; - $insert=str_replace($toreplace, $replacewith, $car); - //echo "$insert

\n"; - $result=mysql_query($insert) or die ("Couldn't insert condition
$insert
".mysql_error()); } echo "SURVEY SUMMARY:
\n";