From a5a431605ae7b56e52c4aabadc21a23e6193c379 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 18 Apr 2010 22:39:55 +0000 Subject: [PATCH] Support Multiple uploads for SFIAB targets --- admin/award_upload.php | 316 ++++++++++++++++++++++++++--------------- 1 file changed, 198 insertions(+), 118 deletions(-) diff --git a/admin/award_upload.php b/admin/award_upload.php index 369ee02..a13210f 100644 --- a/admin/award_upload.php +++ b/admin/award_upload.php @@ -27,7 +27,7 @@ require_once('curl.inc.php'); user_auth_required('committee', 'admin'); //function get_cwsf_award_winners() -function get_winners($awardid, $catmap_str=NULL, $divmap_str=NULL) +function get_winners($awardid, $fairs_id, $catmap_str=NULL, $divmap_str=NULL) { global $config; @@ -65,143 +65,183 @@ function get_winners($awardid, $catmap_str=NULL, $divmap_str=NULL) 'teacheremail'=>'teacheremail'); - - /* Get the award */ - $q=mysql_query("SELECT * FROM award_awards WHERE id='$awardid' AND year='{$config['FAIRYEAR']}'"); - if(mysql_num_rows($q)!=1) { - error_("Can't find award id $awardid"); - return false; + $awards = array(); + if($awardid == -1) { + /* Get all for this fair */ + $q=mysql_query("SELECT * FROM award_awards WHERE award_source_fairs_id='$fairs_id' AND year='{$config['FAIRYEAR']}'"); + if(mysql_num_rows($q) == 0) { + error_("Can't find award id $awardid"); + return false; + } + while($a = mysql_fetch_assoc($q)) { + $awards[] = $a; + } + } else { + /* Get the award */ + $q=mysql_query("SELECT * FROM award_awards WHERE id='$awardid' AND year='{$config['FAIRYEAR']}'"); + if(mysql_num_rows($q)!=1) { + error_("Can't find award id $awardid"); + return false; + } + $award=mysql_fetch_assoc($q); + $awards[] = $award; } - $award=mysql_fetch_assoc($q); /* Get the fair for the div/cat mappings */ $q = mysql_query("SELECT * FROM fairs WHERE id='{$award['award_source_fairs_id']}'"); $fair = mysql_fetch_assoc($q); - $catmap = unserialize(($catmap_str != NULL) ? $catmap_str : $fair['catmap']); - $divmap = unserialize(($divmap_str != NULL) ? $divmap_str : $fair['divmap']); + $catmap = unserialize($fair['catmap']); + $divmap = unserialize($fair['divmap']); - $winners=array( 'prizes' => array(), - 'award_name' => $award['name'], - 'external_identifier' => $award['external_identifier'], - 'external_postback' => $award['external_postback']); - /* Get the prizes */ - $q=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='{$award['id']}'"); - while($prize=mysql_fetch_assoc($q)) { - $pid = $prize['id']; - $wq=mysql_query("SELECT projects.* FROM award_prizes - LEFT JOIN winners ON winners.awards_prizes_id=award_prizes.id - LEFT JOIN projects ON projects.id=winners.projects_id - WHERE - awards_prizes_id='$pid' AND - winners.year='{$config['FAIRYEAR']}'"); - echo mysql_error(); - /* Get all projects assigned to this prize */ - $prizewinners = array(); - while($project=mysql_fetch_assoc($wq)) { + foreach($awards as $award) { + $winners=array( 'award_name' => $award['name'], + 'external_identifier' => $award['external_identifier'], + 'year' => $config['FAIRYEAR'], + 'prizes' => array()); - /* Get the students */ - $sq=mysql_query("SELECT * FROM students WHERE registrations_id='{$project['registrations_id']}' - AND year='{$config['FAIRYEAR']}'"); - $students=array(); - while($s=mysql_fetch_assoc($sq)) { - - /* Get the student's school */ - $schoolq=mysql_query("SELECT * FROM schools WHERE id='{$s['schools_id']}'"); - $schoolr=mysql_fetch_assoc($schoolq); - $school = array("xml_type"=>"school");/* for ysc compatability */ - foreach($school_fields as $k=>$v) - $school[$k] = $schoolr[$v]; - - /* Pack up the student data too */ - $student = array('xml_type'=>'student',/* for ysc compatability */ - 'school' => $school); - foreach($student_fields as $k=>$v) - $student[$k] = $s[$v]; - - $students[] = $student; - - } - /* Turn our load ID into a server-side cat/div id */ - $cat_id = $catmap[$project['projectcategories_id']]; - $div_id = $divmap[$project['projectdivisions_id']]; - - /* Save the project info => students */ - $prizewinners[]=array( 'xml_type' => 'project',/* for ysc compatability */ - 'projectid'=>$project['id'], - 'projectnumber'=>$project['projectnumber'], - 'title'=>$project['title'], - 'abstract'=>$project['summary'], - 'language'=>$project['language'], - 'projectcategories_id'=>$cat_id, - 'projectdivisions_id'=>$div_id, - 'students'=>$students ); + if($fair['type'] != 'sfiab') { + /* YSC Compatability */ + $winners['external_postback'] = $award['external_postback']; } - /* Save the prize info => projects */ - $winners['prizes'][$prize['prize']] = array( - 'xml_type'=>'prize', /* For ysc compatability */ - 'identifier'=>$prize['external_identifier'], /* for ysc compatability */ - 'projects'=>$prizewinners); + + /* Get the prizes */ + $q=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='{$award['id']}'"); + while($prize=mysql_fetch_assoc($q)) { + $pid = $prize['id']; + $wq=mysql_query("SELECT projects.* FROM award_prizes + LEFT JOIN winners ON winners.awards_prizes_id=award_prizes.id + LEFT JOIN projects ON projects.id=winners.projects_id + WHERE + awards_prizes_id='$pid' AND + winners.year='{$config['FAIRYEAR']}'"); + echo mysql_error(); + /* Get all projects assigned to this prize */ + $prizewinners = array(); + while($project=mysql_fetch_assoc($wq)) { + + /* Get the students */ + $sq=mysql_query("SELECT * FROM students WHERE registrations_id='{$project['registrations_id']}' + AND year='{$config['FAIRYEAR']}'"); + $students=array(); + while($s=mysql_fetch_assoc($sq)) { + + /* Get the student's school */ + $schoolq=mysql_query("SELECT * FROM schools WHERE id='{$s['schools_id']}'"); + $schoolr=mysql_fetch_assoc($schoolq); + $school = array("xml_type"=>"school");/* for ysc compatability */ + foreach($school_fields as $k=>$v) + $school[$k] = $schoolr[$v]; + + /* Pack up the student data too */ + $student = array('xml_type'=>'student',/* for ysc compatability */ + 'school' => $school); + foreach($student_fields as $k=>$v) + $student[$k] = $s[$v]; + + $students[] = $student; + + } + /* Turn our load ID into a server-side cat/div id */ + $cat_id = $catmap[$project['projectcategories_id']]; + $div_id = $divmap[$project['projectdivisions_id']]; + + /* Save the project info => students */ + $prizewinners[]=array( 'xml_type' => 'project',/* for ysc compatability */ + 'projectid'=>$project['id'], + 'projectnumber'=>$project['projectnumber'], + 'title'=>$project['title'], + 'abstract'=>$project['summary'], + 'language'=>$project['language'], + 'projectcategories_id'=>$cat_id, + 'projectdivisions_id'=>$div_id, + 'students'=>$students ); + } + /* Save the prize info => projects */ + $winners['prizes'][$prize['prize']] = array( + 'xml_type'=>'prize', /* For ysc compatability */ + 'identifier'=>$prize['external_identifier'], /* for ysc compatability */ + 'projects'=>$prizewinners); + } + $all_winners[] = $winners; } - return $winners; + return $all_winners; } switch($_GET['action']) { case 'award_upload': $award_awards_id = intval($_GET['id']); - $winners = get_winners($award_awards_id); + $fairs_id = intval($_GET['fairs_id']); + $all_winners = get_winners($award_awards_id, $fairs_id); /* Get the fair */ - $q = mysql_query("SELECT award_source_fairs_id FROM award_awards WHERE id='$award_awards_id'"); - $a = mysql_fetch_assoc($q); - $q = mysql_query("SELECT * FROM fairs WHERE id='{$a['award_source_fairs_id']}'"); + $q = mysql_query("SELECT * FROM fairs WHERE id='$fairs_id}'"); $fair = mysql_fetch_assoc($q); echo '
'; - if($winners == false) { - echo notice(i18n('No winners selected for this award')); - } else { - if($fair['type'] == 'ysc') { - $w = array(); - foreach($winners['prizes'] as $prize_name=>$prize) { - $w[] = $prize; + /* Check that we're going to upload something */ + $upload_something = false; + foreach($all_winners as $w) { + foreach($w['prizes'] as $p) { + if(count($p['projects'])) { + $upload_something = true; + break; } - $req=array("awardwinners"=>array( - "username"=>$fair['username'], - "password"=>$fair['password'], - "identifier"=>$winners['external_identifier'], - "prizes"=>$w, - ) - ); - $url = $winners['external_postback']; - } else { - $req = array('award_upload' => $winners); - $req['award_upload']['year'] = $config['FAIRYEAR']; - $url = ''; /* url is ignored for type = sfiab */ - } - echo i18n("Sending %1 winners to %2...", array(''.$winners['award_name'].'', - ''.$fair['name'].'')); - echo '
'; -// echo "
"; print_r($req); echo "
"; - - $data = curl_query($fair, $req, $url); - - if($data['error'] != 0) { - echo error("Server said: ".htmlspecialchars(print_r($data,true))); - } else { - if(is_array($data['notice'])) - echo notice("{$fair['name']} server said:
".join("\n", $data['notice'])."
"); - else if(is_array($data['message'])) - echo notice("{$fair['name']} server said:
".join("\n", $data['message'])."
"); - else if($data['message']) { - echo notice("{$fair['name']} server said:
".$data['message']."
"); - } - else - echo notice("{$fair['name']} server said:
".htmlspecialchars(print_r($data,true))."
"); - echo happy(i18n("Upload completed successfully")); } } + + if($upload_something == false) { + echo notice(i18n('No winners to be uploaded')); + exit; + } + + if($fair['type'] == 'ysc') { + if($award_awards_id == -1) { + echo "Multiple uploads not supported for YSC targets.\n"; + exit; + } + /* Pull the single-award out, get_winners() will never + * return more than one award for YSC targets */ + $winners = array_shift($all_winners); + $w = array(); + foreach($winners['prizes'] as $prize_name=>$prize) { + $w[] = $prize; + } + $req=array("awardwinners"=>array( + "username"=>$fair['username'], + "password"=>$fair['password'], + "identifier"=>$winners['external_identifier'], + "prizes"=>$w, + ) + ); + $url = $winners['external_postback']; + } else { + $req = array(); + $req['awards_upload'] = $all_winners; + $url = ''; /* url is ignored for type = sfiab */ + + } + echo i18n("Sending winners to %1...", array(''.$fair['name'].'')); + echo '
'; +// echo "
"; print_r($req); echo "
"; + + $data = curl_query($fair, $req, $url); + + if($data['error'] != 0) { + echo error("Server said: ".htmlspecialchars(print_r($data,true))); + } else { + if(is_array($data['notice'])) + echo notice("{$fair['name']} server said:
".join("\n", $data['notice'])."
"); + else if(is_array($data['message'])) + echo notice("{$fair['name']} server said:
".join("\n", $data['message'])."
"); + else if($data['message']) + echo notice("{$fair['name']} server said:
".$data['message']."
"); + else + echo notice("{$fair['name']} server said:
".htmlspecialchars(print_r($data,true))."
"); + echo happy(i18n("Upload completed successfully")); + } exit; + case 'catdiv_load': $fairs_id = intval($_GET['id']); $q = mysql_query("SELECT * FROM fairs WHERE id='$fairs_id'"); @@ -277,7 +317,8 @@ case 'catdiv_save': case 'confirm_load': $award_awards_id = intval($_GET['id']); - $winners = get_winners($award_awards_id); + $fairs_id = intval($_GET['fairs_id']); + $winners = get_winners($award_awards_id, $fairs_id); print_r($winners); exit; @@ -340,7 +381,7 @@ function catdiv_load() } function award_upload(id) { - $("#award_upload_status").load("?action=award_upload&id="+id); + $("#award_upload_status").load("?action=award_upload&id="+id+"&fairs_id="+fairs_id); } function catdiv_save() @@ -360,7 +401,7 @@ function catdiv_next() function confirm_load() { - $("#confirm_data").load("?action=confirm_load&id="+award_awards_id); + $("#confirm_data").load("?action=confirm_load&id="+award_awards_id+"&fairs_id="+fairs_id); } function confirm_upload() @@ -465,7 +506,6 @@ $(document).ready(function() { echo ""; echo ''; } -/* " />*/ ?>
@@ -498,6 +538,7 @@ $(document).ready(function() { + +

:

+ + + + + + +\n"; + echo ""; + echo ""; +} +?> +
{$r->name}{$r->AWARD_COUNT}"; + if($r->type == 'sfiab') + echo "id},-1)\" >".i18n("Send All").""; + else + echo "Not available yet, we're working on it!"; + echo "
+
+
+ + + +

: