<?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)
############################################################################
# viewproject.php: last changed 09/04/05 2.4 
#                by Alain Riffart (gnuoulipo@free.fr), Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: viewproject.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");
}

$n0 = "\n";
$n1 = "\n\t";
$n2 = "\n\t\t";
$n3 = "\n\t\t\t";
$n4 = "\n\t\t\t\t";
$n5 = "\n\t\t\t\t\t";
$n6 = "\n\t\t\t\t\t\t";

function get_boss()
{
	global $classid, $table_users;
	$query = "SELECT name FROM $table_users 
			WHERE (project LIKE '%,$classid,%' OR project like '$classid,%' OR project like '%,$classid' OR project like '$classid')
			AND (type = 2 OR type = 22)";
	$result = mysql_query($query) or die_script($query, mysql_error()); //get teachers for the project
	if (!$result)
	{
		$boss = "";
	}
	else
	{
		if (mysql_num_rows($result) > 0)
		{
			while ($boss_list = mysql_fetch_array($result))
			{
				$boss .= $boss_list[0] . ", ";
			}
			$boss = substr($boss, 0, -2);
		}
		else
		{
			$boss = "";
		}
	}
	return $boss;
}

function head()
{
	global $pagetitle, $n1, $n2, $n3, $sas_lang;
	print "<table width=\"100%\">" . $n1;
	print "<tr align=\"center\">" . $n2;
	print "<td width=\"10\">&nbsp;</td>" . $n3;
	print "<td width=\"85%\" valign=\"top\"><h3>" . $sas_lang['student_project'] . " " . $pagetitle . "</h3>" . $n3;
	print "</td>" . $n3;
	print "<td width=\"15%\"><a name=\"top\"><a href=\"#bottom\">" . $sas_lang['student_project_down'] . "</a></a>" . $n2;
	print "</td>" . $n1;
	print "</tr>" . $n1;
}

function pagecontent()
{
	global $content, $boss, $n2, $n3, $n4, $n5, $n6, $sas_lang;
	print "<tr>" . $n2;
	print "<td width=\"10\">&nbsp;</td>" . $n3;
	print "<td width=\"85%\" valign=\"top\">" . $content . "</td>" . $n2;
	print "<td width=\"12%\" valign=\"top\">" . $n3;
	print "<table width=\"100%\">" . $n4;
	print "<tr>" . $n5;
	print "<td align=\"center\">" . $n6;
	print "<strong>" . $sas_lang['student_project_index'] . "</strong>" . $n5;
	print "</td>" . $n4;
	print "</tr>" . $n4;
}

function sort_pages($classid)
{
	global $classid, $table_users, $table_studentpages, $pagetitle, $name, $sas_lang;
	$query1 = "SELECT id, name, class FROM $table_users WHERE class='$classid' AND (type = 1 OR type = 0 OR type = 21) ORDER BY name";
	$result1 = mysql_query($query1) or die_script($query1, mysql_error());
	$i = 0;
	while ($student_list = mysql_fetch_array($result1))
	{
		$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)
		{
			while ($page_list = mysql_fetch_array($result2))
			{
				$pagelist[$i] = $page_list[1] . "_" . $page_list[0];
				$i++;
			}
		}
	}
	if ($pagelist)
	{
		//sort ($pagelist);
		return $pagelist;
	}
	else
	{
		$name = $sas_lang['student_project_nostudent'] . $pagetitle;
		foot($name);
		exit;
	}

}

function index()
{
	global $pagelist, $current_title, $section, $page, $n4, $n5, $n6;
	$nb = count($pagelist);
	for ($i = 0; $i < $nb; $i++)
	{
		$ref = explode("_", $pagelist[$i]);
		if (count($ref) == 3)
		{
			$ref[0] = $ref[1];
			$ref[1] = $ref[2];
		}
		if ($current_title == $ref[0])
		{
			print "<tr bgcolor=\"eaeaea\">" . $n5;
		}
		else
		{
			print "<tr>" . $n5;
		}
		print "<td height=\"10%\">" . $n6;
		print "<a href=index.php?section=$section&page=$page&student=$ref[1]>$ref[0]</a>" . $n5;
		print "</td>" . $n4;
		print "</tr>" . $n4;
	}
}

function end_table()
{
	global $n1, $n2, $n1;
	print "</table>" . $n3;
	print "</td>" . $n2;
	print "</tr>" . $n1;
}

function foot($name)
{
	global $name, $boss, $sas_lang, $n1, $n2, $n3;
	$name = $name . "&nbsp;&nbsp;&nbsp;&nbsp;<br>" . $sas_lang['student_project_boss'] . $boss;
	print "</td>" . $n1;
	print "</tr>" . $n1;
	print "<tr>" . $n2;
	print "<td width=\"10\">&nbsp;</td>" . $n3;
	print "<td align=\"center\">" . $name . $n2;
	print "</td>" . $n2;
	print "<td align=\"center\"><a name=\"bottom\"><a href=\"#top\">" . $sas_lang['student_project_up'] . "</a></a>" . $n2;
	print "</td>" . $n1;
	print "</tr>" . $n0;
	print "</table><br>";
}

function get_content()
{
	global $student, $pagelist, $table_studentpages, $table_users, $content, $current_title, $classid, $name, $sas_lang;
	$query = "SELECT title, dir_number, content FROM $table_studentpages WHERE studentid = '$student'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	if (mysql_num_rows($result) > 0)
	{
		$title = mysql_result($result, 0, 0);
		$sep = explode("_", $title);
		if (count($sep) == 2)
		{
			$sep[0] = $sep[1];
		}
		$current_title = $sep[0];
		$userid = mysql_result($result, 0, 1);
		$content = mysql_result($result, 0, 2);
		unset ($query);
		unset ($result);
		if ($content != "")
		{
			pagecontent($content);
			index($pagelist);
			end_table();
			$query1 = "SELECT name FROM $table_users 
							WHERE (type = 1 OR type = 21) AND 
							(project LIKE '%,$classid,%' OR project like '$classid,%' OR project like '%,$classid' OR project like '$classid')";
			$result1 = mysql_query($query1) or die_script($query1, mysql_error()); //get the project members
			while ($member_list = mysql_fetch_array($result1))
			{
				$members .= $member_list[0] . ", ";
			}
			$members = substr($members, 0, -2);
			$name = $sas_lang['student_project_edit'] . "<br>" . $members;
			unset ($query);
			unset ($result);
			foot($name);
		}
		else
		{
			$name = $sas_lang['student_project_empty'];
			foot($name);
		}
	}
	else
	{
		$name = $sas_lang['student_project_noedit'] . $student;
		foot($name);
	}
}

if (IsSet ($_GET['student']))
{
	$student = $_GET['student'];
}
if (IsSet ($_GET['viewclass']))
{
	$viewclass = $_GET['viewclass'];
}

$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());
$nbrows = mysql_num_rows($result);
if ($nbrows > 0)
{
	$classid = mysql_result($result, 0);
	$boss = get_boss();
}
else
{
	$classid = 0;
}

head();

if (!IsSet ($student))
{
	if ($classid)
	{
		$pagelist = sort_pages($classid);
		$ref = explode("_", $pagelist[0]);
		if (count($ref) == 3)
		{
			$ref[0] = $ref[1];
			$ref[1] = $ref[2];
		}
		$student = $ref[1];
		get_content();
	}
	else
	{
		$name = $sas_lang['class_no_linkpage'];
		foot($name);
	}
}
else
{
	$pagelist = sort_pages($classid);
	get_content();
}
?>
