Support Multiple uploads for SFIAB targets

This commit is contained in:
dave 2010-04-18 22:39:55 +00:00
parent 14df5adc39
commit a5a431605a

View File

@ -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 '<br />';
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('<b>'.$winners['award_name'].'</b>',
'<b>'.$fair['name'].'</b>'));
echo '<br />';
// echo "<pre>"; print_r($req); echo "</pre>";
$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: <pre>".join("\n", $data['notice'])."</pre>");
else if(is_array($data['message']))
echo notice("{$fair['name']} server said: <pre>".join("\n", $data['message'])."</pre>");
else if($data['message']) {
echo notice("{$fair['name']} server said: <pre>".$data['message']."</pre>");
}
else
echo notice("{$fair['name']} server said: <pre>".htmlspecialchars(print_r($data,true))."</pre>");
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('<b>'.$fair['name'].'</b>'));
echo '<br />';
// echo "<pre>"; print_r($req); echo "</pre>";
$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: <pre>".join("\n", $data['notice'])."</pre>");
else if(is_array($data['message']))
echo notice("{$fair['name']} server said: <pre>".join("\n", $data['message'])."</pre>");
else if($data['message'])
echo notice("{$fair['name']} server said: <pre>".$data['message']."</pre>");
else
echo notice("{$fair['name']} server said: <pre>".htmlspecialchars(print_r($data,true))."</pre>");
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("<?=$_SERVER['PHP_SELF']?>?action=award_upload&id="+id);
$("#award_upload_status").load("<?=$_SERVER['PHP_SELF']?>?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("<?=$_SERVER['PHP_SELF']?>?action=confirm_load&id="+award_awards_id);
$("#confirm_data").load("<?=$_SERVER['PHP_SELF']?>?action=confirm_load&id="+award_awards_id+"&fairs_id="+fairs_id);
}
function confirm_upload()
@ -465,7 +506,6 @@ $(document).ready(function() {
echo "<option value=\"-1\">Loading...</option>";
echo '</select></td></tr>';
}
/* <input type="submit" onClick="catdiv_save();return false;" value="<?=i18n("Save")?>" /></td>*/
?>
</table>
<br />
@ -498,6 +538,7 @@ $(document).ready(function() {
</div></div>
<?
@ -508,6 +549,44 @@ if(!function_exists('curl_init')) {
exit;
}
/* Fairs first */
$q = mysql_query("SELECT fairs.id, fairs.name, fairs.type, COUNT(award_awards.id) as AWARD_COUNT FROM fairs
LEFT JOIN award_awards ON award_awards.award_source_fairs_id=fairs.id
WHERE award_awards.award_source_fairs_id IS NOT NULL
AND award_awards.year='{$config['FAIRYEAR']}'
GROUP BY fairs.id
ORDER BY fairs.name ");
echo mysql_error();
?>
<h4><?=i18n('Upload all winners to a source')?>:</h4>
<table class="tableview"><thead>
<tr><th><?=i18n("Source Name")?></th>
<th><?=i18n("Number of Awards")?></th>
<th><?=i18n("Send All")?></th>
</tr></thead>
<?
while($r=mysql_fetch_object($q)) {
echo "<tr><td>{$r->name}</td>\n";
echo "<td>{$r->AWARD_COUNT}</td>";
echo "<td align=\"center\">";
if($r->type == 'sfiab')
echo "<a href=\"#\" onClick=\"popup_upload({$r->id},-1)\" >".i18n("Send All")."</a>";
else
echo "Not available yet, we're working on it!";
echo "</td></tr>";
}
?>
</table>
<br />
<br />
<?
$q = mysql_query("SELECT award_awards.id, award_awards.name AS awardname,
fairs.name as fairname, award_source_fairs_id,
fairs.type as fairtype, award_awards.external_additional_materials
@ -519,6 +598,7 @@ $q = mysql_query("SELECT award_awards.id, award_awards.name AS awardname,
echo mysql_error();
?>
<h4><?=i18n('Upload individual winners to a source')?>:</h4>
<table class="tableview"><thead>
<tr><th><?=i18n("Award Name")?></th>