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

	session_start();
	html_start("Assignments");

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

	echo "<SCRIPT type=\"text/javascript\">\n";
	echo "function show(sid) {\n";
	echo "mydiv = document.getElementById('results_'+sid);\n";
	echo "mydiv.style.display = 'block';\n";
	echo "mylink = document.getElementById('show_'+sid);\n";
	echo "mylink.style.display = 'none';\n";
	echo "}\n";
	echo "function hide(sid) {\n";
	echo "mydiv = document.getElementById('results_'+sid);\n";
	echo "mydiv.style.display = 'none';\n";
	echo "mylink = document.getElementById('show_'+sid);\n";
	echo "mylink.style.display = 'inline';\n";
	echo "}\n";
	echo "</SCRIPT>\n";

	echo "<H1>View Task</H1>\n";

	$link = db_connect();
	$tid = intval($_REQUEST["tid"]);

	if($tid == 0) {
		echo "<A href=\"tasks.php\">View all assigments</A><BR>\n";
		die();
	}

	$query = "SELECT title, text FROM tasks WHERE tid=$tid";
	$result = mysql_query($query, $link) or die("Can't fetch Task: ".mysql_error($link));
	if(mysql_num_rows($result) != 1) {
		echo "Not one taks for this tid<BR>\n";
		echo "<A href=\"tasks.php\">View all assigments</A><BR>\n";
		die();
	}
	$task = mysql_fetch_array($result);

	echo "<H3>{$task["title"]}</H3>\n";
	echo "<P>{$task["text"]}</P>\n";

	if(!check_capa($_SESSION["id"], "taskedit", $link)) {
		echo "<FORM enctype=\"multipart/form-data\" action=\"solution.php\" method=\"post\">\n";
		echo "<INPUT type=\"hidden\" name=\"tid\" value=\"$tid\"><BR>\n";
		echo "<INPUT type=\"file\" name=\"file\" accept=\"text/*\"><BR>\n";
		echo "<INPUT type=\"submit\" value=\"Submit solution\">\n";
		echo "</FORM>\n";
	}

	echo "<B>Solutions:</B><BR>\n";

	if(!check_capa($_SESSION["id"], "taskedit", $link)) {
		$query = "SELECT sid, id FROM solutions WHERE tid=$tid AND id={$_SESSION["id"]} ORDER BY sid DESC";
	} else {
		$query = "SELECT sid, id FROM solutions WHERE tid=$tid ORDER BY sid DESC, id";
	}
	$result = mysql_query($query, $link) or die("Can't fetch solutions: ".mysql_error($link));
	echo "<UL>\n";
	while($solution = mysql_fetch_array($result)) {
		$sid = $solution["sid"];
		$link2 = db_connect();

		echo "<LI class=\"".testresults($sid)."\">";
		echo "Solution {$solution["sid"]} ";
		if(check_capa($_SESSION["id"], "taskedit", $link)) {
			echo "submitted by ".user_from_id($solution["id"])." ";
		}
		echo "<A href=\"getsource.php?sid=$sid\"><IMG src=\"icons/page_save.png\" alt=\"Get Source\"></A>";
		echo "<A href=\"dotest.php?sid=$sid\"><IMG src=\"icons/script_gear.png\" alt=\"Run Test\"></A>";
		echo "<A href=\"\" onClick=\"javascript:show($sid); return false;\" id=\"show_$sid\"><IMG src=\"icons/script_go.png\" alt=\"View Results\"></A>";
		echo "<DIV style=\"display: none\" id=\"results_$sid\">";
		$query = "SELECT testno, retval, stdout, stderr FROM results WHERE sid = $sid ORDER BY testno";
		$result2 = mysql_query($query, $link2) or die ("Can't get test results: ".mysql_error($link2));
		echo "<UL>\n";
		while($line = mysql_fetch_array($result2)) {
			echo "<LI";
			if($line["testno"] != 0) {
				echo " class=\"testfail\"";
			} else {
				echo " class=\"testok\"";
			}
			echo ">";
			echo "<B>Test {$line["testno"]}</B> <BR>\n";
			echo "<B>Stdout:</B><BR>\n<PRE>{$line["stdout"]}</PRE>\n";
			echo "<B>Stderr:</B><BR>\n<PRE>{$line["stderr"]}</PRE>\n";
			echo "</LI>\n";
		}
		echo "</UL>\n";
		echo "<A href=\"\" onClick=\"javascript:hide($sid); return false;\"><IMG src=\"icons/script_go_back.png\" alt=\"Hide Results\"></A>";
		echo "</DIV>\n";
		echo "</LI>\n";
		mysql_close($link2);
	}
	echo "</UL>\n";

	if(check_capa($_SESSION["id"], "taskedit", $link)) {
		echo "<B>Tests:</B><BR>\n";
		$query = "SELECT testno, input, inputfile, output, outputfile FROM tests WHERE tid = $tid";
		$result = mysql_query($query, $link) or die("Can't fetch tests");
		echo "<UL>\n";
		while($line = mysql_fetch_array($result)) {
			echo "<LI class=\"test\">";
			echo "<B>{$line["testno"]}</B> ";
			if($line["input"] == "" ) {
				echo "empty input ";
			} else {
				echo "{$line["input"]} ";
			}
			if($line["inputfile"] != "" ) {
				echo "+ inputfile";
			}
			echo " =&gt; ";
			if($line["output"] == "") {
				echo "empty output ";
			} else {
				echo "[{$line["output"]} ";
			}
			if($line["outputfile"] != "" ) {
				echo "+ outputfile";
			}
			echo " \n";
			echo "<A href=\"tests.php?todo=edit&testno={$line["testno"]}\">";
			echo "<IMG src=\"icons/cog_edit.png\" alt=\"edit test\"></A>\n";
			echo "<A href=\"tests.php?todo=rm&testno={$line["testno"]}\">";
			echo "<IMG src=\"icons/cog_delete.png\" alt=\"delete test\"></A>\n";
			echo "</LI>\n";
		}
		echo "</UL>\n";
		echo "<A href=\"tests.php?todo=add&tid=$tid\"><IMG src=\"icons/cog_add.png\" alt=\"add test\">Add test</A>\n";
		echo "<BR><BR>\n";
	}
	mysql_close($link);

	echo "<A href=\"tasks.php\">All tasks</A>\n";

	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>
