<?php
//    read a server status variable (todo =131). See saveServerVar for a more complex version of this task
// Fields in the request are:
//        session: the survey session
//        varname: variable to be saved
//        default: value to return if no such status veriable
  require("phpVars.php");

 $sessName='';
 $varname=''   ;
 $default='';
 foreach ($_REQUEST as $vname=>$oof) {
     $avar=strtoupper(trim($vname)) ;
     if ($avar=='SESSION') $sessName=trim($oof) ;
     if ($avar=='VARNAME') $varname=trim($oof) ;
     if ($avar=='DEFAULT') $default=trim($oof) ;
 }

header("Content-Type: text/plain");

if ($sessName=='') {
  print '- ERROR: No SESSION=  in call to   readServerStatusVar.php ';
  exit ;
}

if ($varname=='') {
  print '- ERROR: No VARNAME=  in call to   readServerStatusVar.php ';
  exit ;
}

$cvar=strtoupper(trim($varname));

// add variables to STATUSVARS table

$idate =time();

$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {
   print ' - Error. Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
   exit ;
}

$mdata=$hdrMysql . $sessName ;
$qq=mysql_select_db($mdata, $link);
if (!$qq) {
   print '- Error. Unable to select mysql database ' . $mdata . ': '  . mysql_error();
   mysql_close($link);
   return 0;
}


 $sql="SELECT * FROM statusvars WHERE varname='$cvar'" ;

 $qq=mysql_query($sql,$link);
 if (!$qq) {
     print '- Error. Unable to read status variable ' .$cvar . ' in STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
      mysql_close($link);
     return 0;
 }

 $row = mysql_fetch_array($qq);        // just use first row (should only be one row)
 if (!$row) {
     $cval=$default;
 } else {
   $cval=$row['varvalue'] ;
 }

 mysql_close($link);
 print $cval ;
 exit;



?>

