- Convert fair stats to JSON

This commit is contained in:
dave 2009-05-05 07:27:02 +00:00
parent ee978816b2
commit 687d4ea875

View File

@ -28,9 +28,8 @@
require_once('xml.inc.php');
require_once('stats.inc.php');
function stats_to_xml($fair, $stats)
function stats_to_ysf($fair, $stats)
{
global $output;
if($fair['type'] == 'ysf') {
/* Map data into YSF tags */
$y=array();
@ -51,22 +50,9 @@
$y["proj11up"]=$stats['projects_11'];
$y["committee"]=$stats['committee_members'];
$y["judges"]=$stats['judges'];
$xmldata=array("affiliation"=>array(
"ysf_region_id"=>$fair['username'],
"ysf_region_password"=>$fair['password'],
"year"=>$year,
"stats"=>$y)
);
} else {
$xmldata=array("sfiab"=>array(
"username"=>$fair['username'],
"password"=>$fair['password'],
"stats"=>$stats));
}
$output="";
xmlCreateRecurse($xmldata);
return $output;
return $y;
}
return $stats;
}
@ -89,14 +75,30 @@
$server_config['fair_stats_scholarships'] = 'no';
$server_config['fair_stats_delegates'] = 'no';
function curl_query($fair, $xml)
function curl_query($fair, $data)
{
global $output;
switch($fair['type']) {
case 'sfiab':
$url = $fair['url'].'/xmltransport.php';
$var = 'json';
$d = array();
$d['auth'] = array('username' => $fair['username'],
'password' => $fair['password']);
$str = json_encode(array_merge($d, $data));
break;
case 'ysf':
$url = "https://secure.ysf-fsj.ca/registration/xmlaffiliation.php";
$var = 'xml';
$d = array();
$d['affiliation'] = array(
"ysf_region_id"=>$fair['username'],
"ysf_region_password"=>$fair['password'],
"year"=>$year);
foreach($data as $k=>$v) $d['affiliation'][$k] = $v;
$output="";
xmlCreateRecurse($d);
$str = $output;
break;
}
@ -104,25 +106,30 @@
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control
curl_setopt ($ch, CURLOPT_POST, 1); /// tell it to make a POST, not a GET
curl_setopt ($ch, CURLOPT_POSTFIELDS, "xml=".urlencode($xml)); /// put the query string here starting with "?"
curl_setopt ($ch, CURLOPT_POSTFIELDS, "$var=".urlencode($str)); /// put the query string here starting with "?"
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $datastream
curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 360);
curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
$datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream
$datastream = str_replace(" standalone=\"yes\"","",$datastream);
curl_close ($ch); /// close the curl session
// echo "<pre>$datastream</pre>";
switch($fair['type']) {
case 'sfiab':
$d=xml_parsexml(urldecode($datastream));
$datastream = $d['sfiab'][0];
$ret=json_decode(urldecode($datastream), true);
break;
case 'ysf':
$datastream = str_replace(" standalone=\"yes\"","",$datastream);
/* Return is plaintext, make a return array */
$ret['error'] = 0;
$ret['message'] = $datastream;
break;
}
return $datastream;
// echo "ret: ";print_r($ret);echo "<br>";
return $ret;
}
if($_GET['year']) $year=intval($_GET['year']);
@ -144,14 +151,18 @@
$stats[$k] = $_POST[$k];
}
$stats['year'] = $year;
$xml = stats_to_xml($fair, $stats);
if($fair['type'] == 'ysf') {
$st = stats_to_ysf($fair, $stats);
} else {
$st = $stats;
}
if(function_exists('curl_init')) {
$r = curl_query($fair, $xml);
if($r['error'][0] == 0)
echo happy(i18n("The %1 Server said:", array($fair['name'])).' '.$r['message'][0]);
$r = curl_query($fair, array('stats'=>$st));
if($r['error'] == 0)
echo happy(i18n("The %1 Server said:", array($fair['name'])).' '.$r['message']);
else
echo error(i18n("The %1 Server said:", array($fair['name'])).' '.$r['message'][0]);
echo error(i18n("The %1 Server said:", array($fair['name'])).' '.$r['message']);
// $fairs_id = -1;
// $year = $config['FAIRYEAR'];
} else {
@ -195,19 +206,12 @@
echo notice(i18n('Getting stats request and downloading existing stats from server %1', array($fair['url'])));
/* Query the server to see what stats we need */
$xmldata=array("sfiab"=>array(
"username"=>$fair['username'],
"password"=>$fair['password'],
"getstats"=>array('year'=>$year)));
$q=array('getstats' => array('year' => $year));
$output="";
xmlCreateRecurse($xmldata);
$xml=$output;
$data = curl_query($fair, $q);
$data = curl_query($fair, $xml);
if($data['error'][0] != 0) {
echo error("Server said: {$data['message'][0]}<br />");
if($data['error'] != 0) {
echo error("Server said: {$data['message']}<br />");
send_footer();
exit;
}
@ -218,7 +222,7 @@
echo '<br /><br />';
foreach($server_config as $k=>$v) {
$server_config[$k] = $data['statconfig'][0][$k][0];
$server_config[$k] = $data['statconfig'][$k];
}
/* Gather all stats, then we'll decide what to send */
@ -226,9 +230,9 @@
$stats['year'] = $year;
/* Now, overwrite all the stats with what we pulled down from the server */
if(is_array($data['stats'][0])) {
foreach($data['stats'][0] as $k=>$v) {
$stats[$k] = $v[0];
if(is_array($data['stats'])) {
foreach($data['stats'] as $k=>$v) {
$stats[$k] = $v;
}
}
// print_r($data['stats'][0]);