2007-11-23 22:07:55 +00:00
< ?
/*
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 .
*/
?>
< ?
2009-09-09 00:26:12 +00:00
require_once ( '../common.inc.php' );
require_once ( '../user.inc.php' );
2007-11-23 22:07:55 +00:00
user_auth_required ( 'committee' , 'admin' );
2009-09-09 00:26:12 +00:00
require_once ( 'curl.inc.php' );
2010-04-22 05:09:50 +00:00
require_once ( 'awards.inc.php' );
2007-11-23 22:07:55 +00:00
2010-02-22 20:26:23 +00:00
2009-09-25 07:11:43 +00:00
switch ( $_GET [ 'action' ]) {
case 'check' :
$fairs_id = intval ( $_GET [ 'fairs_id' ]);
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " SELECT * FROM fairs WHERE id=' $fairs_id ' " );
$q -> execute ();
$fair = $q -> fetch ( PDO :: FETCH_ASSOC );
2009-09-09 00:26:12 +00:00
if ( ! ( $fair [ 'username' ] && $fair [ 'password' ])) {
echo error ( i18n ( " Username and Password are not set for source '%1'. Please set them in the SFIAB Configuration/External Award Sources editor first " , array ( $r -> name )));
return ;
}
2007-11-28 18:46:32 +00:00
2009-09-09 00:26:12 +00:00
echo i18n ( " Checking %1 for awards... " , array ( $fair [ 'name' ]));
echo " <br /> " ;
2009-09-19 06:13:22 +00:00
if ( $fair [ 'type' ] == 'ysc' ) {
2009-09-09 00:26:12 +00:00
$req = array ( " awardrequest " => array (
2009-09-21 07:18:39 +00:00
" username " => $fair [ 'username' ],
" password " => $fair [ 'password' ],
2009-09-09 00:26:12 +00:00
" year " => $config [ 'FAIRYEAR' ],
)
);
} else {
$req [ 'getawards' ] = array ( 'year' => $config [ 'FAIRYEAR' ]);
}
2007-11-23 22:07:55 +00:00
2009-09-09 00:26:12 +00:00
$data = curl_query ( $fair , $req );
2007-11-29 18:38:06 +00:00
2009-09-09 00:26:12 +00:00
if ( $data [ 'error' ] != 0 ) {
echo error ( " Server said: { $data [ 'message' ] } <br /> " );
send_footer ();
exit ;
}
echo notice ( i18n ( 'Server said: Success' ));
2009-02-11 22:41:18 +00:00
// echo "sending [".nl2br(htmlspecialchars($xmldata))."]";
2007-11-27 22:55:45 +00:00
2009-09-09 00:26:12 +00:00
$keys = array_keys ( $data );
if ( ! array_key_exists ( 'awards' , $data )) {
echo error ( i18n ( " Invalid XML response. Expecting '%1' in '%2' " , array ( " awards " , join ( ',' , array_keys ( $data )))));
2010-01-12 06:43:22 +00:00
// echo "response=".print_r($data);
2009-09-09 00:26:12 +00:00
return ;
}
//get a list of all the existing awards for this external source
2024-12-07 01:54:02 +00:00
$aq = $pdo -> prepare ( " SELECT * FROM award_awards WHERE award_source_fairs_id=' $fairs_id ' AND year=' { $config [ 'FAIRYEAR' ] } ' " );
$aq -> execute ();
2009-09-09 00:26:12 +00:00
$existingawards = array ();
2024-12-07 01:54:02 +00:00
while ( $ar = $aq -> fetch ( PDO :: FETCH_OBJ )) {
2009-09-09 00:26:12 +00:00
$existingawards [ $ar -> id ] = true ;
}
echo " <i> " ;
$awards = $data [ 'awards' ];
$postback = $data [ 'postback' ];
echo i18n ( " Postback URL: %1 " , array ( $postback )) . " <br /> " ;
$numawards = is_array ( $awards ) ? count ( $awards ) : 0 ;
echo i18n ( " Number of Awards: %1 " , array ( $numawards )) . " <br /> " ;
if ( $numawards == 0 ) {
echo i18n ( 'No awards to process' ) . '</i> <br />' ;
return ;
}
2010-02-22 20:26:23 +00:00
$divs = projectdivisions_load ();
$cats = projectcategories_load ();
2009-09-09 00:26:12 +00:00
foreach ( $awards as $award ) {
$identifier = $award [ 'identifier' ];
$year = $award [ 'year' ];
echo i18n ( " Award Identifier: %1 " , array ( $identifier )) . " " ;
echo i18n ( " Award Year: %1 " , array ( $year )) . " <br /> " ;
echo i18n ( " Award Name: %1 " , array ( $award [ 'name_en' ])) . " <br /> " ;
if ( $year != $config [ 'FAIRYEAR' ]) {
echo error ( i18n ( " Award is not for the current fair year... skipping " ));
echo '<br />' ;
continue ;
}
2024-12-07 01:54:02 +00:00
$tq = $pdo -> prepare ( " SELECT * FROM award_awards WHERE
2009-09-09 00:26:12 +00:00
external_identifier = '$identifier' AND
2009-09-25 07:11:43 +00:00
award_source_fairs_id = '$fairs_id' AND
2009-09-09 00:26:12 +00:00
year = '$year' " );
2024-12-07 01:54:02 +00:00
$tq -> execute ();
if ( $tq -> rowCount () == 0 ) {
2009-09-09 00:26:12 +00:00
/* Award doesn't exist, create it, then update it with the common code below */
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " INSERT INTO award_awards (award_types_id,
2009-09-09 00:26:12 +00:00
year , external_identifier ,
award_source_fairs_id )
VALUES ( 2 , '{$year}' ,
2024-12-07 01:54:02 +00:00
'".$identifier."' ,
2009-09-25 07:11:43 +00:00
'$fairs_id' ) " );
2024-12-07 01:54:02 +00:00
$q -> execute ();
$award_id = $q -> insertLastId ();
2010-02-22 20:26:23 +00:00
/* By default make all divs/cats eligible */
foreach ( $divs as $id => $d )
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " INSERT INTO award_awards_projectdivisions(award_awards_id,projectdivisions_id,year) VALUES (' $award_id ',' $id ',' { $config [ 'FAIRYEAR' ] } ') " );
$q -> execute ();
2010-02-22 20:26:23 +00:00
foreach ( $cats as $id => $c )
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " INSERT INTO award_awards_projectcategories(award_awards_id,projectcategories_id,year) VALUES (' $award_id ',' $id ',' { $config [ 'FAIRYEAR' ] } ') " );
$q -> execute ();
2009-09-09 00:26:12 +00:00
} else {
echo i18n ( " Award already exists, updating info " ) . " <br /> " ;
2024-12-07 01:54:02 +00:00
$awardrecord = Tq -> fetch ( PDO :: FETCH_OBJ );
2009-09-09 00:26:12 +00:00
$award_id = $awardrecord -> id ;
}
//remove it from the existingawards list
unset ( $existingawards [ $award_id ]);
//check if the sponsor exists, if not, add them
2024-12-07 01:54:02 +00:00
$sponsor_str = $award [ 'sponsor' ];
$sponsorq = $pdo -> prepare ( " SELECT * FROM sponsors WHERE organization=' $sponsor_str ' " );
$sponsorq -> execute ();
if ( $sponsorr = $sponsorq -> fetch ( PDO :: FETHC_OBJ )) {
2009-09-09 00:26:12 +00:00
$sponsor_id = $sponsorr -> id ;
} else {
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " INSERT INTO sponsors (organization,year,notes)
VALUES ( '$sponsor_str' , '$year' , '"."Imported from external source: $r->name"."' ) " );
$q -> execute ();
echo $q -> errroInfo ();
$sponsor_id = $pdo -> lastInsertId ();
2009-09-09 00:26:12 +00:00
}
2009-09-25 07:11:43 +00:00
2010-04-03 16:46:43 +00:00
$self_nominate = ( $award [ 'self_nominate' ] == 'yes' ) ? 'yes' : 'no' ;
$schedule_judges = ( $award [ 'schedule_judges' ] == 'yes' ) ? 'yes' : 'no' ;
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " UPDATE award_awards SET
2009-09-09 00:26:12 +00:00
sponsors_id = '$sponsor_id' ,
2024-12-07 01:54:02 +00:00
name = '".$award[' name_en ']."' ,
criteria = '".$award[' criteria_en ']."' ,
external_postback = '".$postback."' ,
2009-09-25 07:11:43 +00:00
external_register_winners = '".(($award[' external_register_winners ']==1)?1:0)."' ,
2010-04-03 16:46:43 +00:00
external_additional_materials = '".(($award[' external_additional_materials ']==1)?1:0)."' ,
self_nominate = '$self_nominate' ,
schedule_judges = '$schedule_judges'
2009-09-09 00:26:12 +00:00
WHERE
id = '$award_id'
2024-12-07 01:54:02 +00:00
AND external_identifier = '".$identifier."'
2009-09-09 00:26:12 +00:00
AND year = '$year'
" );
2024-12-07 01:54:02 +00:00
$q -> execute ();
echo $q -> errorInfo ();
2009-09-09 00:26:12 +00:00
//update the prizes
$prizes = $award [ 'prizes' ];
2009-09-14 05:33:49 +00:00
if ( ! is_array ( $prizes )) {
continue ;
}
2007-11-28 18:46:32 +00:00
2009-09-14 05:33:49 +00:00
echo i18n ( " Number of prizes: %1 " , array ( count ( $prizes ))) . " <br /> " ;
/* Get existing prizes */
2024-12-07 01:54:02 +00:00
$pq = $pdo -> prepare ( " SELECT * FROM award_prizes WHERE award_awards_id=' $award_id ' " );
$pq -> execute ();
2009-09-14 05:33:49 +00:00
$existingprizes = array ();
2024-12-07 01:54:02 +00:00
while ( $pr = $pq -> fetch ( PDO :: FETCH_ASSOC ))
2009-09-14 05:33:49 +00:00
$existingprizes [ $pr [ 'prize' ]] = $pr ;
/* Iterate over the downloaded pizes */
foreach ( $prizes AS $prize ) {
//if it doesn't exist, add it
if ( ! array_key_exists ( $prize [ 'prize_en' ], $existingprizes )) {
/* Add a base entry , then update it below , yes it ' s two sql queries ,
* but it ' s much shorter code , and means changing things in only
* one spot */
echo " " . i18n ( " Adding prize %1 " , array ( $prize [ 'prize_en' ])) . " <br /> " ;
2024-12-07 01:54:02 +00:00
$p = stripslashes ( $prize [ 'prize_en' ]);
$q = $pdo -> prepare ( " INSERT INTO award_prizes (award_awards_id,prize,year,external_identifier)
2009-09-14 05:33:49 +00:00
VALUES ( '$award_id' , '$p' , '$year' , '$p' ) " );
2024-12-07 01:54:02 +00:00
$q -> execute ();
$prize_id = $pdo -> insertLastId ();
2009-09-14 05:33:49 +00:00
} else {
$ep = $existingprizes [ $prize [ 'prize_en' ]];
echo " " . i18n ( " Updating prize %1 " , array ( $ep [ 'prize' ])) . " <br /> " ;
$prize_id = $ep [ 'id' ];
//remove it from the list
unset ( $existingprizes [ $ep [ 'prize' ]]);
2007-11-27 22:47:06 +00:00
}
2009-09-14 05:33:49 +00:00
2010-03-30 16:47:30 +00:00
if ( ! array_key_exists ( 'identifier' , $prize )) $prize [ 'identifier' ] = $prize [ 'prize_en' ];
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " UPDATE award_prizes SET
2009-09-14 05:33:49 +00:00
cash = '".intval($prize[' cash '])."' ,
scholarship = '".intval($prize[' scholarship '])."' ,
value = '".intval($prize[' value '])."' ,
2024-12-07 01:54:02 +00:00
prize = '".$prize[' prize_en ']."' ,
2009-09-14 05:33:49 +00:00
number = '".intval($prize[' number '])."' ,
2009-11-02 05:45:08 +00:00
`order` = '".intval($prize[' ord '])."' ,
2024-12-07 01:54:02 +00:00
external_identifier = '".stripslashes($prize[' identifier '])."' ,
2009-11-02 05:45:08 +00:00
trophystudentkeeper = '".intval($prize[' trophystudentkeeper '])."' ,
trophystudentreturn = '".intval($prize[' trophystudentreturn '])."' ,
trophyschoolkeeper = '".intval($prize[' trophyschoolkeeper '])."' ,
trophyschoolreturn = '".intval($prize[' trophyschoolreturn '])."'
2009-09-14 05:33:49 +00:00
WHERE
id = '$prize_id' " );
2024-12-07 01:54:02 +00:00
$q -> execute ();
2009-09-14 05:33:49 +00:00
2024-12-07 01:54:02 +00:00
echo $pdo -> errorInfo ();
2009-09-09 00:26:12 +00:00
//FIXME: update the translations
2007-11-23 22:07:55 +00:00
}
2009-09-14 05:33:49 +00:00
/* Delete local entries that weren't downloaded */
foreach ( $existingprizes AS $ep ) {
echo " " . i18n ( " Removing prize %1 " , array ( $ep [ 'prize' ])) . " <br /> " ;
2010-04-22 05:09:50 +00:00
award_prize_delete ( $ep [ 'id' ]);
2009-09-14 05:33:49 +00:00
}
2007-11-23 22:07:55 +00:00
}
2009-09-09 00:26:12 +00:00
echo " <br /> " ;
2009-09-14 05:33:49 +00:00
//remove any awards that are left in the $existingawards array, they must have been removed from the source
2010-04-22 04:39:27 +00:00
foreach ( $existingawards AS $aid => $val ) {
2009-09-09 00:26:12 +00:00
echo i18n ( " Removing award id %1 that was removed from external source " , array ( $aid )) . " <br /> " ;
2010-04-22 05:09:50 +00:00
award_delete ( $aid );
2009-09-09 00:26:12 +00:00
}
echo " </i> " ;
2009-09-25 07:11:43 +00:00
exit ;
}
2007-11-28 18:46:32 +00:00
2009-09-25 07:11:43 +00:00
send_header ( " Download Awards " ,
array ( 'Committee Main' => 'committee_main.php' ,
'Administration' => 'admin/index.php' ,
'Awards Main' => 'admin/awards.php' ));
2009-09-09 00:26:12 +00:00
2009-09-25 07:11:43 +00:00
?>
< script type = " text/javascript " >
function award_download ( id )
{
if ( id == - 1 ) return false ;
$ ( " #award_download_status " ) . load ( " <?= $_SERVER['PHP_SELF'] ?>?action=check&fairs_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 " ));
$links = false ;
} else {
$links = true ;
}
2007-11-28 18:46:32 +00:00
2009-09-25 07:11:43 +00:00
?>
2010-03-31 18:44:42 +00:00
< table class = " tableview " >< thead >
2009-09-25 07:11:43 +00:00
< tr >< th >< ? = i18n ( " Source Name " ) ?> </th>
< th >< ? = i18n ( " Source Location URL " ) ?> </th>
< th >< ? = i18n ( " Check " ) ?> </th>
2010-03-31 18:44:42 +00:00
</ tr ></ thead >
2009-09-25 07:11:43 +00:00
< ?
2007-11-23 22:07:55 +00:00
2024-12-07 01:54:02 +00:00
$q = $pdo -> prepare ( " SELECT * FROM fairs WHERE enable_awards='yes' ORDER BY name " );
$q -> execute ();
while ( $r = $q -> fetch ( PDO :: FETCH_OBJ )) {
2009-09-25 07:11:43 +00:00
echo " <tr> " ;
echo " <td> { $r -> name } </td> \n " ;
echo " <td> { $r -> url } </td> " ;
echo " <td align= \" center \" > " ;
if ( $links )
echo " <a href= \" # \" onclick= \" award_download( { $r -> id } ) \" > " . i18n ( " check " ) . " </a> " ;
else
echo " n/a " ;
echo " </td> " ;
echo " </tr> " ;
// $checkurl.="&check[]={$r->id}";
2007-11-23 22:07:55 +00:00
}
2009-09-25 07:11:43 +00:00
/*
if ( $links )
echo " <a href= \" award_download.php?action=check $checkurl\ " > " .i18n( " Check all sources " ). " </ a > " ;
*/
?>
</ table >
< br />
< div id = " award_download_status " ></ div >
< ?
2007-11-23 22:07:55 +00:00
send_footer ();
?>