<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 - 2.4): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# con_students.inc.php: last changed 04/10/2006 2.4.10                                        
#                       by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: con_students.inc.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2006 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
?>
<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
   if (form.fullname.value == "") 
    {
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.fullname.focus();
    return false ;
    }
    if (form.username.value == "") 
    {
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.username.focus();
    return false ;
    }
	if ((document.form.send_email[0].checked==true) && (document.form.check[0].checked==true))
	{
	alert("<?php echo $sas_lang['student_project_warning'] ?>");
	return false ;
	}
  return true ;
}
</script>
<?php


if (IsSet ($_GET['editclass']))
{
	$editclass = $_GET['editclass'];
}
if (IsSet ($_GET['saveclass']))
{
	$saveclass = $_GET['saveclass'];
}
if (IsSet ($_GET['moveclass']))
{
	$moveclass = $_GET['moveclass'];
}
if (IsSet ($_GET['savestudent']))
{
	$savestudent = $_GET['savestudent'];
}
if (IsSet ($_GET['editstudent']))
{
	$editstudent = $_GET['editstudent'];
}
if (IsSet ($_GET['class1']))
{
	$class1 = $_GET['class1'];
}
if (IsSet ($_GET['deletestudent']))
{
	$deletestudent = $_GET['deletestudent'];
}
if (IsSet ($_GET['deleteclass']))
{
	$deleteclass = $_GET['deleteclass'];
}

if (IsSet ($_POST['class1']))
{
	$class1 = $_POST['class1'];
}
if (IsSet ($_POST['fullname']))
{
	$fullname = addslashes($_POST['fullname']);
}
if (IsSet ($_POST['username']))
{
	$username = $_POST['username'];
}
if (IsSet ($_POST['password']))
{
	$password = $_POST['password'];
}
if (IsSet ($_POST['project']))
{
	$project = $_POST['project'];
}
if (IsSet ($_POST['email']))
{
	$email = $_POST['email'];
}
if (IsSet ($_POST['check']))
{
	$check = $_POST['check'];
}
if (IsSet ($_POST['check1']))
{
	$check1 = $_POST['check1'];
}
if (IsSet ($_POST['send_email']))
{
	$send_email = $_POST['send_email'];
}
if (IsSet ($_POST['move_to_class']))
{
	$move_to_class = $_POST['move_to_class'];
}
if (IsSet ($_GET['modoption']))
{
	$modoption = $_GET['modoption'];
}
else
{
	$modoption = "";
}
$d = date("Y-m-d"); //todays date

if (($sas_config[use_encryption] == 1) AND ($password != "") AND ($password != "********")) //passwords are encrypted and not blank
{
	$password = md5($password);
}

if (substr("$access", 8, 1) == "0") //bit 9 off the accessrights
{
	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($saveclass))
{
	$result = mysql_query("SELECT id FROM $table_classes WHERE name = '$fullname' AND id != '$saveclass'");
	if (mysql_num_rows($result) == 0)
	{
		if ($saveclass == "new")
		{
			mysql_query("INSERT INTO $table_classes (name, teachers, project) VALUES ('$fullname', '', '0')");
		}
		else
		{
			mysql_query("UPDATE $table_classes SET name='$fullname' WHERE id='$saveclass'");
		}
	}
	else
	{
		print "<script language=\"Javascript\">
					   <!--
					   alert (\"" . $sas_lang['project_name_exists'] . "\");
					   history.go(-1) ;	
					   //-->
					   </script>";
	}
}

if (IsSet ($deletestudent))
{
	mysql_query("DELETE FROM $table_users WHERE id='$deletestudent'"); //delete the user
	mysql_query("DELETE FROM $table_studentpages WHERE dir_number='$deletestudent'"); //delete the studentpage

	if (IsSet ($_GET['delete_student_files']))
	{
		$delete_student_files = $_GET['delete_student_files'];
	}
	else
	{
		$delete_student_files = "";
	}

	$student_path = $sas_config['serverpath'] . $sas_config['user_path'] . "/studentpages/" . $deletestudent . "/";

	if (file_exists($student_path)) //first check if the student directory exists
	{
		delete_files($deletestudent); //delete all the file in the pupils directory
		rmdir($student_path); //delete the pupils root directory

		//return to the students class view
		print "<script language='javascript'>
				  window.location = 'index.php?option=configuration&suboption=students&class1=$class1'
				  </script>";
	}
}
if (IsSet ($deleteclass))
{
	$query = "SELECT * FROM $table_users WHERE class='$deleteclass'"; // check if all students are gone
	$result3 = mysql_query($query);
	if (mysql_num_rows($result3) == 0)
	{
		mysql_query("DELETE FROM $table_classes WHERE id='$deleteclass'");
	}
	else
	{
		print "<script language=\"Javascript\">
					   <!--
					   alert (\"" . $sas_lang['class_delete_error'] . "\")
					   //-->
					   </script>";
	}

}

if (IsSet ($moveclass))
{
	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");
	print "<span class=\"heading\">" . $sas_lang['students'] . "</span><p>";
	if ($moveclass == "save")
	{
		$query = "SELECT name FROM $table_classes where id='$class1'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$classname = mysql_result($result, 0);
		unset ($query);
		unset ($result);
		mysql_query("UPDATE $table_users SET class='$class1' WHERE class='$move_to_class'");
		print "<span class=\"text\"><b>" . $sas_lang['students_have_moved'] . " $classname</b></span>";
		print "<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
					onClick=\"javascript:parent.location='index.php?option=configuration&suboption=students&class1=$class1';\" class=\"backbutton\">";
	}
	else
	{
		$query = "SELECT name FROM $table_classes where id='$moveclass'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$classname = mysql_result($result, 0);
		unset ($query);
		unset ($result);
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=students&moveclass=save\">";
		print "<span class=text><b>$classname</b><br>" . $sas_lang['students_move_to'] . ":
					<select class=\"text\" size=\"1\" tabindex=\"5\" name=\"class1\">";
		$query = "SELECT id, name FROM $table_classes";
		$result = mysql_query($query) or die_script($query, mysql_error());
		while ($class_list = mysql_fetch_array($result))
		{
			print "<option value=\"$class_list[0]\">$class_list[1]</option>\n";
		}
		print "</select><br><INPUT type=\"hidden\" name=\"move_to_class\" value=\"$moveclass\">";
		print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
					onClick=\"javascript:parent.location='index.php?option=configuration&suboption=students';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"backbutton\" class=\"savebutton\">";
		print "</form></span>";
	}

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($editclass))
{
	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");
	if ($editclass == "new")
	{
		print "<span class=\"heading\">" . $sas_lang['students'] . "</span><br>
					<span class=\"text\">" . $sas_lang['newclass'] . "<br></span>";
		print "<span class=text><FORM METHOD=\"POST\" NAME=\"form\" 
					ACTION=\"" . $_SERVER['PHP_SELF'] . "?option=configuration&suboption=students&modoption=pupil_list&saveclass=$editclass\">";
		print "<p><br><b>" . $sas_lang['fullname'] . ":</b>&nbsp;
					<input type=text size=60 class=\"textfield\" name=\"fullname\" value=\"$fullname\">&nbsp;<p>";

		print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
					onClick=\"javascript:parent.location='index.php?option=configuration&suboption=students&modoption=$modoption';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"backbutton\" class=\"savebutton\">";
		print "</form></span>";
	}
	else
	{
		$query = "SELECT name, project FROM $table_classes WHERE id='$editclass'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$fullname = stripslashes(mysql_result($result, 0, 0));
		$project = mysql_result($result, 0, 1);
		unset ($query);
		unset ($result);
		print "<span class=\"heading\">" . $sas_lang['students'] . "</span><br>
					<span class=\"text\">" . $sas_lang['edit'] . " " . $sas_lang['class'] . "<br></span>";
		print "<span class=text><FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=students&saveclass=$editclass\">";
		print "<br><br><b>" . $sas_lang['class'] . ":</b>&nbsp;
					<input type=text size=60 class=\"textfield\" name=\"fullname\" value=\"$fullname\">&nbsp;<p>";

		print "\n<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
					onClick=\"javascript:parent.location='index.php?option=configuration&suboption=students&modoption=$modoption';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"backbutton\" class=\"savebutton\">";
		print "</form></span>";
	}

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($savestudent))
{
	if (count($project) == 0)
	{
		$comma_separated_project = "";
	}
	else
	{
		$comma_separated_project = implode(",", $project);
	}

	if ($check1 == "1") //pupil is also a normal user.
	{
		$type = 21;
	}
	else
	{
		$type = "1";
	}

	if ($savestudent == "new")
	{

		$query = mysql_query("SELECT username FROM $table_users WHERE username = '$username'");
		$results = mysql_num_rows($query);

		if ($results != '0')
		{
			echo "<script>alert ('$sas_lang[student_name_exists]');
					  history.go(-1);
					  </script>";
		}
		else
		{
			mysql_query("INSERT INTO $table_users (username, password, name, class, type, email, access, highvisibility, project) 
								 VALUES ('$username', '$password',  '$fullname', '$class1', '$type', '$email', '000000000000,000000000000', '0', '$comma_separated_project')");
			//create 1 pupilpage		 
			$result = mysql_query("SELECT id FROM $table_users WHERE name = '$fullname'"); //get the id just inserted
			$user_id = mysql_result($result, 0);
			mysql_query("INSERT INTO $table_studentpages VALUES ('', '', '', 0, 0, '0', '[1]', $user_id, '$d', 1, 0)");
			$custompath = $serverpath . "/studentpages/$user_id";
			if (!is_dir($custompath))
			{
				$perm = $sas_config['file_permissions'];
				mkdir($custompath, 0755);
				chmod($custompath, octdec($perm)); //chmod it with permissions stored in configuration table.
			}
		}
	}
	else
	{
		$query = mysql_query("SELECT username FROM $table_users WHERE username = '$username' AND id != '$savestudent'");
		$results = mysql_num_rows($query);
		if ($results != '0') //an other user with this username exists, give error
		{
			print "<script language=\"Javascript\">
						   <!--
						   alert (\"" . $sas_lang['student_name_exists'] . "\");
						   window.location = 'index.php?option=configuration&suboption=users&user=$savestudent' ;	
						   //-->
						   </script>"; //return to the user edit window.
		}
		else
		{
			mysql_query("UPDATE $table_users SET name='$fullname', 
													   username='$username',  
													   email='$email', 
													   class='$class1', 
													   type='$type',
													   project = '$comma_separated_project'
													   WHERE id='$savestudent'");
			if (($password != "") AND ($password != "********")) //only save password when not empty.
			{
				mysql_query("UPDATE $table_users SET password='$password' WHERE id='$savestudent'");
			}
			mysql_query("UPDATE $table_studentpages SET pcheck='$check',
																  send_email='$send_email' 
																  WHERE dir_number='$savestudent'");
		}
	}
}
if (IsSet ($editstudent))
{
	if ($editstudent == "new")
	{
		$class = $class1;
	}
	else
	{
		$query1 = "SELECT id, name, username, password, email, class, type, project FROM $table_users WHERE id='$editstudent'";
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		while ($student_list = mysql_fetch_array($result1))
		{
			$fullname = $student_list[1];
			$username = $student_list[2];
			$password = $student_list[3];
			$email = $student_list[4];
			$class = $student_list[5];
			$user_type = $student_list[6];
			$project = $student_list[7];
		}
		$project_array = explode(",", $project); //create an array

		$query2 = "SELECT pcheck, send_email 
		               FROM $table_studentpages 
		               WHERE dir_number='$editstudent'"; // get the check and email bit
		$result2 = mysql_query($query2) or die_script($query2, mysql_error());
		if (mysql_num_rows($result2) != 0)
		{
			$studentpages_list = mysql_fetch_array($result2);
			$check = $studentpages_list[0];
			$send_email = $studentpages_list[1];
		}
	}
	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");
	print "<span class=\"heading\">" . $sas_lang['students'] . "<br></span>";
	if ($editstudent == "new")
	{
		print "<span class=text>" . $sas_lang['newstudent'] . "</span>";
	}
	else
	{
		print "<span class=text>" . $sas_lang['modifyuser_introduction'] . "</span>";
	}
	print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=students&modoption=$modoption&savestudent=$editstudent\" 
			onsubmit=\"return checkinput(this);\">";
	print "<p><table cellpadding=\"5\" cellspacing=\"2\"><tr>
	       <td><span class=text><b>" . $sas_lang['fullname'] . ": (*)</b></td>
	       <td><input type=text size=40 class=\"textfield\" tabindex=\"1\" name=\"fullname\" value=\"$fullname\">&nbsp;
	       <img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['fullname_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['username'] . ": (*)</b></td>
	        <td><input type=text size=20 class=\"textfield\" tabindex=\"2\" name=\"username\" value=\"$username\">&nbsp;
	        <img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['username_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['password'] . ": (*)</b></td>";

	if ($editstudent == "new")
	{
		print "<td><input type=\"text\" name=\"password\" size=\"20\" class=\"textfield\" tabindex=\"3\">&nbsp;
			<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['password_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	}
	elseif ($sas_config[use_encryption] == 1) //passwords are encrypted
	{
		print "<td><input type=\"text\" name=\"password\" size=\"20\" value=\"********\" class=\"textfield\" tabindex=\"3\">&nbsp;
			<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['password_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	}
	else
	{
		print "<td><input type=\"text\" name=\"password\" size=\"20\" value=\"$password\" class=\"textfield\" tabindex=\"3\">&nbsp;
			<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['password_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	}

	print "<tr><td><span class=text><b>" . $sas_lang['email'] . ":</b></td>
	        <td><input type=text size=60 class=\"textfield\" tabindex=\"4\" name=\"email\" value=\"$email\">&nbsp;
	        <img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['email_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['class'] . ":</td>
	        <td><select class=\"text\" size=\"1\" tabindex=\"5\" name=\"class1\">";

	$query = "SELECT id, name FROM $table_classes WHERE name != '--------' AND project = 0 ORDER BY name";
	$result = mysql_query($query) or die_script($query, mysql_error());
	while ($class_list = mysql_fetch_array($result))
	{
		$class_name = stripslashes($class_list[1]);
		if ($class_list[0] == $class)
		{
			print "<option selected value=\"$class_list[0]\">$class_name</option>\n";
		}
		else
		{
			print "<option value=\"$class_list[0]\">$class_name</option>\n";
		}
	}
	print "</select>&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['student_class_tip'] . "','CCDAEC')\"; 
	       ONMOUSEOUT=\"kill()\"></td></tr>";
	print "<tr><td>&nbsp;</td><td><span class=\"text\">" . $sas_lang['required_fields'] . "</td></tr>";
	print "<tr><td colspan=\"2\"><table width=\"100%\">\n";
	print "<tr><td colspan=\"9\"><br><span class=text><b>" . $sas_lang['student_projects'] . ":&nbsp;&nbsp;
	<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['student_project_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"></td></tr>";

	$query = "SELECT id, name FROM $table_classes WHERE project = 1 ORDER BY name";
	$result = mysql_query($query) or die_script($query, mysql_error());

	print "<tr>";
	$i = 0;
	$j = 0;
	while ($project_list = mysql_fetch_array($result))
	{
		$project_name = stripslashes($project_list[1]);
		if ($editstudent != "new")
		{
			if (in_array($project_list[0], $project_array))
			{
				print "<td width=\"25%\"><span class=text><b>$project_name</b></span></td>
								<td><input type=\"checkbox\" value=\"$project_list[0]\" name=\"project[$j]\" checked></td><td width=\"10%\">&nbsp;&nbsp;</td>\n";
			}
			else
			{
				print "<td width=\"25%\"><span class=text><b>$project_name</b></span></td>
								<td><input type=\"checkbox\" value=\"$project_list[0]\" name=\"project[$j]\"></td><td width=\"10%\">&nbsp;&nbsp;</td>\n";
			}
		}
		else
		{
			print "<td width=\"25%\"><span class=text><b>$project_name</b></span></td>
							<td><input type=\"checkbox\" value=\"$project_list[0]\" name=\"project[$j]\"></td><td width=\"10%\">&nbsp;&nbsp;</td>\n";
		}
		if ($i == 2)
		{
			print "</tr><tr>";
			$i = 0;
		}
		else
		{
			$i++;
		}
		$j++;
	}
	print "</table></td></tr><tr><td colspan=\"5\">&nbsp;</td></tr>";

	print "<tr><td><span class=text>" . $sas_lang['student_is_user'] . " </span></td>";
	print "<td colspan=\"4\"><span class=text>" . $sas_lang['yes'] . "</span>&nbsp;";
	if ($user_type == "21")
	{
		print "<input type=\"radio\" value=\"1\" name=\"check1\" checked>&nbsp;";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
		print "<input type=\"radio\" name=\"check1\" value=\"0\"></td>";
	}
	else
	{
		print "<input type=\"radio\" value=\"1\" name=\"check1\">&nbsp;";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
		print "<input type=\"radio\" name=\"check1\" value=\"0\" checked></td>";
	}
	print "</tr>";

	if ($editstudent != "new")
	{
		$num_rows = mysql_num_rows($result2);
		if ($num_rows == "0") //only show when a page has been created 							                                   	
		{
			print "<tr><td colspan=\"5\">" . $sas_lang['student_no_page'] . "</td></tr>";
		}
		else
		{
			print "<tr><td><span class=text>" . $sas_lang['student_page_check'] . " </span></td>";
			print "<td colspan=\"4\"><span class=text>" . $sas_lang['yes'] . "</span>&nbsp;";
			if ($check == "1")
			{
				print "<input type=\"radio\" value=\"1\" name=\"check\" checked>&nbsp;";
				print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
				print "<input type=\"radio\" name=\"check\" value=\"0\"></td>";
			}
			else
			{
				print "<input type=\"radio\" value=\"1\" name=\"check\">&nbsp;";
				print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
				print "<input type=\"radio\" name=\"check\" value=\"0\" checked></td>";
			}
			print "</tr><tr><td><span class=text>" . $sas_lang['student_page_email'] . " </span></td>";
			print "<td colspan=\"4\"><span class=text>" . $sas_lang['yes'] . "</span>&nbsp;";

			if ($send_email == "1")
			{
				print "<input type=\"radio\" value=\"1\" name=\"send_email\" checked>&nbsp;";
				print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
				print "<input type=\"radio\" name=\"send_email\" value=\"0\"></td>";
			}
			else
			{
				print "<input type=\"radio\" value=\"1\" name=\"send_email\">&nbsp;";
				print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>&nbsp;";
				print "<input type=\"radio\" name=\"send_email\" value=\"0\" checked></td>";
			}
			print "</tr>";
		}
	}
	else
	{
		print "<tr><td colspan=\"4\"><span class=text>" . $sas_lang['student_no_page'] . "</span></td></tr>";
	}

	$query3 = "SELECT id, name FROM $table_modules WHERE location = 'sn_studentpages'";
	$result3 = mysql_query($query3) or die_script($query3, mysql_error());
	$module_id = mysql_result($result3, 0, 0); //get the module id for the link
	$module_name = mysql_result($result3, 0, 1); //get the module name for the link

	print "<tr><td colspan=\"4\"><span class=text>" . $sas_lang['student_directory'] . " 
			<b>$editstudent</b></span>&nbsp;<span class=link>
			<a href=\"index.php?option=modulemanager&module=$module_id&viewclass=$class\">(" . $module_name . ")</a></span></td></tr></table><br>";
	print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
	        onClick=\"javascript:parent.location='index.php?option=configuration&suboption=students&modoption=$modoption&class1=$class1';\" class=\"backbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</form></span>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

// list of students and classes

include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
print "<span class=heading>" . $sas_lang['filemanager_menu'] . "</span><br><br>";
print "<span class=link><a href=index.php?option=configuration&suboption=students>" . $sas_lang['class'] . "</a><p>
<a href=index.php?option=configuration&suboption=students&modoption=pupil_list>" . $sas_lang['students'] . "</a><p>
<a href=index.php?option=configuration&suboption=students&modoption=import>" . $sas_lang['import_import'] . "</a></span><p>";
if ((file_exists("media/" . $sas_config[edex_file_name])) AND ($sas_config[edex_file_name] != ""))
{
	print "<span class=link><a href=index.php?option=configuration&suboption=students&modoption=download>Download</a><br></span>";
}
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

print "<span class=\"heading\">" . $sas_lang['students'] . "<br></span>";

if ($modoption == "download")
{
	$query = "SELECT password, email FROM $table_users WHERE id = '$_SESSION[userid]'";
	$result = mysql_query($query); //get user password , used as a key for decryption and email address
	$email_user = mysql_result($result, 0, 1);
	if ($email_user != "")
	{
		include ("core/cryptography.php"); //include encryption script
		$encryp = new cryptography;

		$encryp->set_key(mysql_result($result, 0, 0)); //use password as encryption key
		$filename = "media/" . $sas_config[edex_file_name];
		$handle = fopen($filename, "r");
		$contents = fread($handle, filesize($filename)); //get encrypted file contents
		fclose($handle);
		$decrypted = $encryp->decrypt($contents); //decrypt the contents                         
		//$decrypted = nl2br($decrypted); 

		require ("core/class.phpmailer.php"); //use the phpmailer class
		$mail = new PHPMailer();
		$mail->From = $sas_config['email']; //email address of submitter
		$mail->FromName = $sas_config['sitename'];
		$mail->Subject = "Imported users via janitor:";
		$mail->Body = $decrypted;
		$mail->IsHTML(false);
		$mail->AddAddress($email_user); //add email address user.
		$mail->Send();
		print "<p><span class=\"text\">You have been send an email (to: $email_user) with a list of users which were imported via the import function!";
		print "<br>You might want to delete file : <b>" . $filename . "</b></span>";
	}
	else
	{
		print "<p><span class=\"text\">" . $sas_lang['no_email'] . "</span>";
	}
	//print $decrypted ;
}
elseif ($modoption == "import")
{
	print "<span class=\"text\">" . $sas_lang['import_import'] . "</span><br><br>";

	require "janitor/functions.php";
	require "janitor/janitor.conf.php";
	require "janitor/config.php";

	// globals
	$Message = "";

	// Dispatcher; see what they want
	$action = $_REQUEST[PARAM_ACTION];
	$tgt = strval(intval($_REQUEST[PARAM_TGT]));
	$src = strval(intval($_REQUEST[PARAM_SRC]));
	include_once "janitor/edex.php";
	edex_screen($tgt, 1);

}
elseif ($modoption == "pupil_list")
{
	print "<span class=\"text\">" . $sas_lang['students_introduction'] . "</span><br><br>";
	$query = "SELECT id, name, class, project, username, email FROM $table_users WHERE type = 1 OR type = 21 ORDER BY ORD(name)";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$RecordCount = mysql_num_rows($result);
	print "<table border=\"0\">
	       <tr><td>&nbsp;</td><td><span class=\"text\">" . $sas_lang['students'] . ":</span></td>
	       <td width=\"30\">&nbsp;</td><td><span class=\"text\">" . $sas_lang['username'] . ":</span></td>
		   <td width=\"30\">&nbsp;</td><td><span class=\"text\">" . $sas_lang['email'] . ":</span></td>
		   <td width=\"30\">&nbsp;</td><td><span class=\"text\">" . $sas_lang['class'] . ":</span></td>
	       <td width=\"30\">&nbsp;</td><td><span class=\"text\">" . $sas_lang['student_projects'] . ":</span></td></tr>";
	while ($student_list = mysql_fetch_array($result))
	{
		$pupil_projects = "";
		$student_name = stripslashes($student_list[1]);
		$query1 = "SELECT name FROM $table_classes WHERE id = '$student_list[2]'";
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$class_name = stripslashes(mysql_result($result1, 0));

		$project_list = explode(",", $student_list[3]);
		foreach ($project_list as $value)
		{
			$query2 = "SELECT name FROM $table_classes WHERE id = '$value'"; // getting the pupils proejcts
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			if (mysql_num_rows($result2) == 1)
			{
				$pupil_projects .= stripslashes(mysql_result($result2, 0)) . "<br>";
			}
			else
			{
				$pupil_projects = " ";
			}
		}

		print "<tr><td height=\"25\" valign=\"top\">";
		print "<a href=\"index.php?option=configuration&suboption=students&deletestudent=$student_list[0]&modoption=pupil_list\" 
						   onClick=\"return confirm('" . $sas_lang['student_delete_confirm'] . "')\">
						   <img border=\"0\" src=\"graphics/delete.gif\" width=\"16\" alt=\"" . $sas_lang['delete'] . "\" height=\"16\"></a>";
		print "</td><td valign=\"top\">";
		print "<span class=link><a href=\"index.php?option=configuration&suboption=students&editstudent=$student_list[0]&modoption=pupil_list\">
							<img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" > $student_name</a></span><br>";
		print "</td><td>&nbsp;</td><td valign=\"top\"><span class=text><b>$student_list[4]</b></span></td>
							<td>&nbsp;</td><td valign=\"top\"><span class=text><b>$student_list[5]</b></span></td>
							<td>&nbsp;</td><td valign=\"top\"><span class=text><b>$class_name</b></span></td><td>&nbsp;</td>
							<td><span class=text><b>$pupil_projects</b></span></td></tr>";
	}
	print "</table>";
}
else
{
	print "<span class=\"text\">" . $sas_lang['students_introduction'] . "</span><br><br>";
	$query = "SELECT id, name FROM $table_classes WHERE project = 0 ORDER BY ORD(name)";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$RecordCount = mysql_num_rows($result);
	if ($result)
	{
		if ($class1 != "")
		{
			$query1 = "SELECT name FROM $table_classes WHERE id='$class1'";
			$result1 = mysql_query($query1) or die_script($query1, mysql_error());
			$class_name = stripslashes(mysql_result($result1, 0));

			print "<span class=link>$class_name</span><br><br>";
			print "<table border=\"0\">";
			$query2 = "SELECT id, name, class FROM $table_users WHERE class='$class1' AND (type = 1 OR type = 21) ORDER BY name";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			while ($student_list = mysql_fetch_array($result2))
			{
				$student_name = stripslashes($student_list[1]);
				print "<tr><td>";
				print "&nbsp;&nbsp;";
				print "</td>";
				print "<td>";
				if ($highvisibility == "1")
				{
					print "<a href=\"index.php?option=configuration&suboption=students&deletestudent=$student_list[0]&class1=$class1\" 
											 onClick=\"return confirm('" . $sas_lang['student_delete_confirm'] . "')\">[" . $sas_lang['delete'] . "]</a>";
				}
				else
				{
					print "<a href=\"index.php?option=configuration&suboption=students&deletestudent=$student_list[0]&class1=$class1\" 
											   onClick=\"return confirm('" . $sas_lang['student_delete_confirm'] . "')\">
											   <img border=\"0\" src=\"graphics/delete.gif\" width=\"16\" alt=\"" . $sas_lang['delete'] . "\" height=\"16\"></a>";
				}

				print "</td><td>";
				if ($highvisibility == "1")
				{
					print "<a href=\"index.php?option=configuration&suboption=students&editstudent=$student_list[0]&class1=$class1\">[" . $sas_lang['edit'] . "]</a><span class=link> $student_name</span>";
				}
				else
				{
					print "<span class=link><a href=\"index.php?option=configuration&suboption=students&editstudent=$student_list[0]&class1=$class1\">
												<img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" > $student_name</a></span><br>";
				}
				print "</td></tr>";
			}
			print "<tr><td colspan=\"2\">&nbsp;</td><td><img src=graphics/folder_closed.gif alt=\"" . $sas_lang['students_move_to'] . "\">
							 <a href=\"index.php?option=configuration&suboption=students&moveclass=$class1\"><span class=link>" . $sas_lang['students_move_to'] . "</span></a>
							 </td></tr></table><br>";
		}

		while ($class_list = mysql_fetch_array($result))
		{
			$class_name = stripslashes($class_list[1]);
			if ($class_list[0] != "$class1")
			{
				if ($highvisibility == "1")
				{
					print "<a href=\"index.php?option=configuration&suboption=students&deleteclass=$class_list[0]\" onClick=\"return confirm('" . $sas_lang['class_delete_confirm'] . "')\">[" . $sas_lang['delete'] . "]</a>";
				}
				else
				{
					print "<a href=\"index.php?option=configuration&suboption=students&deleteclass=$class_list[0]\" onClick=\"return confirm('" . $sas_lang['class_delete_confirm'] . "')\">
											   <img border=\"0\" src=\"graphics/delete.gif\" width=\"16\" alt=\"" . $sas_lang['delete'] . "\" height=\"16\" ></a>";
				}

				if ($highvisibility == "1")
				{
					print "<a href=\"index.php?option=configuration&suboption=students&editclass=$class_list[0]\">[" . $sas_lang['edit'] . "]</a>";
				}
				else
				{
					print "<span class=link><a href=index.php?option=configuration&suboption=students&editclass=$class_list[0]>
											 <img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" ></a>";
				}
				print "&nbsp;<a href=index.php?option=configuration&suboption=students&class1=$class_list[0]>$class_name</a></span><p>";
			}
			//else
			//{

			//}
		}
	}
}
print "<br><table><tr><td>&nbsp;</td>";
if (($modoption != "import") AND ($modoption != "download"))
{
	print "<td><img src=graphics/folder_closed.gif alt=\"" . $sas_lang['newclass'] . "\"></td>
	       <td><a href=\"index.php?option=configuration&suboption=students&modoption=$modoption&editclass=new\"><span class=link>" . $sas_lang['newclass'] . "</span></a></td>
	       <td>&nbsp;</td>";
}

if ($RecordCount > 0)
{
	print "<td><img src=graphics/folder_closed.gif alt=\"" . $sas_lang['newstudent'] . "\"></td>
	       <td><a href=\"index.php?option=configuration&suboption=students&modoption=$modoption&editstudent=new&class1=$class_list[0]\"><span class=link>" . $sas_lang['newstudent'] . "</span></a></td>";
}
print "</tr></table>";
print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
       onClick=\"parent.location='index.php?option=configuration'\" class=\"backbutton\">";

include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");
?>