From 0f198346aa44836d25f62e2ea3c1efac7c252c6d Mon Sep 17 00:00:00 2001 From: james Date: Tue, 27 Nov 2007 22:47:06 +0000 Subject: [PATCH] Update fake scripts for 2008 Add external award downloader functionality --- admin/award_download.php | 215 ++++++++++++++++++++++++++++++- admin/xml.inc.php | 79 ++++++++++++ db/db.code.version.txt | 2 +- db/db.update.69.sql | 3 + scripts/assignprojectnumbers.php | 2 +- scripts/populate_fake.php | 10 +- 6 files changed, 299 insertions(+), 12 deletions(-) create mode 100644 db/db.update.69.sql diff --git a/admin/award_download.php b/admin/award_download.php index f1fcf819..344d05b2 100644 --- a/admin/award_download.php +++ b/admin/award_download.php @@ -34,17 +34,222 @@ 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 "
"; + + $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
"; + curl_close ($ch); /// close the curl session + + echo i18n("The Remote Server said:")."
"; + //echo notice($datastream); + $response=xml_parsexml($datastream); + $keys=array_keys($response); + if($keys[0]=="awardresponse") { + echo ""; + $ar=$response['awardresponse'][0]; + $postback=$ar['postback'][0]; + echo i18n("Postback URL: %1",array($postback))."
"; + $numawards=count($ar['awards'][0]['award']); + echo i18n("Number of Awards: %1",array($numawards))."
"; + foreach($ar['awards'][0]['award'] AS $award) { + $identifier=$award['identifier'][0]; + $year=$award['year'][0]; + echo i18n("Award Identifier: %1",array($identifier))."   "; + echo i18n("Award Year: %1",array($year))."
"; + 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")."
"; + 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))."
"; + $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 " ".i18n("Updating prize %1",array($ep->external_identifier))."
"; + 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 " ".i18n("Adding prize %1",array($prize['identifier'][0]))."
"; + + 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 " ".i18n("Removing prize %1",array($ep->external_identifier))."
"; + 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...")."
"; + 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))."
"; + 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 "
"; + } + 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 "
"; - - - //FIXME: do the check! - - } } else diff --git a/admin/xml.inc.php b/admin/xml.inc.php index 8a1ac91a..e2001dbf 100644 --- a/admin/xml.inc.php +++ b/admin/xml.inc.php @@ -71,4 +71,83 @@ function xmlCreateRecurse($d) } } + # Mainfunction to parse the XML defined by URL + function xml_parsexml ($String) { + $Encoding=xml_encoding($String); + $String=xml_deleteelements($String,"?"); + $String=xml_deleteelements($String,"!"); + $Data=xml_readxml($String,$Data,$Encoding); + return($Data); + } + + # Get encoding of xml + function xml_encoding($String) { + if(substr_count($String,"",$Start); + $Content=substr($String,$Start,$End-$Start); + $EncodingStart=strpos($Content,"encoding=\"")+10; + $EncodingEnd=strpos($Content,"\"",$EncodingStart); + $Encoding=substr($Content,$EncodingStart,$EncodingEnd-$EncodingStart); + }else { + $Encoding=""; + } + return $Encoding; + } + + # Delete elements + function xml_deleteelements($String,$Char) { + while(substr_count($String,"<$Char")) { + $Start=strpos($String,"<$Char"); + $End=strpos($String,">",$Start+1)+1; + $String=substr($String,0,$Start).substr($String,$End); + } + return $String; + } + + # Read XML and transform into array + function xml_readxml($String,$Data,$Encoding='') { + while($Node=xml_nextnode($String)) { + $TmpData=""; + $Start=strpos($String,">",strpos($String,"<$Node"))+1; + $End=strpos($String,"",$Start); + $ThisContent=trim(substr($String,$Start,$End-$Start)); + $String=trim(substr($String,$End+strlen($Node)+3)); + if(substr_count($ThisContent,"<")) { + $TmpData=xml_readxml($ThisContent,$TmpData,$Encoding); + $Data[$Node][]=$TmpData; + }else { + if($Encoding=="UTF-8") { $ThisContent=utf8_decode($ThisContent); } + $ThisContent=str_replace(">",">",$ThisContent); + $ThisContent=str_replace("<","<",$ThisContent); + $ThisContent=str_replace(""e;","\"",$ThisContent); + $ThisContent=str_replace("'","'",$ThisContent); + $ThisContent=str_replace("&","&",$ThisContent); + $Data[$Node][]=$ThisContent; + } + } + return $Data; + } + + # Get next node + function xml_nextnode($String) { + if(substr_count($String,"<") != substr_count($String,"/>")) { + $Start=strpos($String,"<")+1; + while(substr($String,$Start,1)=="/") { + if(substr_count($String,"<")) { return ""; } + $Start=strpos($String,"<",$Start)+1; + } + $End=strpos($String,">",$Start); + $Node=substr($String,$Start,$End-$Start); + if($Node[strlen($Node)-1]=="/") { + $String=substr($String,$End+1); + $Node=xml_nextnode($String); + }else { + if(substr_count($Node," ")){ $Node=substr($Node,0,strpos($String," ",$Start)-$Start); } + } + } + return $Node; + } + + ?> diff --git a/db/db.code.version.txt b/db/db.code.version.txt index 38b10c1b..b5489e5e 100644 --- a/db/db.code.version.txt +++ b/db/db.code.version.txt @@ -1 +1 @@ -68 +69 diff --git a/db/db.update.69.sql b/db/db.update.69.sql new file mode 100644 index 00000000..6ed0edfe --- /dev/null +++ b/db/db.update.69.sql @@ -0,0 +1,3 @@ +ALTER TABLE `award_awards` ADD `external_identifier` VARCHAR( 32 ) DEFAULT NULL , + ADD `external_postback` VARCHAR( 128 ) DEFAULT NULL ; +ALTER TABLE `award_prizes` ADD `external_identifier` VARCHAR( 32 ) DEFAULT NULL ; diff --git a/scripts/assignprojectnumbers.php b/scripts/assignprojectnumbers.php index f8af0325..61a274be 100644 --- a/scripts/assignprojectnumbers.php +++ b/scripts/assignprojectnumbers.php @@ -24,7 +24,7 @@ id; diff --git a/scripts/populate_fake.php b/scripts/populate_fake.php index 73b9c5ff..19b98929 100644 --- a/scripts/populate_fake.php +++ b/scripts/populate_fake.php @@ -41,13 +41,13 @@ $firstnames=array("James","Bob","Ali","Erin","Julia","Dave","Steve","Bryan","Jan $lastnames=array("Grant","Browning","Mehdi","Borque","Temple","Culhane","Sargent","Sing","Belanger","Desjardin","Jones","Smith","Brown","Johnson","Williams","White","Thompson","Baker"); -$domains=array("lightbox.org","microsoft.com","yahoo.com","msn.com","gmail.com","reach.net","slicer.ca","cwsf.ca"."sfiab.ca"); +$domains=array("lightbox.org","microsoft.com","yahoo.com","msn.com","gmail.com","reach.net","slicer.ca","cwsf.ca","sfiab.ca"); $nouns=array("age","animal","arm","baby","ball","bat","bear","boat","camp","car","child","circle","city","cotton","design","dog","dress","duck","ear","egg","enemy"); $starters=array("effects of","study of","analysis of"); $joiners=array("on","combined with","broken apart by","burned with","attacked by","left alone with"); -$numschools=mysql_query("SELECT id FROM schools WHERE year='2007'"); +$numschools=mysql_query("SELECT id FROM schools WHERE year='2008'"); while($s=mysql_fetch_object($numschools)) { //1 in 4 chance of actually using this school @@ -65,7 +65,7 @@ for($x=0;$x<$numprojects;$x++) $pd=rand(1,$prob_unpaid); if($pd==1) $status='paymentpending'; else $status='complete'; - $q=mysql_query("INSERT INTO registrations (num,email,start,status,year) VALUES ('$regnum','$email',NOW(),'$status',2007)"); + $q=mysql_query("INSERT INTO registrations (num,email,start,status,year) VALUES ('$regnum','$email',NOW(),'$status',2008)"); if($id=mysql_insert_id()) { @@ -86,7 +86,7 @@ for($x=0;$x<$numprojects;$x++) $firstname=getrand($firstnames); $email=strtolower($firstname)."@".getrand($domains); - mysql_query("INSERT INTO students (registrations_id,firstname,lastname,email,sex,grade,year,schools_id) VALUES ('$id','$firstname','".getrand($lastnames)."','$email','$sex','$grade','2007','$schools_id')"); + mysql_query("INSERT INTO students (registrations_id,firstname,lastname,email,sex,grade,year,schools_id) VALUES ('$id','$firstname','".getrand($lastnames)."','$email','$sex','$grade','2008','$schools_id')"); } @@ -103,7 +103,7 @@ for($x=0;$x<$numprojects;$x++) if($langnum<4) $lang="fr"; else $lang="en"; - mysql_query("INSERT INTO projects (registrations_id,projectcategories_id,projectdivisions_id,title,year,req_electricity,req_table,language) VALUES ('$id','$cat','$div','$title $lang',2007,'$req_e','$req_t','$lang')"); + mysql_query("INSERT INTO projects (registrations_id,projectcategories_id,projectdivisions_id,title,year,req_electricity,req_table,language) VALUES ('$id','$cat','$div','$title $lang',2008,'$req_e','$req_t','$lang')"); } }