<?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)
############################################################################
# admin.php: last changed 06/07/2006  version 2.4.02
#              by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: admin.php,v 1.2 2006/10/15 19:08:54 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");
}
// initialize or check variables
if (IsSet ($_GET['createpage']))
{
	$createpage = $_GET['createpage'];
}
if (IsSet ($_GET['create']))
{
	$create = $_GET['create'];
}
if (IsSet ($_GET['deletepage']))
{
	$deletepage = $_GET['deletepage'];
}
if (IsSet ($_GET['updatepage']))
{
	$updatepage = $_GET['updatepage'];
}
if (IsSet ($_GET['viewpage']))
{
	$viewpage = $_GET['viewpage'];
}
if (IsSet ($_GET['modoption']))
{
	$modoption = $_GET['modoption'];
}
if (IsSet ($_GET['viewclass']))
{
	$viewclass = $_GET['viewclass'];
}
if (IsSet ($_GET['savestudent']))
{
	$savestudent = $_GET['savestudent'];
}
if (IsSet ($_GET['name']))
{
	$name = rawurldecode($_GET['name']);
}
$d = date("Y-m-d"); //todays date

$num_pages = $sas_config['number_pupilpages']; //get the max number of pupilpages

if (IsSet ($createpage)) //create a pupils page 
{
	$query2 = "SELECT pcheck, send_email FROM $table_studentpages WHERE dir_number='$createpage'"; //get number of pages
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$num_rows = mysql_num_rows($result2);

	$query = "SELECT type FROM $table_users WHERE id='$createpage'"; //get type of user pupil or teacher
	$result = mysql_query($query) or die_script($query, mysql_error());
	$type = mysql_result($result, 0);
	if (($type == "2") OR ($type == "22")) //user is teacher
	{
		$send = 0;
		$check = 0;
	}
	else //user is pupil
		{
		if ($num_rows > 0) //there are previous pages
		{
			$check = mysql_result($result2, 0, 'pcheck'); //get the previous pcheck and send_email values
			$send = mysql_result($result2, 0, 'send_email');
		}
		else
		{
			$check = 1; //use default 1     
			$send = 0;
		}
	}

	if ($modoption == "project")
	{
		mysql_query("INSERT INTO $table_studentpages VALUES ('', '', '', '$check', '0', '$send', '[$create]', '$createpage', '$d', '$create' , 0)");
	}
	else
	{
		if ($num_rows < $num_pages) //not more than the maximum defined pages.
		{
			mysql_query("INSERT INTO $table_studentpages VALUES ('', '', '', '$check', '0', '$send', '[$create]', '$createpage', '$d' , '$create' , 0)");
		}
	}

	$custompath = $serverpath . "/studentpages/$createpage";
	if (!is_dir($custompath))
	{
		mkdir($custompath,0755);				//create directory 
		chmod($custompath,octdec($perm));		//chmod it with permissions stored in configuration table.	
	}
}

if (IsSet ($deletepage))
{
	$query2 = "SELECT dir_number FROM $table_studentpages WHERE studentid='$deletepage'"; //get the dir_number
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$dir_number = mysql_result($result2, 0);
	unset ($query2);
	unset ($result2);
	mysql_query("DELETE FROM $table_studentpages WHERE studentid='$deletepage'"); //delete page from table
	$query2 = "SELECT * FROM $table_studentpages WHERE dir_number='$dir_number'"; //are there any pages left
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$num_rows = mysql_num_rows($result2);

	if ($num_rows == 0) //no pages left delete directory
	{
		$custompath = $serverpath . "/studentpages/$dir_number";
		if (is_dir($custompath))
		{
			if (is_empty_folder($custompath) === TRUE)
			{
				rmdir($custompath); //delete the directory
			}
			else
			{
				print "<script language=\"Javascript\">
								   <!--
								   alert (\"" . $sas_lang['directory'] . " studentpages/" . $dir_number . " " . $sas_lang['directory_not_empty'] . "\");
								   //-->
								   </script>";
			}
		}
	}
}

if (IsSet ($updatepage)) //approve the studentpage
{
	mysql_query("UPDATE $table_studentpages SET 
	             content = newcontent,
	             newcontent = '' 
	             WHERE studentid='$updatepage' AND newcontent <> ''");
}

//start of main page
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=modulemanager&module=$module>" . $sas_lang['student_pages'] . "</a></span><p>";
print "<span class=link><a href=index.php?option=modulemanager&module=$module&modoption=project>" . $sas_lang['student_projects'] . "</a></span><br>";
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

if (IsSet ($viewpage)) // view the studentpage 
{
	$query = "SELECT newcontent FROM $table_studentpages WHERE studentid='$viewpage'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$content = mysql_result($result, 0);
	unset ($query);
	unset ($result);

	if ($content == "") // newcontent is empty so we need to get the 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=\"link\">" . $sas_lang['student_header'] . " <b>$name</b></span>";
	$content = str_replace("../", "", $content);
	print "<table height=\"380\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\"><tr><td valign=\"top\" width=\"100%\">";
	print "<font size=2>$content</font>";
	print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
		   onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
	print "</td></tr></table>";
}
else
{

	if ($modoption == "project")
	{
		print "<span class=\"heading\">" . $sas_lang['student_projects'] . "<br></span>";
		print "<span class=\"text\">" . $sas_lang['student_pages_intro'] . "</span><br><br>";

		print "<table border=\"0\" width=\"90%\">
					<tr>
					   <td>&nbsp;</td>";

		$query1 = "SELECT id, name, class, type FROM $table_users WHERE type = 0 ORDER BY name"; //only get the project page
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		print "<td><span class=text><b>" . $sas_lang['student_project'] . ":</b></span></td>";

		$bgcolor = "E7E7EF";
		print "  <td><span class=text><b>" . $sas_lang['student_update'] . ":</b></span></td>
					   <td><span class=text><b>" . $sas_lang['student_view'] . ":</b></span></td>
					   <td><span class=text><b>" . $sas_lang['student_create_delete'] . ":</b></span></td>
					   <td><span class=text><b>" . $sas_lang['student_create_page'] . ":</b></span></td></tr>";

		while ($student_list = mysql_fetch_array($result1))
		{
			print " <tr bgcolor=\"$bgcolor\">
						   <td><a href=\"index.php?option=configuration&suboption=projects&edit_project=$student_list[2]\">
						   <img src=graphics/edit.gif alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\" border=\"0\"></a>";
			print "</td><td>";

			// block for creation of the student data
			$query2 = "SELECT studentid, newcontent, pcheck, title 
							 FROM $table_studentpages 
							 WHERE dir_number='$student_list[0]' ORDER BY title";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			$num_rows = mysql_num_rows($result2);

			unset ($newcontent_list); //clear the new contents array
			unset ($studentid_list);
			unset ($student_title);

			while ($row = mysql_fetch_array($result2))
			{
				$newcontent_list[] = $row['newcontent'];
				$studentid_list[] = $row['studentid'];
				$student_title[] = $row['title'];
			}

			$name = htmlspecialchars(rawurlencode($student_list[1]));
			print "<span class=link><a href=\"index.php?option=configuration&suboption=projects&edit_project=$student_list[2]\"
						 alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\">$student_list[1]</a></span>";
			if (($student_list[3] == "2") OR ($student_list[3] == "22"))
			{
				print "<font size=\"1\"> (*)</font>";
			}
			print "</td><td>";
			if ($num_rows > 0)
			{
				$i = 0;
				foreach ($newcontent_list as $c) //check if newcontent is filled.
				{
					if ($c != "")
					{
						print "<span class=link><a href=index.php?option=modulemanager&module=$module&modoption=project&updatepage=$studentid_list[$i]&viewclass=$viewclass
												alt=\"$student_title[$i]\" title=\"$student_title[$i]\" >";
						$k = $i +1;
						print "[$k] </a></span>";
					}
					$i++;
				}
				print "&nbsp;</td><td>";
				$i = 0;
				foreach ($studentid_list as $p)
				{
					print "<span class=link><a href=index.php?option=modulemanager&modoption=student&module=$module&modoption=project&viewpage=$studentid_list[$i]&name=$name
											alt=\"$student_title[$i]\" title=\"$student_title[$i]\" >";
					$i++;
					print "[$i] </a></span>";
				}
				print "</td><td>";
				$i = 0;
				foreach ($studentid_list as $p)
				{
					print "<span class=link><a href=index.php?option=modulemanager&modoption=project&module=$module&deletepage=$studentid_list[$i] 
										alt=\"$student_title[$i]\" title=\"$student_title[$i]\" onClick=\"return confirm('" . $sas_lang['page_delete_confirm'] . "')\">";
					$i++;
					print "[$i] </a></span>";
				}
			}
			else
			{
				print "<td>&nbsp;</td><td>&nbsp;</td>";
			}
			print "</td><td>";
			$i++;
			print "<span class=link><a href=index.php?option=modulemanager&modoption=project&module=$module&createpage=$student_list[0]&create=$i&viewclass=$viewclass>[" . $sas_lang['newpage'] . "]</a></span>";
			print "</td>";

			// end of block for creation of the student data		

			print "</td></tr>";

			if ($bgcolor == "#FFFFFF")
			{
				$bgcolor = "#E7E7EF";
			}
			else
			{
				$bgcolor = "#FFFFFF";
			}
		}
		print "</table><p>";
		print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
				onClick=\"javascript:parent.location='index.php?option=modulemanager';\" class=\"backbutton\">";
	}
	else
	{
		print "<span class=\"heading\">" . $sas_lang['student_pages'] . "<br></span>";
		print "<span class=\"text\">" . $sas_lang['student_pages_intro'] . "</span><br><br>";

		if (IsSet ($_GET['viewclass'])) //is there a class selected
		{
			$query = "SELECT name FROM $table_classes WHERE id = '$viewclass'"; //get the class name
			$result = mysql_query($query) or die_script($query, mysql_error());
			$class_name = stripslashes(mysql_result($result, 0, 0));
			print "<span class=link>$class_name</span><br><br>";
			print "<table border=\"0\" width=\"90%\">
							<tr>
							   <td>&nbsp;</td>";

			$query1 = "SELECT id, name, class, type FROM $table_users 
							WHERE class='$viewclass' AND (type = 1 OR type = 21 OR type = 2 OR type = 22) ORDER BY name";
			print "<td><span class=text><b>" . $sas_lang['students'] . ":</b></span></td>";

			$result1 = mysql_query($query1) or die_script($query1, mysql_error());

			$bgcolor = "E7E7EF";
			print "  <td><span class=text><b>" . $sas_lang['student_update'] . ":</b></span></td>
							   <td><span class=text><b>" . $sas_lang['student_view'] . ":</b></span></td>
							   <td><span class=text><b>" . $sas_lang['student_create_delete'] . ":</b></span></td>
							   <td><span class=text><b>" . $sas_lang['student_create_page'] . ":</b></span></td></tr>";
			while ($student_list = mysql_fetch_array($result1))
			{
				print " <tr bgcolor=\"$bgcolor\">
								   <td><img src=graphics/user.gif alt=\"$student_list[1]\">";
				print "</td><td>";

				// block for creation of the student data
				$query2 = "SELECT studentid, newcontent, pcheck, title 
									 FROM $table_studentpages 
									 WHERE dir_number='$student_list[0]' ORDER BY title";
				$result2 = mysql_query($query2) or die_script($query2, mysql_error());
				$num_rows = mysql_num_rows($result2);
				unset ($newcontent_list); //clear the new contents array
				unset ($studentid_list);
				unset ($student_title);
				while ($row = mysql_fetch_array($result2))
				{
					$newcontent_list[] = $row['newcontent'];
					$studentid_list[] = $row['studentid'];
					$student_title[] = $row['title'];
				}

				$name = htmlspecialchars(rawurlencode($student_list[1]));

				if (($student_list[3] == "2") OR ($student_list[3] == "22"))
				{
					print "<span class=link><a href=\"index.php?option=configuration&suboption=teachers&teacher=$student_list[0]&class1=3\"
										alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\">$student_list[1]</a></span>";
					print "<font size=\"1\"> (*)</font>";
				}
				else
				{
					print "<span class=link><a href=\"index.php?option=configuration&suboption=students&editstudent=$student_list[0]&class1=3\"
										alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\">$student_list[1]</a></span>";
				}
				print "</td><td>";

				if ($num_rows > 0) // we have at least 1 page
				{
					$i = 0;
					foreach ($newcontent_list as $c) //check if newcontent is filled.
					{
						if ($c != "")
						{
							print "<span class=link><a href=index.php?option=modulemanager&module=$module&updatepage=$studentid_list[$i]&viewclass=$viewclass
														alt=\"$student_title[$i]\" title=\"$student_title[$i]\" >";
							$k = $i +1;
							print "[$k] </a></span>";
						}
						$i++;
					}
					print "&nbsp;</td><td>";
					$i = 0;
					foreach ($studentid_list as $p)
					{
						print "<span class=link><a href=index.php?option=modulemanager&module=$module&viewclass=$viewclass&viewpage=$studentid_list[$i]&name=$name
													alt=\"$student_title[$i]\" title=\"$student_title[$i]\" >";
						$i++;
						print "[$i] </a></span>";

					}
					print "</td><td>";
					$i = 0;
					foreach ($studentid_list as $p)
					{
						print "<span class=link><a href=index.php?option=modulemanager&module=$module&deletepage=$studentid_list[$i]&viewclass=$viewclass 
												alt=\"$student_title[$i]\" title=\"$student_title[$i]\" onClick=\"return confirm('" . $sas_lang['page_delete_confirm'] . "')\">";
						$i++;
						print "[$i] </a></span>";
					}
					print "</td><td>";
					//if total pages is not equal to max pages so create links

					$k = $i +1; //add 1 for the next page to be created.

					if ($modoption == "project")
					{
						print "<span class=link><a href=index.php?option=modulemanager&modoption=project&module=$module&createpage=$student_list[0]&create=$k&viewclass=$viewclass>[" . $sas_lang['newpage'] . "]</a></span>";
					}
					else
					{
						if ($i < $num_pages)
						{
							print "<span class=link><a href=index.php?option=modulemanager&module=$module&createpage=$student_list[0]&create=$k&viewclass=$viewclass>[" . $sas_lang['newpage'] . "]</a></span>";
						}
					}
					print "</td>";
				}
				else //we have no pages , create a link to create them
					{
					print "<td colspan=\"2\">&nbsp;</td><td>";
					print "<span class=link><a href=index.php?option=modulemanager&module=$module&createpage=$student_list[0]&create=1&viewclass=$viewclass>[" . $sas_lang['newpage'] . "]</a></span>";
				}
				// end of block for creation of the student data		

				print "</td></tr>";

				if ($bgcolor == "#FFFFFF")
				{
					$bgcolor = "#E7E7EF";
				}
				else
				{
					$bgcolor = "#FFFFFF";
				}
			}
			print "<tr><td>&nbsp;</td><td colspan=\"4\"><span class=\"text\">(*) " . $sas_lang['teachers'] . "</span></td></tr></table><p>";
		}

		$query = "SELECT id, name FROM $table_classes  WHERE project = 0 ORDER BY ORD(name)"; //list normal classes.
		$result = mysql_query($query) or die_script($query, mysql_error());
		if ($result)
		{
			while ($class_list = mysql_fetch_array($result))
			{
				$class_name = stripslashes($class_list[1]);
				if ($class_list[0] != "$viewclass")
				{
					if ($class_name != "--------")
					{
						print "<span class=link><a href=index.php?option=modulemanager&module=$module&viewclass=$class_list[0]>$class_name </a></span><p>";
					}
				}
			}
		}
		print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
			onClick=\"javascript:parent.location='index.php?option=modulemanager';\" class=\"backbutton\">";
	}
}

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