science-ation/plugins/evaluations/eval_uploader.php

157 lines
6.6 KiB
PHP

<?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
/* This routine works only to upload data from the Apperson Benchmark Scanners.
Requirement:
1 must setup SFIAB per evaluations Help documentation which means
- all categories Ids are single digit
- all division id are 1 or 2 digits (no more than 99 projects in any division
- no division is assigned to more than one category.
- if remove the first digit from all project numbers the results are all unique numbers (remove the Category digit)
- there must be only 1 or 2 schemes
- each scheme must have exactly 6 criteria (numbered and named per the help file)
- each scheme must have exactly 7 level_id with ids = 0,1,2,3,4,5,7 level id 0 = no mark level id 7 = multiple marks
- level values for level ids 1,2,3,4,5 must be = 1,2,3,4,5 respectively
- etc. If all that is set up correclty then the Apperson exported file will be properly imported to eval_projects-entries.
*/
include "sfiab_common.inc.php"; // check SFIAB install and get config etc.
include "eval_common.inc.php"; // get evaluations setup
if (($_FILES["file"]["type"] == "text/plain")
&& ($_FILES["file"]["size"] < 100000)) // simple checks to start
{
if ($_FILES["file"]["error"] > 0)
{
echo "File Selection / upload Error: Return Code: " . $_FILES["file"]["error"] . "<br />";
exit;
}
else
{
echo "Uploading: " . $_FILES["file"]["name"] . "<br />";
//echo "Type: " . $_FILES["file"]["type"] . "<br />";
//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists - OLD COPY WAS DELETED<br />";
unlink("uploads/" . $_FILES["file"]["name"]);
}
//else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
$filename = "uploads/". $_FILES["file"]["name"];
//echo "<br />Stored in: ".$filename."<br />";
//open the file
$fh = fopen($filename, 'r');
//$theData = fread($fh, filesize($filename));
while (($data = fgetcsv($fh, 60, ",")) !== FALSE) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
if ($num != 9){
echo "Number of fields in file not equal to 9. Incorrect File - Try Again!";
fclose($fh);
// delete the file
unlink($filename);
exit;
}
$row++;
// get each item in the csv record there are 9 items
$Apperson_num = $data[0];
$team_num = $data[1];
// IMPORTANT This only works if the setup is done following the rules for using the Apperson Scanner.
// The project numbers are all unique after removing the first digit (the Category ID)
$qstr = "SELECT judges_teams.num, projects.projectnumber, projects.projectcategories_id, ". "eval_projects_entries.criteria_id, eval_projects_entries.level_id, ".
"eval_projects_entries.id, SUBSTR(projects.projectnumber, 2) AS apppProj ".
"FROM eval_projects_entries, projects, judges_teams ".
"WHERE eval_projects_entries.project_id = projects.id AND ".
"eval_projects_entries.judges_teams_id = judges_teams.id AND ".
"(judges_teams.num = '".$team_num."') AND ".
"(SUBSTR(projects.projectnumber, 2) = '".$Apperson_num."') ORDER BY eval_projects_entries.criteria_id";
$q = mysql_query($qstr) ;
if (!$q OR mysql_num_rows($q) == 0){
if(!$q){
echo "q = FALSE!<br />";
}
echo "No Matching record for team number ".$team_num." and Apperson Project number ".$Apperson_num."<br />";
fclose($fh);
// delete the file
unlink($filename);
exit;
}
if (mysql_num_rows($q) == 6){ // 6 criteria for each judgeteam-project is required to upload Apperson data
while($r=mysql_fetch_object($q)) {
$project_num = $r->projectnumber;
$record_id = $r->id;
switch ($r->criteria_id)
{
case 1: //Creative Ability
//echo " update CAS 1= ".$data[2 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[2]."' WHERE id = '".$record_id."'");
break;
case 2: // Scientific Thought
//echo " update STS 2= ".$data[3 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[3]."' WHERE id = '".$record_id."'");
break;
case 3: // Thouroughness
//echo " update THS 3= ".$data[4 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[4]."' WHERE id = '".$record_id."'");
break;
case 4 : // Skill
//echo " update SKS 4= ".$data[5 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[5]."' WHERE id = '".$record_id."'");
break;
case 5: //Clairity
//echo " update CLS 5= ".$data[6 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[6]."' WHERE id = '".$record_id."'");
break;
case 6: //Teamwork
//echo " update TWS 6= ".$data[7 ];
mysql_query("UPDATE eval_projects_entries SET level_id = '".$data[7]."' WHERE id = '".$record_id."'");
break;
}
} // while
}
else {
echo "Evaluations table eval_projects_entries is Not setup for Apperson Data Import.<br />There are not exactly 6 criteria for judgeteam ".$team_num." project number ".$project_num."<br />";
fclose($fh);
// delete the file
unlink($filename);
exit;
}
}
fclose($fh);
// delete the file
unlink($filename);
echo $row." records entered in database<br />";
//echo "<br /><br />File deleted<br>";
}
}
}
else
{
echo "Invalid file";
}
?>