<?php
require_once('common.inc.php');
require_once('user.inc.php');


if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'])
{
	$title = i18n("School Information");
	send_header($title, array("School Home" => "schoolaccess.php"));

	$q=mysql_query("SELECT * FROM schools WHERE id='".$_SESSION['schoolid']."' AND accesscode='".$_SESSION['schoolaccesscode']."' AND year='".$config['FAIRYEAR']."'");
	echo mysql_error();
	$school=mysql_fetch_object($q);

	if($school->sciencehead_uid > 0) 
		$sh = user_load_by_uid($school->sciencehead_uid);
	else
		$sh = array();
	$sh_email = ($sh['email'] != '' && $sh['email'][0] != '*') ? $sh['email'] : '';


	if($_POST['action']=="save") {
		/* Get info about science head */
		$sciencehead_update = '';
		list($first, $last) = split(' ', $_POST['sciencehead'], 2);
		$em = $_POST['scienceheademail'];
		if($em == '' && ($first != '' || $last != '')) $em = "*$first$last".user_generate_password();

		if($school->sciencehead_uid > 0)
			$sh = user_load_by_uid($school->sciencehead_uid);
		else if($em != '')  {
			$sh = user_create('teacher', $em);
			$sciencehead_update = "sciencehead_uid='{$sh['uid']}',";
		} else
			$sh = false;

		/* If we have a record, either delete it or update it */
		if(is_array($sh)) {
			if($em == '') {
				user_purge($sh, 'teacher');
				$sciencehead_update = 'sciencehead_uid=NULL,';
			} else {
				$sh['firstname'] = $first;
				$sh['lastname'] = $last;
				$sh['phonework'] = $_POST['scienceheadphone'];
				$sh['email'] = $em;
				$sh['username'] = $em;
				user_save($sh);
			}
		}

		mysql_query("UPDATE schools SET
			school='".mysql_escape_string(stripslashes($_POST['school']))."',
			address='".mysql_escape_string(stripslashes($_POST['address']))."',
			city='".mysql_escape_string(stripslashes($_POST['city']))."',
			province_code='".mysql_escape_string(stripslashes($_POST['province_code']))."',
			postalcode='".mysql_escape_string(stripslashes($_POST['postalcode']))."',
			phone='".mysql_escape_string(stripslashes($_POST['phone']))."',
			$sciencehead_update
			fax='".mysql_escape_string(stripslashes($_POST['fax']))."'
			WHERE id='$school->id'");

		echo mysql_error();
		if(mysql_error())
			echo error(i18n("An Error occured trying to save the school information"));
		else
			echo happy(i18n("School information successfully updated"));

	}

}else{
	header('Location: schoolaccess.php');
	exit();
}

// load the school info
$q=mysql_query("SELECT * FROM schools WHERE id='".$_SESSION['schoolid']."' AND accesscode='".$_SESSION['schoolaccesscode']."' AND year='".$config['FAIRYEAR']."'");
echo mysql_error();
$school=mysql_fetch_object($q);

if($school->sciencehead_uid > 0) 
	$sh = user_load_by_uid($school->sciencehead_uid);
else
	$sh = array();
$sh_email = ($sh['email'] != '' && $sh['email'][0] != '*') ? $sh['email'] : '';


draw_page();
send_footer();

function draw_page(){
	global $config, $school, $sh_email, $sh_email, $sh;

	echo i18n("Please make sure your school contact information is correct, make any necessary changes:");
	echo "<form method=POST action=\"schoolinfo.php\">";
	echo "<input type=hidden name=action value=\"save\">";
	echo "<table border=0 cellspacing=0 cellpadding=3>";
	echo "<tr><td>".i18n("School Name")."</td><td><input value=\"$school->school\" type=text name=school size=40></td></tr>";
//	echo "<tr><td>Registration Password</td><td><input value=\"$school->registration_password\" type=text name=\"registration_password\" size=\"20\"></td></tr>";
	echo "<tr><td>".i18n("Address")."</td><td><input value=\"$school->address\" type=text name=address size=40></td></tr>";
	echo "<tr><td>".i18n("City")."</td><td><input value=\"$school->city\" type=text name=city size=30></td></tr>";
	echo "<tr><td>".i18n($config['provincestate'])."</td><td>";
	emit_province_selector("province_code",$school->province_code);
	echo "</td></tr>\n";
	echo "<tr><td>".i18n($config['postalzip'])."</td><td><input value=\"$school->postalcode\"  type=text name=postalcode size=10></td></tr>";
	echo "<tr><td>".i18n("Phone Number")."</td><td><input value=\"$school->phone\" type=text name=phone size=30></td></tr>";
	echo "<tr><td>".i18n("Fax Number")."</td><td><input value=\"$school->fax\" type=text name=fax size=30></td></tr>";
	
	echo "<tr><td>".i18n("Science Teacher")."</td><td><input value=\"{$sh['name']}\" type=text name=sciencehead size=40></td></tr>";
	echo "<tr><td>".i18n("Science Teacher Email")."</td><td><input value=\"$sh_email\" type=text name=scienceheademail size=40></td></tr>";
	echo "<tr><td>".i18n("Science Teacher Phone")."<br><font size=1>(".i18n("If different than above").")</font></td><td><input value=\"{$sh['phonework']}\" type=text name=scienceheadphone size=30></td></tr>";
	echo "</table>";
	echo "<input type=submit value=\"".i18n("Save Changes")."\">";
	echo "</form>";
	echo "<br>";
}

?>
