<?
/* 
   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.
*/
?>
<?
function registrationFormsReceived($reg_id="")
{
	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];
	$q=mysql_query("SELECT status FROM registrations WHERE id='$rid'");
	$r=mysql_fetch_object($q);
	if($r->status=="complete" || $r->status=="paymentpending")
		return true;
	else
		return false;

}
function registrationDeadlinePassed()
{
	global $config;
	$q=mysql_query("SELECT (NOW()<'".$config['dates']['regclose']."') AS datecheck");
	$datecheck=mysql_fetch_object($q);
	if($datecheck->datecheck==1)
		return false;
	else
		return true;

}

function studentStatus($reg_id="")
{
	global $config;
	if($config['participant_student_personal']=="yes")
		$required_fields=array("firstname","lastname","address","city","postalcode","phone","email","grade","dateofbirth","schools_id");
	else
		$required_fields=array("firstname","lastname","email","grade","schools_id");

	if($config['participant_student_tshirt']=="yes")
		$required_fields[]="tshirt";

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

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

	//if we dont have the minimum, return incomplete
	if(mysql_num_rows($q)<$config['minstudentsperproject'])
		return "incomplete";

	while($r=mysql_fetch_object($q))
	{
		foreach ($required_fields AS $req)
		{
			if(!$r->$req)
			{
				return "incomplete";
			}
		}
	}

	//if it made it through without returning incomplete, then we must be complete
	return "complete";
}

function emergencycontactStatus($reg_id="")
{
	global $config;
	$required_fields=array("firstname","lastname","relation","phone1");

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

	$sq=mysql_query("SELECT id FROM students WHERE registrations_id='$rid' AND year='".$config['FAIRYEAR']."'");
	$numstudents=mysql_num_rows($sq);

	while($sr=mysql_fetch_object($sq))
	{
		$q=mysql_query("SELECT * FROM emergencycontact WHERE registrations_id='$rid' AND year='".$config['FAIRYEAR']."' AND students_id='$sr->id'");

		$r=mysql_fetch_object($q);

		foreach ($required_fields AS $req)
		{
			if(!$r->$req)
			{
				return "incomplete";
			}
		}
	}

	//if it made it through without returning incomplete, then we must be complete
	return "complete";
}

function projectStatus($reg_id="")
{
	global $config;
	$required_fields=array("title","projectcategories_id","projectdivisions_id","summary","language","req_table","req_electricity","summarycountok");

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

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

	//if we dont have a project entry yet, return empty
	if(!mysql_num_rows($q))
		return "empty";

	while($r=mysql_fetch_object($q))
	{
		foreach ($required_fields AS $req)
		{
			if(!$r->$req)
			{
				return "incomplete";
			}
		}
	}

	//if it made it through without returning incomplete, then we must be complete
	return "complete";
}


function mentorStatus($reg_id="")
{
	global $config;
	$required_fields=array("firstname","lastname","phone","email","organization","description");

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

	//first check the registrations table to see if 'nummentors' is set, or if its null
	$q=mysql_query("SELECT nummentors FROM registrations WHERE id='$rid' AND year='".$config['FAIRYEAR']."'");
	$r=mysql_fetch_object($q);
	if($r->nummentors==null)
		return "incomplete";

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

	//if we dont have the minimum, return incomplete
	if(mysql_num_rows($q)<$config['minmentorserproject'])
		return "incomplete";

	while($r=mysql_fetch_object($q))
	{
		foreach ($required_fields AS $req)
		{
			if(!$r->$req)
			{
				return "incomplete";
			}
		}
	}

	//if it made it through without returning incomplete, then we must be complete
	return "complete";

}

function safetyStatus($reg_id="")
{
	global $config;

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

	//grab all of their answers
	$q=mysql_query("SELECT * FROM safety WHERE registrations_id='$rid'");
	while($r=mysql_fetch_object($q))
	{
		$safetyanswers[$r->safetyquestions_id]=$r->answer;
	}

	//now grab all the questions
	$q=mysql_query("SELECT * FROM safetyquestions WHERE year='".$config['FAIRYEAR']."' ORDER BY ord");
	while($r=mysql_fetch_object($q))
	{
		if($r->required=="yes" && !$safetyanswers[$r->id])
		{
			return "incomplete";
		}
	}
	return "complete";

}

function spawardStatus($reg_id="")
{
	global $config;
	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

	$q=mysql_query("SELECT * FROM projects WHERE registrations_id='$rid'");
	$project=mysql_fetch_object($q);

	/* We want this query to get any awards with a NULL award_awards_id */
	$awardsq=mysql_query("SELECT
				projects.id AS projects_id
			FROM
				project_specialawards_link,
				projects
			WHERE
				project_specialawards_link.projects_id='".$project->id."'
				AND projects.year='".$config['FAIRYEAR']."'
				");

	if(mysql_num_rows($awardsq))
		return "complete";
	else
		return "incomplete";
}

function tourStatus($reg_id="")
{
	global $config;

	if($reg_id) $rid=$reg_id;
	else $rid=$_SESSION['registration_id'];

	/* Get the students for this project */
	$q=mysql_query("SELECT * FROM students WHERE registrations_id='$rid' AND year='".$config['FAIRYEAR']."'");
	$num_found = mysql_num_rows($q);

	$ret = "complete";
	while($s=mysql_fetch_object($q)) {
		//grab all of their tour prefs
		$sid = $s->id;
		$qq=mysql_query("SELECT * FROM tours_choice WHERE students_id='$sid' and year='{$config['FAIRYEAR']}'");

		$n_tours = mysql_num_rows($qq);
	
		if( ($n_tours >= $config['tours_choices_min']) && ($n_tours <= $config['tours_choices_max']) ){
			continue;
		}
		$ret = "incomplete";
	}
	return $ret;
}


function generateProjectNumber($registration_id)
{
	global $config;

	$reg_id = $registration_id;
	
	$q=mysql_query("SELECT 	projects.projectcategories_id, 
				projects.projectdivisions_id,
				projectcategories.category_shortform,
				projectdivisions.division_shortform
			FROM 
				projects,
				projectcategories,
				projectdivisions
			WHERE 
				registrations_id='$reg_id'
			AND	projects.projectdivisions_id=projectdivisions.id
			AND	projects.projectcategories_id=projectcategories.id
			AND	projectcategories.year='{$config['FAIRYEAR']}'
			AND	projectdivisions.year='{$config['FAIRYEAR']}'
				");
				echo mysql_error();
	$r=mysql_fetch_object($q);

	$projectnumber=$config['project_num_format'];

	//first replace the division and category
	$projectnumber=str_replace('D',$r->projectdivisions_id,$projectnumber);
	$projectnumber=str_replace('C',$r->projectcategories_id,$projectnumber);
	$projectnumber=str_replace('d',$r->division_shortform,$projectnumber);
	$projectnumber=str_replace('c',$r->category_shortform,$projectnumber);

	//now change the N to a % so we can use it as a wildcard 
	$querynum=str_replace('N','%',$projectnumber);
	$searchq=mysql_query("SELECT projectnumber FROM projects WHERE year='".$config['FAIRYEAR']."' AND projectnumber LIKE '$querynum'");
	if(mysql_num_rows($searchq))
	{
		//first, put them all in an array
		$proj_nums=array();
		while($searchr=mysql_fetch_object($searchq))
		{
			$proj_nums[]=$searchr->projectnumber;
		}

		//we will eventually find a good number, so lets loop forever until we find a good one
		$testnum=1;
		$Nnum=1;
		$ok=false;
		do
		{
			$Nnum=sprintf("%02d",$testnum);
			$test_projectnumber=str_replace('N',$Nnum,$projectnumber);
			if(!in_array($test_projectnumber,$proj_nums))
				$ok=true;
			$testnum++;
		}while(!$ok);


	}
	else
	{
		$Nnum="01";
	}

	$projectnumber=str_replace('N',$Nnum,$projectnumber);

	return $projectnumber;
}

function computeRegistrationFee($regid)
{
 	global $config;
	
	$ret = array();
	$q=mysql_query("SELECT * FROM students WHERE registrations_id='$regid' AND year='".$config['FAIRYEAR']."'");
	$n_students = mysql_num_rows($q);
	$n_tshirts = 0;
	while($s = mysql_fetch_object($q)) {
		if($s->tshirt != 'none') $n_tshirts++;
	}

	if($config['regfee_per'] == 'student') {
		$f = $config['regfee'] *  $n_students;
		$ret[] = array( 'id' => 'regfee',
				'text' => "Fair Registration (per student)",
				'base' => $config['regfee'],
				'num' => $n_students,
				'ext' => $f );
 		$regfee += $f; 
	} else {
		$ret[] = array( 'id' => 'regfee',
				'text' => "Fair Registration (per project)",
				'base' => $config['regfee'],
				'num' => 1,
				'ext' => $config['regfee'] );
		$regfee += $config['regfee'];
	}

	if($config['participant_student_tshirt'] == 'yes') {
		$tsc = floatval($config['participant_student_tshirt_cost']);
		if($tsc != 0.0) {
			$f = $n_tshirts * $tsc;
			$regfee += $f;
			if($n_tshirts != 0) {
				$ret[] = array( 'id' => 'tshirt',
						'text' => "T-Shirts",
						'base' => $tsc,
						'num' => $n_tshirts,
						'ext' => $f);
			} 
		}
	}
	return array($regfee, $ret);
}



?>