<?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.3): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# viewpage.php: last changed 12/03/2006 2.4
#               by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: viewpage.php,v 1.1 2006/10/03 10:47:25 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
#
# 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");
}
if (IsSet ($_GET['student']))
{
	$student = $_GET['student'];
}
if (IsSet ($_GET['viewclass']))
{
	$viewclass = $_GET['viewclass'];
}

if (!IsSet ($student))
{
	print "<center><h3>" . $sas_lang['student_pages'] . ":</h3></center><p>";

	if (IsSet ($viewclass)) //is there a class selected
	{
		$query4 = "SELECT name FROM $table_classes WHERE id = '$viewclass'"; //get the class name
		$result4 = mysql_query($query4) or die_script($query4, mysql_error());
		$class_name = mysql_result($result4, 0);
		$query3 = "SELECT id FROM $table_users WHERE name='$class_name'"; // see if student with classname exists

		$result3 = mysql_query($query3) or die_script($query3, mysql_error());
		$class_name = stripslashes($class_name);
		if (mysql_num_rows($result3) > 0)
		{
			$classpageid = mysql_result($result3, 0);
		}
		else
		{
			$classpageid = "";
		}
		unset ($query3);
		unset ($result3);

		if ($classpageid != "")
		{
			$query3 = "SELECT studentid FROM $table_studentpages WHERE dir_number='$classpageid'";
			//get the studentid for the classpage
			$result3 = mysql_query($query3) or die_script($query3, mysql_error());
			if (mysql_num_rows($result3) > 0)
			{
				$pageid = mysql_result($result3, 0);
			}
			$name = htmlspecialchars(rawurlencode($class_name));
			print "<table><tr>
						   <td colspan=\"2\">
						   <a href=index.php?section=$section&page=$page&student=$pageid&name=$name style=\"text-decoration: underline;\">
							  <b>" . $sas_lang['class_page'] . " : $class_name</b></a>
						   <br><br>
						   </td>
						   </tr>";
		}
		else
		{

			print "<table border=\"0\">
							<tr>
							<td colspan=\"5\">
							<b>$class_name</b><br><br>
							</td>
							</tr>";
		}

		$query1 = "SELECT id, name, class FROM $table_users 
					  WHERE class= '$viewclass' OR class like '$viewclass,%' OR class like '%,$viewclass' 
					  AND (type = 1 OR type = 21 OR type = 2 OR type = 22) ORDER BY name";
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		while ($student_list = mysql_fetch_array($result1)) //list the students
		{
			$query2 = "SELECT studentid, title, dir_number FROM $table_studentpages 
								 WHERE dir_number='$student_list[0]' AND content != '' AND invisible = '0'
								 ORDER BY sort";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			$num_rows = mysql_num_rows($result2);

			if ($num_rows > 0)
			{

				if ($class_name != $student_list[1]) // leave the class page out of the list
				{
					print "<tr>
										  <td>&nbsp;&nbsp
										 </td>
										 <td>
										 $student_list[1]
										 </td>";
					while ($page_list = mysql_fetch_array($result2))
					{
						print "<td>&nbsp;</td><td><a href=index.php?section=$section&page=$page&student=$page_list[0]&viewclass=$viewclass>$page_list[1]</a></td>";
					}
					print "</tr>";
				}
			}

		}
		print "</table><br>";
	}
	$query = "SELECT id, name FROM $table_classes 
	          WHERE project = 0 ORDER BY name "; //get the classes
	$result = mysql_query($query) or die_script($query, mysql_error());

	while ($class_list = mysql_fetch_array($result)) // list the classes
	{
		$class_name = stripslashes($class_list[1]);
		if ($class_name != "--------")
		{
			if ($class_list[0] != "$viewclass")
			{
				print "<a href=index.php?section=$section&page=$page&viewclass=$class_list[0]><h4>$class_name</h4></a><p>";
			}
		}
	}
}
else
{
	$query = "SELECT title, dir_number, content FROM $table_studentpages WHERE studentid='$student'";
	$result = mysql_query($query) or die_script($query, mysql_error()); // getting the title and content
	$title = mysql_result($result, 0, 0);
	$studentid = mysql_result($result, 0, 1);
	$content = mysql_result($result, 0, 2);
	unset ($query);
	unset ($result);
	$query1 = "SELECT name FROM $table_users WHERE id='$studentid'"; // getting the student name
	$result1 = mysql_query($query1) or die_script($query1, mysql_error());
	$name = mysql_result($result1, 0, 0);
	unset ($query1);
	unset ($result1);
	$query2 = "SELECT studentid, title FROM $table_studentpages WHERE dir_number='$studentid' AND studentid!='$student' AND content != '' ORDER BY sort";
	$result2 = mysql_query($query2) or die_script($query2, mysql_error()); // getting the title of other pages
	$count = mysql_num_rows($result2);

	print "<center><h3>" . $sas_lang['student_header'] . " $name <p>" . $sas_lang['title'] . ": <b>$title</b></h3></center><p>";
	print "$content";

	print "<br><hr><table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\"><tr>";
	if ($count > 0)
	{
		print "<td>" . $sas_lang['other_pupilpages'] . " $name :</td>";
		while ($title_list = mysql_fetch_array($result2))
		{
			print "<td><a href=index.php?section=$section&page=$page&viewclass=$viewclass&student=$title_list[0]><b>$title_list[1]</b></a></td>";
		}
	}

	if ($viewclass != "")
	{
		print "<td align=\"right\"><a href=index.php?section=$section&page=$page&viewclass=$viewclass><< " . $sas_lang['goback'] . "</a></td></tr></table>";
	}
	else
	{
		print "<td align=\"right\"><a href=index.php?section=$section&page=$page><< " . $sas_lang['goback'] . "</a></td></tr></table>";
	}
}
?>