<?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, 1.2, 2.0, 2.1): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# viewpage.php: last changed 20/11/2004 2.3
#               by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: viewpage2.php,v 1.1 2006/10/03 10:47:24 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))
{
	$query = "SELECT id FROM $table_classes WHERE name = '$pagetitle'"; //get the classid for the class with pagename
	$result = mysql_query($query) or die_script($query, mysql_error());
	if (mysql_num_rows($result) > 0)
	{
		$classid = mysql_result($result, 0);
		print "<center><h3>" . $sas_lang['student_pages'] . ":</h3></center><p>";

		$query3 = "SELECT id FROM $table_users WHERE name='$pagetitle'"; // see if student with classname exists
		$result3 = mysql_query($query3) or die_script($query3, mysql_error());

		if (mysql_num_rows($result3) > 0)
		{
			$classpageid = mysql_result($result3, 0);
		}
		else
		{
			$classpageid = "";
		}
		unset ($query3);
		unset ($result3);
		print "<table border=\"0\"><tr><td>";
		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($pagetitle));
			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'] . " : $pagetitle</b></a>
			    	       <br><br>
			    	       </td>
			    	       </tr>";
		}

		print "<table border=\"0\"><tr>";
		$query1 = "SELECT id, name, class FROM $table_users 
		                WHERE (class= '$classid' OR class like '$classid,%' OR class like '%,$classid') AND (type = 2 OR type = 22) 
		                ORDER BY name";
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());

		$i = 0;
		while ($student_list = mysql_fetch_array($result1)) //list the teachers
		{
			$query2 = "SELECT studentid, title, dir_number FROM $table_studentpages 
			                   WHERE dir_number='$student_list[0]' AND (pcheck = 0 OR pcheck = 2) AND invisible = '0' AND content != ''
			                   ORDER BY sort";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			$num_rows = mysql_num_rows($result2);

			if ($num_rows > 0)
			{
				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]>$page_list[1]</a></td>";
				}
				print "</tr>";
				$i++;
			}
		}
		if ($i > 0)
		{
			print "</table><tr><td><hr color=\"black\" width=\"100%\" size=\"1\" /></td></tr><tr><td><table border=\"0\">";
		}
		else
		{
			print "</table><tr><td><table border=\"0\">";
		}
		$query1 = "SELECT id, name, class FROM $table_users WHERE class='$classid' AND (type = 1 OR type = 21) 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 invisible = '0' AND content != '' ORDER BY sort";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			$num_rows = mysql_num_rows($result2);
			if ($num_rows > 0)
			{
				if ($student_list[1] != $pagetitle) // 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]>$page_list[1]</a></td>";
					}
					print "</tr>";
				}
			}
		}
		print "</table></td></tr></table><br>";
	}
	else
	{
		print $sas_lang['class_no_linkpage'];
	}
}

else
{
	$query = "SELECT title, dir_number, content FROM $table_studentpages WHERE studentid='$student'"; // getting the title and content
	$result = mysql_query($query) or die_script($query, mysql_error());
	$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 ($query);
	unset ($result);
	$query2 = "SELECT studentid, title FROM $table_studentpages WHERE dir_number='$studentid' AND studentid!='$student'  AND content != '' ORDER BY sort"; // getting the title of other pages
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$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>";
		}
	}
	print "<td align=\"right\"><a href=index.php?section=$section&page=$page&viewclass=$viewclass><< " . $sas_lang['goback'] . "</a></td></tr></table>";
}
?>