<?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)
############################################################################
# con_projects.inc.php: last changed 04/10/2006 2.4.10                                        
#                       by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: con_projects.inc.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002, 2003, 2004 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");
}
?>
<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
	if (form.project_name.value == "")
    {
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.project_name.focus();
    return false ;
    }
	if ((document.form.send_email[0].checked==true) && (document.form.check[0].checked==true))
	{
	alert("<?php echo $sas_lang['student_project_warning'] ?>");
	return false ;
	}
  return true ;
}
</script>
<?php


if (IsSet ($_GET['edit_project']))
{
	$edit_project = $_GET['edit_project'];
}
if (IsSet ($_GET['save_project']))
{
	$save_project = $_GET['save_project'];
}
if (IsSet ($_GET['class1']))
{
	$class1 = $_GET['class1'];
}
if (IsSet ($_GET['delete_project']))
{
	$delete_project = $_GET['delete_project'];
}
if (IsSet ($_POST['check']))
{
	$check = $_POST['check'];
}
if (IsSet ($_POST['check1']))
{
	$check1 = $_POST['check1'];
}
if (IsSet ($_POST['send_email']))
{
	$send_email = $_POST['send_email'];
}
if (IsSet ($_POST['class1']))
{
	$class1 = $_POST['class1'];
}
if (IsSet ($_POST['project_name']))
{
	$project_name = addslashes($_POST['project_name']);
}
if (IsSet ($_POST['project']))
{
	$project = $_POST['project'];
}
if (IsSet ($_POST['title_id']))
{
	$title_id = $_POST['title_id'];
}
if (IsSet ($_POST['sort_id']))
{
	$sort_id = $_POST['sort_id'];
}
if (IsSet ($_POST['project_pages']))
{
	$project_pages = $_POST['project_pages'];
}
else
{
	$project_pages = $sas_config['number_pupilpages'];
}

$d = date("Y-m-d"); //todays date

if (IsSet ($save_project))
{
	$result = mysql_query("SELECT id FROM $table_classes WHERE name = '$project_name' AND id != '$save_project'");
	if (mysql_num_rows($result) == 0)
	{
		if ($save_project == "new")
		{
			mysql_query("INSERT INTO $table_classes (id, name, teachers, project) VALUES (NULL, '$project_name', '', '1')");
			//class is a project class, create a pupil with same name

			$result = mysql_query("SELECT id FROM $table_classes WHERE name = '$project_name'");
			$project_id = mysql_result($result, 0);
			mysql_query("INSERT INTO $table_users (id, name, username, password, type , class) 
									  VALUES (NULL, '$project_name', '$project_name', '$project_name', '0', '$project_id')");
			$result = mysql_query("SELECT id FROM $table_users WHERE name = '$project_name'"); //get the id just inserted
			$user_id = mysql_result($result, 0);

			for ($i = 1; $i <= $project_pages; $i++)
			{
				mysql_query("INSERT INTO $table_studentpages (studentid, content, newcontent, pcheck, approved, send_email, title, dir_number, change_date, sort, invisible)
							 VALUES ('', '', '', '$check', '', '$send_email', '[$i]', '$user_id', '$d', '$i' , '0')");
			}

			$custompath = $serverpath . "/studentpages/$user_id";

			if (!is_dir($custompath))
			{
				$perm = $sas_config['file_permissions'];
				mkdir($custompath, 0755);
				chmod($custompath, octdec($perm)); //chmod it with permissions stored in configuration table.
			}
		}
		else
		{
			mysql_query("UPDATE $table_users SET name='$project_name', username='$project_name', password='$project_name'
								   WHERE id='$_POST[project_id]'");
			mysql_query("UPDATE $table_classes SET name='$project_name'
								   WHERE id='$save_project'");

			$query = "SELECT id FROM $table_users WHERE class='$save_project'";
			$result = mysql_query($query) or die_script($query, mysql_error());
			$project_id = stripslashes(mysql_result($result, 0, 0));
			mysql_query("UPDATE $table_studentpages SET pcheck='$check',
																  send_email='$send_email' 
																  WHERE dir_number='$project_id'");
			foreach ($title_id as $key => $value)
			{
				mysql_query("UPDATE $table_studentpages SET title='$value' WHERE studentid = '$key'");
			}
			foreach ($sort_id as $key => $value)
			{
				mysql_query("UPDATE $table_studentpages SET sort='$value' WHERE studentid = '$key'");
			}
		}
	}
	else
	{
		print "<script language=\"Javascript\">
					   <!--
					   alert (\"" . $sas_lang['project_name_exists'] . "\");
					   history.go(-1) ;	
					   //-->
					   </script>";
	}
}

if (IsSet ($delete_project))
{
	mysql_query("UPDATE $table_users SET project = '' WHERE project = '$delete_project'"); //blank out project field for pupils
	$result = mysql_query("SELECT id FROM $table_users WHERE class = '$delete_project'"); //get the project id for dummy user
	$project_id = mysql_result($result, 0);

	mysql_query("DELETE FROM $table_studentpages WHERE dir_number = '$project_id'"); //delete the project pages
	mysql_query("DELETE FROM $table_users WHERE type = 0 AND class = '$delete_project'"); //delete dummy project user
	mysql_query("DELETE FROM $table_classes WHERE id = '$delete_project'"); //delete the project from classes table

	if (IsSet ($_GET['delete_project_files']))
	{
		$delete_project_files = $_GET['delete_project_files'];
	}
	else
	{
		$delete_project_files = "";
	}

	$project_path = $sas_config['serverpath'] . $sas_config['user_path'] . "/studentpages/" . $project_id . "/";

	if (file_exists($project_path)) //first check if the project directory exists
	{
		delete_files($project_id); //delete all the file in the project directory
		rmdir($project_path); //delete the project root directory

		//return to the projecs view
		print "<script language='javascript'>
				  window.location = 'index.php?option=configuration&suboption=projects'
				  </script>";
	}
}

if (IsSet ($edit_project))
{
	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");
	if ($edit_project == "new")
	{
		print "<span class=\"heading\">" . $sas_lang['student_projects'] . "</span><br>
					<span class=\"text\">" . $sas_lang['project_new'] . "<br></span>";
		print "<span class=text><FORM METHOD=\"POST\" NAME=\"form\" 
				ACTION=\"" . $_SERVER['PHP_SELF'] . "?option=configuration&suboption=projects&save_project=$edit_project\" onsubmit=\"return checkinput(this);\">";
		print "<p><br><b>" . $sas_lang['student_project'] . " (*):</b>&nbsp;
					<input type=text size=60 class=\"textfield\" name=\"project_name\" value=\"$project_name\">&nbsp;<p>";
		print "<br><table><tr><td><span class=text>" . $sas_lang['student_page_check'] . " </span></td>";
		print "<td><span class=text>" . $sas_lang['yes'] . "</span></td>";
		print "<td><input type=\"radio\" value=\"1\" name=\"check\"></td>";
		print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
		print "<td><input type=\"radio\" name=\"check\" value=\"0\" checked></td>";
		print "</tr><tr><td><span class=text>" . $sas_lang['student_page_email'] . " </span></td>";
		print "<td><span class=text>" . $sas_lang['yes'] . "</td>";
		print "<td><input type=\"radio\" value=\"1\" name=\"send_email\"></td>";
		print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
		print "<td><input type=\"radio\" name=\"send_email\" value=\"0\" checked></td></tr>";
		print "<tr><td colspan=\"5\">&nbsp;</td></tr>";
		print "<tr><td><span class=text>" . $sas_lang['number_of_project_pages'] . ":</span></td>";
		print "<td colspan=\"4\"><input type=text size=5 class=\"textfield\" name=\"project_pages\" value=\"$project_pages\"></td></tr>";
		print "<tr><td>&nbsp;</td></tr><tr><td><span class=\"text\">" . $sas_lang['required_fields'] . "</td></tr>";
		print "</table><br>";
		print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=configuration&suboption=projects';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"backbutton\" class=\"savebutton\">";
		print "</form></span>";
	}
	else
	{
		$query = "SELECT name, project FROM $table_classes WHERE id='$edit_project'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$project_name = stripslashes(mysql_result($result, 0, 0));
		$project = mysql_result($result, 0, 1);
		unset ($query);
		unset ($result);
		$query = "SELECT id FROM $table_users WHERE class='$edit_project'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$project_id = stripslashes(mysql_result($result, 0, 0));

		$query2 = "SELECT pcheck, send_email, title, studentid, dir_number, sort 
		               FROM $table_studentpages 
		               WHERE dir_number='$project_id' ORDER BY sort"; // get the check and email bit   
		$result2 = mysql_query($query2) or die_script($query2, mysql_error());

		if (mysql_num_rows($result2) != 0)
		{
			$studentpages_list = mysql_fetch_array($result2);
			$check = $studentpages_list[0];
			$send_email = $studentpages_list[1];
			$first_title = $studentpages_list[2];
			$first_id = $studentpages_list[3];
			$dir_number = $studentpages_list[4];
			$first_sort = $studentpages_list[5];
		}
		print "<span class=\"heading\">" . $sas_lang['student_projects'] . "</span><br>
		            <span class=\"text\">" . $sas_lang['edit'] . " " . $sas_lang['student_project'] . "<br></span>";
		print "<span class=text><FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=projects&save_project=$edit_project\"
					onsubmit=\"return checkinput(this);\">";
		print "<br><br><b>" . $sas_lang['student_project'] . ":</b>&nbsp;
		            <input type=text size=60 class=\"textfield\" name=\"project_name\" value=\"$project_name\">&nbsp;<br>\n";
		print "<input type=hidden size=5  name=\"project_id\" value=\"$project_id\">";
		print "<br><table><tr><td><span class=text>" . $sas_lang['student_page_check'] . " </span></td>";
		print "<td><span class=text>" . $sas_lang['yes'] . "</span></td>";
		if ($check == "1")
		{
			print "<td><input type=\"radio\" value=\"1\" name=\"check\" checked></td>";
			print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
			print "<td><input type=\"radio\" name=\"check\" value=\"0\"></td>";
		}
		else
		{
			print "<td><input type=\"radio\" value=\"1\" name=\"check\"></td>";
			print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
			print "<td><input type=\"radio\" name=\"check\" value=\"0\" checked></td>";
		}
		print "</tr><tr><td><span class=text>" . $sas_lang['student_page_email'] . " </span></td>";
		print "<td><span class=text>" . $sas_lang['yes'] . "</td>";

		if ($send_email == "1")
		{
			print "<td><input type=\"radio\" value=\"1\" name=\"send_email\" checked></td>";
			print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
			print "<td><input type=\"radio\" name=\"send_email\" value=\"0\"></td>";
		}
		else
		{
			print "<td><input type=\"radio\" value=\"1\" name=\"send_email\"></td>";
			print "<td><span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span></td>";
			print "<td><input type=\"radio\" name=\"send_email\" value=\"0\" checked></td>";
		}

		print "<tr><td valign=\"top\"><table>";

		if (mysql_num_rows($result2) != 0)
		{
			print "<tr><td colspan=\"5\">&nbsp;</td></tr><tr><td colspan=\"5\" valign=\"top\">
							<span class=text>" . $sas_lang['student_project_page_names'] . ":</span></td>
							<td><span class=text>" . $sas_lang['order'] . ":</span></td></tr>";
			print "<tr><td colspan=\"5\"><span class=text>1 :&nbsp;</span>
							<input type=text size=20 class=\"textfield\" name=\"title_id[$first_id]\" value=\"$first_title\"></td>
							<td align=\"right\"><input type=text size=4 class=\"textfield\" name=\"sort_id[$first_id]\" value=\"$first_sort\"></td></tr>";
		}
		$i = 2;
		while ($project_titles = mysql_fetch_array($result2))
		{
			print "<tr><td colspan=\"5\"><span class=text>$i :&nbsp;</span>
							<input type=text size=20 class=\"textfield\" name=\"title_id[$project_titles[3]]\" value=\"$project_titles[2]\"></td>
							<td align=\"right\"><input type=text size=4 class=\"textfield\" name=\"sort_id[$project_titles[3]]\" value=\"$project_titles[5]\"></td></tr>";
			$i++;
		}

		print "</table><td>&nbsp;</td><td colspan=\"3\" valign=\"top\"><table>
					<tr><td>&nbsp;</td></tr>
					<tr><td><span class=text>" . $sas_lang['student_project_members'] . "</span></td></tr>";
		$query3 = "SELECT name, type FROM $table_users 
						WHERE (project LIKE '%,$edit_project,%' OR project LIKE '$edit_project,%' OR project LIKE '%,$edit_project' OR project LIKE '$edit_project')
						AND (type = 1 OR type = 21 OR type = 2 OR type = 22) ORDER BY name"; //get project members
		$result3 = mysql_query($query3) or die_script($query3, mysql_error());

		if (mysql_num_rows($result3) > 0)
		{
			while ($project_list = mysql_fetch_array($result3))
			{
				print "<tr><td><span class=text><b>" . $project_list[0] . "</b>";
				if (($project_list[1] == 2) OR ($project_list[1] == 22))
				{
					print " (*)";
				}
				print "</span></td></tr>";
			}
		}

		$query3 = "SELECT id FROM $table_modules WHERE location = 'sn_studentpages'";
		$result3 = mysql_query($query3) or die_script($query3, mysql_error());
		$module_id = mysql_result($result3, 0); //get the module id for the link

		print "<tr><td><br><span class=text><b>(*) </b>" . $sas_lang['teachers'] . "</span></td></tr></table></td></tr></table><br>";
		print "<p><span class=text>" . $sas_lang['student_directory'] . " <b>$dir_number</b></span>&nbsp;<span class=link>
				<a href=\"index.php?option=modulemanager&module=$module_id&modoption=project\">(" . $sas_lang['student_project_page_names'] . ")</a></span><br><br>";
		print "\n<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=configuration&suboption=projects';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"backbutton\" class=\"savebutton\">";
		print "</form></span>";
	}

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

// list of projecs 
include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

if (substr("$access", 8, 1) == "0") //bit 9 off the accessrights
{
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}
print "<span class=\"heading\">" . $sas_lang['student_projects'] . "<br></span>";
print "<span class=\"text\">" . $sas_lang['projects_introduction'] . "</span><br><br>";

$query = "SELECT id, name FROM $table_classes WHERE project = 1 ORDER BY ORD(name)";
$result = mysql_query($query) or die_script($query, mysql_error());
if ($result)
{
	while ($project_list = mysql_fetch_array($result))
	{
		$project_name = stripslashes($project_list[1]);

		if ($highvisibility == "1")
		{
			print "<a href=\"index.php?option=configuration&suboption=projects&delete_project=$project_list[0]\" onClick=\"return confirm('" . $sas_lang['project_delete_confirm'] . "')\">[" . $sas_lang['delete'] . "]</a>";
		}
		else
		{
			print "<a href=\"index.php?option=configuration&suboption=projects&delete_project=$project_list[0]\" onClick=\"return confirm('" . $sas_lang['project_delete_confirm'] . "')\">
						   <img border=\"0\" src=\"graphics/delete.gif\" width=\"16\" alt=\"" . $sas_lang['delete'] . "\" height=\"16\" ></a>";
		}

		if ($highvisibility == "1")
		{
			print "<a href=\"index.php?option=configuration&suboption=projects&edit_project=$project_list[0]\">[" . $sas_lang['edit'] . "]</a>";
		}
		else
		{
			print "<span class=link><a href=index.php?option=configuration&suboption=projects&edit_project=$project_list[0]>
						 <img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" ></a>";
		}
		print "&nbsp;<a href=index.php?option=configuration&suboption=projects&&edit_project=$project_list[0]>$project_name</a></span><p>";

	}
}

print "<br><table><tr><td>&nbsp;</td><td><img src=graphics/folder_closed.gif alt=\"" . $sas_lang['project_new'] . "\"></td>
       <td><a href=\"index.php?option=configuration&suboption=projects&edit_project=new\"><span class=link>" . $sas_lang['project_new'] . "</span></a></td>
       </tr></table>";
print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
       onClick=\"parent.location='index.php?option=configuration'\" class=\"backbutton\">";

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