<?
 require("common.inc.php");
 
 //authenticate based on email address and registration number from the SESSION
 if(!$_SESSION['email'])
 {
 	header("Location: register_participants.php");
	exit;
 }
 if(! ($_SESSION['registration_number'] && $_SESSION['registration_id']))
 {
 	header("Location: register_participants.php");
	exit;
 }

 $q=mysql_query("SELECT registrations.id AS regid, students.id AS studentid, students.name FROM registrations,students ".
 	"WHERE students.email='".$_SESSION['email']."' ".
	"AND registrations.num='".$_SESSION['registration_number']."' ". 
	"AND registrations.id='".$_SESSION['registration_id']."' ".
	"AND students.registrations_id=registrations.id ".
	"AND registrations.year=".$config['FAIRYEAR']." ".
	"AND students.year=".$config['FAIRYEAR']);
echo mysql_error();

 if(mysql_num_rows($q)==0)
 {
 	header("Location: register_participants.php");
	exit;
 
 }
 $r=mysql_fetch_object($q);

 send_header("Participant Registration - Student Information");
 echo "<a href=\"register_participants_main.php\">&lt;&lt; ".i18n("Back to Participant Registration Summary")."</a><br />";
 echo "<br />";

 $q=mysql_query("SELECT * FROM students WHERE registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");

 if(mysql_num_rows($q)==0)
 {
 	//uhh oh, we didnt find any, this isnt possible! lets insert one using the logged in persons email address
	//although... this can never really happen, since the above queries only allow the page to view if the student
	//is found in the students table... soo... well, lets leave it here as a fallback anyways, just incase
	mysql_query("INSERT INTO students (registrations_id,email,year) VALUES ('".$_SESSION['registration_id']."','".mysql_escape_string($_SESSION['email'])."','".$config['FAIRYEAR']."')");
	//if we just inserted it, then we will obviously find 1
	$numfound=1;
 }
 else
 {
 	$numfound=mysql_num_rows($q);
 }

 echo "<form name=\"numstudentsform\" method=\"get\" action=\"register_participants_students.php\">";
 echo i18n("Number of students that worked on the project: ");
 echo "<select name=\"numstudents\" onchange=\"document.forms.numstudentsform.submit()\">\n";
 for($x=$config['minstudentsperproject'];$x<=$config['maxstudentsperproject'];$x++)
 {
 	if($_GET['numstudents']==$x) $selected="selected=\"selected\""; else $selected="";

	echo "<option $selected value=\"$x\">$x</option>\n";
 }
 echo "</select>";
 echo "</form>";

 if($_GET['numstudents'])
 	$numtoshow=$_GET['numstudents'];
 else
 	$numtoshow=$numfound;

 for($x=1;$x<=$numtoshow;$x++)
 {
 	$studentinfo=mysql_fetch_object($q);
	echo "<h3>".i18n("Student %1 Details",array($x))."</h3>";
	echo "<table>";
	echo "<tr><td>".i18n("Name").": </td><td><input type=\"text\" name=\"name$x\" value=\"$studentinfo->name\"></td></tr>";
	echo "</table>";
	echo "<br />";
	echo "<br />";
 }

 send_footer();
?>