<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
   Copyright (C) 2005 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("../common.inc.php");
 auth_required('admin');
 require("../lpdf.php");
 require("../lcsv.php");

 if(!$_GET['type']) $type="csv";
 else $type=$_GET['type'];

if($type=="pdf")
{
	$rep=new lpdf(	i18n($config['fairname']),
			i18n("Participant Tour Selections"),
			$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"
			);

	$rep->newPage();
	$rep->setFontSize(11);
}
else if($type=="csv")
{
	$rep=new lcsv(i18n("Participant Tour Selections"));
}

$table=array();
$table['header']=array( i18n("Student ID"));

$choicesh=array();
$max = $config['tours_choices_max'];
for($x=0; $x<$max; $x++) {
	$choicesh[] = i18n("Choice ".($x+1).($x==0?"(most preferred)":""));
}
$table['header']=array_merge($table['header'],$choicesh);
$table['header'][] = "Name";
$table['header'][] = "Grade";
$table['header'][] = "Email";

/* Load tours */
$tour = array();
$q = mysql_query("SELECT * FROM tours");
while($r = mysql_fetch_object($q)) {
	$tour[$r->id] =$r->name;
}

$q=mysql_query("SELECT students_id
			FROM 
				tours_choice
			WHERE 
				year='{$config['FAIRYEAR']}'
			ORDER BY tours_choice.rank,tours_choice.tour_id");
			
$students_done = array();

while($r=mysql_fetch_object($q))
{

//	print_r($judge_divs);
//	print_r($judge_subdivs);

	if($students_done[$r->students_id] == true) continue;
	$students_done[$r->students_id] = true;

	/* Make sure the student is complete */
	$qq = mysql_query("SELECT registrations.status 
			FROM 
				students,registrations
			WHERE
				students.id='{$r->students_id}'
				AND students.registrations_id=registrations.id
				AND students.year='{$config['FAIRYEAR']}'
				AND registrations.year='{$config['FAIRYEAR']}'
				");
	$rr = mysql_fetch_object($qq);
	if($rr->status != 'complete') continue;
	
	$qq = mysql_query("SELECT * FROM tours_choice WHERE ".
				 " year='".$config['FAIRYEAR']."' AND ".
				 " students_id='".$r->students_id."' ".
				 " ORDER BY rank ");
	$c = array();
	/* Define an array for tour choices */
	for($x=0;$x<$max;$x++) {
		$c[$x] = '';
	}
	/* Load up to $max tour choices */
	$x = 0;
	while($rr = mysql_fetch_object($qq)) {
		$c[$x] = $tour[$rr->tour_id];
		$x++;
		if($x == $max) break;
	}

	$qq = mysql_query("SELECT * FROM students WHERE ".
				" year='".$config['FAIRYEAR']."' AND ".
				" id='".$r->students_id."' "
				);
	$rr = mysql_fetch_object($qq);
	$name = $rr->firstname." ".$rr->lastname;
	$grade = $rr->grade;
	$em = $rr->email;
			
	$tmp=array(
		$r->students_id,
		);
	$tmp = array_merge($tmp,$c);
	$tmp[] = $name;
	$tmp[] = $grade;
	$tmp[] = $em;
//	print_r($tmp);
	$table['data'][]=$tmp;
}

$rep->addTable($table);
$rep->output();

?>