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

   Copyright (C) 2011 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");

	//first, lets make sure someone isng tryint to see something that they arent allowed to!
	$q=$pdo->prepare("SELECT (NOW()>='".$config['dates']['postparticipants']."') AS test");
	$q->execute();
	$r=$q->fetch(PDO::FETCH_OBJ);

	$pn=trim($_GET['n']);

	if($r->test) {

		$q=$pdo->prepare("SELECT  
					registrations.id AS reg_id,
					registrations.status,
					projects.title,
					projects.summary,
					projects.projectnumber,
					projects.projectcategories_id,
					projects.projectdivisions_id,
					projectcategories.category,
					projectdivisions.division
					
				FROM
					registrations
					LEFT JOIN projects on projects.registrations_id=registrations.id
					LEFT JOIN projectcategories ON projectcategories.id=projects.projectcategories_id
					LEFT JOIN projectdivisions ON projectdivisions.id=projects.projectdivisions_id
				WHERE
					registrations.year='".$config['FAIRYEAR']."' 
					AND projectcategories.year='".$config['FAIRYEAR']."' 
					AND projectdivisions.year='".$config['FAIRYEAR']."' 
					AND (status='complete' OR status='paymentpending')
					AND projects.projectnumber='$pn'
				LIMIT 1
				");
			echo $pdo->errorInfo();
		$r=$q->fetch(PDO::FETCH_ASSOC);

		$regid=$r['reg_id'];

		$q2=$pdo->prepare("SELECT firstname,lastname,webfirst,weblast,schools.school FROM students JOIN schools ON students.schools_id=schools.id WHERE registrations_id='$regid' ORDER BY lastname");
		$q2->execute();
		$students="";
		while($stud=$q2->fetch(PDO::FETCH_OBJ)) {

			if($stud->webfirst=="yes")
				$students.="$stud->firstname ";
			if($stud->weblast=="yes")
				$students.="$stud->lastname ";
			if($stud->webfirst=="yes" || $stud->weblast=="yes")
				$students.=", ";

			//we just use the last school, it should match
			$school=$stud->school;
		}
		if(strlen($students))
			$students=substr($students,0,-2);
		
		$ret=array();
		foreach($r AS $k=>$v) {
			$ret[$k]=iconv("ISO-8859-1","UTF-8//TRANSLIT",trim($v));
		}
		$ret['students']=iconv("ISO-8859-1","UTF-8//TRANSLIT",trim($students));
		$ret['school']=iconv("ISO-8859-1","UTF-8//TRANSLIT",trim($school));
		$ret['photo']="";
	}
	//simulate slow loading
//	usleep(2000000);
	echo json_encode($ret);
?>