<?
/* 
   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("../register_participants.inc.php");

 send_header("Participant Registration - List and Statistics");
 echo "<a href=\"index.php\">&lt;&lt; ".i18n("Back to Administration")."</a>";
 echo " &nbsp; ";
 echo "<a href=\"registration.php\">&lt;&lt; ".i18n("Back to Registration")."</a>";
 echo "<br />";
 echo "<br />";
 echo i18n("Choose Status").":";
 echo "<form name=\"statuschangerform\" method=\"get\" action=\"registration_list.php\">";
 echo "<select name=\"showstatus\" onchange=\"document.forms.statuschangerform.submit()\">";

 if($_GET['showstatus']=="") $sel="selected=\"selected\""; else $sel="";
 echo "<option $sel value=\"\">".i18n("Any Status")."</option>\n";
 if($_GET['showstatus']=="complete") $sel="selected=\"selected\""; else $sel="";
 echo "<option $sel value=\"complete\">".i18n("Complete")."</option>\n";

 //if there is no reg fee, then we dont need to show this status, because nobody will ever be in this status
 if($config['regfee']>0)
 {
	 if($_GET['showstatus']=="paymentpending") $sel="selected=\"selected\""; else $sel="";
	 echo "<option $sel value=\"paymentpending\">".i18n("Payment Pending")."</option>\n";
 }
 if($_GET['showstatus']=="open") $sel="selected=\"selected\""; else $sel="";
 echo "<option $sel value=\"open\">".i18n("Open")."</option>\n";
 if($_GET['showstatus']=="new") $sel="selected=\"selected\""; else $sel="";
 echo "<option $sel value=\"new\">".i18n("New")."</option>\n";

 echo "</select>";
 echo "</form>";

if($_GET['showstatus']) $wherestatus="AND status='".$_GET['showstatus']."' ";
else $wherestatus="";
	switch($_GET['sort'])
	{
		case 'status': 	$ORDERBY="registrations.status DESC, projects.title"; break;
		case 'num': 	$ORDERBY="registrations.num"; break;
		case 'title': 	$ORDERBY="projects.title, registrations.status DESC"; break;
		case 'cat': 	$ORDERBY="projects.projectcategories_id, projects.title"; break;
		case 'div': 	$ORDERBY="projects.projectdivisions_id, projects.title"; break;
		default:	$ORDERBY="registrations.status DESC, projects.title"; break;

	}

	$q=mysql_query("SELECT  registrations.id AS reg_id,
				registrations.num AS reg_num,
				registrations.status,
				projects.title,
				projects.projectcategories_id,
				projects.projectdivisions_id
			FROM
				registrations
				left outer join projects on projects.registrations_id=registrations.id
			WHERE
				1
				AND registrations.year='".$config['FAIRYEAR']."' 
				$wherestatus
			ORDER BY
				$ORDERBY
			");
		echo mysql_error();
	
	echo "<table class=\"summarytable\">";
	echo "<tr>";
	if($_GET['showstatus']) $stat="&showstatus=".$_GET['showstatus'];
	echo  "<th><a href=\"registration_list.php?sort=status$stat\">".i18n("Status")."</a></th>";
	echo  "<th><a href=\"registration_list.php?sort=num$stat\">".i18n("Reg Num")."</a></th>";
	echo  "<th><a href=\"registration_list.php?sort=title$stat\">".i18n("Project Title")."</a></th>";
	echo  "<th><a href=\"registration_list.php?sort=cat$stat\">".i18n("Age Category")."</a></th>";
	echo  "<th><a href=\"registration_list.php?sort=div$stat\">".i18n("Division")."</a></th>";
	echo  "<th>".i18n("School(s)")."</th>";
	echo  "<th>".i18n("Student(s)")."</th>";
	echo "</tr>";
	$stats_totalprojects=0;
	$stats_totalstudents=0;
	$stats_divisions=array();
	$stats_categories=array();

	while($r=mysql_fetch_object($q))
	{
		$stats_totalprojects++;
		$stats_divisions[$r->projectdivisions_id]++;
		$stats_categories[$r->projectcategories_id]++;

		switch($r->status)
		{
			case "new": $status_text="New"; break;
			case "open": $status_text="Open"; break;
			case "paymentpending": $status_text="Payment Pending"; break;
			case "complete": $status_text="Complete"; break;
		}
		$status_text=i18n($status_text);

		echo "<tr>";
		echo "<td>$status_text</td>";
		echo "<td>$r->reg_num</td>";
		echo "<td>$r->title</td>";


		//now get thh category and division
		$catq=mysql_query("SELECT category FROM projectcategories WHERE year='".$config['FAIRYEAR']."' AND id='".$r->projectcategories_id."'");
		$catr=mysql_fetch_object($catq);
		$divq=mysql_query("SELECT division FROM projectdivisions WHERE year='".$config['FAIRYEAR']."' AND id='".$r->projectdivisions_id."'");
		$divr=mysql_fetch_object($divq);


		echo "<td>".i18n("$catr->category")."</td>";

		echo "<td>".i18n("$divr->division")."</td>";

		$sq=mysql_query("SELECT students.firstname,
					students.lastname,
					schools.school
				FROM
					students,schools
				WHERE
					students.registrations_id='$r->reg_id'
					AND
					students.schools_id=schools.id
				");
				echo mysql_error();

		$studnum=1;
		$schools="";
		$students="";
		while($studentinfo=mysql_fetch_object($sq))
		{
			$students.="$studentinfo->firstname $studentinfo->lastname <br />";
			$schools.="$studentinfo->school <br />";
			$stats_totalstudents++;
		}
		echo "<td>$schools</td>";
		echo "<td>$students</td>";

		echo "</tr>";
	}
	echo "</table>\n";
	echo "<br />";

	echo "<table style=\"margin-left: 50px;\">";	
	echo "<tr><td colspan=\"2\"><h3>".i18n("Project / Student totals")."</h3></td></tr>";
	echo "<tr><td><b>".i18n("Total Projects").":</td><td align=\"right\">$stats_totalprojects</td></tr>\n";
	echo "<tr><td><b>".i18n("Total Students").":</td><td align=\"right\"> $stats_totalstudents</td></tr>\n";
	echo "<tr><td colspan=\"2\"><hr /></td></tr>";

	echo "<tr><td colspan=\"2\"><h3>".i18n("Projects per age category")."</h3></td></tr>";
	$q=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
	while($r=mysql_fetch_object($q))
		echo "<tr><td><b>".i18n($r->category).":</td><td align=\"right\">".($stats_categories[$r->id]?$stats_categories[$r->id]:"0")."</td></tr>\n";

	echo "<tr><td colspan=\"2\"><hr /></td></tr>";
	echo "<tr><td colspan=\"2\"><h3>".i18n("Projects per division")."</h3></td></tr>";
	$q=mysql_query("SELECT * FROM projectdivisions WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
	while($r=mysql_fetch_object($q))
		echo "<tr><td><b>".i18n($r->division).":</td><td align=\"right\">".($stats_divisions[$r->id]?$stats_divisions[$r->id]:"0")."</td></tr>\n";
	echo "</table>";

	echo "<br />";
	echo i18n("Note: statistics reflect the numbers of the current 'Status' selected at the top of the page");
	echo "<br />";
	echo "<br />";

 send_footer();
?>