<?php
	require("settings.php");
	require_once("helper.php");

	session_start();
	html_start("Tests");

	if(!session_is_registered("id") || $_SESSION["id"] == "") {
		echo "Not logged in";
		login_form("index.php");
		die();
	}

	echo "<H1>Test Managment</H1>\n";

	$link = db_connect();
	$todo = $_REQUEST["todo"];

	if($todo == "add") {
		$tid = intval($_REQUEST["tid"]);
		echo "<H3>Add test</H3>\n";
		echo "<FORM action=\"tests.php\" method=\"post\">\n";
		echo "<INPUT type=\"hidden\" name=\"testno\" value=\"0\">\n";
		echo "<INPUT type=\"hidden\" name=\"todo\" value=\"save\">\n";
		echo "<LABEL for=\"tid\">To task:</LABEL><BR>\n";
		echo "<SELECT name=\"tid\" size=\"1\">\n";
		$query = "SELECT tid, title FROM tasks ORDER BY tid";
		$result = mysql_query($query, $link) or die("Can't get tasks: ".mysql_error($link));
		while($line = mysql_fetch_array($result)) {
			echo "<OPTION value=\"{$line["tid"]}\"";
			if($tid == $line["tid"]) {
				echo " selected=\"selected\"";
			}
			echo ">{$line["title"]}</OPTION>\n";
		}
		echo "</SELECT><BR>\n";
		echo "<LABEL for=\"input\">Input:</LABEL><BR>\n";
		echo "<INPUT type=\"text\" name=\"input\" id=\"input\" size=\"100\"><BR>\n";
		echo "<LABEL for=\"inputfile\">Inputfile:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"inputfile\" id=\"inputfile\" rows=\"5\" cols=\"100\">\n";
		echo "</TEXTAREA><BR>\n";
		echo "<LABEL for=\"input\">Output:</LABEL><BR>\n";
		echo "<INPUT type=\"text\" name=\"output\" id=\"output\" size=\"100\"><BR>\n";
		echo "<LABEL for=\"outputfile\">Outputfile:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"outputfile\" id=\"outputfile\" rows=\"5\" cols=\"100\">\n";
		echo "</TEXTAREA><BR>\n";
		echo "<LABEL for=\"policy\">Policy:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"policy\" id=\"policy\" rows=\"5\" cols=\"100\">\n";
		echo "</TEXTAREA><BR>\n";
		echo "<INPUT type=\"submit\" value=\"Save test\">\n";
		echo "</FORM>\n";
	} else if($todo == "rm") {
		$testno = intval($_REQUEST["testno"]);
		$query = "SELECT tid, input, inputfile, output, outputfile FROM tests WHERE testno = $testno";
		$result = mysql_query($query, $link) or die("Can't fetch test: ".mysql_error($link));
		$num = mysql_num_rows($result);
		if($num < 1) {
			echo "Can't find test.";
		} else if($num > 1) {
			echo "Testno not unique.";
		} else {
			$line = mysql_fetch_array($result);
			echo "Do you really want to delete the test <B>$testno</B>?<BR>\n";
			if($line["input"] == "" ) {
				echo "empty input ";
			} else {
				echo "{$line["input"]} ";
			}
			if($line["inputfile"] != "" ) {
				echo "+ inputfile ";
			}
			if($line["output"] == "") {
				echo "empty output ";
			} else {
				echo "[{$line["output"]} ";
			}
			if($line["outputfile"] != "" ) {
				echo "+ outputfile";
			}
			echo " \n";
			echo "<FORM action=\"tests.php\" method=\"post\">\n";
			echo "<INPUT type=\"hidden\" name=\"testno\" value=\"$testno\">\n";
			echo "<INPUT type=\"hidden\" name=\"todo\" value=\"reallyrm\">\n";
			echo "<INPUT type=\"submit\" value=\"Yes\">\n";
			echo "</FROM>\n";
			echo "<A href=\"assignment.php?tid={$line["tid"]}\"><BUTTON type=\"button\">No</BUTTON></A>\n";
		}
	} else if($todo == "reallyrm") {
		$testno = intval($_REQUEST["testno"]);
		$query = "SELECT tid FROM tests WHERE testno = $testno";
		$result = mysql_query($query, $link) or die("Can't delete test: ".mysql_error($link));
		$num = mysql_num_rows($result);
		if($num < 1) {
			echo "Test not found<BR>\n";
		} else if($num > 1) {
			echo "Testno not unique<BR>\n";
		} else {
			$line = mysql_fetch_array($result);
			$tid = $line["tid"];
			$query = "DELETE FROM tests WHERE testno = $testno";
			$result = mysql_query($query, $link) or die("Can't delete test: ".mysql_error($link));
			$num = mysql_affected_rows($link);
		}

		if($num != 1) {
			echo "Deleting failed<BR>\n";
		} else {
			echo "Finished deleting<BR>\n";
		}
		if($tid != 0) {
			echo "<BR><BR><A href=\"assignment.php?tid=$tid\">View Task</A>\n";
		} else {
			echo "<BR><BR><A href=\"tasks.php\">View Tasks</A>\n";
		}
	} else if ($todo == "edit") {
		$testno = intval($_REQUEST["testno"]);
		$query = "
			SELECT u.tid, u.title, t.input, t.inputfile, t.output, t.outputfile, t.policy
			FROM tests t, tasks u
			WHERE t. testno=$testno AND t.tid = u.tid";
		$result = mysql_query($query, $link) or die("Can't fetch test: ".mysql_error($link));
		$num = mysql_num_rows($result);
		if($num != 1) {
			die("Testno doesn't belong to one test");
		}
		$line = mysql_fetch_array($result);
		echo "Test belongs to: {$line["title"]}<BR>\n";
		echo "<FORM action=\"tests.php\" method=\"post\">\n";
		echo "<INPUT type=\"hidden\" name=\"testno\" value=\"$testno\">\n";
		echo "<INPUT type=\"hidden\" name=\"tid\" value=\"{$line["tid"]}\">\n";
		echo "<INPUT type=\"hidden\" name=\"todo\" value=\"save\">\n";
		echo "<LABEL for=\"input\">Input:</LABEL><BR>\n";
		$input = make_printable($line["input"]);
		echo "<INPUT type=\"text\" name=\"input\" id=\"input\" value=\"$input\" size=\"100\"><BR>\n";
		echo "<LABEL for=\"inputfile\">Inputfile:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"inputfile\" id=\"inputfile\" rows=\"5\" cols=\"100\">\n";
		echo $line["inputfile"];
		echo "</TEXTAREA><BR>\n";
		echo "<LABEL for=\"input\">Output:</LABEL><BR>\n";
		$output = make_printable($line["output"]);
		echo "<INPUT type=\"text\" name=\"output\" id=\"output\" value=\"$output\" size=\"100\"><BR>\n";
		echo "<LABEL for=\"outputfile\">Outputfile:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"outputfile\" id=\"outputfile\" rows=\"5\" cols=\"100\">\n";
		echo $line["outputfile"];
		echo "</TEXTAREA><BR>\n";
		echo "<LABEL for=\"policy\">Policy:</LABEL><BR>\n";
		echo "<TEXTAREA name=\"policy\" id=\"policy\" rows=\"5\" cols=\"100\">\n";
		echo $line["policy"];
		echo "</TEXTAREA><BR>\n";
		echo "<INPUT type=\"submit\" value=\"Save test\">\n";
		echo "</FORM>\n";
	} else if ($todo == "save") {
		$tid = intval($_REQUEST["tid"]);
		$testno = intval($_REQUEST["testno"]);
		$input = mysql_real_escape_string(make_unprintable($_REQUEST["input"]), $link);
		$inputfile = mysql_real_escape_string($_REQUEST["inputfile"], $link);
		$output = mysql_real_escape_string(make_unprintable($_REQUEST["output"]), $link);
		$outputfile = mysql_real_escape_string($_REQUEST["outputfile"], $link);
		$policy = mysql_real_escape_string($_REQUEST["policy"], $link);

		if($testno == 0) {
			$query = "SELECT MAX(testno) AS max FROM tests";
			$result = mysql_query($query, $link) or die("Can't get next testno: ".mysql_error($link));
			$num = mysql_num_rows($result);
			if($num != 1) {
				$testno = 2;
			} else {
				$line = mysql_fetch_array($result);
				$testno = $line["max"] + 1;
			}
			$query = "INSERT INTO tests SET tid=$tid, testno=$testno, input=\"$input\", inputfile=\"$inputfile\", output=\"$output\", outputfile=\"$outputfile\", policy=\"$policy\"";
		} else {
			$query = "UPDATE tests SET input=\"$input\", inputfile=\"$inputfile\", output=\"$output\", outputfile=\"$outputfile\", policy=\"$policy\" WHERE tid=$tid AND testno = $testno";
		}
		$result = mysql_query($query, $link) or die ("Can't save test: ".mysql_error($link));
		$num = mysql_affected_rows($link);
		echo "$num rows changed<BR><BR>\n";

		echo "<A href=\"assignment.php?tid=$tid\">View Task</A><BR>\n";
	}
	mysql_close($link);

	echo "<DIV class=\"footer\">\n";
	echo "<A href=\"index.php\"><IMG src=\"icons/house.png\" alt=\"home\">Home</A>\n";
	echo "<A href=\"logout.php\"><IMG src=\"icons/door_in.png\" alt=\"logout\">Logout</A>\n";
	echo "<DIV>\n";
?>
 </BODY>
</HTML>
