<?php
// return history or simple history data (todo=461)
// expects session=sessname&type=atype ; atype either HISTORY or SIMPLEHISTORY
// Notes: /client/phps/submitEntry.php will add to simpleHistory
//        /admins/phps/saveAnyData.php to add to historyData
//
// simpleHistory
//     arow int(4) AUTO_INCREMENT NOT NULL ,
//       PRIMARY KEY (arow)  ,
//    round int(4),
//    content text ,
//    comment varchar(120),
//    servertime int

//
// historyData
//    round int(4)  NOT NULL ,
//       PRIMARY KEY (round)  ,
//    content text ,
//    servertime int
//
// Note: both of these "provided by client" data is shipped in a quasi-xml structure, with values saved 
// in an field with a CSV format, and with each value escaped. SO don't attempt to escape or unescape it here.

require("phpVars.php");

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

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

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

if ($atype!='HISTORY' && $atype!=='SIMPLEHISTORY') {
  header('Content-type:text/plain');
  print '- Error.  TYPE  must be HISTORY or SIMPLEHISTORY  in call to   getSaveDataHistory ';
  exit ;
}

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

if ($atype=='HISTORY') {
 $sql="SELECT * FROM historyData  WHERE round >= 0 " ;
} 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";
$stuff=$stuff . '<data_'.$atype.' time="'.$vudate.'" session="'.$sessName.'"> ' . "\n";

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

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


 mysql_close($link);


 print $stuff;
 exit ;
 ?>

