science-ation/admin/award_download.php
james 0f198346aa Update fake scripts for 2008
Add external award downloader functionality
2007-11-27 22:47:06 +00:00

282 lines
10 KiB
PHP

<?
/*
This file is part of the 'Science Fair In A Box' project
SFIAB Website: http://www.sfiab.ca
Copyright (C) 2007 James Grant <james@lightbox.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
?>
<?
require("../common.inc.php");
require_once("../user.inc.php");
user_auth_required('committee', 'admin');
send_header("Download Awards",
array('Committee Main' => 'committee_main.php',
'Administration' => 'admin/index.php',
'Awards Main' => 'admin/awards.php' ));
if($_GET['action']=="check")
{
if(count($_GET['check'])) {
require_once("xml.inc.php");
foreach($_GET['check'] AS $ch) {
$q=mysql_query("SELECT * FROM award_sources WHERE id='".intval($ch)."'");
$r=mysql_fetch_object($q);
echo i18n("Checking %1 for awards...",array($r->name));
echo "<br />";
$req=array("awardrequest"=>array(
"username"=>$r->username,
"password"=>$r->password,
"year"=>$config['FAIRYEAR'],
)
);
$output="";
xmlCreateRecurse($req);
$xmldata=$output;
if(function_exists('curl_init'))
{
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_URL,"$r->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=".$xmldata); /// 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);
$datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream
$datastream = str_replace(" standalone=\"yes\"","",$datastream);
// echo "curl close <br />";
curl_close ($ch); /// close the curl session
echo i18n("The Remote Server said:")."<br />";
//echo notice($datastream);
$response=xml_parsexml($datastream);
$keys=array_keys($response);
if($keys[0]=="awardresponse") {
echo "<i>";
$ar=$response['awardresponse'][0];
$postback=$ar['postback'][0];
echo i18n("Postback URL: %1",array($postback))." <br />";
$numawards=count($ar['awards'][0]['award']);
echo i18n("Number of Awards: %1",array($numawards))." <br />";
foreach($ar['awards'][0]['award'] AS $award) {
$identifier=$award['identifier'][0];
$year=$award['year'][0];
echo i18n("Award Identifier: %1",array($identifier))." &nbsp; ";
echo i18n("Award Year: %1",array($year))."<br />";
if($year==$config['FAIRYEAR']) {
$tq=mysql_query("SELECT * FROM award_awards WHERE external_identifier='$identifier' AND year='$year'");
if($awardrecord=mysql_fetch_object($tq)) {
echo i18n("Award already exists, updating info")."<br />";
mysql_query("UPDATE award_awards SET
name='".mysql_escape_string($award['name_en'][0])."',
criteria='".mysql_escape_string($award['criteria_en'][0])."',
postback='".mysql_escape_string($postback)."'
WHERE
id='$awardrecord->id'
AND external_identifier='".mysql_escape_string($identifier)."'
AND year='$year'
");
//FIXME: update the prizes
if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0))
{
$prizes=$award['prizes'][0]['prize'];
$numprizes=count($prizes);
echo i18n("Number of prizes: %1",array($numprizes))."<br />";
$pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$awardrecord->id'");
$existingprizes=array();
while($pr=mysql_fetch_object($pq)) {
$existingprizes[$pr->external_identifier]=$pr;
}
foreach($prizes AS $prize) {
//if it exists -> update it
if(array_key_exists($prize['identifier'][0],$existingprizes)) {
$ep=$existingprizes[$prize['identifier'][0]];
echo "&nbsp;".i18n("Updating prize %1",array($ep->external_identifier))."<br />";
mysql_query("UPDATE award_prizes SET
cash='".intval($prize['cash'][0])."',
scholarship='".intval($prize['scholarship'][0])."',
value='".intval($prize['value'][0])."',
prize='".mysql_escape_string($prize['prize_en'][0])."',
number='".intval($prize['number'][0])."',
`order`='".intval($prize['ord'][0])."'
WHERE
id='$ep->id'");
//remove it from the list
unset($existingprizes[$ep->external_identifier]);
}
else { //if it doesnt exist -> add it
echo "&nbsp;".i18n("Adding prize %1",array($prize['identifier'][0]))."<br />";
mysql_query("INSERT INTO award_prizes (
award_awards_id,
cash,
scholarship,
value,
prize,
number,
`order`,
`year`,
external_identifier
) VALUES (
'$awardrecord->id',
'".intval($prize['cash'][0])."',
'".intval($prize['scholarship'][0])."',
'".intval($prize['value'][0])."',
'".mysql_escape_string($prize['prize_en'][0])."',
'".intval($prize['number'][0])."',
'".intval($prize['ord'][0])."',
'$year',
'".mysql_escape_string($prize['identifier'][0])."'
)");
}
}
//if an entry exists thats not in the xml -> delete it
foreach($existingprizes AS $ep) {
echo "&nbsp;".i18n("Removing prize %1",array($ep->external_identifier))."<br />";
mysql_query("DELETE FROM award_prizes WHERE id='$ep->id'");
}
}
//FIXME: update the translations
}
else {
//check if the sponsor exists, if not, add them
$sponsorq=mysql_query("SELECT * FROM award_sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."' AND year='$year'");
if($sponsorr=mysql_fetch_object($sponsorq)) {
$sponsor_id=$sponsorr->id;
}
else
{
mysql_query("INSERT INTO award_sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')");
$sponsor_id=mysql_insert_id();
}
echo i18n("Award does not exists, adding...")."<br />";
mysql_query("INSERT INTO award_awards (
award_types_id,
award_sponsors_id,
name,
criteria,
year,
external_identifier,
external_postback
)
VALUES (
2,
$sponsor_id,
'".mysql_escape_string($award['name_en'][0])."',
'".mysql_escape_string($award['criteria_en'][0])."',
'".$year."',
'".mysql_escape_string($identifier)."',
'".mysql_escape_string($postback)."'
)");
$award_id=mysql_insert_id();
//import the prizes
if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0))
{
$prizes=$award['prizes'][0]['prize'];
$numprizes=count($prizes);
echo i18n("Number of prizes: %1",array($numprizes))."<br />";
foreach($prizes AS $prize) {
mysql_query("INSERT INTO award_prizes (
award_awards_id,
cash,
scholarship,
value,
prize,
number,
`order`,
`year`,
external_identifier
) VALUES (
'$award_id',
'".intval($prize['cash'][0])."',
'".intval($prize['scholarship'][0])."',
'".intval($prize['value'][0])."',
'".mysql_escape_string($prize['prize_en'][0])."',
'".intval($prize['number'][0])."',
'".intval($prize['ord'][0])."',
'$year',
'".mysql_escape_string($prize['identifier'][0])."'
)");
}
}
else
echo error(i18n("No prizes associated with this award"));
//FIXME: import the translations & prize translations
}
}
else
{
echo error(i18n("Award is not for the current fair year... skipping"));
}
}
echo "</i>";
}
else
echo error(i18n("Invalid XML response. Expecting '%1', received '%2'",array("awardresponse",$keys[0])));
}
else
{
echo error("CURL Support Missing");
echo i18n("Your PHP installation does not support CURL. You will need to login to the YSF system as the regional coodinator and upload the XML data manually");
}
echo "<br />";
}
}
else
echo error(i18n("No sources available to check"));
}
else
{
$q=mysql_query("SELECT * FROM award_sources ORDER BY name");
echo "<table class=\"tableview\">";
echo "<tr><th>".i18n("Source Name")."</th>";
echo "<th>".i18n("Source Location URL")."</th>";
echo "<th>".i18n("Check")."</th>";
echo "</tr>";
while($r=mysql_fetch_object($q)) {
echo "<tr>";
echo "<td>$r->name</td>\n";
echo "<td>$r->url</td>";
echo "<td align=\"center\"><a href=\"award_download.php?action=check&check[]=$r->id\">".i18n("check")."</a></td>";
echo "</tr>";
$checkurl.="&check[]=$r->id";
}
echo "</table>\n";
echo "<br />";
echo "<a href=\"award_download.php?action=check$checkurl\">".i18n("Check all sources")."</a>";
}
send_footer();
?>