<?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)
############################################################################
# students.php: last changed 09/02/2006 2.4
#               by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: student.php,v 1.1 2006/10/03 10:47:25 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 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");
}

$height = $sas_config['htmlarea_editor_rows'] * 20 + 130;

print "<script type=\"text/javascript\">
function NewWindow (link) 
{
var win_left = (screen.width - 780) / 2;
var win_top = (screen.height - $height) / 2;
  window1 = window.open(link, \"Pupileditor\", \"width=780,height=$height,resizable=yes,scrollbars=no\");
  window1.moveTo(win_left,win_top)
  window1.focus();
}
</script>";

if (IsSet ($userid) AND $usertype == "student")
{
	if (IsSet ($_GET[release])) //if pupil presses cancel release the page
	{
		mysql_query("UPDATE $table_studentpages SET approved = '' WHERE studentid='$_GET[release]'");
	}
	$query = "SELECT name, project FROM $table_users WHERE id='$userid'"; // getting the studentname
	$result = mysql_query($query) or die_script($query, mysql_error());
	$student_name = mysql_result($result, 0, 0);
	$student_project = mysql_result($result, 0, 1);
	$project_array = explode(",", $student_project); //create an array
	unset ($query);
	unset ($result);

	switch ($option) // start of switch
	{
		default : // welcome screen

			print $sas_lang['welcome'] . " <b>" . $student_name . "</b>.&nbsp;" . $sas_lang['logged_in'] . ".<br><br>";
			print "<table cellpadding=\"5\">";

			if ($student_project != "") //do we have a project(s)
			{
				foreach ($project_array as $student_project)
				{
					$query = "SELECT id FROM $table_users WHERE class = '$student_project' AND type = '0'";
					$result3 = mysql_query($query); //get pupil connected to project
					if (mysql_num_rows($result3) > 0)
					{
						$project_id = mysql_result($result3, 0, 0);
						$query = "SELECT studentid, title FROM $table_studentpages WHERE dir_number = '$project_id' ORDER BY sort"; //get the project pages
						$result2 = mysql_query($query);
						$num_rows = mysql_num_rows($result2);

						$query = "SELECT name FROM $table_classes WHERE id = '$student_project'";
						$result4 = mysql_query($query);
						$project_name = mysql_result($result4, 0);
						print "<tr><td>" . $sas_lang['student_project'] . "</td><td>:</td><td colspan=\"3\" class=\"sas_header4\">
										 $project_name</td></tr>
											<tr><td>" . $sas_lang['student_view'] . "</td><td>:</td>\n";
						unset ($title_list1);
						unset ($studentid_list1);
						while ($row = mysql_fetch_array($result2))
						{
							$title_list1[] = $row['title'];
							$studentid_list1[] = $row['studentid'];
						}
						if ($num_rows > 0)
						{
							$i = 0;
							$j = 0;
							foreach ($title_list1 as $title)
							{
								print "<td><a href=index.php?section=$section&page=$page&option=viewpage&viewpage=$studentid_list1[$i]>$title </a></td>\n";
								$i++;
								$j++;
								if ($j > 6)
								{
									print "</tr><tr><td>&nbsp;</td>";
									$j = 0;
								}
							}
							print "<tr><td>" . $sas_lang['page_edit'] . "</td><td>:</td>";
							$i = 0;
							$j = 0;
							foreach ($title_list1 as $title)
							{
								//print "<td><font size=2><a href=index.php?section=$section&page=$page&option=editpage&editpage=$studentid_list1[$i]>$title </a></font></td>\n";
								print "<td><a href=\"javascript:NewWindow('" . $sas_config['url'] . "$cmsdir/editors/fckeditor/studenteditor.php?editpage=$studentid_list1[$i]')\">$title </a></td>\n";
								$i++;
								$j++;
								if ($j > 6)
								{
									print "</tr><tr><td>&nbsp;</td>";
									$j = 0;
								}
							}
						}
						print "</tr></table><hr><table cellpadding=\"5\">";
					}
				}
			}
			print "<tr><td class=\"sas_header4\">" . $sas_lang['student_pages'] . ":</td></tr><tr>";
			print "<tr><td>" . $sas_lang['student_view'] . " </td><td>:</td>";

			$query = "SELECT studentid, title FROM $table_studentpages WHERE dir_number = '$userid' ORDER BY sort"; //get the pupil pages
			$result2 = mysql_query($query);
			$num_rows = mysql_num_rows($result2);

			while ($row = mysql_fetch_array($result2))
			{
				$title_list2[] = $row['title'];
				$studentid_list2[] = $row['studentid'];
			}
			if ($num_rows > 0)
			{
				$i = 0;
				foreach ($title_list2 as $title)
				{
					print "<td><a href=index.php?section=$section&page=$page&option=viewpage&viewpage=$studentid_list2[$i]>$title </a></td>\n";
					$i++;
				}
				print "<tr><td>" . $sas_lang['page_edit'] . "</td><td>:</td>";
				$i = 0;
				foreach ($title_list2 as $title)
				{
					//print "<td><font size=2><a href=index.php?section=$section&page=$page&option=editpage&editpage=$studentid_list2[$i]>$title </a></font></td>";
					print "<td><a href=\"javascript:NewWindow('" . $sas_config['url'] . "$cmsdir/editors/fckeditor/studenteditor.php?editpage=$studentid_list2[$i]')\">$title </a></td>";
					$i++;
				}
			}
			else
			{
				print "</tr><tr><td width=40% align='left'>" . $sas_lang['student_no_page'] . "</td>";
			}
			print "</tr></table>";
			print "<br><br><a href=index.php?section=$section&page=$page&logout=yes>
			            <b><< " . $sas_lang['logout'] . " <b>" . $student_name . "</b> >>&nbsp;</b></a>";

			// <p><a href=\"index.php?section=$section&page=$page&option=profile\">Profile</a>" ;

			break;

			// case profile:
			//	include("$cmsdir/modules/sn_studentpages/profile.php") ;
			// break;

		case viewpage :
			$query = "SELECT title, newcontent FROM $table_studentpages WHERE studentid='$viewpage'"; // getting the newcontent
			$result = mysql_query($query) or die_script($query, mysql_error()); // and title
			$title = mysql_result($result, 0, 0);
			$content = mysql_result($result, 0, 1);
			unset ($query);
			unset ($result);

			if ($content == "")
			{
				$query = "SELECT content FROM $table_studentpages WHERE studentid='$viewpage'"; // getting the content
				$result = mysql_query($query) or die_script($query, mysql_error());
				$content = mysql_result($result, 0);
				unset ($query);
				unset ($result);
			}
			print "<span class=\"heading\"><center>" . $sas_lang['student_header'] . " <b>$student_name</b> <p>" . $sas_lang['title'] . ": <b>$title</b></center></span><p>";
			print "$content";
			print "<br><br><span class=\"link\"><a href=index.php?section=$section&page=$page><< " . $sas_lang['goback'] . "</a></span><br>";

			break;
	}
}
?>
