science-ation/admin/award_upload.php
2009-09-21 07:48:54 +00:00

245 lines
7.3 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_once('../common.inc.php');
require_once('../user.inc.php');
require_once('../projects.inc.php');
require_once('curl.inc.php');
user_auth_required('committee', 'admin');
//function get_cwsf_award_winners()
function get_winners($awardid)
{
global $config;
/* Mappings of the name we want => to the column name returned in MYSQL */
$school_fields = array( 'schoolname'=>'school',
'schoollang'=>'schoollang',
'schoollevel'=>'schoollevel',
'board'=>'board',
'district'=>'district',
'phone'=>'phone',
'fax'=>'fax',
'address'=>'address',
'city'=>'city',
'province_code'=>'province_code',
'postalcode'=>'postalcode',
'principal'=>'principal',
'schoolemail'=>'schoolemail',
'sciencehead'=>'sciencehead',
'scienceheademail'=>'scienceheademail',
'scienceheadphone'=>'scienceheadphone');
$student_fields = array('firstname'=>'firstname',
'lastname'=>'lastname',
'email'=>'email',
'gender'=>'sex',
'grade'=>'grade',
'language'=>'lang',
'birthdate'=>'dateofbirth',
'address'=>'address',
'city'=>'city',
'province'=>'province',
'postalcode'=>'postalcode',
'phone'=>'phone',
'teachername'=>'teachername',
'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;
}
$award=mysql_fetch_assoc($q);
$winners=array( 'prizes' => array(),
'award_name' => $award['name'],
'external_identifier' => $award['external_identifier'],
'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)) {
/* 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;
}
/* 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'],
'students'=>$students );
}
/* Save the prize info => projects */
$winners['prizes'][$prize['prize']] = array(
'xml_type'=>'prize', /* For ysc compatability */
'identifier'=>$prize['prize'], /* for ysc compatability */
'projects'=>$prizewinners);
}
return $winners;
}
switch($_GET['action']) {
case 'award_upload':
$award_awards_id = intval($_GET['id']);
$winners = get_winners($award_awards_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']}'");
$fair = mysql_fetch_assoc($q);
echo '<br />';
if($winners == false) {
echo notice(i18n('No winners selected for this award'));
} else {
if($fair['type'] == 'ysc') {
$req=array("awardwinners"=>array(
"username"=>$fair['username'],
"password"=>$fair['password'],
"identifier"=>$winners['external_identifier'],
"prizes"=>$winners,
)
);
$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 />';
$data = curl_query($fair, $req, $url);
if($data['error'] != 0) {
echo error("Server said: $data");
} else {
echo notice("{$fair['name']} server said: <pre>".join("\n", $data['notice'])."</pre>");
echo happy(i18n("Upload completed successfully"));
}
echo "<pre>";
print_r($winners);
echo "</pre>";
}
exit;
}
send_header("Award Upload",
array('Committee Main' => 'committee_main.php',
'Administration' => 'admin/index.php',
'Awards Main' => 'admin/awards.php')
);
echo "<br />";
?>
<script type="text/javascript">
function award_upload(id)
{
$("#award_upload_status").load("<?=$_SERVER['PHP_SELF']?>?action=award_upload&id="+id);
}
</script>
<?
if(!function_exists('curl_init')) {
echo error(i18n("CURL Support Missing"));
echo notice(i18n("Your PHP installation does not support CURL. You will need to have CURL support added by your system administrator before being able to access external award sources"));
send_footer();
exit;
}
$q = mysql_query("SELECT award_awards.id, award_awards.name AS awardname,
fairs.name as fairname, award_sourcE_fairs_id FROM award_awards
LEFT JOIN fairs ON fairs.id=award_awards.award_source_fairs_id
WHERE award_awards.award_source_fairs_id IS NOT NULL
AND award_awards.year='{$config['FAIRYEAR']}'
ORDER BY fairs.name, award_awards.name");
echo mysql_error();
?>
<table class="tableview">
<tr><th><?=i18n("Award Name")?></th>
<th><?=i18n("Source Name")?></th>
<th><?=i18n("Send")?></th>
</tr>
<?
while($r=mysql_fetch_object($q)) {
echo "<tr><td>{$r->awardname}</td>\n";
echo "<td>{$r->fairname}</td>";
echo "<td align=\"center\">";
echo "<a href=\"#\" onClick=\"award_upload({$r->id})\" >".i18n("send")."</a>";
echo "</td></tr>";
}
?>
</table>
<br />
<a href="award_upload.php?action=send<?=$sendurl?>"><?=i18n("Send all awards")?></a>
<div id="award_upload_status"></div>
<?
send_footer();
?>