From ccade49bd424410080cd5ba7a46042ea9c338c9b Mon Sep 17 00:00:00 2001 From: Jason Cleeland Date: Sun, 20 Apr 2003 04:19:27 +0000 Subject: [PATCH] Modified to be able to correctly import conditions git-svn-id: file:///Users/Shitiz/Downloads/lssvn/trunk/unstable@244 b72ed6b6-b9f8-46b5-92b4-906544132732 --- admin/importsurvey.php | 114 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 8 deletions(-) diff --git a/admin/importsurvey.php b/admin/importsurvey.php index ce824ad1aa4..74ef405e758 100644 --- a/admin/importsurvey.php +++ b/admin/importsurvey.php @@ -97,7 +97,7 @@ $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];} @@ -105,16 +105,20 @@ } $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:
\n"; -echo "\n"; +$countconditions = count($conditionsarray); // CREATE SURVEY $sid = substr($tablearray[0], strpos($tablearray[0], "('")+2, (strpos($tablearray[0], "',")-(strpos($tablearray[0], "('")+2))); @@ -122,6 +126,8 @@ //$insert = substr($insert, 0, -1); $iresult = mysql_query($insert) or die("Insert of imported survey completely failed
\n$insert

\n" . mysql_error() . "\n"); +$oldsid=$sid; + //GET NEW SID $sidquery = "SELECT sid FROM surveys ORDER BY sid DESC LIMIT 1"; $sidres = mysql_query($sidquery); @@ -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 @@ -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
\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];} + 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 ("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); } } } +//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
\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()); + } + +echo "SURVEY SUMMARY:
\n"; +echo "\n"; + echo "Survey Import has been completed. Administration"; + echo "\n"; unlink($the_full_file_path); ?> \ No newline at end of file