2011-04-14 21:32:18 +00:00
< ? php
/*
This file is a plug - in to the 'Science Fair In A Box' project
SFIAB Website : http :// www . sfiab . ca
Copyright ( C ) 2011 At Work Software ( dennis @ spanogle . net >
Copyright ( C ) 2011 Dennis Spanogle < dennis @ spanogle . net >
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 .
*/
?>
< ? php
include " sfiab_common.inc.php " ; // check SFIAB install and get config etc.
include " eval_common.inc.php " ; // get evaluations setup
// debug test
$CSVoption = $_GET [ 'option' ];
$Teamworknote = $_GET [ 'teamnote' ];
2011-04-26 22:25:50 +00:00
$filename = " EMPTY FILE Do Not Download Not Implemented.csv " ;
// this section for non-csv files
if ( $CSVoption == 3 || $CSVoption == 4 ){
if ( $CSVoption == 3 ){
// download the ms word preslug doc
$file = " docs/SFIAB_EvaluationsPreSlug.doc " ;
}
if ( $CSVoption == 4 ){
// download the Apperson font
$file = " docs/apperson.ttf " ;
}
if ( file_exists ( $file )) {
header ( 'Content-Description: File Transfer' );
header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
header ( 'Content-Transfer-Encoding: binary' );
header ( 'Expires: 0' );
header ( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header ( 'Pragma: public' );
header ( 'Content-Length: ' . filesize ( $file ));
ob_clean ();
flush ();
readfile ( $file );
}
exit ;
}
// 5 Eval_schemes Table - csv file. Has scheme_id, scheme_name and selection statement - csv file.
if ( $CSVoption == 5 ){
$q = mysql_query ( " SELECT * from eval_schemes ORDER BY scheme_id " );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (You must setup Schemes, Criteria and Levels prior to exporting)<br /> " ;
exit ;
}
$filename = " SFIAB_Evaluations_Schemes.csv " ;
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" scheme_id \" , \" scheme_name \" , \" selection \" " ;
// **********************************************write the header out to the buffer ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
// do the work for the records
while ( $r = mysql_fetch_object ( $q )) {
$recordout = " \" " . $r -> scheme_id . " \" , \" " . $r -> scheme_name . " \" , \" " . $r -> assignto_project_when . " \" " ;
$download = $download . $recordout . " \r \n " ;
}
// proceed to end of code for the actual export
}
//6. Eval_criteria Table with scheme_id, scheme_name and selection statement - csv file.
if ( $CSVoption == 6 ){
$q = mysql_query ( " SELECT * from eval_criteria ORDER BY scheme_id, criteria_id " );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (You must setup Schemes, Criteria and Levels prior to exporting)<br /> " ;
exit ;
}
$filename = " SFIAB_Evaluations_Criteria.csv " ;
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" scheme_id \" , \" criteria_id \" , \" criteria_weight \" , \" criteria_name \" " ;
// **********************************************write the header out to the buffer ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
// do the work for the records
while ( $r = mysql_fetch_object ( $q )) {
$recordout = " \" " . $r -> scheme_id . " \" , \" " . $r -> criteria_id . " \" , \" " . $r -> criteria_weight . " \" , \" " . $r -> criteria_name . " \" " ;
$download = $download . $recordout . " \r \n " ;
}
// proceed to end of code for the actual export
}
//7. Eval_levels Table with scheme_id - csv file.
if ( $CSVoption == 7 ){
$q = mysql_query ( " SELECT * from eval_levels ORDER BY scheme_id, level_id " );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (You must setup Schemes, Criteria and Levels prior to exporting)<br /> " ;
exit ;
}
$filename = " SFIAB_Evaluations_Levels.csv " ;
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" scheme_id \" , \" level_id \" , \" level_value \" , \" level_name \" , \" spec_use \" , \" spec_use_code \" " ;
// **********************************************write the header out to the buffer ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
// do the work for the records
while ( $r = mysql_fetch_object ( $q )) {
$recordout = " \" " . $r -> scheme_id . " \" , \" " . $r -> level_id . " \" , \" " . $r -> level_value . " \" , \" " . $r -> level_name . " \" , \" " . $r -> spec_use . " \" , \" " . $r -> spec_use_code . " \" " ;
$download = $download . $recordout . " \r \n " ;
}
// proceed to end of code for the actual export
}
//8. Eval_levels Table with scheme_id - csv file.
if ( $CSVoption == 8 ){
$q = mysql_query ( " SELECT * from eval_projects ORDER BY project_id " );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (You must Link Evalueations to SFIAB data prior to exporting)<br /> " ;
exit ;
}
$filename = " SFIAB_Projects.csv " ;
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" project_id \" , \" team \" , \" scheme_id \" , \" eval_score \" , \" final_score \" , \" eval_score_status \" , \" overall_status \" , \" last_change \" " ;
// **********************************************write the header out to the buffer ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
// do the work for the records
while ( $r = mysql_fetch_object ( $q )) {
$recordout = " \" " . $r -> project_id . " \" , \" " . $r -> team . " \" , \" " . $r -> scheme_id . " \" , \" " . $r -> eval_score . " \" , \" " . $r -> final_score . " \" , \" " . $r -> eval_score_status . " \" , \" " . $r -> overall_status . " \" , \" " . $r -> last_change . " \" " ;
$download = $download . $recordout . " \r \n " ;
}
// proceed to end of code for the actual export
}
//9. Eval_projects_entries Table - csv file: One record per judgeteam - project - criteria with current level_id (entry)
if ( $CSVoption == 9 ){
$q = mysql_query ( " SELECT * from eval_projects_entries " );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (You must Link Evalueations to SFIAB data prior to exporting)<br /> " ;
exit ;
}
$filename = " SFIAB_Evaluations_ProjectsEntries.csv " ;
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" event_id \" , \" project_id \" , \" judges_teams_id \" , \" judges_timeslots_id \" , \" criteria_id \" , \" level_id \" , \" judge_user_id \" " ;
// **********************************************write the header out to the buffer ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
// do the work for the records
while ( $r = mysql_fetch_object ( $q )) {
$recordout = " \" " . $r -> event_id . " \" , \" " . $r -> project_id . " \" , \" " . $r -> judges_teams_id . " \" , \" " . $r -> judges_timeslots_id . " \" , \" " . $r -> criteria_id . " \" , \" " . $r -> level_id . " \" , \" " . $r -> judge_user_id . " \" " ;
$download = $download . $recordout . " \r \n " ;
}
// proceed to end of code for the actual export
}
2011-04-14 21:32:18 +00:00
2011-04-26 22:25:50 +00:00
if ( $CSVoption == 1 || $CSVoption == 2 ){
// This selects from judges_teams_timeslots_projects_link as top table - gets ONLY projects with assigned teams (I.E. Status = complete when schedule assignements are made
// It gets One record for each project - judges team assignment. I.E. one record for each required evaluation (Apperson Score Form)
$qstr = " SELECT eval_projects.team AS IsTeam, eval_projects.scheme_id, projects.projectnumber, projects.`language`, projects.floornumber, projects.title, " .
2011-04-14 21:32:18 +00:00
" projects.shorttitle, judges_teams.num AS team_num, judges_teams.name AS team_name, projectcategories.category, " .
" projectcategories.category_shortform, projectcategories.mingrade, projectcategories.maxgrade, projectdivisions.division, " .
" projectdivisions.division_shortform, projectdivisions.cwsfdivisionid, eval_projects.eval_score, eval_projects.final_score, " .
" eval_projects.eval_score_status, eval_projects.overall_status, eval_projects.last_change, projects.`year`, judges_timeslots.`date` AS tsdate, " .
" judges_timeslots.starttime AS tsstarttime, judges_timeslots.endtime AS tsendtime, judges_timeslots.name AS tsname, " .
" judges_timeslots.type AS tstype, judges_teams_timeslots_projects_link.judges_timeslots_id, judges_teams_timeslots_projects_link.projects_id, " .
" judges_teams.id, registrations.num, registrations.status, judges_teams_timeslots_projects_link.score " .
" FROM judges_teams, judges_teams_timeslots_projects_link, judges_timeslots, projects, eval_projects, projectcategories, projectdivisions, registrations " .
" WHERE judges_teams.id = judges_teams_timeslots_projects_link.judges_teams_id AND " .
" judges_teams_timeslots_projects_link.judges_timeslots_id = judges_timeslots.id AND " .
" judges_teams_timeslots_projects_link.projects_id = eval_projects.project_id AND projects.id = eval_projects.project_id AND " .
" projects.projectcategories_id = projectcategories.id AND projects.projectdivisions_id = projectdivisions.id AND " .
" projects.registrations_id = registrations.id " .
" ORDER BY projects.projectnumber, team_num " ;
2011-04-26 22:25:50 +00:00
$q = mysql_query ( $qstr );
if ( ! $q OR mysql_num_rows ( $q ) == 0 ){
echo " Error reading tables or no records. (Did you Link SFIAB to Evaluations? Did 'score' get added to the judges_teams_timeslots_projects_link table?)<br /> " ;
exit ;
2011-04-14 21:32:18 +00:00
}
2011-04-26 22:25:50 +00:00
if ( $CSVoption == 1 )
{
// here is what I want in the data. may take several queries to get it. Need one record for each record in eval_projects_entries
$header = " \" ProjectNum \" , \" AppersonNum \" , \" ShortTitle \" , \" Title \" , \" IsTeamProject \" , \" FloorNum \" , \" Language \" , \" TeamProjectNote \" , \" JudgeTeamNum \" , \" JudgeTeamName \" , \" JudgeTeamTime \" , \" JudgeTeamLeader \" , \" Category \" , \" CatShort \" , \" Division \" , \" DivShort \" , \" Year \" , \" AppersonBarCode \" , \" scheme_Id \" , \" score \" " ;
$filename = " SFIAB_Evauations_ScoreFormPreSlug.csv " ;
2011-04-14 21:32:18 +00:00
}
2011-04-26 22:25:50 +00:00
if ( $CSVoption == 2 ){
// Apperson Scanner Student data.// "Student ID","FirstName","LastName"
$header = " \" Student ID \" , \" FirstName \" , \" LastName \" " ;
$filename = " SFIAB_Evaluations_ScannerStudentData.csv " ;
2011-04-14 21:32:18 +00:00
}
2011-04-26 22:25:50 +00:00
// **********************************************write the header out to the file ************************************
$download = $header . " \r \n " ;
$num_records = mysql_num_rows ( $q );
while ( $r = mysql_fetch_object ( $q )) {
// get some sepecial items from the raw data.
// this is for test only at this time. will be used in the Apperson Scanner export and import.
// to create the file necessary to preslug the score forms.
// Barcode = 1 or 2 for team, 4 digits exn, 4 digits team number, 2 digits checksum?
$AppPrjNum = substr ( $r -> projectnumber , 1 );
if ( strlen ( $AppPrjNum ) < 4 )
{
$AppPrjNum = sprintf ( " %04d " , $AppPrjNum ); // "0".$AppPrjNum;
}
$teamtime = $r -> tsdate . " " . $r -> tsstarttime . " - " . $r -> tsendtime ;
$tcode = " 1 " ;
2011-04-14 21:32:18 +00:00
$Teamnote = " " ;
if ( $r -> IsTeam == " yes " ){
2011-04-26 22:25:50 +00:00
$tcode = " 2 " ;
// passed $Teamworknote when page opened
$Teamnote = $Teamworknote ;
2011-04-14 21:32:18 +00:00
}
2011-04-26 22:25:50 +00:00
$fmtTeamNum = sprintf ( " %04d " , $r -> team_num );
$StudentID = $tcode . $AppPrjNum . $fmtTeamNum ;
// calculate the checksum
$checksum = ( int ) substr ( $StudentID , 0 , 1 );
$checksum += ( int ) substr ( $StudentID , 1 , 1 );
$checksum += ( int ) substr ( $StudentID , 2 , 1 );
$checksum += ( int ) substr ( $StudentID , 3 , 1 );
$checksum += ( int ) substr ( $StudentID , 4 , 1 );
$checksum += ( int ) substr ( $StudentID , 5 , 1 );
$checksum += ( int ) substr ( $StudentID , 6 , 1 );
$checksum += ( int ) substr ( $StudentID , 7 , 1 );
$checksum += ( int ) substr ( $StudentID , 8 , 1 );
$checksum = sprintf ( " %02d " , $checksum );
$AppBarCode = " < " . $tcode . $AppPrjNum . $fmtTeamNum . " / " . $checksum . " > " ;
// Get the Team leader name (to be done)
$TeamLeader = " n/a " ;
// ready to create the files
if ( $CSVoption == 1 ){
// $header = "\"ProjectNum\",\"AppersonNum\",\"ShortTitle\",\"Title\",\"IsTeam\",\"FloorNum\",\"Language\",\"TeamNote\",\"TeamNum \",\"TeamName\",\"TeamTime\",\"TeamLeader\",\"Category\",\"CatShort\",\"Division\",\"DivShort\",\"Year\",\"AppersonBarCode\",\"scheme_Id\",\"criteria_id\",\"Level_id\",\"score\"";
$title = str_replace ( " \" " , " ' " , $r -> title );
$recordout = " \" " . $r -> projectnumber . " \" , \" " . $AppPrjNum . " \" , \" " . $r -> shorttitle . " \" , \" " . $title . " \" , \" " . $r -> IsTeam . " \" , \" " . $r -> floornumber . " \" , \" " . $r -> language . " \" , \" " . $Teamnote . " \" , \" " .
$r -> team_num . " \" , \" " . $r -> team_name . " \" , \" " . $teamtime . " \" , \" " . $TeamLeader . " \" , \" " . $r -> category . " \" , \" " . $r -> category_shortform . " \" , \" " . $r -> division . " \" , \" " . $r -> division_shortform . " \" , \" " . $r -> year . " \" , \" " .
$AppBarCode . " \" , \" " . $r -> scheme_id . " \" , \" " . $r -> score . " \" " ;
// ********************************************write the record out to the file************************************************
$download = $download . $recordout . " \r \n " ;
2011-04-14 21:32:18 +00:00
}
2011-04-26 22:25:50 +00:00
if ( $CSVoption == 2 ){ // Apperson Scanner Student data
// "TPPPPJJJJ", "PRJ PPPPP 'TEAM' TITLETruncated?","JUN JJJJ JLASTNAME, JFIRSTNAME"
$Teamnote = " " ;
if ( $r -> IsTeam == " yes " ){
$Teamnote = " (Team Project) " ;
}
$Firstname = " Proj " . $r -> projectnumber . " " . $Teamnote . " " . $r -> title ;
if ( strlen ( $Firstname ) > 100 ){
$Firstname = substr ( $Firstname , 100 ); // limit to 100 max in Apperson Datalink
}
// replace and double quotes in $Firstname with single quotes so CSV import will work.
$Firstname = str_replace ( " \" " , " ' " , $Firstname );
2011-04-14 21:32:18 +00:00
2011-04-26 22:25:50 +00:00
$recordout = " \" " . $StudentID . " \" , \" " . $Firstname . " \" , \" JudgeTeamNum " . $r -> team_num . " \" " ;
// ********************************************write the record out to the file************************************************
$download = $download . $recordout . " \r \n " ;
}
} // while $r=mysql_fetch_object($q)
} // end of if option = 1 or 2
// code below works for all csv files.
2011-04-14 21:32:18 +00:00
ob_start ();
header ( " Content-type: text/x-csv " );
header ( " Content-disposition: inline; filename= " . $filename );
header ( " Content-length: " . strlen ( $download ));
header ( " Pragma: public " );
echo $download ;
//header("Location: index.php");
//ob_get_status()
2011-04-26 22:25:50 +00:00
exit ;
2011-04-14 21:32:18 +00:00
?>