<?php
// return  GENERAL, USERS, or RESULTS , HISTORY , or SIMPLEHISTORY, or CUSTOM data (admin todo=461)
//
//
// simpleHistory
//     arow int(4) AUTO_INCREMENT NOT NULL ,
//       PRIMARY KEY (arow)  ,
//    round int(4),
//    content text ,
//    comment varchar(120),
//    servertime int

//
// historyData, resultsData, generalData, usersData, participantsData
//    round int(4)  NOT NULL ,
//       PRIMARY KEY (round)  ,
//    content text ,
//    servertime int

// customData
//  round int(4)  NOT NULL ,
//  cname varchar(60) NOT NULL ,
//     PRIMARY KEY (cname,round)  ,
//  content text ,
//  servertime int


//      Thus, they should be unescaped before sending back in database is sent back as is; it might be escaped
//       saveData will make some choices about unescaping stuff
//       a) xxxData (such as generalData) the xml data that was escaped for safe passage will be unescaped bu saveData (hence stored as the original xml)
//       b) simpleHistory that was escaped for proper parsing,   will NOT be unescaped by saveData


require("phpVars.php");

  $sessName=''; $atype='';$cname='';  $contents='';
  $around=0;
  foreach ($_REQUEST as $vname=>$oof) {
     $avar=trim(strtoupper($vname)) ;
     if ($avar=='SESSION') $sessName=trim($oof);
     if ($avar=='TYPE')  $atype=trim(strtoupper($oof)) ;
     if ($avar=='CNAME' || $avar=='NAME') $cname=trim(strtoupper($oof)) ;
  }


if ($sessName=='') {
  header('Content-type:text/plain');
  print '- Error. No NAME or SESSION= in call to   getSavedAnyData ';
  exit ;
}

if ($atype=='') {
  header('Content-type:text/plain');
  print '- Error. No TYPE in call to   getSavedAnyData ';
  exit ;
}

if ($atype!='HISTORY' && $atype!=='SIMPLEHISTORY' &&   $atype!=='PARTICIPANTS' &&  $atype!=='GENERAL' && $atype!=='USERS' && $atype!=='RESULTS'  && $atype!=='CUSTOM'  )  {
  header('Content-type:text/plain');
  print '- Error.  TYPE  must be HISTORY, SIMPLEHISTORY, PARTICIPANTS,  GENERAL, USERS ,RESULTS, or CUSTOM in getSavedAnyData ';
  exit ;
}

if ($atype=='CUSTOM' && $cname=='') {
  header('Content-type:text/plain');
  print '- Error. No CNAME in type=custom call to   getSavedAnyData ';
  exit ;
}

$link=connectDsurveyDatabase($sessName);
$mdata=$currentDatabase ;

if ($atype=='HISTORY') {
 $sql="SELECT * FROM historyData  WHERE round > 0 " ;
} elseif ($atype=='GENERAL') {
 $sql="SELECT * FROM generalData  WHERE round > 0 " ;
} elseif ($atype=='RESULTS') {
 $sql="SELECT * FROM resultsData  WHERE round > 0 " ;
} elseif ($atype=='USERS') {
 $sql="SELECT * FROM usersData  WHERE round > 0 " ;
 } elseif ($atype=='PARTICIPANTS') {
 $sql="SELECT * FROM participantsData  WHERE round > 0 " ;

} elseif ($atype=='CUSTOM') {
 $sql="SELECT * FROM customData  WHERE cname = '$cname' " ;

} else {
 $sql="SELECT * FROM simpleHistory  WHERE round > 0 " ;
}

 $qq=mysql_query($sql,$link);
 if (!$qq) {
     header('Content-type:text/plain');
     print '- Error. Unable to read  ' . $mdata . ': '  . mysql_error();
     mysql_close($link);
     return 0;
 }


 header('Content-type:text/xml');
 $stuff='';

$vudate=date(DATE_RFC822)  ;

$stuff='<?xml version="1.0" encoding="UTF-8" ?>' . " \n";
if ($atype=='CUSTOM') {
  $stuff=$stuff . '<data_'.$atype.' time="'.$vudate.'" session="'.$sessName.'" custom="'.$cname.'" > ' . "\n";
} else {
  $stuff=$stuff . '<data_'.$atype.' time="'.$vudate.'" session="'.$sessName.'"> ' . "\n";
}

 while($row = mysql_fetch_array($qq)) {
    $aline=$row['content'];                 // 'content' contains origional submission, which was an "xml-line"  or "set of xml-lines"
    $stuff=$stuff . "$aline \n";
}

$stuff=$stuff . "</data_$atype> \n";


 mysql_close($link);


 print $stuff;
 exit ;
 ?>
